Cyclotomic polynomial: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 14:
=={{header|C++}}==
{{trans|Java}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <initializer_list>
Line 532:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Task 1: cyclotomic polynomials for n <= 30:
Line 581:
{{trans|Java}}
{{works with|C sharp|8}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections;
using System.Collections.Generic;
Line 816:
};
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 865:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.exception;
import std.format;
Line 1,303:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Task 1: cyclotomic polynomials for n <= 30:
Line 1,351:
=={{header|Fermat}}==
This isn't terribly efficient if you have to calculate many cyclotomics- store them in an array rather than using recursion instead if you need to do that- but it showcases Fermat's strength at polynomial expressions.
<langsyntaxhighlight lang="fermat">
&(J=x); {adjoin x as the variable in the polynomials}
 
Line 1,381:
od;
!!(m,' : ',i);
od;</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Java}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,824:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,877:
Uses synthetic polynomial division and simple memoization.
 
<langsyntaxhighlight lang="haskell">import Data.List
import Data.Numbers.Primes (primeFactors)
 
Line 1,915:
-- general case
(p, m):ps -> let cm = cyclotomics !! (n `div` (p ^ m))
in lift (lift cm p `shortDiv` cm) (p^(m-1))</langsyntaxhighlight>
 
Simple examples
Line 1,930:
The task solution
 
<langsyntaxhighlight lang="haskell">showPoly [] = "0"
showPoly p = foldl1 (\r -> (r ++) . term) $
dropWhile null $
Line 1,965:
 
skipBy n [] = []
skipBy n lst = let (x:_, b) = splitAt n lst in x:skipBy n b</langsyntaxhighlight>
 
Result
Line 2,017:
For values up to 70, we can find cyclotomic polynomials by finding a polynomial with roots of unity relatively prime to the order of the polynomial:
 
<langsyntaxhighlight Jlang="j">cyclo=: {{<.-:1+(++) p. 1;^0j2p1* y%~1+I.1=y+.1+i.y}}</langsyntaxhighlight>
 
This approach suggests that cyclotomic polynomial zero should be <tt>f<sub>0</sub>(x)= 1</tt>
Line 2,023:
Routine to find the nth cyclotomic polynomial:
 
<langsyntaxhighlight Jlang="j">{{ if.0>nc<'cache' do.cache=:y end.}} (,1);_1 1
 
cyclotomic=: {{
Line 2,067:
end.
end.q
}}</langsyntaxhighlight>
 
If you take all the divisors of a number. (For example, for 12, the divisors are: 1, 2, 3, 4, 6 and 12) and find the product of their cyclotomic polynomials (for example, for 12, x-1, x+1, x<sup>2</sup>+x+1, x<sup>2</sup>+1, x<sup>2</sup>-x+1, and x<sup>4</sup>-x<sup>2</sup>+1) you get x<sup>n</sup>-1 (for 12, that would of course be x<sup>12</sup>-1).
Line 2,082:
Task examples:
 
<langsyntaxhighlight Jlang="j">taskfmt=: {{
c=. ":each j=.cyclotomic y
raw=. rplc&'_-' ;:inv}.,'+';"0|.(*|j)#c('(',[,],')'"_)each '*x^',&":L:0 <"0 i.#c
Line 2,141:
8 6545
9 6545
10 10465</langsyntaxhighlight>
 
=== Another approach ===
Line 2,147:
As noted in the [http://jsoftware.com/pipermail/programming/2022-March/060209.html J programming forum], we can improve the big-O character of this algorithm by using the [[Fast Fourier transform#J|fast fourier transform]] for polynomial multiplication and division.
 
<langsyntaxhighlight Jlang="j">NB. install'math/fftw'
require'math/fftw'
 
Line 2,169:
}}
 
roundreal =: [: <. 0.5 + 9&o.</langsyntaxhighlight>
 
This variation for polynomial division is only valid when there's no remainder to be concerned with (which is the case, here). The article mentioned in the comments is an essay on using [[j:Essays/FFT|fft for polynomial multiplication]]
Line 2,176:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Collections;
Line 2,703:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,752:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes, Polynomials
# memoize cache for recursive calls
Line 2,809:
println("The cyclotomic polynomial Φ(", abs(n), ") has a coefficient that is ", n < 0 ? -i : i)
end
</langsyntaxhighlight>{{out}}
<pre>
First 30 cyclotomic polynomials:
Line 2,856:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.util.TreeMap
import kotlin.math.abs
import kotlin.math.pow
Line 3,312:
} else coefficient.toString() + "x^" + exponent
}
}</langsyntaxhighlight>
{{out}}
<pre>Task 1: cyclotomic polynomials for n <= 30:
Line 3,359:
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">with(NumberTheory):
for n to 30 do lprint(Phi(n,x)) od:
 
