From 035c5e291585b5f9d0543153d961f01e1c6cdf9a Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 22 Feb 2014 19:04:06 +0100 Subject: [PATCH] Evaluate: routine to evaluate a compley polynomial --- src/Numerics/SpecialFunctions/Evaluate.cs | 42 +++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Numerics/SpecialFunctions/Evaluate.cs b/src/Numerics/SpecialFunctions/Evaluate.cs index 366b0d9c..73fc6ed2 100644 --- a/src/Numerics/SpecialFunctions/Evaluate.cs +++ b/src/Numerics/SpecialFunctions/Evaluate.cs @@ -50,6 +50,10 @@ namespace MathNet.Numerics { using System; +#if !NOSYSNUMERICS + using Complex = System.Numerics.Complex; +#endif + /// /// Evaluation functions, useful for function approximation. /// @@ -74,6 +78,44 @@ namespace MathNet.Numerics return sum; } + /// + /// Evaluate a polynomial at point x. + /// Coefficients are ordered by power with power k at index k. + /// Example: coefficients [3,-1,2] represent y=2x^2-x+3. + /// + /// The location where to evaluate the polynomial at. + /// The coefficients of the polynomial, coefficient for power k at index k. + public static Complex Polynomial(Complex z, params double[] coefficients) + { + Complex sum = coefficients[coefficients.Length - 1]; + for (int i = coefficients.Length - 2; i >= 0; --i) + { + sum *= z; + sum += coefficients[i]; + } + + return sum; + } + + /// + /// Evaluate a polynomial at point x. + /// Coefficients are ordered by power with power k at index k. + /// Example: coefficients [3,-1,2] represent y=2x^2-x+3. + /// + /// The location where to evaluate the polynomial at. + /// The coefficients of the polynomial, coefficient for power k at index k. + public static Complex Polynomial(Complex z, params Complex[] coefficients) + { + Complex sum = coefficients[coefficients.Length - 1]; + for (int i = coefficients.Length - 2; i >= 0; --i) + { + sum *= z; + sum += coefficients[i]; + } + + return sum; + } + /// /// Numerically stable series summation ///