Polynomial derivative: Difference between revisions
m
ec
(add FreeBASIC) |
m (ec) |
||
Line 16:
IN: math.polynomials
: pdiff ( p -- p' ) dup length <iota> v* rest ;</lang>
=={{header|Julia}}==▼
<lang julia>using Polynomials▼
testcases = [▼
("5", [5]),▼
("-3x+4", [4, -3]),▼
("5x2+6x-1", [-1, 6, 5]),▼
("x3-2x2+3x-4", [-4, 3, -2, 1]),▼
("-x4-x3+x+1", [1, 1, 0, -1, -1]),▼
]▼
for (s, coef) in testcases▼
println("Derivative of $s: ", derivative(Polynomial(coef)))▼
end▼
</lang>{{out}}▼
<pre>▼
Derivative of 5: 0▼
Derivative of -3x+4: -3▼
Derivative of 5x2+6x-1: 6 + 10*x▼
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▼
</pre>▼
=={{header|FreeBASIC}}==
Line 130 ⟶ 105:
Differentiates to
1 - 3x^2- 4x^3</pre>
▲=={{header|Julia}}==
▲<lang julia>using Polynomials
▲testcases = [
▲ ("5", [5]),
▲ ("-3x+4", [4, -3]),
▲ ("5x2+6x-1", [-1, 6, 5]),
▲ ("x3-2x2+3x-4", [-4, 3, -2, 1]),
▲ ("-x4-x3+x+1", [1, 1, 0, -1, -1]),
▲]
▲for (s, coef) in testcases
▲ println("Derivative of $s: ", derivative(Polynomial(coef)))
▲end
▲</lang>{{out}}
▲<pre>
▲Derivative of 5: 0
▲Derivative of -3x+4: -3
▲Derivative of 5x2+6x-1: 6 + 10*x
▲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
▲</pre>
|