turboflow.math module

turboflow.math.all_non_negative(array)[source]

Check if all items in Numpy array are non-negative

turboflow.math.all_numeric(array)[source]

Check if all items in Numpy array are numeric (floats or ints)

turboflow.math.arccosd(x)[source]

Compute the arccosine of a value and return the result in degrees.

turboflow.math.arcsind(x)[source]

Compute the arcsine of a value and return the result in degrees.

turboflow.math.arctand(x)[source]

Compute the arctangent of a value and return the result in degrees.

turboflow.math.cosd(x)[source]

Compute the cosine of an angle given in degrees.

turboflow.math.is_even(number)[source]

Check if a number is even. Returns True if the provided number is even, and False otherwise.

turboflow.math.is_odd(number)[source]

Check if a number is odd. Returns True if the provided number is odd, and False otherwise.

turboflow.math.sigmoid_hyperbolic(x, x0=0.5, alpha=1)[source]

Compute the sigmoid hyperbolic function.

This function calculates a sigmoid function based on the hyperbolic tangent. The formula is given by:

\[\sigma(x) = \frac{1 + \tanh\left(\frac{x - x_0}{\alpha}\right)}{2}\]
Parameters:
xarray_like

Input data.

x0float, optional

Center of the sigmoid function. Default is 0.5.

alphafloat, optional

Scale parameter. Default is 0.1.

Returns:
array_like

Output of the sigmoid hyperbolic function.

turboflow.math.sigmoid_rational(x, n, m)[source]

Compute the sigmoid rational function.

This function calculates a sigmoid function using an algebraic approach based on a rational function. The formula is given by:

\[\sigma(x) = \frac{x^n}{x^n + (1-x)^m}\]
Parameters:
xarray_like

Input data.

nint

Power of the numerator.

mint

Power of the denominator.

Returns:
array_like

Output of the sigmoid algebraic function.

turboflow.math.sigmoid_smootherstep(x)[source]

Compute the smoother step function.

This function calculates a smoother step function with zero second-order derivatives at the endpoints. It is a modification of the smoothstep function to provide smoother transitions.

Parameters:
xarray_like

Input data.

Returns:
array_like

Output of the sigmoid smootherstep function.

turboflow.math.sigmoid_smoothstep(x)[source]

Compute the smooth step function.

This function calculates a smooth step function with zero first-order derivatives at the endpoints. More information available at: https://resources.wolframcloud.com/FunctionRepository/resources/SmoothStep/

Parameters:
xarray_like

Input data.

Returns:
array_like

Output of the sigmoid smoothstep function.

turboflow.math.sind(x)[source]

Compute the sine of an angle given in degrees.

turboflow.math.smooth_abs(x, method='quadratic', epsilon=1e-05)[source]

Compute a smooth approximation of the absolute value function according to the specified method.

  1. The quadratic approximation is given by [Ramirez et al., 2013]:

\[f_{\epsilon}(x) = \sqrt{x^2 + \epsilon}\]
  1. The hyperbolic tangent approximation is given by [Bagul, 2017]:

\[f_{\epsilon}(x) = \epsilon \tanh{x / \epsilon}\]
  1. The log-cosh approximation is given by [Saleh and Saleh, 2022]:

\[f_{\epsilon}(x) = \epsilon \log\left(\cosh\left(\frac{x}{\epsilon}\right)\right)\]

The quadratic method is the most computationally efficient, but also requires a smaller value of \(\epsilon\) to yield a good approximation. The transcendental methods give a better approximation to the absolute value function, at the expense of a higher computational time.

Parameters:
xfloat or np.ndarray

Input value(s) for which the approximation is computed.

methodstr, optional

The method of approximation:

  • quadratic (default)

  • hyperbolic

  • logcosh

epsilonfloat, optional

A small positive constant affecting the approximation. Default is 1e-5.

Returns:
float or np.ndarray

Smooth approximation value(s) of the absolute function.

Raises:
ValueError

If an unsupported approximation method is provided.

turboflow.math.smooth_maximum(x1, x2, method='boltzmann', alpha=10)[source]

Element-wise smoooth maximum approximation of array elements. Smoothed version of numpy.maximum().

The \(p\)-norm approximation to the maximum is given by [Weisstein, 2023]:

\[f_{\alpha}(x) = \left( \sum_i x_i^\alpha \right)^{\frac{1}{\alpha}}\]

The Boltzmann approximation is given by [Blanchard et al., 2021]:

\[f_{\alpha}(x) = \frac{1}{\alpha} \log \left( \sum_i e^{\alpha \,(x_i - \hat{x})} \right) + \hat{x}\]

This LogSumExp approximation is given by [Blanchard et al., 2021]:

\[f_{\alpha}(x) = \frac{1}{\alpha} \log \left( \sum_i e^{\alpha \,(x_i - \hat{x})} \right) + \hat{x}\]

where the shift \(\hat{x}\) is defined as:

\[\hat{x} = \text{sign}(\alpha) \cdot \max(\text{sign}(\alpha) \cdot x)\]

Shifting x prevents numerical overflow due to the finite precision of floating-point operations

Parameters:
xarray_like

Input data.

methodstr, optional

Method to be used for the approximation. Supported methods are:

  • boltzmann (default)

  • logsumexp

  • p-norm

alphafloat, optional

Sharpness parameter. Large values produce a tight approximation to the maximum function

axisNone or int or tuple of ints, optional

Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of ints, the maximum is selected over multiple axes, instead of a single axis or all the axes as before.

keepdimsbool, optional

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

Returns:
max_approxndarray or scalar

Approximate maximum of x using the specified method.

Raises:
ValueError

If an unsupported method is specified.

turboflow.math.smooth_minimum(x1, x2, method='boltzmann', alpha=10)[source]

Element-wise smoooth minimum approximation of array elements. Smoothed version of numpy.minimum().

The smooth minimum approximation is equivalent to the smooth maximum with a negative value of the sharpness parameter \(\alpha\). See documentation of the `smooth_max() function for more information about the smoothing methods available.

Parameters:
xarray_like

Input data.

methodstr, optional

Method to be used for the approximation. Supported methods are:

  • boltzmann (default)

  • logsumexp

  • p-norm

alphafloat, optional

Sharpness parameter.Large values produce a tight approximation to the minimum function

axisNone or int or tuple of ints, optional

Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of ints, the minimum is selected over multiple axes, instead of a single axis or all the axes as before.

keepdimsbool, optional

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

Returns:
min_approxndarray or scalar

Approximate minimum of x using the specified method.

Raises:
ValueError

If an unsupported method is specified.

turboflow.math.tand(x)[source]

Compute the tangent of an angle given in degrees.