Special Functions (labtoolbox.special)#
Special mathematical functions for LabToolbox.
Includes sign, pulse and waveform functions such as rectangle, triangle, Sawtooth, square and Lorentzian profiles.
- labtoolbox.special.lorentz(x, x0=0.0, g=1.0)[source]#
Compute the Lorentzian (Cauchy) distribution function.
- Parameters:
x (float or numpy.ndarray) – Input value(s), scalar or 1D array of real numbers.
x0 (float, optional) – Center of the Lorentzian distribution. Defaults to 0.0.
g (float, optional) – Width parameter. Must be positive. Defaults to 1.0.
- Returns:
The Lorentzian function evaluated at x. Returns a scalar if x is scalar, otherwise a 1D array.
- Return type:
float or numpy.ndarray
- Raises:
TypeError – If x contains non-real numbers, or x0 or g are not finite real numbers.
ValueError – If x is not a scalar or 1D array, contains non-finite values, or g is not positive.
Examples
>>> from labtoolbox.special import lorentz >>> lorentz(0.0, x0=0.0, g=1.0) 0.3183098861837907 >>> lorentz([0, 1, 2], x0=0.0, g=1.0) array([0.31830989, 0.15915494, 0.06366198])
- labtoolbox.special.rect(x)[source]#
Compute the rectangular function (boxcar function).
- Parameters:
x (float or numpy.ndarray) – Input value(s), scalar or 1D array of real numbers.
- Returns:
The rectangular function evaluated at x. Returns a scalar if x is scalar, otherwise a 1D array.
- Return type:
float or numpy.ndarray
- Raises:
TypeError – If x contains non-real numbers.
ValueError – If x is not a scalar or 1D array, or contains non-finite values.
Examples
>>> from labtoolbox.special import rect >>> rect(0.4) 1.0 >>> rect([-0.6, 0.5, 0.0]) array([0. , 0.5, 1. ])
- labtoolbox.special.saw(x, T=1.0)[source]#
Compute the sawtooth wave function.
- Parameters:
x (float or numpy.ndarray) – Input value(s), scalar or 1D array of real numbers.
T (float, optional) – Period of the sawtooth wave. Must be positive. Defaults to 1.0.
- Returns:
The sawtooth wave evaluated at x. Returns a scalar if x is scalar, otherwise a 1D array.
- Return type:
float or numpy.ndarray
- Raises:
TypeError – If x contains non-real numbers.
ValueError – If x is not a scalar or 1D array, contains non-finite values, or T is not positive.
Examples
>>> from labtoolbox.special import saw >>> saw(0.25, T=1.0) 0.5 >>> saw([0, 0.5, 1.0], T=1.0) array([ 0., 1., -1.])
- labtoolbox.special.sgn(x)[source]#
Compute the sign function of the input.
- Parameters:
x (float or numpy.ndarray) – Input value(s), scalar or 1D array of real numbers.
- Returns:
The sign of x. Returns a scalar if x is scalar, otherwise a 1D array.
- Return type:
float or numpy.ndarray
- Raises:
TypeError – If x contains non-real numbers.
ValueError – If x is not a scalar or 1D array, or contains non-finite values.
Examples
>>> from labtoolbox.special import sgn >>> sgn(3.0) 1.0 >>> sgn([-2, 0, 1]) array([-1., 0., 1.])
- labtoolbox.special.square(x, T=1.0)[source]#
Compute the square wave function.
The function is periodic with period T, alternating between -1 and 1 based on the sign of sin(2 * pi * x / T).
- Parameters:
x (float or numpy.ndarray) – Input value(s), scalar or 1D array of real numbers.
T (float, optional) – Period of the square wave. Must be positive. Defaults to 1.0.
- Returns:
The square wave evaluated at x. Returns a scalar if x is scalar, otherwise a 1D array.
- Return type:
float or numpy.ndarray
- Raises:
TypeError – If x contains non-real numbers or T is not a finite real number.
ValueError – If x is not a scalar or 1D array, contains non-finite values, or T is not positive.
Examples
>>> from labtoolbox.special import square >>> square(0.25) 1.0 >>> square([0, 0.5, 1.0]) array([ 1., -1., 1.])
- labtoolbox.special.step(x)[source]#
Compute the Heaviside step function.
Returns 0 for x < 0, 1 for x >= 0.
- Parameters:
x (float or numpy.ndarray) – Input value(s), scalar or 1D array of real numbers.
- Returns:
The step function evaluated at x. Returns a scalar if x is scalar, otherwise a 1D array.
- Return type:
float or numpy.ndarray
- Raises:
TypeError – If x contains non-real numbers.
ValueError – If x is not a scalar or 1D array, or contains non-finite values.
Examples
>>> from labtoolbox.special import step >>> step(-1.0) 0.0 >>> step([-1, 0, 1]) array([0., 1., 1.])
- labtoolbox.special.tri(x)[source]#
Compute a triangle pulse. Not to be confused with labtoolbox.triangle.
- Parameters:
x (float or numpy.ndarray) – Input value(s), scalar or 1D array of real numbers.
- Returns:
The triangle wave evaluated at x. Returns a scalar if x is scalar, otherwise a 1D array.
- Return type:
float or numpy.ndarray
- Raises:
TypeError – If x contains non-real numbers.
ValueError – If x is not a scalar or 1D array, or contains non-finite values.
Examples
>>> from labtoolbox.special import tri >>> tri(0.25) 0.75 >>> tri([0, 0.25, 0.5]) array([1, 0.75, 0.5])
- labtoolbox.special.triangle(x, T=1.0)[source]#
Compute the triangle wave function.
- Parameters:
x (float or numpy.ndarray) – Input value(s), scalar or 1D array of real numbers.
T (float, optional) – Period of the triangle wave. Must be positive. Defaults to 1.0.
- Returns:
The triangle wave evaluated at x. Returns a scalar if x is scalar, otherwise a 1D array.
- Return type:
float or numpy.ndarray
- Raises:
TypeError – If x contains non-real numbers or T is not a finite real number.
ValueError – If x is not a scalar or 1D array, contains non-finite values, or T is not positive.
Examples
>>> from labtoolbox.special import triangle >>> triangle(0.25) 1.0 >>> triangle([0, 0.25, 0.5]) array([0., 1., 0.])