Formal power series: Difference between revisions

→‎{{header|Perl 6}}: more fixes, 'tan' works too
(→‎{{header|Perl 6}}: GLR fixes, sin and cos functioning)
(→‎{{header|Perl 6}}: more fixes, 'tan' works too)
Line 1,911:
 
=={{header|Perl 6}}==
Much less broken, <code>sin</code> and <code>cos</code> working. <code>tan</code> a WIP...
<lang perl6>class DerFPS { ... }
class IntFPS { ... }
Line 1,922 ⟶ 1,921:
method pretty($n) {
sub super($i) { $i.trans('0123456789' => '⁰¹²³⁴⁵⁶⁷⁸⁹') }
my $str = $.coeffs[0].perl;
for flat 1..$n Z $.coeffs[1..$n] -> $p, $c {
when $c > 0 { $str ~= " + {(+ $c) .perlnude.join: '/'}∙x{super($p)}" }
when $c < 0 { $str ~= " - {(-$c) .perlnude.join: '/'}∙x{super($p)}" }
}
$str;
Line 1,952 ⟶ 1,951:
method coeffs {
# see http://en.wikipedia.org/wiki/Formal_power_series#Inverting_series
flat gather {
my @a := $.x.coeffs;
@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 9*)) - $y }
 
# define sine and cosine in terms of each other
Line 1,991 ⟶ 1,990:
 
# define tangent in terms of sine and cosine
#my $tan = $sin / $cos;
 
say 'sin(x) ≈ ', ~ $sin.pretty(10);
say 'cos(x) ≈ ', ~ $cos.pretty(10);
#say 'tan(x) ≈ ', ~ $tan.pretty(10);</lang>
 
{{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¹⁰</pre>
<!-- tan(x) ≈ 0/1 + 1/1∙x¹ + 1/3∙x³ + 2/15∙x⁵ + 17/315∙x⁷ + 62/2835∙x⁹ --</pre>
 
=={{header|Phix}}==
2,392

edits