Arithmetic-geometric mean: Difference between revisions

Content added Content deleted
(Add NetRexx implementation)
(add R)
Line 761:
<pre>0.847213084793979086606499123482191636481445910326942185060579372659734</pre>
All the digits shown are correct.
 
=={{header|R}}==
<lang r>arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
 
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
 
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))</lang>
{{out}}
<pre> agm rel_error
1 0.8472130847939792 1.310441309927519e-16</pre>
This function also works on vectors a and b (following the spirit of R):
<lang r>a <- c(1, 1, 1)
b <- c(1/sqrt(2), 1/sqrt(3), 1/2)
agm <- arithmeticGeometricMean(a, b)
print(format(agm, digits=16))</lang>
{{out}}
<pre> agm rel_error
1 0.8472130847939792 1.310441309927519e-16
2 0.7741882646460426 0.000000000000000e+00
3 0.7283955155234534 0.000000000000000e+00</pre>
 
=={{header|REXX}}==