sarcomp.abs#

sarcomp.abs(array: ndarray | Array, method: str = 'ac') Array#

Compute the element-wise absolute value or magnitude of the input array.

Parameters:
  • array (Union[np.ndarray, Array]) – The input array, which can be a NumPy ndarray or an instance of the custom Array class.

  • method (str, optional) –

    The method used for calculating the absolute value. Applicable only for complex and split-complex types:
    • ’ac’: Accurate (default). Calculates the absolute value with full precision.

    • ’ap’: Approximate. Uses SIMD rsqrt for fast but less accurate results.

    • ’nr’: Newton-Raphson. Refines rsqrt for improved accuracy.

    For float32 and float64, the method parameter is ignored since their absolute value is calculated accurately by reverting the sign bit.

Returns:

A new Array instance containing the element-wise absolute value or magnitude of the input array.

Return type:

Union[np.ndarray, Array]

Raises:
  • ValueError – If the specified method is not supported for complex or split-complex types.

  • KeyError – If the specified conversion function is not found in the functions dictionary.

Examples

>>> import numpy as np
>>> import sarcomp as sc
>>> np_array = np.array([1.0, -4.0, 9.0], dtype=np.float32)
>>> result = sc.abs(np_array)
>>> # Using an Array instance for split-complex input
>>> array = sc.Array(data=np_array, imag=np_array * 0.5)
>>> result = sc.abs(array, method='ap')