Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,32 +1,34 @@
|
|||
#include<math.h>
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
/*Arithmetic Geometric Mean of 1 and 1/sqrt(2)
|
||||
|
||||
double agm( double a, double g ) {
|
||||
/* arithmetic-geometric mean */
|
||||
double iota = 1.0E-16;
|
||||
double a1, g1;
|
||||
Nigel_Galloway
|
||||
February 7th., 2012.
|
||||
*/
|
||||
|
||||
if( a*g < 0.0 ) {
|
||||
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
|
||||
exit(1);
|
||||
}
|
||||
#include "gmp.h"
|
||||
|
||||
while( fabs(a-g)>iota ) {
|
||||
a1 = (a + g) / 2.0;
|
||||
g1 = sqrt(a * g);
|
||||
|
||||
a = a1;
|
||||
g = g1;
|
||||
}
|
||||
|
||||
return a;
|
||||
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
|
||||
mpf_add (out1, in1, in2);
|
||||
mpf_div_ui (out1, out1, 2);
|
||||
mpf_mul (out2, in1, in2);
|
||||
mpf_sqrt (out2, out2);
|
||||
}
|
||||
|
||||
int main( void ) {
|
||||
double x, y;
|
||||
printf( "Enter two numbers: " );
|
||||
scanf( "%lf%lf", &x, &y );
|
||||
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
|
||||
return 0;
|
||||
int main (void) {
|
||||
mpf_set_default_prec (65568);
|
||||
mpf_t x0, y0, resA, resB;
|
||||
|
||||
mpf_init_set_ui (y0, 1);
|
||||
mpf_init_set_d (x0, 0.5);
|
||||
mpf_sqrt (x0, x0);
|
||||
mpf_init (resA);
|
||||
mpf_init (resB);
|
||||
|
||||
for(int i=0; i<7; i++){
|
||||
agm(x0, y0, resA, resB);
|
||||
agm(resA, resB, x0, y0);
|
||||
}
|
||||
gmp_printf ("%.20000Ff\n", x0);
|
||||
gmp_printf ("%.20000Ff\n\n", y0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,13 @@
|
|||
import std.stdio, std.math, std.typecons;
|
||||
import std.stdio, std.math, std.typecons, std.typetuple;
|
||||
|
||||
real agm(real a, real g, in int bitPrecision=60) pure nothrow {
|
||||
real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe {
|
||||
do {
|
||||
//(a, g) = tuple((a + g) / 2.0, sqrt(a * g));
|
||||
immutable ag = tuple((a + g) / 2.0, sqrt(a * g));
|
||||
a = ag[0];
|
||||
g = ag[1];
|
||||
//{a, g} = {(a + g) / 2.0, sqrt(a * g)};
|
||||
TypeTuple!(a, g) = tuple((a + g) / 2.0, sqrt(a * g));
|
||||
} while (feqrel(a, g) < bitPrecision);
|
||||
return a;
|
||||
}
|
||||
|
||||
void main() {
|
||||
void main() @safe {
|
||||
writefln("%0.19f", agm(1, 1 / sqrt(2.0)));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
double agm (double a, double g) {
|
||||
double an = a, gn = g
|
||||
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
|
||||
an
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
println "agm(1, 0.5**0.5) = agm(1, ${0.5**0.5}) = ${agm(1, 0.5**0.5)}"
|
||||
assert (0.8472130847939792 - agm(1, 0.5**0.5)).abs() <= 10.0**-14
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
function agm(a, b, tolerance)
|
||||
if not tolerance or tolerance < 1e-15 then
|
||||
tolerance = 1e-15
|
||||
end
|
||||
repeat
|
||||
a, b = (a + b) / 2, math.sqrt(a * b)
|
||||
until math.abs(a-b) < tolerance
|
||||
return a
|
||||
end
|
||||
|
||||
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
import math // import for sqrt() function
|
||||
|
||||
amean: func (x: Double, y: Double) -> Double {
|
||||
(x + y) / 2.
|
||||
}
|
||||
gmean: func (x: Double, y: Double) -> Double {
|
||||
sqrt(x * y)
|
||||
}
|
||||
agm: func (a: Double, g: Double) -> Double {
|
||||
while ((a - g) abs() > pow(10, -12)) {
|
||||
(a1, g1) := (amean(a, g), gmean(a, g))
|
||||
(a, g) = (a1, g1)
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
main: func {
|
||||
"%.16f" printfln(agm(1., sqrt(0.5)))
|
||||
}
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
#
|
||||
# The flt package (http://flt.rubyforge.org/) is useful for high-precision floating-point math.
|
||||
# It lets us control 'context' of numbers, individually or collectively -- including precision
|
||||
# (which adjusts the context's value of epsilon accordingly).
|
||||
|
|
@ -8,7 +7,7 @@ include Flt
|
|||
|
||||
BinNum.Context.precision = 512 # default 53 (bits)
|
||||
|
||||
def AGM(a,g)
|
||||
def agm(a,g)
|
||||
new_a = BinNum a
|
||||
new_g = BinNum g
|
||||
while new_a - new_g > new_a.class.Context.epsilon do
|
||||
|
|
@ -19,4 +18,4 @@ def AGM(a,g)
|
|||
new_g
|
||||
end
|
||||
|
||||
puts AGM 1, 1 / BinNum(2).sqrt
|
||||
puts agm(1, 1 / BinNum(2).sqrt)
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
PRECISION = 100
|
||||
EPSILON = 0.1 ** (PRECISION/2)
|
||||
BigDecimal::limit(PRECISION)
|
||||
|
||||
def agm(a,g)
|
||||
while a - g > EPSILON
|
||||
a, g = (a+g)/2, (a*g).sqrt(PRECISION)
|
||||
end
|
||||
[a, g]
|
||||
end
|
||||
|
||||
a = BigDecimal(1)
|
||||
g = 1 / BigDecimal(2).sqrt(PRECISION)
|
||||
puts agm(a, g)
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
// http://rosettacode.org/wiki/Arithmetric-geometric_mean
|
||||
// Accepts two command line arguments
|
||||
// cargo run --name agm arg1 arg2
|
||||
|
||||
use std::num;
|
||||
|
||||
#[cfg(not(test))]
|
||||
fn main () {
|
||||
let args = std::os::args();
|
||||
let args = args.as_slice();
|
||||
|
||||
let x = from_str::<f32>(args[1].as_slice()).unwrap() ;
|
||||
let y = from_str::<f32>(args[2].as_slice()).unwrap() ;
|
||||
|
||||
let result = agm(x,y);
|
||||
println!("The arithmetic-geometric mean is {}", result);
|
||||
}
|
||||
|
||||
fn agm (x: f32, y: f32) -> f32 {
|
||||
let e: f32 = 0.000001;
|
||||
let mut a = x;
|
||||
let mut g = y;
|
||||
let mut a1: f32;
|
||||
let mut g1: f32;
|
||||
|
||||
if a * g < 0f32 { panic!("The arithmetric-geometric mean is undefined for numbers less than zero!"); }
|
||||
else {
|
||||
loop {
|
||||
a1 = (a + g) / 2f32;
|
||||
g1 = (a * g).sqrt();
|
||||
a = a1;
|
||||
g = g1;
|
||||
if num::abs( a - g) < e { return a; }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
function agm {
|
||||
float a=$1 g=$2 eps=${3:-1e-11} tmp
|
||||
while (( abs(a-g) > eps )); do
|
||||
print "debug: a=$a\tg=$g"
|
||||
tmp=$(( (a+g)/2.0 ))
|
||||
g=$(( sqrt(a*g) ))
|
||||
a=$tmp
|
||||
done
|
||||
echo $a
|
||||
}
|
||||
|
||||
agm $((1/sqrt(2))) 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
while (( abs(a-g) > eps ))
|
||||
|
|
@ -0,0 +1 @@
|
|||
while [[ $a != $g ]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue