CORDIC: Difference between revisions

Content added Content deleted
Line 119: Line 119:


cordic=: {{alpha=. y
cordic=: {{alpha=. y
XY=. 1 0 assert. 0 <: alpha assert. 0.5p1 >: alpha
XY=. 1 0 assert. 0 <: alpha assert. 0.25p1 >: alpha
while. epsilon < alpha do.
while. epsilon < alpha do.
k=. phin I. alpha
k=. phin I. alpha
Line 128: Line 128:
}}
}}


CORDIC -9
CORDIC=: {{
'octant angle'=. 8 0.25p1#:y
select. octant
case. 0 do. cordic angle
case. 1 do. |.cordic 0.25p1-angle
case. 2 do. _1 1*|.cordic angle
case. 3 do. _1 1* cordic 0.25p1-angle
case. 4 do. _1 _1* cordic angle
case. 5 do. _1 _1*|.cordic 0.25p1-angle
case. 6 do. 1 _1*|.cordic angle
case. 7 do. 1 _1* cordic 0.25p1-angle
end.
}}</syntaxhighlight>

Task examples (cos is the left value in the result, argument is in radians):

<syntaxhighlight lang=J> CORDIC -9
_0.92954 _0.420445
_0.92954 _0.420445
CORDIC 0
CORDIC 0
1 0
1 0
CORDIC 1.5
CORDIC 1.5
0.103588 1.46074
0.070762 0.997844
CORDIC 6
CORDIC 6
0.405107 _1.39209</syntaxhighlight>
0.970161 _0.282323</syntaxhighlight>

Task examples (cos is the left value in the result, argument is in radians):

<syntaxhighlight lang=J>FIXME</syntaxhighlight>


=== Notes ===
=== Notes ===


CAUTION: At the time of this writing, the task description declares that the cordic algorithm is valid in the range 0 .. π/2. But it appears that the algorithm can only be valid in the range 0..π/4.
Here, we use three constants, two of which are lookup tables (<code>tent</code> gives us negative powers of ten as a lookup table):

In this J implementation, we use three constants, two of which are lookup tables (<code>tent</code> gives us negative powers of ten as a lookup table):


<syntaxhighlight lang=J> epsilon
<syntaxhighlight lang=J> epsilon
Line 156: Line 170:
<syntaxhighlight lang=J> {&phin
<syntaxhighlight lang=J> {&phin
{ &0.785398163397448279 0.0996686524911620381 0.00999966668666523936 0.000999999666666867007 9.99999996666667089e_5 9.99999999966667302e_6 9.99999999999667283e_7 9.9999999999999744e_8 1.00000000000000085e_8 1.00000000000000089e_9 1.00000000000000107e_10 1....</syntaxhighlight>
{ &0.785398163397448279 0.0996686524911620381 0.00999966668666523936 0.000999999666666867007 9.99999996666667089e_5 9.99999999966667302e_6 9.99999999999667283e_7 9.9999999999999744e_8 1.00000000000000085e_8 1.00000000000000089e_9 1.00000000000000107e_10 1....</syntaxhighlight>

That said, it's not clear that this algorithm can be more accurate than something near 2% for the general case.


Also: [[j:Vocabulary/SpecialCombinations#Preexecuting_verbs_with_.28.28_.29.29|double parenthesis around a noun phrase]] tells the interpreter that that expression is a constant which should be evaluated once, ahead of time.
Also: [[j:Vocabulary/SpecialCombinations#Preexecuting_verbs_with_.28.28_.29.29|double parenthesis around a noun phrase]] tells the interpreter that that expression is a constant which should be evaluated once, ahead of time.