Line 3,396:
[seq(ListTools:-SelectFirst(s->member(n,s),PhiSet,output=indices),n=1..20)];
#[1, 105, 385, 1365, 1785, 2805, 3135, 6545, 6545, 10465, 10465,
# 10465, 10465, 10465, 11305, 11305, 11305, 11305, 11305, 11305]</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Cyclotomic[#, x] & /@ Range[30] // Column
i = 1;
n = 10;
Line 3,420:
];
i++;
]</langsyntaxhighlight>
{{out}}
<pre>-1+x
Line 3,469:
We use Java algorithm with ideas from C#, D, Go and Kotlin. We have kept only algorithm number 2 as other algorithms are much less efficient. We have also done some Nim specific improvements in order to get better performances.
 
<langsyntaxhighlight Nimlang="nim">import algorithm, math, sequtils, strformat, tables
 
type
Line 3,803:
echo &"Φ{'(' & $n & ')':7} has coefficient with magnitude = {i}"
dec n
break</langsyntaxhighlight>
 
{{out}}
Line 3,854:
=={{header|PARI/GP}}==
Cyclotomic polynomials are a built-in function.
<langsyntaxhighlight lang="parigp">
for(n=1,30,print(n," : ",polcyclo(n)))
 
Line 3,860:
 
for(d=1,10,i=1; while(contains_coeff(i,d)==0,i=i+1);print(d," : ",i))
</syntaxhighlight>
</lang>
 
{{out}}<pre>
Line 3,907:
=={{header|Perl}}==
Conveniently, the module <code>Math::Polynomial::Cyclotomic</code> exists to do all the work. An <code>exponent too large</code> error prevents reaching the 10th step of the 2nd part of the task.
<langsyntaxhighlight lang="perl">use feature 'say';
use List::Util qw(first);
use Math::Polynomial::Cyclotomic qw(cyclo_poly_iterate);
Line 3,924:
$n++;
}
}</langsyntaxhighlight>
{{out}}
<pre>First 30 cyclotomic polynomials:
Line 3,972:
{{trans|Julia}}
Uses several routines from [[Polynomial_long_division#Phix]], tweaked slightly to check remainder is zero and trim the quotient.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Cyclotomic_Polynomial.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 4,087:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
{{out}}
If you disable the cheating, and if in a particularly self harming mood replace it with n+=1, you will get exactly the same output, eventually.<br>
Line 4,136:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import count, chain
from collections import deque
 
Line 4,257:
while want in c or -want in c:
print(f'C[{want}]: {n}')
want += 1</langsyntaxhighlight>
{{out}}
Only showing first 10 polynomials to avoid clutter.
Line 4,303:
 
Uses the same library as Perl, so comes with the same caveats.
<syntaxhighlight lang="raku" perl6line>use Math::Polynomial::Cyclotomic:from<Perl5> <cyclo_poly_iterate cyclo_poly>;
 
say 'First 30 cyclotomic polynomials:';
Line 4,326:
sub super ($str) {
$str.subst( / '^' (\d+) /, { $0.trans([<0123456789>.comb] => [<⁰¹²³⁴⁵⁶⁷⁸⁹>.comb]) }, :g)
}</langsyntaxhighlight>
<pre>First 30 cyclotomic polynomials:
Φ(1) = (x - 1)
Line 4,372:
=={{header|Sidef}}==
Solution based on polynomial interpolation (slow).
<langsyntaxhighlight lang="ruby">var Poly = require('Math::Polynomial')
Poly.string_config(Hash(fold_sign => true, prefix => "", suffix => ""))
 
Line 4,392:
})
say "Φ(#{k}) has coefficient with magnitude #{n}"
}</langsyntaxhighlight>
 
Slightly faster solution, using the '''Math::Polynomial::Cyclotomic''' Perl module.
<langsyntaxhighlight lang="ruby">var Poly = require('Math::Polynomial')
require('Math::Polynomial::Cyclotomic')
 
Line 4,412:
})
say "Φ(#{k}) has coefficient with magnitude = #{n}"
}</langsyntaxhighlight>
 
{{out}}
Line 4,460:
=={{header|Visual Basic .NET}}==
{{trans|C++}}
<langsyntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
Line 4,942:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Task 1: cyclotomic polynomials for n <= 30:
Line 4,995:
{{libheader|Wren-fmt}}
Second part is very slow. Limited to first 7 to finish in a reasonable time - 5 minutes on my machine.
<langsyntaxhighlight lang="ecmascript">import "/trait" for Stepped
import "/sort" for Sort
import "/math" for Int, Nums
Line 5,334:
}
}
}</langsyntaxhighlight>
 
{{out}}
10,333

edits