pytanksim.utils.finitedifferences

Module for calculating derivatives numerically.

Functions

pardev(→ float)

Calculate the first derivative using centered finite difference.

partial_derivative(→ float)

Calculate the first partial derivative using centered finite difference.

backdev(→ float)

Calculate the first derivative using backwards finite difference.

backward_partial_derivative(→ float)

Find the first partial derivative using backwards finite difference.

fordev(→ float)

Calculate the first derivative using forwards finite difference.

forward_partial_derivative(→ float)

Find the first partial derivative using forwards finite difference.

second_derivative(→ float)

Find the second partial derivative using centered finite difference.

secforder(→ float)

Calculate the second derivative using forwards finite difference.

second_forward_derivative(→ float)

Find the second partial derivative using forwards finite difference.

secbackder(→ float)

Calculate the second derivative using backwards finite difference.

second_backward_derivative(→ float)

Find the second partial derivative using backwards finite difference.

mixed_second_derivative(→ float)

Find the mixed second derivative using finite difference.

Module Contents

pytanksim.utils.finitedifferences.pardev(func: Callable[[float], float], loc: float, stepsize: float) float

Calculate the first derivative using centered finite difference.

This function in particular only works with functions which have only one argument.

Parameters:
  • func (Callable[[float], float]) – A function that takes in a floating point number and outputs a floating point number.

  • loc (float) – Location where the first derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation.

Returns:

The first derivative of func evaluated at loc.

Return type:

float

pytanksim.utils.finitedifferences.partial_derivative(func: Callable[Ellipsis, float], var: int, point: List, stepsize: float = 0.001) float

Calculate the first partial derivative using centered finite difference.

This version of the function works for functions which have an arbitrary number of arguments (one or more).

Parameters:
  • func (Callable[... , float]) – A function that outputs a floating point number, and has at least one argument which is a floating point number.

  • var (int) –

    The integer showing the input order of the independent variable with respect to which the derivative of func is to be approximated. It uses python’s convention for indexing (i.e., it counts from 0, 1, 2, 3, …). For example, if the function func has the following signature:

    def some_function(x1, x2, x3):
        ....
        return y
    

    If we want to find the partial derivative w.r.t. x3, then var should be 2.

  • point (List) – A list of input values for func. These input values indicate the location where the partial derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation. The default is 1e-3.

Returns:

The first partial derivative of func w.r.t. the variable specified by var evaluated at point.

Return type:

float

pytanksim.utils.finitedifferences.backdev(func: Callable[[float], float], loc: float, stepsize: float) float

Calculate the first derivative using backwards finite difference.

This function in particular only works with functions which have only one argument.

Parameters:
  • func (Callable[[float], float]) – A function that takes in a floating point number and outputs a floating point number.

  • loc (float) – Location where the first derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation.

Returns:

The first derivative of func evaluated at loc.

Return type:

float

pytanksim.utils.finitedifferences.backward_partial_derivative(func: Callable[Ellipsis, float], var: int, point: List, stepsize: float = 0.001) float

Find the first partial derivative using backwards finite difference.

This version of the function works for functions which have an arbitrary number of arguments (one or more).

Parameters:
  • func (Callable[... , float]) – A function that outputs a floating point number, and has at least one argument which is a floating point number.

  • var (int) –

    The integer showing the input order of the independent variable with respect to which the derivative of func is to be approximated. It uses python’s convention for indexing (i.e., it counts from 0, 1, 2, 3, …). For example, if the function func has the following signature:

    def some_function(x1, x2, x3):
        ....
        return y
    

    If we want to find the partial derivative w.r.t. x3, then var should be 2.

  • point (List) – A list of input values for func. These input values indicate the location where the partial derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation. The default is 1e-3.

Returns:

The first partial derivative of func w.r.t. the variable specified by var evaluated at point.

Return type:

float

pytanksim.utils.finitedifferences.fordev(func: Callable[[float], float], loc: float, stepsize: float) float

Calculate the first derivative using forwards finite difference.

This function in particular only works with functions which have only one argument.

Parameters:
  • func (Callable[[float], float]) – A function that takes in a floating point number and outputs a floating point number.

  • loc (float) – Location where the first derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation.

Returns:

The first derivative of func evaluated at loc.

Return type:

float

pytanksim.utils.finitedifferences.forward_partial_derivative(func: Callable[Ellipsis, float], var: int, point: List, stepsize: float = 0.001) float

Find the first partial derivative using forwards finite difference.

This version of the function works for functions which have an arbitrary number of arguments (one or more).

Parameters:
  • func (Callable[... , float]) – A function that outputs a floating point number, and has at least one argument which is a floating point number.

  • var (int) –

    The integer showing the input order of the independent variable with respect to which the derivative of func is to be approximated. It uses python’s convention for indexing (i.e., it counts from 0, 1, 2, 3, …). For example, if the function func has the following signature:

    def some_function(x1, x2, x3):
        ....
        return y
    

    If we want to find the partial derivative w.r.t. x3, then var should be 2.

  • point (List) – A list of input values for func. These input values indicate the location where the partial derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation. The default is 1e-3.

Returns:

The first partial derivative of func w.r.t. the variable specified by var evaluated at point.

Return type:

float

pytanksim.utils.finitedifferences.second_derivative(func: Callable[Ellipsis, float], var: int, point: List, stepsize: float = 1e-06) float

Find the second partial derivative using centered finite difference.

This version of the function works for functions which have an arbitrary number of arguments (one or more).

Parameters:
  • func (Callable[... , float]) – A function that outputs a floating point number, and has at least one argument which is a floating point number.

  • var (int) –

    The integer showing the input order of the independent variable with respect to which the derivative of func is to be approximated. It uses python’s convention for indexing (i.e., it counts from 0, 1, 2, 3, …). For example, if the function func has the following signature:

    def some_function(x1, x2, x3):
        ....
        return y
    

    If we want to find the second partial derivative w.r.t. x3, then var should be 2.

  • point (List) – A list of input values for func. These input values indicate the location where the second partial derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation. The default is 1e-6.

Returns:

The second partial derivative of func w.r.t. the variable specified by var evaluated at point.

Return type:

float

pytanksim.utils.finitedifferences.secforder(function: Callable[[float], float], location: float, stepsize: float = 1e-06) float

Calculate the second derivative using forwards finite difference.

This function in particular only works with functions which have only one argument.

Parameters:
  • function (Callable[[float], float]) – A function that takes in a floating point number and outputs a floating point number.

  • location (float) – Location where the second derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation. The default is 1e-6.

Returns:

The second derivative of function evaluated at location.

Return type:

float

pytanksim.utils.finitedifferences.second_forward_derivative(func: Callable[Ellipsis, float], var: int, point: List, stepsize: float = 1e-06) float

Find the second partial derivative using forwards finite difference.

This version of the function works for functions which have an arbitrary number of arguments (one or more).

Parameters:
  • func (Callable[... , float]) – A function that outputs a floating point number, and has at least one argument which is a floating point number.

  • var (int) –

    The integer showing the input order of the independent variable with respect to which the derivative of func is to be approximated. It uses python’s convention for indexing (i.e., it counts from 0, 1, 2, 3, …). For example, if the function func has the following signature:

    def some_function(x1, x2, x3):
        ....
        return y
    

    If we want to find the second partial derivative w.r.t. x3, then var should be 2.

  • point (List) – A list of input values for func. These input values indicate the location where the second partial derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation. The default is 1e-6.

Returns:

The second partial derivative of func w.r.t. the variable specified by var evaluated at point.

Return type:

float

pytanksim.utils.finitedifferences.secbackder(function: Callable[[float], float], location: float, stepsize: float = 1e-06) float

Calculate the second derivative using backwards finite difference.

This function in particular only works with functions which have only one argument.

Parameters:
  • function (Callable[[float], float]) – A function that takes in a floating point number and outputs a floating point number.

  • location (float) – Location where the second derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation. The default is 1e-6.

Returns:

The first derivative of function evaluated at location.

Return type:

float

pytanksim.utils.finitedifferences.second_backward_derivative(func: Callable[Ellipsis, float], var: int, point: List, stepsize: float = 1e-06) float

Find the second partial derivative using backwards finite difference.

This version of the function works for functions which have an arbitrary number of arguments (one or more).

Parameters:
  • func (Callable[... , float]) – A function that outputs a floating point number, and has at least one argument which is a floating point number.

  • var (int) –

    The integer showing the input order of the independent variable with respect to which the derivative of func is to be approximated. It uses python’s convention for indexing (i.e., it counts from 0, 1, 2, 3, …). For example, if the function func has the following signature:

    def some_function(x1, x2, x3):
        ....
        return y
    

    If we want to find the second partial derivative w.r.t. x3, then var should be 2.

  • point (List) – A list of input values for func. These input values indicate the location where the second partial derivative is to be evaluated.

  • stepsize (float) – The stepsize for the finite difference approximation. The default is 1e-6.

Returns:

The second partial derivative of func w.r.t. the variable specified by var evaluated at point.

Return type:

float

pytanksim.utils.finitedifferences.mixed_second_derivative(func: Callable[Ellipsis, float], var: List[int], point: List, stepsize: List[float] = [1000.0, 0.0001]) float

Find the mixed second derivative using finite difference.

This version of the function works for functions which have an arbitrary number of arguments (two or more).

Parameters:
  • func (Callable[... , float]) – A function that outputs a floating point number, and has at least two arguments which are floating point numbers.

  • var (List[int]) –

    A list of integers showing the input order of the two variables with respect to which the mixed second derivative of func is to be approximated. It uses python’s convention for indexing (i.e., it counts from 0, 1, 2, 3, …). For example, if the function func has the following signature:

    def some_function(x1, x2, x3):
        ....
        return y
    

    If we want to find the mixed second partial derivative w.r.t. x1 and x3, then var should be [0, 2].

  • point (List) – A list of input values for func. These input values indicate the location where the mixed second partial derivative is to be evaluated.

  • stepsize (List[float]) – The stepsizes for the finite difference approximation. It is a list of two floating point numbers. The default is [1E3, 1E-4].

Returns:

The mixed second partial derivative of func w.r.t. the two variables specified by var evaluated at point.

Return type:

float