How to resolve the algorithm Arithmetic-geometric mean step by step in the J programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Arithmetic-geometric mean step by step in the J programming language
Table of Contents
Problem Statement
Write a function to compute the arithmetic-geometric mean of two numbers.
The arithmetic-geometric mean of two numbers can be (usefully) denoted as
a g m
( a , g )
{\displaystyle \mathrm {agm} (a,g)}
, and is equal to the limit of the sequence: Since the limit of
a
n
−
g
n
{\displaystyle a_{n}-g_{n}}
tends (rapidly) to zero with iterations, this is an efficient method. Demonstrate the function by calculating:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Arithmetic-geometric mean step by step in the J programming language
Source code in the j programming language
mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
~.(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792
(mean, */ %:~ #)^:a: 1,%%:2
1 0.7071067811865475
0.8535533905932737 0.8408964152537145
0.8472249029234942 0.8472012667468915
0.8472130848351929 0.8472130847527654
0.8472130847939792 0.8472130847939791
DP=:101
round=: DP&$: : (4 : 0)
b %~ <.1r2+y*b=. 10x^x
)
sqrt=: DP&$: : (4 : 0) " 0
assert. 0<:y
%/ <.@%: (2 x: (2*x) round y)*10x^2*x+0>.>.10^.y
)
ln=: DP&$: : (4 : 0) " 0
assert. 0<y
m=. <.0.5+2^.y
t=. (<:%>:) (x:!.0 y)%2x^m
if. x<-:#":t do. t=. (1+x) round t end.
ln2=. 2*+/1r3 (^%]) 1+2*i.>.0.5*(%3)^.0.5*0.1^x+>.10^.1>.m
lnr=. 2*+/t (^%]) 1+2*i.>.0.5*(|t)^.0.5*0.1^x
lnr + m * ln2
)
exp=: DP&$: : (4 : 0) " 0
m=. <.0.5+y%^.2
xm=. x+>.m*10^.2
d=. (x:!.0 y)-m*xm ln 2
if. xm<-:#":d do. d=. xm round d end.
e=. 0.1^xm
n=. e (>i.1:) a (^%!@]) i.>.a^.e [ a=. |y-m*^.2
(2x^m) * 1++/*/\d%1+i.n
)
fmt=:[: ;:inv DP&$: : (4 :0)&.>
x{.deb (x*2j1)":y
)
root=: ln@] exp@% [
epsilon=: 1r9^DP
fmt sqrt 2
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572
fmt *~sqrt 2
2.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
fmt epsilon
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000418
fmt 2 root 2
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572
geomean=: */ root~ #
geomean2=: [: sqrt */
fmt geomean 3 5
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517
fmt geomean2 3 5
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517
fmt (mean, geomean2)^:(epsilon <&| -/)^:a: 1,%sqrt 2
1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.707106781186547524400844362104849039284835937688474036588339868995366239231053519425193767163820786
0.853553390593273762200422181052424519642417968844237018294169934497683119615526759712596883581910393 0.840896415253714543031125476233214895040034262356784510813226085974924754953902239814324004199292536
0.847224902923494152615773828642819707341226115600510764553698010236303937284714499763460443890601464 0.847201266746891460403631453693352397963981013612000500823295747923488191871327668107581434542353536
0.847213084835192806509702641168086052652603564606255632688496879079896064578021083935520939216477500 0.847213084752765366704298051779902070392110656059452583317776227659438896688518556753569298762449381
0.847213084793979086607000346473994061522357110332854108003136553369667480633269820344545118989463440 0.847213084793979086605997900490389211440534858586261300461413929971399281619068666682569108141224710
0.847213084793979086606499123482191636481445984459557704232275241670533381126169243513557113565344075 0.847213084793979086606499123482191636481445836194326665888883503648934628542100275932846717790147361
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723201915677745718 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723198672311476741
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the Prolog programming language
You may also check:How to resolve the algorithm Floyd's triangle step by step in the Java programming language
You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the Nim programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Rust programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the Lua programming language