From f3c123f84ddfd6f24219e4f21ae3d09d483989b6 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 10 Jan 2015 12:10:01 +0100 Subject: [PATCH] Differentiation: Static facade class for simple use cases --- src/Numerics/Differentiate.cs | 208 ++++++++++++++++++ .../FiniteDifferenceCoefficients.cs | 2 +- .../Differentiation/NumericalDerivative.cs | 4 +- src/Numerics/Integrate.cs | 2 +- src/Numerics/Numerics.csproj | 1 + 5 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 src/Numerics/Differentiate.cs diff --git a/src/Numerics/Differentiate.cs b/src/Numerics/Differentiate.cs new file mode 100644 index 00000000..1774149f --- /dev/null +++ b/src/Numerics/Differentiate.cs @@ -0,0 +1,208 @@ +// +// 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-2015 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.Differentiation; + +namespace MathNet.Numerics +{ + /// + /// Numerical Derivative. + /// + public static class Differentiate + { + /// + /// Initialized a NumericalDerivative with the given points and center. + /// + public static NumericalDerivative Points(int points, int center) + { + return new NumericalDerivative(points, center); + } + + /// + /// Initialized a NumericalDerivative with the default points and center for the given order. + /// + public static NumericalDerivative Order(int order) + { + var points = order + (order.IsEven() ? 1 : 2); + return new NumericalDerivative(points, points/2); + } + + /// + /// Evaluates the derivative of a scalar univariate function. + /// + /// Univariate function handle. + /// Point at which to evaluate the derivative. + /// Derivative order. + public static double Derivative(Func f, double x, int order) + { + return Order(order).EvaluateDerivative(f, x, order); + } + + /// + /// Creates a function handle for the derivative of a scalar univariate function. + /// + /// Univariate function handle. + /// Derivative order. + public static Func DerivativeFunc(Func f, int order) + { + return Order(order).CreateDerivativeFunctionHandle(f, order); + } + + /// + /// Evaluates the first derivative of a scalar univariate function. + /// + /// Univariate function handle. + /// Point at which to evaluate the derivative. + public static double FirstDerivative(Func f, double x) + { + return Order(1).EvaluateDerivative(f, x, 1); + } + + /// + /// Creates a function handle for the first derivative of a scalar univariate function. + /// + /// Univariate function handle. + public static Func FirstDerivativeFunc(Func f) + { + return Order(1).CreateDerivativeFunctionHandle(f, 1); + } + + /// + /// Evaluates the second derivative of a scalar univariate function. + /// + /// Univariate function handle. + /// Point at which to evaluate the derivative. + public static double SecondDerivative(Func f, double x) + { + return Order(2).EvaluateDerivative(f, x, 2); + } + + /// + /// Creates a function handle for the second derivative of a scalar univariate function. + /// + /// Univariate function handle. + public static Func SecondDerivativeFunc(Func f) + { + return Order(2).CreateDerivativeFunctionHandle(f, 2); + } + + /// + /// Evaluates the partial derivative of a multivariate function. + /// + /// Multivariate function handle. + /// Vector at which to evaluate the derivative. + /// Index of independent variable for partial derivative. + /// Derivative order. + public static double PartialDerivative(Func f, double[] x, int parameterIndex, int order) + { + return Order(order).EvaluatePartialDerivative(f, x, parameterIndex, order); + } + + /// + /// Creates a function handle for the partial derivative of a multivariate function. + /// + /// Multivariate function handle. + /// Index of independent variable for partial derivative. + /// Derivative order. + public static Func PartialDerivativeFunc(Func f, int parameterIndex, int order) + { + return Order(order).CreatePartialDerivativeFunctionHandle(f, parameterIndex, order); + } + + /// + /// Evaluates the first partial derivative of a multivariate function. + /// + /// Multivariate function handle. + /// Vector at which to evaluate the derivative. + /// Index of independent variable for partial derivative. + public static double FirstPartialDerivative(Func f, double[] x, int parameterIndex) + { + return PartialDerivative(f, x, parameterIndex, 1); + } + + /// + /// Creates a function handle for the first partial derivative of a multivariate function. + /// + /// Multivariate function handle. + /// Index of independent variable for partial derivative. + public static Func FirstPartialDerivativeFunc(Func f, int parameterIndex) + { + return PartialDerivativeFunc(f, parameterIndex, 1); + } + + /// + /// Evaluates the partial derivative of a bivariate function. + /// + /// Bivariate function handle. + /// First argument at which to evaluate the derivative. + /// Second argument at which to evaluate the derivative. + /// Index of independent variable for partial derivative. + /// Derivative order. + public static double PartialDerivative2(Func f, double x, double y, int parameterIndex, int order) + { + return Order(order).EvaluatePartialDerivative(array => f(array[0], array[1]), new[] { x, y }, parameterIndex, order); + } + + /// + /// Creates a function handle for the partial derivative of a bivariate function. + /// + /// Bivariate function handle. + /// Index of independent variable for partial derivative. + /// Derivative order. + public static Func PartialDerivative2Func(Func f, int parameterIndex, int order) + { + var handle = Order(order).CreatePartialDerivativeFunctionHandle(array => f(array[0], array[1]), parameterIndex, order); + return (x, y) => handle(new[] { x, y }); + } + + /// + /// Evaluates the first partial derivative of a bivariate function. + /// + /// Bivariate function handle. + /// First argument at which to evaluate the derivative. + /// Second argument at which to evaluate the derivative. + /// Index of independent variable for partial derivative. + public static double FirstPartialDerivative2(Func f, double x, double y, int parameterIndex) + { + return PartialDerivative2(f, x, y, parameterIndex, 1); + } + + /// + /// Creates a function handle for the first partial derivative of a bivariate function. + /// + /// Bivariate function handle. + /// Index of independent variable for partial derivative. + public static Func FirstPartialDerivative2Func(Func f, int parameterIndex) + { + return PartialDerivative2Func(f, parameterIndex, 1); + } + } +} diff --git a/src/Numerics/Differentiation/FiniteDifferenceCoefficients.cs b/src/Numerics/Differentiation/FiniteDifferenceCoefficients.cs index 6c5de78f..142db86d 100644 --- a/src/Numerics/Differentiation/FiniteDifferenceCoefficients.cs +++ b/src/Numerics/Differentiation/FiniteDifferenceCoefficients.cs @@ -84,7 +84,7 @@ namespace MathNet.Numerics.Differentiation if (order >= _coefficients.Length) throw new ArgumentOutOfRangeException("order", "Maximum difference order is points-1."); - // Return proper row + // Return proper row var columns = _coefficients[center].GetLength(1); var array = new double[columns]; for (int i = 0; i < columns; ++i) diff --git a/src/Numerics/Differentiation/NumericalDerivative.cs b/src/Numerics/Differentiation/NumericalDerivative.cs index a0f88e12..58534dea 100644 --- a/src/Numerics/Differentiation/NumericalDerivative.cs +++ b/src/Numerics/Differentiation/NumericalDerivative.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.Differentiation /// /// Class to evaluate the numerical derivative of a function using finite difference approximations. /// Variable point and center methods can be initialized . - /// This class can also be used to return function handles (delagates) for a fixed derivative order and variable. + /// This class can also be used to return function handles (delegates) for a fixed derivative order and variable. /// It is possible to evaluate the derivative and partial derivative of univariate and multivariate functions respectively. /// public class NumericalDerivative @@ -94,7 +94,7 @@ namespace MathNet.Numerics.Differentiation public double BaseStepSize { get { return _baseStepSize; } - set + set { //Base 2 yields more accurate results... var p = Math.Log(Math.Abs(value)) / Math.Log(2); diff --git a/src/Numerics/Integrate.cs b/src/Numerics/Integrate.cs index 0499d21d..84398dbe 100644 --- a/src/Numerics/Integrate.cs +++ b/src/Numerics/Integrate.cs @@ -34,7 +34,7 @@ using MathNet.Numerics.Integration; namespace MathNet.Numerics { /// - /// Numeric Integration (Quadrature). + /// Numerical Integration (Quadrature). /// public static class Integrate { diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 2172e7d4..b84e6f97 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -80,6 +80,7 @@ +