Roots of unity

From Rosetta Code
Revision as of 05:13, 13 December 2007 by rosettacode>Roger Hui (page creation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Roots of unity
You are encouraged to solve this task according to the task description, using any language you may know.

The purpose of this task is to explore working with complex numbers. Given n , find the n-th roots of unity.

J

   rou=: [: ^ i. * (o.0j2) % ]

   rou 4
1 0j1 _1 0j_1

   rou 5
1 0.309017j0.951057 _0.809017j0.587785 _0.809017j_0.587785 0.309017j_0.951057

The computation can also be written as a loop, shown here for comparison only.

rou1=: 3 : 0
 z=. 0 $ r=. ^ o. 0j2 % y [ e=. 1
 for. i.y do.
  z=. z,e
  e=. e*r
 end.
 z
)