Polynomial derivative: Difference between revisions

→‎{{header|Raku}}: Add a Raku example
(Added Wren)
(→‎{{header|Raku}}: Add a Raku example)
Line 128:
Derivative of -x4-x3+x+1: 1 - 3*x^2 - 4*x^3
</pre>
 
=={{header|Raku}}==
 
<lang perl6>use Lingua::EN::Numbers:ver<2.8+>;
 
sub pretty (@poly) {
my $p = join '+', (^@poly).reverse.map: { @poly[$_] ~ "x{.&super}" }
$p.=subst(/'+-'/, '-', :g).subst(/^'+'/, '').subst(/'x¹'/, 'x').subst(/'x⁰'/, '')\
.subst(/['+'|'-']'0x'<[⁰¹²³⁴⁵⁶⁷⁸⁹]>*/, '', :g).subst(/(['+'|'-'|^])'1x'/, {"$0x"}, :g) || 0
}
 
for [5], [4,-3], [-1,3,-2,1], [-1,6,5], [1,1,0,-1,-1] -> $test {
say "Polynomial: " ~ pretty $test;
my @poly = |$test;
(^@poly).map: { @poly[$_] *= $_ };
shift @poly;
say "Derivative: " ~ pretty @poly;
say '';
}</lang>
 
{{out}}
<pre>Polynomial: 5
Derivative: 0
 
Polynomial: -3x+4
Derivative: -3
 
Polynomial: x³-2x²+3x-1
Derivative: 3x²-4x+3
 
Polynomial: 5x²+6x-1
Derivative: 10x+6
 
Polynomial: -x⁴-x³+x+1
Derivative: -4x³-3x²+1</pre>
 
=={{header|Wren}}==
10,327

edits