How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Groovy programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Groovy programming language
Table of Contents
Problem Statement
A fast scheme for evaluating a polynomial such as: when is to arrange the computation as follows: And compute the result from the innermost brackets outwards as in this pseudocode: Task Description Cf. Formal power series
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Groovy programming language
Source code in the groovy programming language
def hornersRule = { coeff, x -> coeff.reverse().inject(0) { accum, c -> (accum * x) + c } }
def coefficients = [-19g, 7g, -4g, 6g]
println (["p coefficients":coefficients])
def testPoly = hornersRule.curry(coefficients)
println (["p(3)":testPoly(3g)])
println (["p(0)":testPoly(0g)])
def derivativeCoefficients = { coeff -> (1..<(coeff.size())).collect { coeff[it] * it } }
println (["p' coefficients":derivativeCoefficients(coefficients)])
def testDeriv = hornersRule.curry(derivativeCoefficients(coefficients))
println (["p'(3)":testDeriv(3g)])
println (["p'(0)":testDeriv(0g)])
def newtonRaphson = { x, f, fPrime ->
while (f(x).abs() > 0.0001) {
x -= f(x)/fPrime(x)
}
x
}
def root = newtonRaphson(3g, testPoly, testDeriv)
println ([root:root.toString()[0..5], "p(root)":testPoly(root).toString()[0..5], "p'(root)":testDeriv(root).toString()[0..5]])
You may also check:How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the Python programming language
You may also check:How to resolve the algorithm Optional parameters step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Maple programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the Rust programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Miranda programming language