Roots of unity: Difference between revisions

Content added Content deleted
(Added Haskell example)
No edit summary
Line 78: Line 78:


=={{header|C++}}==
=={{header|C++}}==
#include <complex>
<cpp>#include <complex>
#include <iostream>
#include <cmath>
#include <ostream>
#include <iostream>

double const pi = 4*atan(1);
double const pi = 4 * std::atan(1);

int main()
int main()
{
{
for (int n = 2; n <= 10; ++n)
for (int n = 2; n <= 10; ++n)
{
{
std::cout << n << ": ";
std::cout << n << ": ";
for (int k = 0; k < n; ++k)
for (int k = 0; k < n; ++k)
std::cout << std::polar(1, 2*pi*k/n) << " ";
std::cout << std::polar(1, 2*pi*k/n) << " ";
std::cout << std::endl;
std::cout << std::endl;
}
}
}</cpp>
}


=={{header|Forth}}==
=={{header|Forth}}==
Line 154: Line 154:
=={{header|Java}}==
=={{header|Java}}==
Java doesn't have a nice way of dealing with complex numbers, so the real and imaginary parts are calculated separately based on the angle and printed together. There are also checks in this implementation to get rid of extremely small values (< 1.0E-3 where scientific notation sets in for <tt>Double</tt>s). Instead, they are simply represented as 0. To remove those checks (for very high <tt>n</tt>'s), remove both if statements.
Java doesn't have a nice way of dealing with complex numbers, so the real and imaginary parts are calculated separately based on the angle and printed together. There are also checks in this implementation to get rid of extremely small values (< 1.0E-3 where scientific notation sets in for <tt>Double</tt>s). Instead, they are simply represented as 0. To remove those checks (for very high <tt>n</tt>'s), remove both if statements.
public static void unity(int n){
<java>public static void unity(int n){
//all the way around the circle at even intervals
//all the way around the circle at even intervals
for(double angle = 0;angle < 2 * Math.PI;angle += (2 * Math.PI) / n){
for(double angle = 0;angle < 2 * Math.PI;angle += (2 * Math.PI) / n){
double real = Math.cos(angle); //real axis is the x axis
double real = Math.cos(angle); //real axis is the x axis
if(Math.abs(real) < 1.0E-3) real = 0.0; //get rid of annoying sci notation
if(Math.abs(real) < 1.0E-3) real = 0.0; //get rid of annoying sci notation
double imag = Math.sin(angle); //imaginary axis is the y axis
double imag = Math.sin(angle); //imaginary axis is the y axis
if(Math.abs(imag) < 1.0E-3) imag = 0.0; //get rid of annoying sci notation
if(Math.abs(imag) < 1.0E-3) imag = 0.0; //get rid of annoying sci notation
System.out.print(real + " + " + imag + "i\t"); //tab-separated answers
System.out.print(real + " + " + imag + "i\t"); //tab-separated answers
}
}
}</java>
}


=={{header|Perl}}==
=={{header|Perl}}==
{{works with|Perl|5.8.8}}
{{works with|Perl|5.8.8}}
{{libheader|Math::Complex}}
{{libheader|Math::Complex}}
use Math::Complex;
<perl>use Math::Complex;

foreach $n (1 .. 10) {
foreach $n (1 .. 10) {
printf "%2d ", $n;
printf "%2d ", $n;
foreach $k (1 .. $n) {
foreach $k (1 .. $n) {
$ret = exp($k * 2*i * pi / $n);
$ret = cplxe(1, 2 * pi * $k / $n);
$ret->display_format('style' => 'cartesian', 'format' => '%.3f');
$ret->display_format('style' => 'cartesian', 'format' => '%.3f');
print "($ret)";
print "($ret)";
}
}
print "\n";
print "\n";
}</perl>
}


Output:
Output:
Line 198: Line 198:


Function <code>nthroots()</code> returns all n-th roots of unity.
Function <code>nthroots()</code> returns all n-th roots of unity.
from cmath import exp, pi
<python>from cmath import exp, pi
def nthroots(n):
def nthroots(n):
return [exp(k * 2j * pi / n) for k in range(1, n + 1)]
return [exp(k * 2j * pi / n) for k in range(1, n + 1)]</python>
Example:
Example:
>>> f = nthroots
>>> f = nthroots