Utility functions

Create scalars and arrays

apytypes.fx(value: int | float | Sequence[int] | Sequence[float], int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None, force_complex: bool = False) APyCFixed | APyFixed | APyFixedArray

Create an APyFixed, APyCFixed or APyFixedArray object.

Convenience method that applies APyFixed.from_float(), APyCFixed.from_complex() or APyFixedArray.from_float() depending on if the input, value, is a scalar or not. For scalar values, return APyCFixed if value is complex or if force_complex is True.

Added in version 0.3.

Parameters:
valint, float, list(int), list(float)

Floating point 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 even if value is real.

Returns:
APyFixed, APyFixedArray
apytypes.fp(value: int | float | Sequence[int] | Sequence[float], exp_bits: int, man_bits: int, bias: int | None = None) APyFloat | APyFloatArray

Create an APyFloat or APyFloatArray object.

Convenience method that applies APyFloat.from_float() or APyFloatArray.from_float() depending on if the input, value, is a scalar or not.

Added in version 0.3.

Parameters:
valueint, float, list(int), list(float)

Floating point 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.

Returns:
APyFloat, APyFloatArray

Evaluate functions

apytypes.fn(fn: Callable, *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.

args: APyFixed, APyFloat, APyFixedArray, APyFloatArray

The argument(s) to evaluate the function for.

Returns:
APyFloat, APyFloatArray, APyFixed, APyFixedArray

Examples

>>> from apytypes import APyFixed, fn
>>> import math
>>> a = APyFixed(19, 1, 5)
>>> fn(math.sin, a)
APyFixed(18, bits=6, int_bits=1)