Formal power series: Difference between revisions

→‎{{header|Perl 6}}: GLR fixes, sin and cos functioning
(→‎{{header|Perl 6}}: GLR fixes, sin and cos functioning)
Line 1,911:
 
=={{header|Perl 6}}==
Much less broken, <code>sin</code> and <code>cos</code> working. <code>tan</code> a WIP...
 
{{broken|Perl 6}}
 
<lang perl6>class DerFPS { ... }
class IntFPS { ... }
Line 1,925 ⟶ 1,923:
sub super($i) { $i.trans('0123456789' => '⁰¹²³⁴⁵⁶⁷⁸⁹') }
my $str = $.coeffs[0].perl;
for flat 1..$n Z $.coeffs[1..$n] -> $ip, $_c {
when *$c > 0 { $str ~= " + {(+$_c).perl}∙x{super($ip)}" }
when *$c < 0 { $str ~= " - {(-$_c).perl}∙x{super($ip)}" }
}
$str;
Line 1,970 ⟶ 1,968:
class IntFPS does FPS {
has FPS $.x;
method coeffs { 0, |(0..*).map: { $.x.coeffs[$_] / ($_+1) } }
}
 
Line 1,985 ⟶ 1,983:
 
# an example of a mixed-type operator:
multi infix:<->(Numeric $x, FPS $y) { ExplicitFPS.new(:coeffs(flat $x, 0 xx *9)) - $y }
 
# define sine and cosine in terms of each other
Line 1,993 ⟶ 1,991:
 
# 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