diff --git a/src/Numerics/Fit.cs b/src/Numerics/Fit.cs index 46965db5..3b3c34be 100644 --- a/src/Numerics/Fit.cs +++ b/src/Numerics/Fit.cs @@ -31,6 +31,7 @@ using System; using System.Linq; using MathNet.Numerics.LinearAlgebra.Double; +using MathNet.Numerics.LinearAlgebra.Generic; using MathNet.Numerics.LinearAlgebra.Generic.Factorization; using MathNet.Numerics.Properties; @@ -122,7 +123,6 @@ namespace MathNet.Numerics /// public static double[] LinearCombination(double[] x, double[] y, params Func[] functions) { - // TODO: consider to use a specific algorithm instead return DenseMatrix .OfColumns(x.Length, functions.Length, functions.Select(f => DenseVector.Create(x.Length, i => f(x[i])))) .QR(QRMethod.Thin).Solve(new DenseVector(y)) @@ -138,5 +138,17 @@ namespace MathNet.Numerics var parameters = LinearCombination(x, y, functions); return z => functions.Zip(parameters, (f, p) => p*f(z)).Sum(); } + + /// + /// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to an arbitrary linear combination y : X -> p0*f0(x0) + p1*f1(x1) + ... + pk*fk(xk), + /// returning its best fitting parameters as [p0, p1, p2, ..., pk] array. + /// + public static double[] LinearCombinationVector(Vector[] x, double[] y, params Func[] functions) + { + return DenseMatrix + .OfColumns(x.Length, functions.Length, functions.Select((f, k) => DenseVector.Create(x.Length, i => f(x[i].At(k))))) + .QR(QRMethod.Thin).Solve(new DenseVector(y)) + .ToArray(); + } } }