jaxprop.math module#

jaxprop.math.all_non_negative(array)[source]#

Check if all items in Numpy array are non-negative

jaxprop.math.all_numeric(array)[source]#

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

jaxprop.math.arccosd(x)[source]#

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

jaxprop.math.arcsind(x)[source]#

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

jaxprop.math.arctand(x)[source]#

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

jaxprop.math.cosd(x)[source]#

Compute the cosine of an angle given in degrees.

jaxprop.math.is_even(number)[source]#

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

jaxprop.math.is_odd(number)[source]#

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

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

Compute a sigmoid using the hyperbolic tangent.

The formula is

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

Input data.

x0float, optional

Center of the sigmoid; default 0.5.

alphafloat, optional

Nonzero scale parameter; default 1. A positive value gives an increasing transition, while a negative value reverses its direction. Its magnitude controls the transition width. The scale is not validated at runtime.

Returns:
jax.Array

Sigmoid values with the input shape.

jaxprop.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.

jaxprop.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.

jaxprop.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.

jaxprop.math.sind(x)[source]#

Compute the sine of an angle given in degrees.

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

Compute an element-wise smooth approximation of the absolute value.

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

\[f_{\epsilon}(x) = \sqrt{x^2 + \epsilon}.\]

The hyperbolic-tangent approximation is given by [Bagul, 2017]:

\[f_{\epsilon}(x) = x\tanh\left(\frac{x}{\epsilon}\right).\]

The log-cosh approximation is given by [Saleh and Saleh, 2022]:

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

All three are even, smooth functions approaching \(|x|\) as positive \(\epsilon\) tends to zero. Their smoothing scales differ: the quadratic formula adds epsilon to \(x^2\), whereas the other two divide x by epsilon. Equal numerical epsilon values do not imply equal smoothing error or physical units. The log-cosh implementation uses logaddexp to avoid overflowing an intermediate cosh at small smoothing widths.

Parameters:
xfloat or jax.Array

Input value or array of values.

method{“quadratic”, “hyperbolic”, “logcosh”, “logarithmic”}, optional

Approximation method. quadratic is the default. logarithmic is an alias of logcosh.

epsilonfloat, optional

Finite positive smoothing parameter; default 1e-5. Its numerical interpretation depends on the selected formula as described above.

Returns:
jax.Array

Element-wise smooth approximation with the input shape.

Raises:
ValueError

If an unsupported approximation method is provided.

Notes

Positivity and finiteness of epsilon are caller preconditions, not runtime validation. The formulas retain their ordinary floating-point limitations; for example, the direct quadratic expression can overflow when squaring an extremely large input.

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

Element-wise smooth maximum of two broadcast-compatible inputs.

This is a smooth counterpart of numpy.maximum(x1, x2), not an axis reduction. The two inputs broadcast to a common shape and are compared at each element. Let \(a=|\alpha|>0\) and \(m=\max(x_1,x_2)\).

The positive-input power-sum approximation is given by [Weisstein, 2023]:

\[P_a(x_1,x_2) = \left(x_1^a+x_2^a\right)^{1/a}.\]

The Boltzmann approximation is an exponentially weighted mean:

\[B_a(x_1,x_2) = \frac{\sum_{i=1}^{2}x_i\exp\left(a(x_i-m)\right)} {\sum_{i=1}^{2}\exp\left(a(x_i-m)\right)}.\]

It favors the larger input and remains between the inputs up to numerical roundoff. The implementation adds machine epsilon to the weight denominator. This weighted mean differs from the log-sum-exp approximation described by [Blanchard et al., 2021]:

\[L_a(x_1,x_2) = m + \frac{1}{a} \log\left(\sum_{i=1}^{2}\exp\left(a(x_i-m)\right)\right).\]

Log-sum-exp approaches the maximum from above, with an excess no greater than \(\log(2)/a\). Shifting the exponential arguments by \(m\) avoids overflow in those weights. The direct power-sum implementation does not use this exponential stabilization.

Parameters:
x1, x2array_like

Scalar or array inputs compatible under JAX broadcasting.

method{“boltzmann”, “logsumexp”, “p-norm”}, optional

Approximation method. The default is the Boltzmann weighted mean.

alphafloat, optional

Finite nonzero sharpness parameter; default 10. Its magnitude is used, so its sign does not select a minimum. Increasing its magnitude tightens the approximation in the method’s valid numerical regime.

Returns:
max_approxjax.Array

Element-wise approximation with the common broadcast shape. Scalar inputs produce a zero-dimensional array.

Raises:
ValueError

If an unsupported method is specified.

See also

smooth_minimum

Element-wise approximation favoring the smaller input.

Notes

Use strictly positive inputs for p-norm. It evaluates powers of the raw inputs, not their absolute values, and is not a signed-vector norm. Its positive-input domain and the nonzero-sharpness requirement are caller preconditions, not runtime validation. Nonfinite inputs, zero sharpness or overflowing powers can therefore produce nonfinite numerical results.

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

Element-wise smooth minimum of two broadcast-compatible inputs.

This is a smooth counterpart of numpy.minimum(x1, x2), not an axis reduction. It uses the same methods as smooth_maximum() with the internally signed exponent \(-a\), where \(a=|\alpha|>0\).

For log-sum-exp, let \(m=\min(x_1,x_2)\):

\[L_{-a}(x_1,x_2) = m - \frac{1}{a} \log\left(\sum_{i=1}^{2}\exp\left(-a(x_i-m)\right)\right).\]

It approaches the minimum from below, with a deficit no greater than \(\log(2)/a\). Boltzmann instead uses the weighted mean

\[B_{-a}(x_1,x_2) = \frac{\sum_{i=1}^{2}x_i\exp\left(-a(x_i-m)\right)} {\sum_{i=1}^{2}\exp\left(-a(x_i-m)\right)},\]

favoring the smaller input and remaining between the inputs up to numerical roundoff. The implementation adds machine epsilon to the weight denominator. The power-sum method uses

\[P_{-a}(x_1,x_2) = \left(x_1^{-a}+x_2^{-a}\right)^{-1/a}.\]

Its negative exponent requires strictly positive inputs; zero inputs are singular and negative inputs do not define the intended real-valued method.

Parameters:
x1, x2array_like

Scalar or array inputs compatible under JAX broadcasting.

method{“boltzmann”, “logsumexp”, “p-norm”}, optional

Approximation method. The default is the Boltzmann weighted mean.

alphafloat, optional

Finite nonzero sharpness parameter; default 10. Only its magnitude is used. Increasing its magnitude tightens the approximation in the method’s valid numerical regime.

Returns:
min_approxjax.Array

Element-wise approximation with the common broadcast shape. Scalar inputs produce a zero-dimensional array.

Raises:
ValueError

If an unsupported method is specified.

See also

smooth_maximum

Element-wise approximation favoring the larger input.

Notes

The p-norm implementation powers the raw inputs without taking absolute values or stabilizing the power sum. Positive inputs and nonzero sharpness are caller preconditions, not runtime validation. Extreme powers or invalid inputs can produce nonfinite numerical results.

jaxprop.math.tand(x)[source]#

Compute the tangent of an angle given in degrees.