From ee9d75417234849306bacc26a7e5aea1aac2ec35 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 20 Oct 2013 16:59:21 +0200 Subject: [PATCH] Fit: more direct and slightly faster implementation; add MultiDim --- src/Numerics/Fit.cs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Numerics/Fit.cs b/src/Numerics/Fit.cs index 92633274..48a08efc 100644 --- a/src/Numerics/Fit.cs +++ b/src/Numerics/Fit.cs @@ -61,13 +61,32 @@ namespace MathNet.Numerics return z => intercept + slope*z; } + /// + /// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to a linear surface y : X -> p0*x0 + p1*x1 + ... + pk*xk, + /// returning its best fitting parameters as [p0, p1, p2, ..., pk] array. + /// + public static double[] MultiDim(double[][] x, double[] y) + { + return MultipleRegression.NormalEquations(x, y); + } + + /// + /// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to a linear surface y : X -> p0*x0 + p1*x1 + ... + pk*xk, + /// returning a function y' for the best fitting combination. + /// + public static Func MultiDimFunc(double[][] x, double[] y) + { + var parameters = MultipleRegression.NormalEquations(x, y); + return z => Control.LinearAlgebraProvider.DotProduct(parameters, z); + } + /// /// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k, /// returning its best fitting parameters as [p0, p1, p2, ..., pk] array, compatible with Evaluate.Polynomial. /// public static double[] Polynomial(double[] x, double[] y, int order) { - var design = Matrix.Build.DenseOfColumns(x.Length, order + 1, Enumerable.Range(0, order + 1).Select(j => x.Select(xi => Math.Pow(xi, j)))); + var design = Matrix.Build.Dense(x.Length, order + 1, (i, j) => Math.Pow(x[i], j)); return MultipleRegression.QR(design, Vector.Build.Dense(y)).ToArray(); } @@ -87,7 +106,7 @@ namespace MathNet.Numerics /// public static double[] LinearCombination(double[] x, double[] y, params Func[] functions) { - var design = Matrix.Build.DenseOfColumns(x.Length, functions.Length, functions.Select(f => x.Select(xi => f(xi)))); + var design = Matrix.Build.Dense(x.Length, functions.Length, (i, j) => functions[j](x[i])); return MultipleRegression.QR(design, Vector.Build.Dense(y)).ToArray(); } @@ -107,7 +126,7 @@ namespace MathNet.Numerics /// public static double[] LinearMultiDim(double[][] x, double[] y, params Func[] functions) { - var design = Matrix.Build.DenseOfRows(x.Select(xi => functions.Select(f => f(xi)))); + var design = Matrix.Build.Dense(x.Length, functions.Length, (i, j) => functions[j](x[i])); return MultipleRegression.QR(design, Vector.Build.Dense(y)).ToArray(); } @@ -127,7 +146,7 @@ namespace MathNet.Numerics /// public static double[] LinearGeneric(T[] x, double[] y, params Func[] functions) { - var design = Matrix.Build.DenseOfRows(x.Select(xi => functions.Select(f => f(xi)))); + var design = Matrix.Build.Dense(x.Length, functions.Length, (i, j) => functions[j](x[i])); return MultipleRegression.QR(design, Vector.Build.Dense(y)).ToArray(); }