Formal power series: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: GLR fixes, sin and cos functioning)
Line 1,911: Line 1,911:


=={{header|Perl 6}}==
=={{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 { ... }
<lang perl6>class DerFPS { ... }
class IntFPS { ... }
class IntFPS { ... }
Line 1,925: Line 1,923:
sub super($i) { $i.trans('0123456789' => '⁰¹²³⁴⁵⁶⁷⁸⁹') }
sub super($i) { $i.trans('0123456789' => '⁰¹²³⁴⁵⁶⁷⁸⁹') }
my $str = $.coeffs[0].perl;
my $str = $.coeffs[0].perl;
for 1..$n Z $.coeffs[1..$n] -> $i, $_ {
for flat 1..$n Z $.coeffs[1..$n] -> $p, $c {
when * > 0 { $str ~= " + {(+$_).perl}∙x{super($i)}" }
when $c > 0 { $str ~= " + {(+$c).perl}∙x{super($p)}" }
when * < 0 { $str ~= " - {(-$_).perl}∙x{super($i)}" }
when $c < 0 { $str ~= " - {(-$c).perl}∙x{super($p)}" }
}
}
$str;
$str;
Line 1,970: Line 1,968:
class IntFPS does FPS {
class IntFPS does FPS {
has FPS $.x;
has FPS $.x;
method coeffs { 0, (0..*).map: { $.x.coeffs[$_] / ($_+1) } }
method coeffs { 0, |(0..*).map: { $.x.coeffs[$_] / ($_+1) } }
}
}


Line 1,985: Line 1,983:


# an example of a mixed-type operator:
# an example of a mixed-type operator:
multi infix:<->(Numeric $x, FPS $y) { ExplicitFPS.new(:coeffs($x, 0 xx *)) - $y }
multi infix:<->(Numeric $x, FPS $y) { ExplicitFPS.new(:coeffs(flat $x, 0 xx 9)) - $y }


# define sine and cosine in terms of each other
# define sine and cosine in terms of each other
Line 1,993: Line 1,991:


# define tangent in terms of sine and cosine
# define tangent in terms of sine and cosine
my $tan = $sin / $cos;
#my $tan = $sin / $cos;


say 'sin(x) ≈ ', $sin.pretty(10);
say 'sin(x) ≈ ', $sin.pretty(10);
say 'cos(x) ≈ ', $cos.pretty(10);
say 'cos(x) ≈ ', $cos.pretty(10);
say 'tan(x) ≈ ', $tan.pretty(10);</lang>
#say 'tan(x) ≈ ', $tan.pretty(10);</lang>


{{out}}
{{out}}
<pre>sin(x) ≈ 0 + 1/1∙x¹ - 1/6∙x³ + 1/120∙x⁵ - 1/5040∙x⁷ + 1/362880∙x⁹
<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¹⁰
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>
<!-- tan(x) ≈ 0/1 + 1/1∙x¹ + 1/3∙x³ + 2/15∙x⁵ + 17/315∙x⁷ + 62/2835∙x⁹ -->


=={{header|Phix}}==
=={{header|Phix}}==