From 9570ae1d6ee42d0cbb968a4973ad6d77d3dacc20 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Wed, 1 May 2013 21:14:58 +0200 Subject: [PATCH] RootFinding: file headers, white space --- MathNet.Numerics.sln.DotSettings | 34 ++++++++++ src/Numerics/RootFinding/Bracketing.cs | 38 +++++++++-- src/Numerics/RootFinding/FindRoots.cs | 74 +++++++++++++++------ src/UnitTests/RootFindingTests/BrentTest.cs | 32 ++++++++- 4 files changed, 151 insertions(+), 27 deletions(-) diff --git a/MathNet.Numerics.sln.DotSettings b/MathNet.Numerics.sln.DotSettings index d7e15e370..76dcc8894 100644 --- a/MathNet.Numerics.sln.DotSettings +++ b/MathNet.Numerics.sln.DotSettings @@ -1,4 +1,6 @@  + <?xml version="1.0" encoding="utf-16"?><Profile name="Full Cleanup (Math.NET)"><CSArrangeThisQualifier>True</CSArrangeThisQualifier><CSRemoveCodeRedundancies>True</CSRemoveCodeRedundancies><CSUseAutoProperty>True</CSUseAutoProperty><CSMakeFieldReadonly>True</CSMakeFieldReadonly><CSUpdateFileHeader>True</CSUpdateFileHeader><CSOptimizeUsings><OptimizeUsings>True</OptimizeUsings><EmbraceInRegion>False</EmbraceInRegion><RegionName></RegionName></CSOptimizeUsings><CSShortenReferences>True</CSShortenReferences><CSReformatCode>True</CSReformatCode><XMLReformatCode>True</XMLReformatCode><CssAlphabetizeProperties>True</CssAlphabetizeProperties><CssReformatCode>True</CssReformatCode><JsReformatCode>True</JsReformatCode><JsInsertSemicolon>True</JsInsertSemicolon><VBFormatDocComments>True</VBFormatDocComments><VBReformatCode>True</VBReformatCode><VBShortenReferences>True</VBShortenReferences><VBOptimizeImports>True</VBOptimizeImports><HtmlReformatCode>True</HtmlReformatCode><AspOptimizeRegisterDirectives>True</AspOptimizeRegisterDirectives><CSReorderTypeMembers>True</CSReorderTypeMembers><CSUseVar><BehavourStyle>CAN_CHANGE_BOTH</BehavourStyle><LocalVariableStyle>IMPLICIT_WHEN_INITIALIZER_HAS_TYPE</LocalVariableStyle><ForeachVariableStyle>ALWAYS_EXPLICIT</ForeachVariableStyle></CSUseVar></Profile> + Full Cleanup (Math.NET) False False False @@ -15,5 +17,37 @@ False True False + <copyright file="$FILENAME$" company="Math.NET"> +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-$CURRENT_YEAR$ 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. +</copyright> + CDF + SAS + SPSS <data /> <data><IncludeFilters /><ExcludeFilters /></data> \ No newline at end of file diff --git a/src/Numerics/RootFinding/Bracketing.cs b/src/Numerics/RootFinding/Bracketing.cs index 0cc1f6035..f90fb8bbb 100644 --- a/src/Numerics/RootFinding/Bracketing.cs +++ b/src/Numerics/RootFinding/Bracketing.cs @@ -1,4 +1,34 @@ -using System; +// +// 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-2013 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. +// + +using System; using MathNet.Numerics.Properties; namespace MathNet.Numerics.RootFinding @@ -23,7 +53,7 @@ public static bool SearchOutward(Func f, ref double xmin, ref do double fmin = f(xmin); double fmax = f(xmax); - for(int i=0;i f, ref double xmin, ref do if (Math.Abs(fmin) < Math.Abs(fmax)) { - xmin += factor * (xmin - xmax); + xmin += factor*(xmin - xmax); fmin = f(xmin); } else { - xmax += factor * (xmax - xmin); + xmax += factor*(xmax - xmin); fmax = f(xmax); } } diff --git a/src/Numerics/RootFinding/FindRoots.cs b/src/Numerics/RootFinding/FindRoots.cs index 6a4e243bd..b62b58b8a 100644 --- a/src/Numerics/RootFinding/FindRoots.cs +++ b/src/Numerics/RootFinding/FindRoots.cs @@ -1,22 +1,52 @@ -using System; +// +// 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-2013 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. +// + +using System; namespace MathNet.Numerics.RootFinding { - public static class FindRoots - { - /// Find a solution of the equation f(x)=0. - /// The function to find roots from. - /// The low value of the range where the root is supposed to be. - /// The high value of the range where the root is supposed to be. - /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. - /// Maximum number of iterations. Usually 100. - /// Returns the root with the specified accuracy. + public static class FindRoots + { + /// Find a solution of the equation f(x)=0. + /// The function to find roots from. + /// The low value of the range where the root is supposed to be. + /// The high value of the range where the root is supposed to be. + /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. + /// Maximum number of iterations. Usually 100. + /// Returns the root with the specified accuracy. /// /// Algorithm by by Brent, Van Wijngaarden, Dekker et al. /// Implementation inspired by Press, Teukolsky, Vetterling, and Flannery, "Numerical Recipes in C", 2nd edition, Cambridge University Press /// /// - public static double BrentMethod(Func f, double xmin, double xmax, double accuracy = 1e-8, int maxIterations = 100) + public static double BrentMethod(Func f, double xmin, double xmax, double accuracy = 1e-8, int maxIterations = 100) { double fxmin = f(xmin); double fxmax = f(xmax); @@ -45,8 +75,8 @@ public static double BrentMethod(Func f, double xmin, double xma } // convergence check - double xAcc1 = 2.0 * Precision.DoubleMachinePrecision * Math.Abs(root) + 0.5 * accuracy; - double xMid = (xmax - root) / 2.0; + double xAcc1 = 2.0*Precision.DoubleMachinePrecision*Math.Abs(root) + 0.5*accuracy; + double xMid = (xmax - root)/2.0; if (Math.Abs(xMid) <= xAcc1 || froot.AlmostEqualWithAbsoluteError(0, froot, accuracy)) { return root; @@ -55,20 +85,20 @@ public static double BrentMethod(Func f, double xmin, double xma if (Math.Abs(e) >= xAcc1 && Math.Abs(fxmin) > Math.Abs(froot)) { // Attempt inverse quadratic interpolation - double s = froot / fxmin; + double s = froot/fxmin; double p; double q; if (xmin.AlmostEqual(xmax)) { - p = 2.0 * xMid * s; + p = 2.0*xMid*s; q = 1.0 - s; } else { - q = fxmin / fxmax; - double r = froot / fxmax; - p = s * (2.0 * xMid * q * (q - r) - (root - xmin) * (r - 1.0)); - q = (q - 1.0) * (r - 1.0) * (s - 1.0); + q = fxmin/fxmax; + double r = froot/fxmax; + p = s*(2.0*xMid*q*(q - r) - (root - xmin)*(r - 1.0)); + q = (q - 1.0)*(r - 1.0)*(s - 1.0); } if (p > 0.0) @@ -77,11 +107,11 @@ public static double BrentMethod(Func f, double xmin, double xma q = -q; } p = Math.Abs(p); - if (2.0 * p < Math.Min(3.0 * xMid * q - Math.Abs(xAcc1 * q), Math.Abs(e * q))) + if (2.0*p < Math.Min(3.0*xMid*q - Math.Abs(xAcc1*q), Math.Abs(e*q))) { // Accept interpolation e = d; - d = p / q; + d = p/q; } else { @@ -119,5 +149,5 @@ static double Sign(double a, double b) { return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a); } - } + } } diff --git a/src/UnitTests/RootFindingTests/BrentTest.cs b/src/UnitTests/RootFindingTests/BrentTest.cs index 2956caadc..1610b7421 100644 --- a/src/UnitTests/RootFindingTests/BrentTest.cs +++ b/src/UnitTests/RootFindingTests/BrentTest.cs @@ -1,4 +1,34 @@ -using MathNet.Numerics.RootFinding; +// +// 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-2013 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. +// + +using MathNet.Numerics.RootFinding; using NUnit.Framework; namespace MathNet.Numerics.UnitTests.RootFindingTests