How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Rust 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 Rust 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 Rust programming language
Source code in the rust programming language
fn horner(v: &[f64], x: f64) -> f64 {
v.iter().rev().fold(0.0, |acc, coeff| acc*x + coeff)
}
fn main() {
let v = [-19., 7., -4., 6.];
println!("result: {}", horner(&v, 3.0));
}
extern crate num; // 0.2.0
use num::Zero;
use std::ops::{Add, Mul};
fn horner<Arr, Arg, Out>(v: &[Arr], x: Arg) -> Out
where
Arr: Clone,
Arg: Clone,
Out: Zero + Mul<Arg, Output = Out> + Add<Arr, Output = Out>,
{
v.iter()
.rev()
.fold(Zero::zero(), |acc, coeff| acc * x.clone() + coeff.clone())
}
fn main() {
let v = [-19., 7., -4., 6.];
let output: f64 = horner(&v, 3.0);
println!("result: {}", output);
}
You may also check:How to resolve the algorithm Brilliant numbers step by step in the Factor programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the MAD programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Fennel programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Parse an IP Address step by step in the PowerShell programming language