APyCFixedArray
¶
- class apytypes.APyCFixedArray¶
Class for configurable complex-valued array fixed-point formats.
Added in version 0.3.
- Attributes:
Methods
Broadcast array to new shape.
Change format of the fixed-point 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
APyCFixedArray
objects are identical.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.
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.
Returns the product of the elements along specified axis/axes.
Return a copy of the array collapsed into one dimension.
Reshape the APyCFixedArray 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 array as a
numpy.ndarray
ofnumpy.complex128
.Return copy of array with axes transposed.
Constructor¶
Conversion to other types¶
- to_numpy(self) numpy.ndarray[dtype=complex128] ¶
Return array as a
numpy.ndarray
ofnumpy.complex128
.The returned array has the same shape and values as self. This method rounds away from infinity on ties.
- 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) APyCFixedArray ¶
Create an
APyCFixedArray
object from an ndarray.The initialized fixed-point values are the one closest to the input floating-point value, rounded away from zero on ties. 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 APyCFixedArray >>> import numpy as np >>> >>> a = APyCFixedArray.from_array( ... np.array([ ... [1.0, 2.0, 3.0], ... [4.0, 5.0, 6.0], ... ]), ... int_bits=10, ... frac_bits=0 ... )
- from_complex(complex_sequence: Sequence, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyCFixedArray ¶
Create an
APyCFixedArray
object from a sequence ofint
,float
,complex
,APyFixed
,APyFloat
, orAPyCFixed
.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:
- complex_sequencesequence of float, int, or complex
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 APyCFixedArray >>> >>> a = APyCFixedArray.from_complex( ... [1.0, 1.25j, 1.49 - 0.5j], ... int_bits=2, ... frac_bits=2 ... ) >>> b = APyCFixedArray.from_complex( ... [ ... [1.0 + 1.0j, 2.0 - 3.0j, 3.0 - 1.0j], ... [4.0 - 2.0j, 5.0 + 2.0j, 6.0 + 1j], ... ], ... bits=5, ... frac_bits=0 ... )
- from_float(number_seq: Sequence, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyCFixedArray ¶
Create an
APyCFixedArray
object from a sequence ofint
,float
,complex
,APyFixed
,APyFloat
, orAPyCFixed
. This is an alias forfrom_complex()
, look there for more documentation.- 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:
Other creation functions¶
- zeros(shape: tuple, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyCFixedArray ¶
Initializes an array with zeros.
- Parameters:
- Returns:
APyCFixedArray
An array initialized with zeros.
- ones(shape: tuple, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyCFixedArray ¶
Initializes an array with ones.
- Parameters:
- Returns:
APyCFixedArray
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) APyCFixedArray ¶
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) APyCFixedArray ¶
Initializes an identity matrix with ones on the diagonal.
- Parameters:
- Returns:
APyCFixedArray
An identity matrix with ones on the diagonal.
- full(shape: tuple, fill_value: APyCFixed) APyCFixedArray ¶
Initializes an array with the specified value.
- Parameters:
- Returns:
APyCFixedArray
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) APyCFixedArray ¶
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: APyCFixedArray) bool ¶
Test if two
APyCFixedArray
objects are identical.Two
APyCFixedArray
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:
Convolution¶
Transposition¶
- transpose(self, axes: tuple | None = None) APyCFixedArray ¶
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:
APyCFixedArray
a with its axes permuted.
Examples
>>> from apytypes import APyCFixedArray >>> >>> a = APyCFixedArray.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.+0.j, -4.+0.j], [ 2.+0.j, -5.+0.j], [ 3.+0.j, -6.+0.j]])
>>> a = APyCFixedArray.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) APyCFixedArray ¶
Return a copy of the array collapsed into one dimension.
- Returns:
Examples
>>> from apytypes import APyCFixedArray >>> >>> arr = APyCFixedArray([[2, 3], [4, 5]], int_bits=2, frac_bits=1) >>> arr.to_numpy() array([[ 1. +0.j, 1.5+0.j], [-2. +0.j, -1.5+0.j]])
>>> arr.flatten().to_numpy() array([ 1. +0.j, 1.5+0.j, -2. +0.j, -1.5+0.j])
- ravel(self) APyCFixedArray ¶
Return a copy of the array collapsed into one dimension. Same as flatten with current memory-copy model.
- Returns:
Examples
>>> from apytypes import APyCFixedArray >>> >>> arr = APyCFixedArray([[2, 3], [4, 5]], int_bits=2, frac_bits=1) >>> arr.to_numpy() array([[ 1. +0.j, 1.5+0.j], [-2. +0.j, -1.5+0.j]])
>>> arr.ravel().to_numpy() array([ 1. +0.j, 1.5+0.j, -2. +0.j, -1.5+0.j])
- reshape(self, number_sequence: tuple) APyCFixedArray ¶
Reshape the APyCFixedArray 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 APyCFixedArray >>> >>> a = APyCFixedArray([2, 3, 4, 5], int_bits=2, frac_bits=1) >>> a.to_numpy() array([ 1. +0.j, 1.5+0.j, -2. +0.j, -1.5+0.j])
>>> a.reshape((2, 2)).to_numpy() array([[ 1. +0.j, 1.5+0.j], [-2. +0.j, -1.5+0.j]])
>>> a.reshape((4,)).to_numpy() array([ 1. +0.j, 1.5+0.j, -2. +0.j, -1.5+0.j])
>>> a.reshape((2, -1)).to_numpy() array([[ 1. +0.j, 1.5+0.j], [-2. +0.j, -1.5+0.j]])
- squeeze(self, axis: int | tuple | None = None) APyCFixedArray ¶
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) APyCFixedArray ¶
Interchange two axes of an array.
- Parameters:
- Returns:
- a_swapped
APyCFixedArray
Copy of a with axes swapped
- a_swapped
Examples
>>> from apytypes import APyCFixedArray >>> >>> x = APyCFixedArray.from_float([[1, 2, 3]], bits=5, frac_bits=0) >>> x.swapaxes(0,1).to_numpy() array([[1.+0.j], [2.+0.j], [3.+0.j]])
>>> x = APyCFixedArray.from_float( ... [[[0, 1], [2, 3]], [[4, 5], [6, 7]]], ... bits=5, ... frac_bits=0 ... ) >>> x.to_numpy() array([[[0.+0.j, 1.+0.j], [2.+0.j, 3.+0.j]], [[4.+0.j, 5.+0.j], [6.+0.j, 7.+0.j]]])
>>> x.swapaxes(0,2).to_numpy() array([[[0.+0.j, 4.+0.j], [2.+0.j, 6.+0.j]], [[1.+0.j, 5.+0.j], [3.+0.j, 7.+0.j]]])
Mathematical functions¶
- sum(self, axis: tuple | int | None = None) APyCFixedArray | APyCFixed ¶
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 APyCFixedArray >>> >>> a = APyCFixedArray( ... [1, 2, 3, 4, 5, 6], ... int_bits=10, ... frac_bits=0 ... ) >>> a.sum() APyCFixed((21, 0), bits=13, int_bits=13)
- prod(self, axis: tuple | int | None = None) APyCFixedArray | APyCFixed ¶
Returns 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 APyCFixedArray >>> >>> a = APyCFixedArray.from_complex( ... [1, 2 + 1j, 3 + 3j, -4 + 1j, 5 - 2j, 6 - 3j], ... int_bits=10, ... frac_bits=0 ... ) >>> complex(a.prod()) (-1395-225j)
- cumsum(self, axis: int | None = None) APyCFixedArray ¶
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 APyCFixedArray >>> a = APyCFixedArray.from_complex( ... [ ... [1+1j, 2+3j, 3-2j], ... [4-1j, 5+2j, 6+0j] ... ], ... int_bits=10, ... frac_bits=0 ... ) >>> a.cumsum().to_numpy() array([ 1.+1.j, 3.+4.j, 6.+2.j, 10.+1.j, 15.+3.j, 21.+3.j])
>>> a.cumsum(0).to_numpy() array([[1.+1.j, 2.+3.j, 3.-2.j], [5.+0.j, 7.+5.j, 9.-2.j]])
>>> a.cumsum(1).to_numpy() array([[ 1.+1.j, 3.+4.j, 6.+2.j], [ 4.-1.j, 9.+1.j, 15.+1.j]])
- cumprod(self, axis: int | None = None) APyCFixedArray ¶
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 APyCFixedArray >>> >>> a = APyCFixedArray.from_complex( ... [[1+1j, 2+1j, 3+2j], ... [4-2j, 5+2j, 6+1j]], ... int_bits=10, ... frac_bits=0 ... ) >>> a.cumprod().to_numpy() array([ 1.+1.00e+00j, 1.+3.00e+00j, -3.+1.10e+01j, 10.+5.00e+01j, -50.+2.70e+02j, -570.+1.57e+03j])
>>> a.cumprod(0).to_numpy() array([[ 1. +1.j, 2. +1.j, 3. +2.j], [ 6. +2.j, 8. +9.j, 16.+15.j]])
>>> a.cumprod(1).to_numpy() array([[ 1. +1.j, 1. +3.j, -3.+11.j], [ 4. -2.j, 24. -2.j, 146.+12.j]])
Broadcasting¶
- broadcast_to(self, shape: tuple | int) APyCFixedArray ¶
Broadcast array to new shape.
- Parameters:
- Returns:
Properties¶
Real and imaginary part¶
- property real¶
Real part.
- Returns:
- property imag¶
Imaginary part.
- Returns:
Word length¶
Array properties¶
Transposition¶
- property T¶
The transposition of the array.
Equivalent to calling
APyCFixedArray.transpose()
.- Returns: