diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 32c01fa0..0200d0db 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -88,6 +88,7 @@ + diff --git a/src/Numerics/Optimization/BrentMinimizer.cs b/src/Numerics/Optimization/BrentMinimizer.cs index 4897de5c..08156946 100644 --- a/src/Numerics/Optimization/BrentMinimizer.cs +++ b/src/Numerics/Optimization/BrentMinimizer.cs @@ -1,10 +1,37 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2010 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// namespace MathNet.Numerics.Optimization { + using System; + /// /// Options for Brent Minimization. /// diff --git a/src/Numerics/Optimization/IMinimizer.cs b/src/Numerics/Optimization/IMinimizer.cs new file mode 100644 index 00000000..3e333a11 --- /dev/null +++ b/src/Numerics/Optimization/IMinimizer.cs @@ -0,0 +1,72 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2010 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace MathNet.Numerics.Optimization +{ + using System; + + /// + /// Interface implemented by a class that minimizes f(p) where p is a vector of model parameters. + /// A class implenting this interface can then be used to solve curve fitting problems. + /// + public interface IMinimizer + { + double[] Minimize(Func function, double[] pInitialGuess); + } + + public static class NonLinearCurveFitExtensions + { + /// + /// Non-Linear Least-Squares fitting the points (x,y) to a specified function of y : x -> f(x, p), p being a vector of parameters. + /// returning its best fitting parameters p. + /// + /// + /// + /// + /// Initial guess of parameters, p. + /// + public static double[] CurveFit(this IMinimizer minimizer, double[] x, double[] y, Func f, + double[] pStart) + { + // Need to minimize sum of squares of residuals; create this function: + Func function = (p) => + { + double sum = 0; + for (int i = 0; i < x.Length; ++i) + { + double temp = y[i] - f(x[i], p); + sum += temp * temp; + } + return sum; + }; + return minimizer.Minimize(function, pStart); + } + } +} diff --git a/src/Numerics/Optimization/NonLinearLeastSquaresMinimizer.cs b/src/Numerics/Optimization/NonLinearLeastSquaresMinimizer.cs index caaf1a99..387eb3ed 100644 --- a/src/Numerics/Optimization/NonLinearLeastSquaresMinimizer.cs +++ b/src/Numerics/Optimization/NonLinearLeastSquaresMinimizer.cs @@ -1,12 +1,39 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using MathNet.Numerics.Providers.Optimization; -using MathNet.Numerics.Providers.Optimization.Mkl; +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2010 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// namespace MathNet.Numerics.Optimization { + using System; + using MathNet.Numerics.Providers.Optimization; + using MathNet.Numerics.Providers.Optimization.Mkl; + /// /// Options for Non-Linear Least Squares Minimization. /// @@ -66,9 +93,9 @@ namespace MathNet.Numerics.Optimization /// /// This class is a special function minimizer that minimizes functions of the form - /// f(p) = |r(p)|^2 where r is a vector of residuals and p is a vector of model parameters. + /// f(p) = |r(p)|^2 where r is a vector of residuals and p is a vector of model parameters. /// - public class NonLinearLeastSquaresMinimizer + public class NonLinearLeastSquaresMinimizer // Note does not implement IMinimizer, since it curve fitting problems can be solved more efficiently. { public NonLinearLeastSquaresResult Result { get; private set; } diff --git a/src/Numerics/Optimization/PowellMinimizer.cs b/src/Numerics/Optimization/PowellMinimizer.cs index 884ebd43..4bb5931c 100644 --- a/src/Numerics/Optimization/PowellMinimizer.cs +++ b/src/Numerics/Optimization/PowellMinimizer.cs @@ -1,10 +1,38 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2010 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// namespace MathNet.Numerics.Optimization { + using System; + using System.Linq; + /// /// Options for Powell Minimization. /// @@ -33,38 +61,12 @@ namespace MathNet.Numerics.Optimization /// /// Minimizes f(p) where p is a vector of model parameters using the Powell method. /// - public class PowellMinimizer + public class PowellMinimizer : IMinimizer { public PowellResult Result { get; private set; } public readonly PowellOptions Options = new PowellOptions(); - /// - /// Non-Linear Least-Squares fitting the points (x,y) to a specified function of y : x -> f(x, p), p being a vector of parameters. - /// returning its best fitting parameters p. - /// - /// - /// - /// - /// Initial guess of parameters, p. - /// - public double[] CurveFit(double[] x, double[] y, Func f, - double[] pStart) - { - // Need to minimize sum of squares of residuals; create this function: - Func function = (p) => - { - double sum = 0; - for (int i = 0; i < x.Length; ++i) - { - double temp = y[i] - f(x[i], p); - sum += temp * temp; - } - return sum; - }; - return Minimize(function, pStart); - } - /// /// Find the minimum of the supplied function using the Powell method. /// diff --git a/src/UnitTests/OptimizationTests/FunctionMinimizationTests.cs b/src/UnitTests/OptimizationTests/FunctionMinimizationTests.cs index 55386034..6e4b0735 100644 --- a/src/UnitTests/OptimizationTests/FunctionMinimizationTests.cs +++ b/src/UnitTests/OptimizationTests/FunctionMinimizationTests.cs @@ -28,12 +28,12 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using System; -using MathNet.Numerics.Optimization; -using NUnit.Framework; - namespace MathNet.Numerics.UnitTests.OptimizationTests { + using System; + using MathNet.Numerics.Optimization; + using NUnit.Framework; + [TestFixture] public class FunctionMinimizationTests { diff --git a/src/UnitTests/OptimizationTests/NonLinearLeastSquaresTest.cs b/src/UnitTests/OptimizationTests/NonLinearLeastSquaresTest.cs index ef943205..6807061e 100644 --- a/src/UnitTests/OptimizationTests/NonLinearLeastSquaresTest.cs +++ b/src/UnitTests/OptimizationTests/NonLinearLeastSquaresTest.cs @@ -28,12 +28,12 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using System; -using MathNet.Numerics.Optimization; -using NUnit.Framework; - namespace MathNet.Numerics.UnitTests.OptimizationTests { + using System; + using MathNet.Numerics.Optimization; + using NUnit.Framework; + [TestFixture] public class NonLinearLeastSquaresTest {