Array functions¶
Array creation functions¶
- apytypes.zeros(shape: tuple[int, ...], int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None, force_complex: bool | None = None) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Create an array of shape with all zeros.
Word lengths need to be specified and the return type is deduced from the bit specifiers. Either specify exactly two of three from int_bits, frac_bits, and bits, for
APyFixedArray
, or specify both exp_bits and man_bits forAPyFloatArray
. Specify force_complex to retrieve a complex-valued array type.- Parameters:
- shape
tuple
ofint
Shape of the array.
- int_bits
int
, optional Number of fixed-point integer bits.
- frac_bits
int
, optional Number of fixed-point fractional bits.
- bits
int
, optional Number of fixed-point bits.
- exp_bits
int
, optional Number of floating-point exponential bits.
- man_bits
int
, optional Number of floating-point mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.- force_complex
bool
, optional Retrieve the complex-valued array type,
APyCFixedArray
for fixed-point andAPyCFloatArray
for floating-point.
- shape
- Returns:
- result
APyFloatArray
orAPyFixedArray
orAPyCFixedArray
The initialized array with zeros.
- result
- apytypes.ones(shape: tuple[int, ...], int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None, force_complex: bool | None = None) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Create an array of shape with all ones (stored value).
Word lengths need to be specified and the return type is deduced from the bit specifiers. Either specify exactly two of three from int_bits, frac_bits, and bits, for
APyFixedArray
, or specify both exp_bits and man_bits forAPyFloatArray
. Specify force_complex to retrieve a complex-valued array type.- Parameters:
- shape
tuple
ofint
Shape of the array.
- int_bits
int
, optional Number of fixed-point integer bits.
- frac_bits
int
, optional Number of fixed-point fractional bits.
- bits
int
, optional Number of fixed-point bits.
- exp_bits
int
, optional Number of floating-point exponential bits.
- man_bits
int
, optional Number of floating-point mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.- force_complex
bool
, optional Retrieve the complex-valued array type,
APyCFixedArray
for fixed-point andAPyCFloatArray
for floating-point.
- shape
- Returns:
- result
APyFloatArray
orAPyFixedArray
orAPyCFixedArray
The array initialized filled with ones.
- result
- apytypes.eye(n: int, m: int | None = None, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None, force_complex: bool | None = None) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Return a 2-D array with ones (stored value) on the main diagonal and zeros elsewhere.
Word lengths need to be specified and the return type is deduced from the bit specifiers. Either specify exactly two of three from int_bits, frac_bits, and bits, for
APyFixedArray
, or specify both exp_bits and man_bits forAPyFloatArray
. Specify force_complex to retrieve a complex-valued array type.- Parameters:
- n
int
Number of rows in the output.
- m
int
, optional Number of columns in the output. If None, defaults to n.
- int_bits
int
, optional Number of fixed-point integer bits.
- frac_bits
int
, optional Number of fixed-point fractional bits.
- bits
int
, optional Number of fixed-point bits.
- exp_bits
int
, optional Number of floating-point exponential bits.
- man_bits
int
, optional Number of floating-point mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.- force_complex
bool
, optional Retrieve the complex-valued array type,
APyCFixedArray
for fixed-point andAPyCFloatArray
for floating-point.
- n
- Returns:
- result
APyFloatArray
orAPyFixedArray
orAPyCFixedArray
An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.
- result
- apytypes.identity(n: int, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None, force_complex: bool | None = None) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Return the identity array.
Word lengths need to be specified and the return type is deduced from the bit specifiers. Either specify exactly two of three from int_bits, frac_bits, and bits, for
APyFixedArray
, or specify both exp_bits and man_bits forAPyFloatArray
. Specify force_complex to retrieve a complex-valued array type.- Parameters:
- n
int
Number of rows and columns in output.
- int_bits
int
, optional Number of fixed-point integer bits.
- frac_bits
int
, optional Number of fixed-point fractional bits.
- bits
int
, optional Number of fixed-point bits.
- exp_bits
int
, optional Number of floating-point exponential bits.
- man_bits
int
, optional Number of floating-point mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.- force_complex
bool
, optional Retrieve the complex-valued array type,
APyCFixedArray
for fixed-point andAPyCFloatArray
for floating-point.
- n
- Returns:
- result
APyFloatArray
orAPyFixedArray
orAPyCFixedArray
n x n array with ones (stored value) on the main diagonal and zeros elsewhere.
- result
- apytypes.full(shape: tuple[int, ...], fill_value: APyFixed | APyCFixed | APyFloat | int | float, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Return a new array of given shape and type, filled with fill_value.
If fill_value is of type int or float, you must specify the word lengths. Either specify exactly two of three from int_bits, frac_bits, and bits, for
APyFixedArray
, or specify both exp_bits and man_bits forAPyFloatArray
. If fill_value is of type APyFloat or APyFixed, the array will use the provided word lengths only if specified, otherwise the word lengths are inherited from fill_value.- Parameters:
- shape
tuple
Shape of the array.
- fill_value
APyFloat
orAPyFixed
orint
or :class`float` Fill value.
- int_bits
int
, optional Number of fixed-point integer bits.
- frac_bits
int
, optional Number of fixed-point fractional bits.
- bits
int
, optional Number of fixed-point bits.
- exp_bits
int
, optional Number of floating-point exponential bits.
- man_bits
int
, optional Number of floating-point mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.
- shape
- Returns:
- result
APyFloatArray
orAPyFixedArray
Array of all fill_value with the given shape.
- result
- apytypes.zeros_like(a: APyFixedArray | APyCFixedArray | APyFloatArray, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Return an array of all zeros with the same shape and type as a.
The type and bit-specifiers of the returned array can be overwritten through the bit-specifier arguments. To overwrite the type, either specify exactly two of three from int_bits, frac_bits, and bits, for
APyFixedArray
, or specify both exp_bits and man_bits forAPyFloatArray
.- Parameters:
- a
APyFloatArray
orAPyFixedArray
The shape and data-type define these same attributes of the returned array.
- int_bits
int
, optional Number of fixed-point integer bits.
- frac_bits
int
, optional Number of fixed-point fractional bits.
- bits
int
, optional Number of fixed-point bits.
- exp_bits
int
, optional Number of floating-point exponential bits.
- man_bits
int
, optional Number of floating-point mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.
- a
- Returns:
- result
APyFloatArray
orAPyFixedArray
The initialized array with all zeros.
- result
- apytypes.ones_like(a: APyFixedArray) APyFixedArray ¶
- apytypes.ones_like(a: APyCFixedArray) APyCFixedArray
- apytypes.ones_like(a: APyFloatArray) APyFloatArray
Return an array of all ones (stored value) with the same shape and type as a.
The type and bit-specifiers of the returned array can be overwritten through the bit-specifier arguments. To overwrite the type, either specify exactly two of three from int_bits, frac_bits, and bits, for
APyFixedArray
, or specify both exp_bits and man_bits forAPyFloatArray
.- Parameters:
- a
APyFloatArray
orAPyFixedArray
The shape and data-type of a define these same attributes of the returned array.
- int_bits
int
, optional Number of fixed-point integer bits.
- frac_bits
int
, optional Number of fixed-point fractional bits.
- bits
int
, optional Number of fixed-point bits.
- exp_bits
int
, optional Number of floating-point exponential bits.
- man_bits
int
, optional Number of floating-point mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.
- a
- Returns:
- result
APyFloatArray
orAPyFixedArray
The array initialized filled with all ones.
- result
- apytypes.full_like(a: APyFixedArray | APyCFixedArray | APyFloatArray, fill_value: APyFixed | APyCFixed | APyFloat | int | float, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Return an array with all values initialized to fill_value, with the same shape and type as a.
The type and bit-specifiers of the returned array can be overwritten through the bit-specifier arguments. To overwrite the type, either specify exactly two of three from int_bits, frac_bits, and bits, for
APyFixedArray
, or specify both exp_bits and man_bits forAPyFloatArray
.- Parameters:
- a
APyFloatArray
orAPyFixedArray
The shape and array type of the returned array.
- fill_value
APyFloat
orAPyFixed
or int or float The value to fill the array with.
- int_bits
int
, optional Number of fixed-point integer bits.
- frac_bits
int
, optional Number of fixed-point fractional bits.
- bits
int
, optional Number of fixed-point bits.
- exp_bits
int
, optional Number of floating-point exponential bits.
- man_bits
int
, optional Number of floating-point mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.
- a
- Returns:
- result
APyFloatArray
orAPyFixedArray
Array filled with fill_value, having the same shape and type as a.
- result
- apytypes.arange(start: APyFixed | APyCFixed | APyFloat | int | float, stop: APyFixed | APyCFixed | APyFloat | int | float | None = None, step: APyFixed | APyCFixed | APyFloat | int | float = 1, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Create an array with evenly spaced values within a given interval.
The function can be called with varying number of positional arguments:
arange(stop)
: Values are generated within the half-open interval[0, stop)
(in other words, the interval includingstart
but excludingstop
).arange(start, stop)
: Values are generated within the half-open interval[start, stop)
.arange(start, stop, step)
: Values are generated within the half-open interval[start, stop)
, with spacing between values given bystep
.
If no bit-specifiers are given, the array type is deduced based on
start
,stop
, andstep
. In this case, all APyTypes scalars must be of the same format.- Parameters:
- start
int
,float
,APyFloat
,APyFixed
Start number.
- stop
int
,float
,APyFloat
,APyFixed
, optional Stop number.
- step
int
,float
,APyFloat
,APyFixed
, optional Step size in range.
- int_bits
int
, optional Number of fixed-point integer bits.
- frac_bits
int
, optional Number of fixed-point fractional bits.
- bits
int
, optional Number of fixed-point bits.
- exp_bits
int
, optional Number of floating-point exponential bits.
- man_bits
int
, optional Number of floating-point mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.
- start
- Returns:
- result
APyFloatArray
orAPyFixedArray
Array with evenly spaced values within a given interval.
- result
Array modification functions¶
- apytypes.expand_dims(a: APyFixedArray | APyCFixedArray | APyFloatArray, axis: int | tuple[int, ...]) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Expand the shape of an array.
Insert a new axis that will appear at the axis position in the expanded array shape.
- Parameters:
- a
APyFloatArray
,APyFixedArray
, orAPyCFixedArray
The array whose axes should be reordered.
- axis
int
or tuple of ints Position in the expanded axes where the new axis (or axes) is placed.
- a
- Returns:
- result
APyFloatArray
orAPyFixedArray
copy of a with the number of dimensions increased.
- result
Examples
>>> import apytypes as apy >>> x = apy.APyFixedArray.from_float([1, 2], int_bits=5, frac_bits=0) >>> x.shape (2,)
>>> y = apy.expand_dims(x, axis=0) >>> y.to_numpy() array([[1., 2.]]) >>> y.shape (1, 2)
>>> y = apy.expand_dims(x, axis=1) >>> y.to_numpy() array([[1.], [2.]]) >>> y.shape (2, 1)
axis
may also be a tuple:>>> y = apy.expand_dims(x, axis=(0, 1)) >>> y.to_numpy() array([[[1., 2.]]])
>>> y = apy.expand_dims(x, axis=(2, 0)) >>> y.to_numpy() array([[[1.], [2.]]])
- apytypes.ravel(a: APyFixedArray | APyCFixedArray | APyFloatArray) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Return a copy of the array collapsed into one dimension.
Same as
flatten()
with current memory-copy model.- Returns:
- collapsed
APyFloatArray
orAPyFixedArray
copy of a but collapsed
- collapsed
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.])
- apytypes.moveaxis(a: APyFixedArray | APyCFixedArray | APyFloatArray, source, destination) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Move axes of an array to new positions.
Other axes remain in their original order.
- Parameters:
- a
APyFloatArray
orAPyFixedArray
The array whose axes should be reordered.
- source
int
or sequence of int Original positions of the axes to move. These must be unique.
- destination
int
or sequence of int Destination positions for each of the original axes. These must also be unique.
- a
- Returns:
- result
APyFloatArray
orAPyFixedArray
Copy of array with moved axes.
- result
Examples
import apytypes as apy
>>> x = apy.APyFixedArray.from_float([0] * 60, int_bits=2**16, frac_bits=0).reshape( ... (3, 4, 5) ... ) >>> apy.moveaxis(x, 0, -1).shape (4, 5, 3) >>> apy.moveaxis(x, -1, 0).shape (5, 3, 4)
These all achieve the same result:
>>> apy.transpose(x).shape (5, 4, 3) >>> apy.swapaxes(x, 0, -1).shape (5, 4, 3) >>> apy.moveaxis(x, [0, 1], [-1, -2]).shape (5, 4, 3) >>> apy.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape (5, 4, 3)
- apytypes.squeeze(a: APyFixedArray | APyCFixedArray | APyFloatArray, axis=None) APyFixedArray | APyCFixedArray | APyFloatArray | APyFixed | APyCFixed | APyFloat ¶
Remove axes of length one from a.
- Parameters:
- a
APyFloatArray
,APyFixedArray
, orAPyCFixedArray
Input data.
- axis
int
or tuple of ints, optional Selects a subset of the entries of length one in the shape.
- a
- Returns:
- squeezed
APyFloatArray
,APyFixedArray
, orAPyCFixedArray
The input array, but with all or a subset of the dimensions of length 1 removed.
- squeezed
- Raises:
- ValueError
If axis is not None, and an axis being squeezed is not of length 1
- apytypes.swapaxes(a: APyFixedArray | APyCFixedArray | APyFloatArray, axis1: int, axis2: int) APyFixedArray | APyCFixedArray | APyFloatArray ¶
Interchange two axes of an array.
- Parameters:
- a
APyFloatArray
orAPyFixedArray
The array whose axes should be reordered.
- axis1
int
First axis.
- axis2
int
Second axis.
- a
- Returns:
- a_swapped
APyFloatArray
orAPyFixedArray
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=3 ... ) >>> 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.]]])
Array utility functions¶
- apytypes.convolve(a: APyFixedArray | APyCFixedArray | APyFloatArray, v: APyFixedArray | APyCFixedArray | APyFloatArray, mode: Literal['full', 'same', 'valid'] = 'full') APyFixedArray | APyCFixedArray | APyFloatArray ¶
Return the discrete linear convolution of two one-dimensional arrays.
- Parameters:
- a
APyFloatArray
orAPyFixedArray
First one-dimensional array of length
M
- v
APyFloatArray
orAPyFixedArray
Second one-dimensional array of length
N
.- 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
- a
- Returns:
- convolved
APyFloatArray
orAPyFixedArray
The convolved array.
- convolved
- apytypes.shape(arr: APyFixedArray | APyCFixedArray | APyFloatArray) tuple[int, ...] ¶
Return the shape of an array.
- Parameters:
- arr
APyFloatArray
orAPyFixedArray
Input data.
- arr
- Returns:
- Raises:
- TypeError
If the input does not have a shape attribute.