Utility functions

Create scalars and arrays

apytypes.fx(value: int | float, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, *, force_complex: Literal[False] = False) APyFixed
apytypes.fx(value: int | float, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, *, force_complex: Literal[True]) APyCFixed
apytypes.fx(value: complex, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, *, force_complex: bool = False) APyCFixed
apytypes.fx(value: Iterable[Any], int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, *, force_complex: Literal[False] = False) APyFixedArray
apytypes.fx(value: Iterable[Any], int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, *, force_complex: Literal[True]) APyCFixedArray

Create a fixed-point scalar or array.

Convenience function that passes value and bit-specifiers to one of:

depending on value. Returns APyFixed when value is int or float. Returns APyCFixed when value is complex or when force_complex is True. Returns APyFixedArray or APyCFixedArray when value is a sequence of numbers.

Added in version 0.3.

Parameters:
valint, float, list of int or float, ndarray

Floating-point/integer/complex value(s) to initialize from.

int_bitsint, optional

Number of integer bits in the created fixed-point object.

frac_bitsint, optional

Number of fractional bits in the created fixed-point object.

bitsint, optional

Total number of bits in the created fixed-point object.

force_complexbool, default: False

If True, force the return value to be APyCFixed or APyCFixedArray, even if value is real.

Returns:
APyFixed, APyFixedArray, APyCFixed, or
APyCFixedArray
apytypes.fp(value: int | float, exp_bits: int, man_bits: int, bias: int | None = None, *, force_complex: Literal[False] = False) APyFloat
apytypes.fp(value: int | float, exp_bits: int, man_bits: int, bias: int | None = None, *, force_complex: Literal[True]) APyCFloat
apytypes.fp(value: complex, exp_bits: int, man_bits: int, bias: int | None = None, *, force_complex: bool = False) APyCFloat
apytypes.fp(value: Iterable[Any], exp_bits: int, man_bits: int, bias: int | None = None, *, force_complex: Literal[False] = False) APyFloatArray
apytypes.fp(value: Iterable[Any], exp_bits: int, man_bits: int, bias: int | None = None, *, force_complex: Literal[True]) APyCFloatArray

Create a floating-point scalar or array.

Convenience function that passes value and bit-specifiers to one of:

depending on value. Returns APyFloat when value is int or float. Returns APyCFloat when value is complex or when force_complex is True. Returns APyFloatArray or APyCFloatArray when value is a sequence of numbers.

Added in version 0.3.

Parameters:
valueint, float, list of int or float, ndarray

Floating-point/integer/complex value(s) to initialize from.

exp_bitsint

Number of exponent bits.

man_bitsint

Number of mantissa bits.

biasint, optional

Exponent bias. If not provided, bias is 2**(exp_bits - 1) - 1.

force_complexbool, default: False

If True, force the return value to be APyCFloat or APyCFloatArray, even if value is real.

Returns:
APyFloat, APyFloatArray, APyCFloat, or
APyCFloatArray
apytypes.from_bits(bit_patterns: int, *, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: None = None, man_bits: None = None, bias: None = None, force_complex: Literal[False] = False) APyFixed
apytypes.from_bits(bit_patterns: int, *, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: None = None, man_bits: None = None, bias: None = None, force_complex: Literal[True]) APyCFixed
apytypes.from_bits(bit_patterns: int, *, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: None = None, man_bits: None = None, bias: None = None, force_complex: bool) APyFixed | APyCFixed
apytypes.from_bits(bit_patterns: Iterable[Any], *, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: None = None, man_bits: None = None, bias: None = None, force_complex: Literal[False] = False) APyFixedArray
apytypes.from_bits(bit_patterns: Iterable[Any], *, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: None = None, man_bits: None = None, bias: None = None, force_complex: Literal[True]) APyCFixedArray
apytypes.from_bits(bit_patterns: Iterable[Any], *, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, exp_bits: None = None, man_bits: None = None, bias: None = None, force_complex: bool) APyFixedArray | APyCFixedArray
apytypes.from_bits(bit_patterns: int, *, int_bits: None = None, frac_bits: None = None, bits: None = None, exp_bits: int, man_bits: int, bias: int | None = None, force_complex: Literal[False] = False) APyFloat
apytypes.from_bits(bit_patterns: int, *, int_bits: None = None, frac_bits: None = None, bits: None = None, exp_bits: int, man_bits: int, bias: int | None = None, force_complex: Literal[True]) APyCFloat
apytypes.from_bits(bit_patterns: int, *, int_bits: None = None, frac_bits: None = None, bits: None = None, exp_bits: int, man_bits: int, bias: int | None = None, force_complex: bool) APyFloat | APyCFloat
apytypes.from_bits(bit_patterns: Iterable[Any], *, int_bits: None = None, frac_bits: None = None, bits: None = None, exp_bits: int, man_bits: int, bias: int | None = None, force_complex: Literal[False] = False) APyFloatArray
apytypes.from_bits(bit_patterns: Iterable[Any], *, int_bits: None = None, frac_bits: None = None, bits: None = None, exp_bits: int, man_bits: int, bias: int | None = None, force_complex: Literal[True]) APyCFloatArray
apytypes.from_bits(bit_patterns: Iterable[Any], *, int_bits: None = None, frac_bits: None = None, bits: None = None, exp_bits: int, man_bits: int, bias: int | None = None, force_complex: bool) APyFloatArray | APyCFloatArray

Create an APyType scalar or array from bit pattern.

Added in version 0.6.

Parameters:
bit_patternsint or Iterable

Bit representation of the number(s). Number of rows and columns in output.

int_bitsint, optional

Number of fixed-point integer bits.

frac_bitsint, optional

Number of fixed-point fractional bits.

bitsint, optional

Number of fixed-point bits.

exp_bitsint, optional

Number of floating-point exponential bits.

man_bitsint, optional

Number of floating-point mantissa bits.

biasint, optional

Exponent bias. If not provided, bias is 2**(exp_bits - 1) - 1.

force_complexbool, optional

Returns a complex-valued type.

Returns:
resultAPyFixed, APyFloat, APyFixedArray, APyFloatArray, APyCFixed, APyCFloat, APyCFixedArray or APyCFloatArray

Evaluate functions

apytypes.fn(fn: Callable[[int | float], int | float], *args: APyFloat | APyFixed | APyFixedArray | APyFloatArray) APyFloat | APyFixed | APyFixedArray | APyFloatArray

Utility function to evaluate functions on arguments and convert back.

This does exactly:

  1. Convert argument(s) to float

  2. Evaluate function with float argument(s)

  3. Convert result back to the same type as the argument

Hence, there may be numerical issues, but it provides a simple way to perform, e.g., sin, assuming that it is based on a look-up table.

Added in version 0.3.

Parameters:
fncallable

The function to evaluate.

argsAPyFixed, APyFloat, APyFixedArray, APyFloatArray

The argument(s) to evaluate the function for.

Returns:
APyFloat, APyFloatArray, APyFixed, APyFixedArray

Examples

>>> import apytypes as apy
>>> import math
>>> a = apy.APyFixed(19, 1, 5)
>>> apy.fn(math.sin, a)
APyFixed(18, int_bits=1, frac_bits=5)

Enum types

class apytypes.ConvolutionMode(*values)
FULL = 0
SAME = 1
VALID = 2