diff --git a/src/Numerics/Fit.cs b/src/Numerics/Fit.cs index a0f0088f..d339b72d 100644 --- a/src/Numerics/Fit.cs +++ b/src/Numerics/Fit.cs @@ -140,46 +140,25 @@ namespace MathNet.Numerics } /// - /// 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), + /// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to an arbitrary linear combination y : X -> p0*f0(x) + p1*f1(x) + ... + pk*fk(x), /// returning its best fitting parameters as [p0, p1, p2, ..., pk] array. /// - public static double[] LinearMultiDim(double[][] x, double[] y, params Func[] functions) + public static double[] LinearMultiDim(double[][] x, double[] y, params Func[] functions) { return DenseMatrix - .OfRows(x.Length, functions.Length, x.Select(xi => functions.Select((f, k) => f(xi[k])))) + .OfRows(x.Length, functions.Length, x.Select(xi => functions.Select(f => f(xi)))) .QR(QRMethod.Thin).Solve(new DenseVector(y)) .ToArray(); } /// - /// 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), + /// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to an arbitrary linear combination y : X -> p0*f0(x) + p1*f1(x) + ... + pk*fk(x), /// returning a function y' for the best fitting combination. /// - public static Func LinearMultiDimFunc(double[][] x, double[] y, params Func[] functions) + public static Func LinearMultiDimFunc(double[][] x, double[] y, params Func[] functions) { var parameters = LinearMultiDim(x, y, functions); - return z => functions.Select((f, i) => parameters[i]*f(z[i])).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 Vector LinearVector(Vector[] x, double[] y, Func, Vector> functions) - { - return DenseMatrix - .OfRowVectors(x.Select(functions).ToArray()) // PERF: Array.map instead of seq - .QR(QRMethod.Thin).Solve(new DenseVector(y)); - } - - /// - /// 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 a function y' for the best fitting combination. - /// - public static Func, double> LinearVectorFunc(Vector[] x, double[] y, Func, Vector> functions) - { - var parameters = LinearVector(x, y, functions); - return z => functions(z).Select((yi, i) => parameters[i]*yi).Sum(); + return z => functions.Zip(parameters, (f, p) => p * f(z)).Sum(); } } }