Formal power series: Difference between revisions
→{{header|Perl 6}}: more fixes, 'tan' works too
SqrtNegInf (talk | contribs) (→{{header|Perl 6}}: GLR fixes, sin and cos functioning) |
SqrtNegInf (talk | contribs) (→{{header|Perl 6}}: more fixes, 'tan' works too) |
||
Line 1,911:
=={{header|Perl 6}}==
<lang perl6>class DerFPS { ... }
class IntFPS { ... }
Line 1,922 ⟶ 1,921:
method pretty($n) {
sub super($i) { $i.trans('0123456789' => '⁰¹²³⁴⁵⁶⁷⁸⁹') }
my $str = $.coeffs[0]
for flat 1..$n Z $.coeffs[1..$n] -> $p, $c {
when $c > 0 { $str ~= " + {
when $c < 0 { $str ~= " - {
}
$str;
Line 1,952 ⟶ 1,951:
method coeffs {
# see http://en.wikipedia.org/wiki/Formal_power_series#Inverting_series
flat gather {
my @a
@a[0] != 0 or fail "Cannot invert power series with zero constant term.";
take my @b = (1 / @a[0]);
Line 1,983 ⟶ 1,982:
# an example of a mixed-type operator:
multi infix:<->(Numeric $x, FPS $y) { ExplicitFPS.new(:coeffs(lazy flat $x, 0 xx
# define sine and cosine in terms of each other
Line 1,991 ⟶ 1,990:
# define tangent in terms of sine and cosine
say 'sin(x) ≈ '
say 'cos(x) ≈ '
{{out}}
<pre>sin(x) ≈ 0 + 1/1∙x¹ - 1/6∙x³ + 1/120∙x⁵ - 1/5040∙x⁷ + 1/362880∙x⁹
cos(x) ≈ 1 - 1/2∙x² + 1/24∙x⁴ - 1/720∙x⁶ + 1/40320∙x⁸ - 1/3628800∙x¹⁰
=={{header|Phix}}==
|