Polynomial derivative: Difference between revisions
Content added Content deleted
m (→{{header|FreeBASIC}}: - formatting derp) |
(Added Wren) |
||
Line 127: | Line 127: | ||
Derivative of x3-2x2+3x-4: 3 - 4*x + 3*x^2 |
Derivative of x3-2x2+3x-4: 3 - 4*x + 3*x^2 |
||
Derivative of -x4-x3+x+1: 1 - 3*x^2 - 4*x^3 |
Derivative of -x4-x3+x+1: 1 - 3*x^2 - 4*x^3 |
||
</pre> |
|||
=={{header|Wren}}== |
|||
<lang ecmascript>var derivative = Fn.new { |p| |
|||
if (p.count == 1) return [0] |
|||
var d = p[1..-1].toList |
|||
for (i in 0...d.count) d[i] = p[i+1] * (i + 1) |
|||
return d |
|||
} |
|||
System.print("The derivatives of the following polynomials are:") |
|||
var polys = [ [5], [4, -3], [-1, 6, 5], [-4, 3, -2, 1], [1, 1, 0, -1, -1] ] |
|||
for (poly in polys) { |
|||
var deriv = derivative.call(poly) |
|||
System.print("%(poly) -> %(deriv)") |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
The derivatives of the following polynomials are: |
|||
[5] -> [0] |
|||
[4, -3] -> [-3] |
|||
[-1, 6, 5] -> [6, 10] |
|||
[-4, 3, -2, 1] -> [3, -4, 3] |
|||
[1, 1, 0, -1, -1] -> [1, 0, -3, -4] |
|||
</pre> |
</pre> |