Roots of unity: Difference between revisions

→‎{{header|REXX}}: added/changed whitespace and comments, simplified and optimized some functions.
(adding lambdatalk)
(→‎{{header|REXX}}: added/changed whitespace and comments, simplified and optimized some functions.)
Line 1,595:
(See the value of the REXX variable &nbsp; '''frac''', &nbsp; 4<sup>th</sup> line).
<lang rexx>/*REXX program computes the K roots of unity (which usually includes complex roots).*/
numeric digits length( pi() ) - 1; length d= digits(.) /*use number of decimal digits in pi. */
parse arg n frac . /*get optional arguments from the C.L. */
if n=='' | n=="," then n= 1 /*Not specified? Then use the default.*/
Line 1,601:
start= abs(n) /*assume only one K is wanted. */
if n<0 then start= 1 /*Negative? Then use a range of K's. */
do #=start to abs(n) pi2= pi + pi /*obtainshow theunity valueroots of(for a range pior 1 doubledK. */
say right(# 'roots of unity', 40, "─") ' (showing' frac "fractional decimal digits)"
do #=start to abs(n) /*show unity roots (for a range or 1 K.*/
do angle=0 by pi*2/# for # /*compute the angle for each root. */
say right(# 'roots of unity', 40, "─") ' (showing' frac "fractional decimal digits)"
doRp= adj( cos(angle=0) ) by pi2/# for # /*compute thereal angle for each root.part via COS function.*/
rpIp= adj( cossin(angle) ) /*compute realimaginary part via SIN COS functionfunct.*/
if left(rp, 1) \== '-' if Rp>=0 then rpRp= " "rpRp /*notNot negativeneg? Then pad with a blank char.*/
ip= adj( sin(angle) ) if Ip>=0 then Ip= "+"Ip /*compute imaginary" " " " " " plus part via" SIN funct.*/
if left(ip,Ip=0 1) \==then '-'say Rp then ip= "+"ip /*Not negative? Then pad with + char. /*Only real part? Ignore imaginary part*/
if ip=0 then say rp else say left(Rp, frac+4)Ip'i' /*Onlydisplay the real part? Ignoreand imaginary part. */
else say left(rp, frac+4)ip'i' /*display the real and imaginary part. */
end /*angle*/
end end /*#*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
adj: parse arg x; if abs(x) < ('1e-')(d*9%10) then x= 0; return format(x, , frac) / 1
pi: pi=3.141592653589793238462643383279502884197169399375105820974944592307816; return pi
r2r: return arg(1) // (pi2= pi() + pi; ) return arg(1) // pi2 /*reduce #radians: -2pi ─► +2pi radians*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
adjcos: procedure; parse arg x; near0= '1e-' || x= r2r(digits(x); * 9 %a= 10abs(x); numeric fuzz min(9, digits() - /*compute a tiny number.*/9)
ifpi1_3=pi/3; abs(x) < near0 then xif a=pi1_3 0 then return .5; if /a=pi*if.5 it's| near zero,a=pi2 then assumereturn zero.*/0
returnif format(x,a=pi , frac)then return -1; / if a=pi1_3*2 1 then return -.5; z= 1; _= 1; /*fraction digits past decimal point.$x= x */ x
/*──────────────────────────────────────────────────────────────────────────────────────*/
cos: procedure; parse arg x; x= r2r(x); a= abs(x); numeric fuzz min(9, digits()-9)
if a=pi/3 then return .5; if a=pi/2 | a=pi*2 then return 0
if a=pi then return -1; if a=pi*2/3 then return -.5; z= 1; _= 1; $x= x * x
do k=2 by 2 until p=z; p=z; _= -_ * $x / (k*(k-1)); z= z + _; end; return z
/*──────────────────────────────────────────────────────────────────────────────────────*/
sin: procedure; parse arg x; x= r2r(x); numeric fuzz min(5, digits() - 3)
if abs(x)=pi then return 0; z= x; _= x; $x= x * x
do k=2 by 2 until p=z; p=z; _= -_ * $x / (k*(k+1)); z= z + _; end; return z</lang>