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 method that passes value and bit-specifiers to one of:
depending on value. Returns
APyFixed
when value isint
orfloat
. ReturnsAPyCFixed
when value iscomplex
or force_complex is True. ReturnsAPyFixedArray
orAPyCFixedArray
when value is a sequence of numbers.Added in version 0.3.
Hint
Currently, this function will not detect sequences of complex values. Set force_complex to True.
- Parameters:
- val
int
,float
, list of int or float, ndarray Floating-point/integer/complex value(s) to initialize from.
- int_bits
int
, optional Number of integer bits in the created fixed-point object.
- frac_bits
int
, optional Number of fractional bits in the created fixed-point object.
- bits
int
, optional Total number of bits in the created fixed-point object.
- force_complex
bool
, default: False If True, force the return value to be
APyCFixed
orAPyCFixedArray
, even if value is real.
- val
- Returns:
- 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 method that passes value and bit-specifiers to one of:
depending on value. Returns
APyFloat
when value isint
orfloat
. ReturnsAPyCFloat
when value iscomplex
or when is_complex is True. ReturnsAPyFloatArray
orAPyCFloatArray
when value is a sequence of numbers.Added in version 0.3.
Hint
Currently, this function will not detect sequences of complex values. Set force_complex to True.
- Parameters:
- value
int
,float
, list of int or float, ndarray Floating-point/integer/complex value(s) to initialize from.
- exp_bits
int
Number of exponent bits.
- man_bits
int
Number of mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.- force_complex
bool
, default: False If True, force the return value to be
APyCFloat
orAPyCFloatArray
, even if value is real.
- value
- Returns:
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:
Convert argument(s) to float
Evaluate function with float argument(s)
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:
Examples
>>> import apytypes as apy >>> import math >>> a = apy.APyFixed(19, 1, 5) >>> apy.fn(math.sin, a) APyFixed(18, bits=6, int_bits=1)