Polynomial derivative

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"
}

Output:

Last updated

Was this helpful?