Polynomial derivative: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: fixed pretty printing) |
(Added Sidef) |
||
Line 480: | Line 480: | ||
Polynomial: [1,1,0,-1,-1] ➡ -x⁴-x³+x+1 |
Polynomial: [1,1,0,-1,-1] ➡ -x⁴-x³+x+1 |
||
Derivative: [1,0,-3,-4] ➡ -4x³-3x²+1</pre> |
Derivative: [1,0,-3,-4] ➡ -4x³-3x²+1</pre> |
||
=={{header|Sidef}}== |
|||
<lang ruby>func derivative(f) { |
|||
Poly(f.coeffs.map_2d{|e,k| [e-1, k*e] }.flat...) |
|||
} |
|||
var coeffs = [ |
|||
[5], |
|||
[4,-3], |
|||
[-1,6,5], |
|||
[-4,3,-2,1], |
|||
[-1, 6, 5], |
|||
[1,1,0,-1,-1], |
|||
] |
|||
for c in (coeffs) { |
|||
var poly = Poly(c.flip) |
|||
var derv = derivative(poly) |
|||
var d = { derv.coeff(_) }.map(0..derv.degree) |
|||
say "Polynomial : #{'%20s' % c} = #{poly}" |
|||
say "Derivative : #{'%20s' % d} = #{derv || 0}\n" |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Polynomial : [5] = 5 |
|||
Derivative : [0] = 0 |
|||
Polynomial : [4, -3] = -3*x + 4 |
|||
Derivative : [-3] = -3 |
|||
Polynomial : [-1, 6, 5] = 5*x^2 + 6*x - 1 |
|||
Derivative : [6, 10] = 10*x + 6 |
|||
Polynomial : [-4, 3, -2, 1] = x^3 - 2*x^2 + 3*x - 4 |
|||
Derivative : [3, -4, 3] = 3*x^2 - 4*x + 3 |
|||
Polynomial : [-1, 6, 5] = 5*x^2 + 6*x - 1 |
|||
Derivative : [6, 10] = 10*x + 6 |
|||
Polynomial : [1, 1, 0, -1, -1] = -x^4 - x^3 + x + 1 |
|||
Derivative : [1, 0, -3, -4] = -4*x^3 - 3*x^2 + 1 |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |