Approximation#

sigmaepsilon.math.approx.lagrange.approx_Lagrange_1d(source: Iterable, target: Iterable, lambdify: bool = False) Callable[source]#

Returns a callable that maps from ‘source’ to ‘target’ in 1d.

Parameters:
  • source (Iterable[float]) – An n-tuple of floats.

  • target (Iterable[float]) – An n-tuple of floats.

  • lambdify (bool, Optional) – If True, the returned function is turned into a lambda function. Default is False.

Returns:

A vectorized function if ‘lambdify’ is True, or a SymPy expression otherwise.

Return type:

Callable

Example

>>> from sigmaepsilon.math.approx.lagrange import approx_1d
>>> approx = approx_1d([-1, 1], [0, 10], lambdify=True)
>>> approx(-1), approx(1), approx(0)
(0, 10, 5)

A symbolic example:

>>> import sympy as sy
>>> L = sy.symbols('L', real=True, positive=True)
>>> fnc = approx_1d([-1, 1], [0, L])
>>> str(fnc)
'L*(x/2 + 1/2)'

To get the Jacobian of the transformation [0, L] -> [-1, 1]:

>>> L = sy.symbols('L', real=True, positive=True)
>>> fnc = approx_1d([0, L], [-1, 1])
>>> dfnc = fnc.diff('x')
>>> str(dfnc)
'2/L'

Or the other way:

>>> L = sy.symbols('L', real=True, positive=True)
>>> fnc = approx_1d([-1, 1], [0, L])
>>> dfnc = fnc.diff('x')
>>> str(dfnc)
'L/2'
sigmaepsilon.math.approx.lagrange.gen_Lagrange_1d(*_, x: Iterable | None = None, i: Iterable[int] | None = None, xsym: str | None = None, fsym: str | None = None, sym: bool = False, N: int | None = None, lambdify: bool = False, out: dict | None = None) dict[source]#

Generates Lagrange polynomials and their derivatives up to 3rd, for approximation in 1d space, based on N input pairs of position and value. Geometrical parameters can be numeric or symbolic.

Parameters:
  • x (Iterable, Optional) – The locations of the data points. If not specified and sym=False, a range of [-1, 1] is assumed and the locations are generated as np.linspace(-1, 1, N), where N is the number of data points. If sym=True, the calculation is entirely symbolic. Default is None.

  • i (Iterable[int]) – If not specified, indices are assumed as [1, …, N], but this is only relevant for symbolic representation, not the calculation itself, which only cares about the number of data points, regardless of their actual indices.

  • xsym (str, Optional) – Symbol of the variable in the symbolic representation of the generated functions. Default is \(x\).

  • fsym (str, Optional) – Symbol of the function in the symbolic representation of the generated functions. Default is ‘f’.

  • sym (bool, Optional.) – If True, locations of the data points are left in a symbolic state. This requires the inversion of a symbolic matrix, which has some reasonable limitations. Default is False.

  • N (int, Optional) – If neither ‘x’ nor ‘i’ is specified, this controls the number of functions to generate. Default is None.

  • lambdify (bool, Optional) – If True, the functions are turned into lambda functions and stored in the output for each index with keyword ‘fnc’. Default is False.

  • out (dict, Optional) – A dictionary to store the values in. Default is None.

Returns:

A dictionary containing the generated functions for the reuested nodes. The keys of the dictionary are the indices of the points, the values are dictionaries with the following keys and values:

symbol : the SymPy symbol of the function

0 : the function

1 : the first derivative as a SymPy expression

2 : the second derivative as a SymPy expression

3 : the third derivative as a SymPy expression

Return type:

dict

Example

>>> from sigmaepsilon.math.approx.lagrange import gen_Lagrange_1d

To generate approximation functions for a 2-noded line:

>>> gen_Lagrange_1d(x=[-1, 1])

or equivalently

>>> gen_Lagrange_1d(N=2)

To generate the same functions in symbolic form:

>>> gen_Lagrange_1d(i=[1, 2], sym=True)

Notes

Inversion of a heavily symbolic matrix may take quite some time, and is not suggested for N > 3. Fixing the locations as constant real numbers symplifies the process and makes the solution much faster.