teflow.mathext module

teflow.mathext.fermidirac(x, inverse=False)
Fermi-Dirac function:

1/(1+e^x)

The inverse function:

ln(1/y-1)

参数:
  • x (array_like) – Argument of the Fermi-Dirac function

  • inverse (bool, optional) – Calculate the value of inverse function, by default False

返回:

An array of the same shape as x

返回类型:

ndarray

teflow.mathext.interpx(x, y, xp, left=None, right=None)

Extends numpy.interp() to perform linear extrapolation for xp values outside the x range, differing from the constant extrapolation with end values or specified fill values used by the original function.

参数:
  • x (array_like) – A 1-dimensional array of x-coordinates of the data points. It must be strictly monotonically increasing and contain at least two elements.

  • y (array_like) – A 1-dimensional array of y-coordinates corresponding to x. The arrays x and y must have the same length.

  • xp (array_like) – The x-coordinates at which to interpolate/extrapolate values.

  • left (float, optional) – Value to return for x < xp[0], default linear extrapolation.

  • right (float, optional) – Value to return for x > xp[-1], default linear extrapolation.

返回:

Interpolated/extrapolated values at xp, with the output shape matching that of xp.

返回类型:

ndarray

teflow.mathext.mixing(datas, weight=None, scale=None)

Mix several datas with the same shape.

参数:
  • datas (sequence of array_like) – datas with the same shape

  • weight (sequence of float, optional) – weights of mixing, with the same shape of datas, by default ones_like(datas) array

  • scale (float, optional) – scale factor, by default 1/sum(weight)

返回:

mixing result, with the same shape of data

返回类型:

ndarray

teflow.mathext.smoothstep(x, inverse=False, shift=True)

Smoothstep function:

0, x < 0

3x^2-2x^3, x <= 0 <= 1

1, x > 1

The inverse function:

1/2 - sin(arcsin(1-2*y)/3)

It might be more convenient when the function is shifted x’ = (1-x)/2, which drops from 1 to 0 where -1 <= 0 <= 1.

参数:
  • x (array_like) – Argument of the smoothstep function (https://en.wikipedia.org/wiki/Smoothstep)

  • inverse (bool, optional) – Calculate the value of inverse function, by default False

  • shift (bool, optional) – shift the origin smoothstep function with x’ = (1-x)/2, by default True

返回:

An array of the same shape as x

返回类型:

ndarray

teflow.mathext.vinterp(x, y, xp, method='linear', reorder=True, **kwargs)

Vectorized interpolation of values.

参数:
  • x ((N,) array_like) – The x-coordinates of the data points.

  • y ((,N) array_like) – The y-coordinates corresponding to x. The length of the last dimension must be the same as x.

  • xp (array_like) – The x-coordinates at which to evaluate the interpolated values.

  • method (str, optional) –

    Specifies the interpolation method to be used. Supported methods include:

    • ’linear’ (default): Uses interpx() for linear interpolation.

    • ’line’: Uses raw numpy.interp() for linear interpolation.

    • ’poly<N>’: Uses numpy.polynomial.polynomial.Polynomial.fit() for polynomial interpolation of degree N. Replace <N> with the degree of the polynomial, e.g., ‘poly2’ for quadratic.

    • ’cubic’: Uses scipy.interpolate.CubicSpline() for cubic spline interpolation.

    • ’pchip’: Uses scipy.interpolate.PchipInterpolator() for Piecewise Cubic Hermite Interpolating Polynomial.

    • ’akima’: Uses scipy.interpolate.Akima1DInterpolator() for Akima interpolation.

    • ’spline’: Uses scipy.interpolate.make_interp_spline() to customize a B-spline representation of the data.

  • reorder (bool, optional) – If True (default), automatically reorders x and y if x is not monotonically increasing. If False, an error is raised if x is not monotonically increasing.

  • **kwargs (any, optional) – Additional keyword arguments to be passed to the corresponding interpolation method.

返回:

The interpolated values.

返回类型:

ndarray

teflow.mathext.vquad(func, a, b, args=(), *, where=True, fill_value=0, **kwargs)

Extend scipy.integrate.quad by adding support for broadcasting over a, b, args, where, and fill_value. The where and fill_value parameters are introduced for enhanced flexibility in controlling integration.

Note that this function is not a true ufunc in the numpy sense. It leverages broadcasting and Python loops to offer a convenient interface for integrating a function across varying ranges and with diverse parameters. It prioritizes ease of use over optimal performance.

参数:
  • func (callable) – A Python function or method to integrate.

  • a (array_like) – Lower limit of integration.

  • b (array_like) – Upper limit of integration.

  • args (tuple of array_like, optional) – Extra arguments to pass to func. Each element of args will be broadcasted to match the shape of a and b. Default is an empty tuple.

  • where (array_like, optional) – Boolean mask to specify where to perform the integration. Default is True, which means that the integration is performed everywhere.

  • fill_value (array_like, optional) – The value to use for masked positions. Default is 0.

  • **kwargs – Additional keyword arguments passed to scipy.integrate.quad.

返回:

  • ndarray – Array of computed integral values with the same shape as the broadcasted shape of a, b, and each element of args.

  • ndarray – Array of estimated integration errors with the same shape as the first returned ndarray.

示例

Consider a function func(x, m, n) defined as:

>>> def func(x, m, n):
...     return m*x**2 if x<0 else n*x**3

We can integrate func over the intervals [-1, 1] and [-2, 2] with args as (3, [[4], [8], [12]]) . Here, 3 corresponds to the parameter m in func, and the column vector [[4], [8], [12]] corresponds to the parameter n in func. The broadcasting results in an output shape of (3, 2):

>>> vquad(func, [-1, -2], [1, 2], args=(3, [[4], [8], [12]]))[0]
array([[ 2., 24.],
       [ 3., 40.],
       [ 4., 56.]])