//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// 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 IStation.Numerics.Differentiation;
namespace IStation.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);
}
}
}