APyFloatArray
¶
- class apytypes.APyFloatArray¶
Class for multidimensional arrays with configurable floating-point formats.
APyFloatArray
is a class for multidimensional arrays with configurable floating-point formats. The interface is much like the one of NumPy, and direct plotting is supported by most functions in Matplotlib.APyFloatArray
should always be preferred, if possible, when working with arrays as it allows for better performance, and integration with other features of APyTypes.For information about the workings of floating-point numbers, see its scalar equivalent
APyFloat
.- Attributes:
Methods
Broadcast array to new shape.
Change format of the floating-point number.
Cast to bfloat16 format.
Cast to IEEE 754 binary64 (double-precision) format.
Cast to IEEE 754 binary16 (half-precision) format.
Cast to IEEE 754 binary32 (single-precision) format.
Return the discrete linear convolution with another one-dimensional array.
Return the cumulative product of the elements along a given axes.
Return the cumulative sum of the elements along a given axis.
Return a copy of the array collapsed into one dimension.
from_bits
Test if two
APyFloatArray
objects are identical.Return the maximum value from an array or the maximum values along an axis.
Return the minimum value from an array or the minimum values along an axis.
Return the cumulative product of the elements along a given axis treating NaN as 0.
Return the cumulative sum of the elements along a given axis treating NaN as 0.
Return the maximum value from an array or the maximum values along an axis, ignoring NaN.
Return the minimum value from an array or the minimum values along an axis, ignoring NaN.
Return the product of the elements along a given axis treating NaN as 0.
Return the sum of the elements along specified axis/axes treating NaN as 0.
Return the product of the elements along specified axis/axes.
Return a copy of the array collapsed into one dimension.
Reshape the APyFloatArray to the specified shape without changing its data.
Remove axes of size one at the specified axis/axes, if no axís is given removes all dimensions with size one.
Return the sum of the elements along specified axis/axes.
Interchange two axes of an array.
Return the underlying bit representations.
Return array as a
numpy.ndarray
ofnumpy.float64
.Return copy of array with axes transposed.
Constructor¶
- __init__(self, signs: Sequence, exps: Sequence, mans: Sequence, exp_bits: int, man_bits: int, bias: int | None = None) None ¶
Create an
APyFloat
object.- Parameters:
- signssequence of bools or ints
The sign of the float. False/0 means positive. True/non-zero means negative.
- expssequence of ints
Exponents of the floats as stored, i.e., actual value + bias.
- manssequence of ints
Mantissas of the floats as stored, i.e., without a hidden one.
- exp_bits
int
Number of exponent bits.
- man_bits
int
Number of mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.
- Returns:
Conversion to other types¶
- to_numpy(self) numpy.ndarray[dtype=float64] ¶
Return array as a
numpy.ndarray
ofnumpy.float64
.The returned array has the same shape and values as self. This method rounds away from infinity on ties.
- Returns:
- to_bits(self, numpy: bool = False) list | numpy.ndarray[dtype=uint64] | numpy.ndarray[dtype=uint32] | numpy.ndarray[dtype=uint16] | numpy.ndarray[dtype=uint8] ¶
Return the underlying bit representations.
When numpy is true, the bit representations are returned in a
numpy.ndarray
. Otherwise, they are returned in alist
.- Returns:
Creation from other types¶
- from_array(ndarray: ndarray[order='C'], exp_bits: int, man_bits: int, bias: int | None = None) APyFloatArray ¶
Create an
APyFloatArray
object from an ndarray.- Parameters:
- Returns:
Examples
>>> from apytypes import APyFloatArray >>> import numpy as np
Array a, initialized from NumPy ndarray
>>> a = APyFloatArray.from_array( ... np.array([ ... [1.0, 2.0, 3.0], ... [4.0, 5.0, 6.0], ... ]), ... man_bits=10, ... exp_bits=10 ... )
- from_float(number_sequence: Sequence, exp_bits: int, man_bits: int, bias: int | None = None) APyFloatArray ¶
Create an
APyFloatArray
object from a sequence ofint
,float
,APyFixed
, orAPyFloat
.- Parameters:
- number_sequencesequence of numbers
Floating point values to initialize from. The tensor shape will be taken from the sequence shape.
- exp_bits
int
Number of exponent bits in the created floating-point tensor
- man_bits
int
Number of mantissa bits in the created floating-point tensor
- bias
int
, optional Bias in the created floating-point tensor
- Returns:
Examples
>>> from apytypes import APyFloatArray
Array a, initialized from floating-point values.
>>> a = APyFloatArray.from_float([1.0, 1.25, 1.49], exp_bits=10, man_bits=15)
Array lhs (2 x 3 matrix), initialized from floating-point values.
>>> lhs = APyFloatArray.from_float( ... [ ... [1.0, 2.0, 3.0], ... [4.0, 5.0, 6.0], ... ], ... exp_bits=5, ... man_bits=2 ... )
Other creation functions¶
- zeros(shape: tuple, exp_bits: int, man_bits: int, bias: int | None = None) APyFloatArray ¶
Initializes an array with zeros.
- ones(shape: tuple, exp_bits: int, man_bits: int, bias: int | None = None) APyFloatArray ¶
Initializes an array with ones.
- eye(n: int, exp_bits: int, man_bits: int, m: int | None = None, bias: int | None = None) APyFloatArray ¶
Initializes an array with ones on the diagonal.
- Parameters:
- Returns:
- APyFloatArray
An array with the specified value on the diagonal.
- identity(n: int, exp_bits: int, man_bits: int, bias: int | None = None) APyFloatArray ¶
Initializes an identity matrix with ones on the diagonal.
- full(shape: tuple, fill_value: APyFloat) APyFloatArray ¶
Initializes an array filled with the specified value.
- Parameters:
- shape
tuple
Shape of the array.
- fill_valueAPyFloat
Value to fill the array.
- shape
- Returns:
- APyFloatArray
An array filled with the specified value.
Change word length¶
- cast(self, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None, quantization: QuantizationMode | None = None) APyFloatArray ¶
Change format of the floating-point number.
This is the primary method for performing quantization when dealing with APyTypes floating-point numbers.
- Parameters:
- exp_bits
int
, optional Number of exponent bits in the result.
- man_bits
int
, optional Number of mantissa bits in the result.
- bias
int
, optional Bias used in the result.
- quantization
QuantizationMode
, optional. Quantization mode to use in this cast. If None, use the global quantization mode.
- exp_bits
- Returns:
Comparison¶
- is_identical(self, other: APyFloatArray) bool ¶
Test if two
APyFloatArray
objects are identical.- Two
APyFloatArray
objects are considered identical if, and only if: They represent exactly the same tensor shape
They store the exact same floating-ppint values in all tensor elements
They have the exact same bit format (exp_bits, man_bits, and bias)
- Returns:
- Two
Convolution¶
- convolve(self, other: APyFloatArray, mode: str = 'full') APyFloatArray ¶
Return the discrete linear convolution with another one-dimensional array.
Requires that
ndim = 1
for both self and other.- Parameters:
- other
APyFloatArray
The one-dimensional array of length
N
to convolve with.- mode{‘full’, ‘same’, ‘valid’}, default: ‘full’
- ‘full’:
Return the full convolution for each point of overlap. The resulting single-dimensional shape will have length
N + M - 1
. Boundary effects occurs for points where the a and v do not overlap completely.- ‘same’:
Return a convolution of length
max(M, N)
. Boundary effects still occur around the edges of the result.- ‘valid’:
Return the convolution for each point of full overlap. The resulting single-dimensional shape will have length
max(M, N) - min(M, N) + 1
- other
- Returns:
- convolved
APyFloatArray
The convolved array.
- convolved
Transposition¶
- transpose(self, axes: tuple | None = None) APyFloatArray ¶
Return copy of array with axes transposed.
For a 1-D array, this return the same array. For a 2-D array, this is the standard matrix transpose. For an n-D array, if axes are given, their order indicates how the axes are permuted (see Examples). If axes are not provided, then
a.transpose(a).shape == a.shape[::-1]
.- Parameters:
- axes
tuple
ofint
, optional If specified, it must be a tuple or list which contains a permutation of [0,1,…,N-1] where N is the number of axes of a. The i’th axis of the returned array will correspond to the axis numbered
axes[i]
of the input. If not specified, defaults torange(a.ndim)[::-1]
, which reverses the order of the axes.
- axes
- Returns:
APyFloatArray
a with its axes permuted.
Examples
>>> from apytypes import APyFloatArray >>> a = APyFloatArray.from_float([[1.0, 2.0, 3.0], [-4.0, -5.0, -6.0]], exp_bits=5, man_bits=2) >>> a.to_numpy() array([[ 1., 2., 3.], [-4., -5., -6.]]) >>> a = a.transpose() >>> a.to_numpy() array([[ 1., -4.], [ 2., -5.], [ 3., -6.]])
>>> a = APyFloatArray.from_float([1.0] * 6, exp_bits=5, man_bits=2).reshape((1, 2, 3)) >>> a.transpose((1, 0, 2)).shape (2, 1, 3)
>>> a.transpose((-2, -3, -1)).shape (2, 1, 3)
Array shape manipulation¶
- flatten(self) APyFloatArray ¶
Return a copy of the array collapsed into one dimension.
- Returns:
Examples
>>> from apytypes import APyFloatArray >>> signs = [[0, 0], [1, 1]] >>> exps = [[127, 128], [128, 129]] >>> mans = [[0, 0], [4194304, 0]] >>> arr = APyFloatArray(signs=signs, exps=exps, mans=mans, exp_bits=8, man_bits=23) >>> arr.to_numpy() array([[ 1., 2.], [-3., -4.]])
>>> arr.flatten().to_numpy() array([ 1., 2., -3., -4.])
- ravel(self) APyFloatArray ¶
Return a copy of the array collapsed into one dimension. Same as flatten with current memory-copy model.
- Returns:
Examples
>>> from apytypes import APyFloatArray >>> signs = [[0, 0], [1, 1]] >>> exps = [[127, 128], [128, 129]] >>> mans = [[0, 0], [4194304, 0]] >>> arr = APyFloatArray(signs=signs, exps=exps, mans=mans, exp_bits=8, man_bits=23) >>> arr.to_numpy() array([[ 1., 2.], [-3., -4.]])
>>> arr.ravel().to_numpy() array([ 1., 2., -3., -4.])
- reshape(self, number_sequence: tuple) APyFloatArray ¶
Reshape the APyFloatArray to the specified shape without changing its data.
- Parameters:
- new_shapetuple
The new shape should be compatible with the original shape. If a dimension is -1, its value will be inferred from the length of the array and remaining dimensions. Only one dimension can be -1.
- Returns:
- Raises:
ValueError
If negative dimensions less than -1 are provided, if the total size of the new array is not unchanged and divisible by the known dimensions, or if the total number of elements does not match the original array.
Examples
>>> from apytypes import APyFloatArray >>> signs = [0, 0, 1, 1] >>> exps = [127, 128, 128, 129] >>> mans = [0, 0, 4194304, 0] >>> arr = APyFloatArray(signs=signs, exps=exps, mans=mans, exp_bits=8, man_bits=23) >>> arr.to_numpy() array([ 1., 2., -3., -4.])
>>> arr.reshape((2, 2)).to_numpy() array([[ 1., 2.], [-3., -4.]])
>>> arr.reshape((4,)).to_numpy() array([ 1., 2., -3., -4.])
>>> arr.reshape((2, -1)).to_numpy() array([[ 1., 2.], [-3., -4.]])
- squeeze(self, axis: int | tuple | None = None) APyFloatArray ¶
Remove axes of size one at the specified axis/axes, if no axís is given removes all dimensions with size one.
- Parameters:
- Returns:
- Raises:
ValueError
If given an axis of a size other than one, a ValueError will be thrown.
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
- swapaxes(self, axis1: int, axis2: int) APyFloatArray ¶
Interchange two axes of an array.
- Parameters:
- Returns:
- a_swapped
APyFloatArray
Copy of a with axes swapped
- a_swapped
Examples
>>> from apytypes import APyFloatArray >>> x = APyFloatArray.from_float([[1 ,2, 3]], exp_bits=5, man_bits=2) >>> x.swapaxes(0,1).to_numpy() array([[1.], [2.], [3.]])
>>> x = APyFloatArray.from_float([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], exp_bits=5, man_bits=5) >>> x.to_numpy() array([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]])
>>> x.swapaxes(0,2).to_numpy() array([[[0., 4.], [2., 6.]], [[1., 5.], [3., 7.]]])
Mathematical functions¶
- sum(self, axis: tuple | int | None = None) APyFloatArray | APyFloat ¶
Return the sum of the elements along specified axis/axes.
- Parameters:
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
Examples
>>> from apytypes import APyFloatArray
>>> a = APyFloatArray.from_float( ... [1,2,3,4,5,6], ... exp_bits=10, ... man_bits=10 ... )
>>> a.sum() APyFloat(sign=0, exp=515, man=320, exp_bits=10, man_bits=10)
- prod(self, axis: tuple | int | None = None) APyFloatArray | APyFloat ¶
Return the product of the elements along specified axis/axes.
- Parameters:
- axis
tuple
, int, optional The axis/axes to calculate the product across. If not given an axis it will return the product of the flattened array.
- axis
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
Examples
>>> from apytypes import APyFloatArray
>>> a = APyFloatArray.from_float( ... [1,2,3,4,5,6], ... exp_bits=10, ... man_bits=10 ... )
>>> a.prod() APyFloat(sign=0, exp=520, man=416, exp_bits=10, man_bits=10)
- nansum(self, axis: tuple | int | None = None) APyFloatArray | APyFloat ¶
Return the sum of the elements along specified axis/axes treating NaN as 0.
- Parameters:
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
Examples
>>> from apytypes import APyFloatArray
>>> nan = float("nan")
>>> a = APyFloatArray.from_float( ... [1,2,3,4,5,nan], ... exp_bits=10, ... man_bits=10 ... )
>>> a.nansum() APyFloat(sign=0, exp=514, man=896, exp_bits=10, man_bits=10)
- nanprod(self, axis: tuple | int | None = None) APyFloatArray | APyFloat ¶
Return the product of the elements along a given axis treating NaN as 0.
- Parameters:
- axis
int
, optional The axis to calculate the product across. If not given an axis it will return the product of the flattened array.
- axis
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
- cumsum(self, axis: int | None = None) APyFloatArray ¶
Return the cumulative sum of the elements along a given axis.
- Parameters:
- axis
int
, optional The axis to summate across. If not given an axis it will return the cumulative sum of the flattened array.
- axis
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
Examples
>>> from apytypes import APyFloatArray
>>> a = APyFloatArray.from_float( ... [[1,2,3],[4,5,6]], ... exp_bits=10, ... man_bits=10 ... )
>>> a.cumsum() APyFloatArray([0, 0, 0, 0, 0, 0], [511, 512, 513, 514, 514, 515], [0, 512, 512, 256, 896, 320], shape=(6,), exp_bits=10, man_bits=10, bias=511)
>>> a.cumsum(0) APyFloatArray([0, 0, 0, 0, 0, 0], [511, 512, 512, 513, 513, 514], [0, 0, 512, 256, 768, 128], shape=(2, 3), exp_bits=10, man_bits=10, bias=511)
>>> a.cumsum(1) APyFloatArray([0, 0, 0, 0, 0, 0], [511, 512, 513, 513, 514, 514], [0, 512, 512, 0, 128, 896], shape=(2, 3), exp_bits=10, man_bits=10, bias=511)
- cumprod(self, axis: int | None = None) APyFloatArray ¶
Return the cumulative product of the elements along a given axes.
- Parameters:
- axis
int
, optional The axes to calculate the product across. If not given an axis it will return the cumulative product of the flattened array.
- axis
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
Examples
>>> from apytypes import APyFloatArray
>>> a = APyFloatArray.from_float( ... [[1,2,3],[4,5,6]], ... exp_bits=10, ... man_bits=10 ... )
>>> a.cumprod() APyFloatArray([0, 0, 0, 0, 0, 0], [511, 512, 513, 515, 517, 520], [0, 0, 512, 512, 896, 416], shape=(6,), exp_bits=10, man_bits=10, bias=511)
>>> a.cumprod(0) APyFloatArray([0, 0, 0, 0, 0, 0], [511, 512, 512, 513, 514, 515], [0, 0, 512, 0, 256, 128], shape=(2, 3), exp_bits=10, man_bits=10, bias=511)
>>> a.cumprod(1) APyFloatArray([0, 0, 0, 0, 0, 0], [511, 512, 513, 513, 515, 517], [0, 0, 512, 0, 256, 896], shape=(2, 3), exp_bits=10, man_bits=10, bias=511)
- nancumsum(self, axis: int | None = None) APyFloatArray ¶
Return the cumulative sum of the elements along a given axis treating NaN as 0.
- Parameters:
- axis
int
, optional The axis to summate across. If not given an axis it will return the cumulative sum of the flattened array.
- axis
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
Examples
>>> from apytypes import APyFloatArray
>>> nan = float("nan")
>>> a = APyFloatArray.from_float( ... [[1,2,3],[4,5,6]], ... exp_bits=10, ... man_bits=10 ... )
>>> a.nancumsum() APyFloatArray([0, 0, 0, 0, 0, 0], [511, 512, 513, 514, 514, 515], [0, 512, 512, 256, 896, 320], shape=(6,), exp_bits=10, man_bits=10, bias=511)
>>> a.nancumsum(0) APyFloatArray([0, 0, 0, 0, 0, 0], [511, 512, 512, 513, 513, 514], [0, 0, 512, 256, 768, 128], shape=(2, 3), exp_bits=10, man_bits=10, bias=511)
>>> a.nancumsum(1) APyFloatArray([0, 0, 0, 0, 0, 0], [511, 512, 513, 513, 514, 514], [0, 512, 512, 0, 128, 896], shape=(2, 3), exp_bits=10, man_bits=10, bias=511)
- nancumprod(self, axis: int | None = None) APyFloatArray ¶
Return the cumulative product of the elements along a given axis treating NaN as 0.
- Parameters:
- axis
int
, optional The axis to calculate the product across. If not given an axis it will return the cumulative product of the flattened array.
- axis
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
- max(self, axis: tuple | int | None = None) APyFloatArray | APyFloat ¶
Return the maximum value from an array or the maximum values along an axis.
- Parameters:
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
- Examples
>>> from apytypes import APyFloatArray ..
- Array a, array to get the maximum along.
>>> a = APyFloatArray.from_float( ..
- … [[1, 2, 3], [4, 5, 6]],
- … exp_bits=10,
- … man_bits=10
- … )
>>> a.max() ..
- APyFloat(sign=0, exp=513, man=512, exp_bits=10, man_bits=10)
>>> a.max(0) ..
- APyFloatArray([0, 0, 0], [513, 513, 513], [0, 256, 512], shape=(3,), exp_bits=10, man_bits=10, bias=511)
>>> a.max(1) ..
- APyFloatArray([0, 0], [512, 513], [512, 512], shape=(2,), exp_bits=10, man_bits=10, bias=511)
- min(self, axis: tuple | int | None = None) APyFloatArray | APyFloat ¶
Return the minimum value from an array or the minimum values along an axis.
- Parameters:
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
- Examples
>>> from apytypes import APyFloatArray ..
- Array a, array to get the minimum along.
>>> a = APyFloatArray.from_float( ..
- … [[1, 2, 3], [4, 5, 6]],
- … exp_bits=10,
- … man_bits=10
- … )
>>> a.min() ..
- APyFloat(sign=0, exp=511, man=0, exp_bits=10, man_bits=10)
>>> a.min(0) ..
- APyFloatArray([0, 0, 0], [511, 512, 512], [0, 0, 512], shape=(3,), exp_bits=10, man_bits=10, bias=511)
>>> a.min(1) ..
- APyFloatArray([0, 0], [511, 513], [0, 0], shape=(2,), exp_bits=10, man_bits=10, bias=511)
- nanmax(self, axis: tuple | int | None = None) APyFloatArray | APyFloat ¶
Return the maximum value from an array or the maximum values along an axis, ignoring NaN.
Issues a warning when encountering an all-nan slice or axis.
- Parameters:
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
- nanmin(self, axis: tuple | int | None = None) APyFloatArray | APyFloat ¶
Return the minimum value from an array or the minimum values along an axis, ignoring NaN.
Issues a warning when encountering an all-nan slice or axis.
- Parameters:
- Returns:
- Raises:
IndexError
If a specified axis is outside of the existing number of dimensions for the array.
Broadcasting¶
- broadcast_to(self, shape: tuple | int) APyFloatArray ¶
Broadcast array to new shape.
Added in version 0.2.
- Parameters:
- Returns:
Convenience casting methods¶
- cast_to_bfloat16(self, quantization: QuantizationMode | None = None) APyFloatArray ¶
Cast to bfloat16 format.
Convenience method corresponding to
f.cast(exp_bits=8, man_bits=7)
- Parameters:
- quantization
QuantizationMode
, optional Quantization mode to use. If not provided, the global mode, see
get_float_quantization_mode()
, is used.
- quantization
- cast_to_double(self, quantization: QuantizationMode | None = None) APyFloatArray ¶
Cast to IEEE 754 binary64 (double-precision) format.
Convenience method corresponding to
f.cast(exp_bits=11, man_bits=52)
- Parameters:
- quantization
QuantizationMode
, optional Quantization mode to use. If not provided, the global mode, see
get_float_quantization_mode()
, is used.
- quantization
- cast_to_half(self, quantization: QuantizationMode | None = None) APyFloatArray ¶
Cast to IEEE 754 binary16 (half-precision) format.
Convenience method corresponding to
f.cast(exp_bits=5, man_bits=10)
- Parameters:
- quantization
QuantizationMode
, optional Quantization mode to use. If not provided, the global mode, see
get_float_quantization_mode()
, is used.
- quantization
- cast_to_single(self, quantization: QuantizationMode | None = None) APyFloatArray ¶
Cast to IEEE 754 binary32 (single-precision) format.
Convenience method corresponding to
f.cast(exp_bits=8, man_bits=23)
- Parameters:
- quantization
QuantizationMode
, optional Quantization mode to use. If not provided, the global mode, see
get_float_quantization_mode()
, is used.
- quantization
Properties¶
Word length¶
Array properties¶
Transposition¶
- property T¶
The transposition of the array.
Equivalent to calling
APyFloatArray.transpose()
.- Returns: