APyFixedArray
¶
- class apytypes.APyFixedArray¶
- Attributes:
Methods
Broadcast array to new shape.
Change format of the fixed-point array.
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 axes.
Return a copy of the array collapsed into one dimension.
Test if two
APyFixedArray
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.
nancumprod
Return the cumulative product of the elements along a given axis treating NaN as 0.
nancumsum
Return the cumulative sum of the elements along a given axis treating NaN as 0.
nanmax
Return the maximum value from an array or the maximum values along an axis, ignoring NaN.
nanmin
Return the minimum value from an array or the minimum values along an axis, ignoring NaN.
nanprod
Return the product of the elements along a given axis treating NaN as 0.
nansum
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 APyFixedArray to the specified shape without changing its data.
Removes axes of size one at the specified axis/axes, if no axis 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¶
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'], int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyFixedArray ¶
Create an
APyFixedArray
object from an ndarray.The input is quantized using
QuantizationMode.RND_INF
and overflow is handled using theOverflowMode.WRAP
mode. Exactly two of the three bit-specifiers (bits, int_bits, frac_bits) must be set.Using NumPy arrays as input is in general faster than e.g. lists.
- Parameters:
- ndarrayndarray
Values to initialize from. The tensor shape will be taken from the ndarray shape.
- int_bits
int
, optional Number of integer bits in the created fixed-point tensor.
- frac_bits
int
, optional Number of fractional bits in the created fixed-point tensor.
- bits
int
, optional Total number of bits in the created fixed-point tensor.
- Returns:
Examples
>>> from apytypes import APyFixedArray >>> import numpy as np >>> >>> a = APyFixedArray.from_array( ... np.array([ ... [1.0, 2.0, 3.0], ... [4.0, 5.0, 6.0], ... ]), ... int_bits=10, ... frac_bits=0 ... )
- from_float(number_seq: Sequence, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyFixedArray ¶
Create an
APyFixedArray
object from a sequence ofint
,float
,APyFixed
, orAPyFloat
.The input is quantized using
QuantizationMode.RND_INF
and overflow is handled using theOverflowMode.WRAP
mode. Exactly two of the three bit-specifiers (bits, int_bits, frac_bits) must be set.Using NumPy arrays as input is in general faster than e.g. lists.
- Parameters:
- number_seqsequence of numbers
Values to initialize from. The tensor shape will be taken from the sequence shape.
- int_bits
int
, optional Number of integer bits in the created fixed-point tensor.
- frac_bits
int
, optional Number of fractional bits in the created fixed-point tensor.
- bits
int
, optional Total number of bits in the created fixed-point tensor.
- Returns:
Examples
>>> from apytypes import APyFixedArray >>> >>> a = APyFixedArray.from_float([1.0, 1.25, 1.49], int_bits=2, frac_bits=2) >>> b = APyFixedArray.from_float( ... [ ... [1.0, 2.0, 3.0], ... [4.0, 5.0, 6.0], ... ], ... bits=5, ... frac_bits=0 ... )
Other creation functions¶
- zeros(shape: tuple, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyFixedArray ¶
Initializes an array with zeros.
- Parameters:
- Returns:
APyFixedArray
An array initialized with zeros.
- ones(shape: tuple, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyFixedArray ¶
Initializes an array with ones.
- Parameters:
- Returns:
APyFixedArray
An array initialized with ones.
- eye(n: int, m: int | None = None, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyFixedArray ¶
Initializes an array with ones on the diagonal.
- identity(n: int, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyFixedArray ¶
Initializes an identity matrix with ones on the diagonal.
- Parameters:
- Returns:
APyFixedArray
An identity matrix with ones on the diagonal.
- full(shape: tuple, fill_value: APyFixed) APyFixedArray ¶
Initializes an array with the specified value.
- Parameters:
- Returns:
APyFixedArray
An array filled with the specified value.
Change word length¶
- cast(self, int_bits: int | None = None, frac_bits: int | None = None, quantization: QuantizationMode | None = None, overflow: OverflowMode | None = None, bits: int | None = None) APyFixedArray ¶
Change format of the fixed-point array.
This is the primary method for performing quantization and overflowing/saturation when dealing with APyTypes fixed-point arrays.
Exactly two of three bit-specifiers (bits, int_bits, frac_bits) must be set.
- Parameters:
- int_bits
int
, optional Number of integer bits in the result.
- frac_bits
int
, optional Number of fractional bits in the result.
- quantization
QuantizationMode
, optional Quantization mode to use in this cast.
- overflow
OverflowMode
, optional Overflowing mode to use in this cast.
- bits
int
, optional Total number of bits in the result.
- int_bits
- Returns:
Comparison¶
- is_identical(self, other: APyFixedArray) bool ¶
Test if two
APyFixedArray
objects are identical.- Two
APyFixedArray
objects are considered identical if, and only if: They represent exactly the same tensor shape
They store the exact same fixed-point values in all tensor elements
They have the exact same bit specification (bits, int_bits, and frac_bits are all equal)
- Returns:
- Two
Convolution¶
- convolve(self, other: APyFixedArray, mode: str = 'full') APyFixedArray ¶
Return the discrete linear convolution with another one-dimensional array.
Requires that
ndim = 1
for both self and other.- Parameters:
- other
APyFixedArray
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
APyFixedArray
The convolved array.
- convolved
Transposition¶
- transpose(self, axes: tuple | None = None) APyFixedArray ¶
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:
APyFixedArray
a with its axes permuted.
Examples
>>> from apytypes import APyFixedArray >>> >>> a = APyFixedArray.from_float( ... [[ 1.0, 2.0, 3.0], ... [-4.0, -5.0, -6.0]], ... bits=5, ... frac_bits=0 ... ) >>> a.transpose().to_numpy() array([[ 1., -4.], [ 2., -5.], [ 3., -6.]])
>>> a = APyFixedArray.from_float( ... [1.0] * 6, ... bits=5, ... frac_bits=0 ... ).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) APyFixedArray ¶
Return a copy of the array collapsed into one dimension.
- Returns:
Examples
>>> from apytypes import APyFixedArray >>> >>> arr = APyFixedArray([[2, 3], [4, 5]], int_bits=2, frac_bits=1) >>> arr.to_numpy() array([[ 1. , 1.5], [-2. , -1.5]])
>>> arr.flatten().to_numpy() array([ 1. , 1.5, -2. , -1.5])
- ravel(self) APyFixedArray ¶
Return a copy of the array collapsed into one dimension. Same as flatten with current memory-copy model.
- Returns:
Examples
>>> from apytypes import APyFixedArray >>> >>> arr = APyFixedArray([[2, 3], [4, 5]], int_bits=2, frac_bits=1) >>> arr.to_numpy() array([[ 1. , 1.5], [-2. , -1.5]])
>>> arr.ravel().to_numpy() array([ 1. , 1.5, -2. , -1.5])
- reshape(self, number_sequence: tuple) APyFixedArray ¶
Reshape the APyFixedArray 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 APyFixedArray >>> >>> a = APyFixedArray([2, 3, 4, 5], int_bits=2, frac_bits=1) >>> a.to_numpy() array([ 1. , 1.5, -2. , -1.5]) >>> a.reshape((2, 2)).to_numpy() array([[ 1. , 1.5], [-2. , -1.5]]) >>> a.reshape((4,)).to_numpy() array([ 1. , 1.5, -2. , -1.5]) >>> a.reshape((2, -1)).to_numpy() array([[ 1. , 1.5], [-2. , -1.5]])
- squeeze(self, axis: int | tuple | None = None) APyFixedArray ¶
Removes axes of size one at the specified axis/axes, if no axis 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) APyFixedArray ¶
Interchange two axes of an array.
- Parameters:
- Returns:
- a_swapped
APyFixedArray
Copy of a with axes swapped
- a_swapped
Examples
>>> from apytypes import APyFixedArray >>> >>> x = APyFixedArray.from_float([[1, 2, 3]], bits=5, frac_bits=0) >>> x.swapaxes(0,1).to_numpy() array([[1.], [2.], [3.]]) >>> x = APyFixedArray.from_float( ... [[[0, 1], [2, 3]], [[4, 5], [6, 7]]], ... bits=5, ... frac_bits=0 ... ) >>> 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) APyFixedArray | APyFixed ¶
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 APyFixedArray >>> >>> a = APyFixedArray( ... [1, 2, 3, 4, 5, 6], ... int_bits=10, ... frac_bits=0 ... ) >>> a.sum() APyFixed(21, bits=13, int_bits=13)
- prod(self, axis: tuple | int | None = None) APyFixedArray | APyFixed ¶
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 APyFixedArray >>> >>> a = APyFixedArray( ... [1,2,3,4,5,6], ... int_bits=10, ... frac_bits=0 ... ) >>> a.prod() APyFixed(720, bits=60, int_bits=60)
- cumsum(self, axis: int | None = None) APyFixedArray ¶
Return the cumulative sum of the elements along a given axes.
- Parameters:
- axis
int
, optional The axes 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 APyFixedArray >>> >>> a = APyFixedArray( ... [[1,2,3],[4,5,6]], ... int_bits=10, ... frac_bits=0 ... ) >>> a.cumsum() APyFixedArray([1, 3, 6, 10, 15, 21], shape=(6,), bits=13, int_bits=13) >>> a.cumsum(0) APyFixedArray([1, 2, 3, 5, 7, 9], shape=(2, 3), bits=11, int_bits=11) >>> a.cumsum(1) APyFixedArray([1, 3, 6, 4, 9, 15], shape=(2, 3), bits=12, int_bits=12)
- cumprod(self, axis: int | None = None) APyFixedArray ¶
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 APyFixedArray >>> >>> a = APyFixedArray( ... [[1,2,3],[4,5,6]], ... int_bits=10, ... frac_bits=0 ... ) >>> a.cumprod() APyFixedArray([1, 2, 6, 24, 120, 720], shape=(6,), bits=60, int_bits=60) >>> a.cumprod(0) APyFixedArray([1, 2, 3, 4, 10, 18], shape=(2, 3), bits=20, int_bits=20) >>> a.cumprod(1) APyFixedArray([1, 2, 6, 4, 20, 120], shape=(2, 3), bits=30, int_bits=30)
- max(self, axis: tuple | int | None = None) APyFixedArray | APyFixed ¶
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 APyFixedArray >>> >>> a = APyFixedArray( ... [[1,2,3],[4,5,6]], ... int_bits=10, ... frac_bits=0 ... ) >>> a.max() APyFixed(6, bits=10, int_bits=10) >>> a.max(0) APyFixedArray([4, 5, 6], shape=(3,), bits=10, int_bits=10) >>> a.max(1) APyFixedArray([3, 6], shape=(2,), bits=10, int_bits=10)
- min(self, axis: tuple | int | None = None) APyFixedArray | APyFixed ¶
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 APyFixedArray >>> >>> a = APyFixedArray( ... [[1,2,3], ... [4,5,6]], ... int_bits=10, ... frac_bits=0 ... ) >>> a.min() APyFixed(1, bits=10, int_bits=10) >>> a.min(0) APyFixedArray([1, 2, 3], shape=(3,), bits=10, int_bits=10) >>> a.min(1) APyFixedArray([1, 4], shape=(2,), bits=10, int_bits=10)
Broadcasting¶
- broadcast_to(self, shape: tuple | int) APyFixedArray ¶
Broadcast array to new shape.
Added in version 0.2.
- Parameters:
- Returns:
Properties¶
Word length¶
Array properties¶
Transposition¶
- property T¶
The transposition of the array.
Equivalent to calling
APyFixedArray.transpose()
.- Returns: