APyCFixed¶
- class apytypes.APyCFixed(*args, **kwargs)¶
Class for configurable complex-valued scalar fixed-point formats.
Added in version 0.3.
APyCFixedis an arbitrary precision complex-valued two’s complement fixed-point scalar type. In many ways it behaves like the built-in Python typecomplexin that it can be used within ordinary arithmetic expressions that allows complex numbers. Every fixed-point instance has an associated word length, determined by itsbits,int_bits, andfrac_bitsbit specifiers. These specifiers determine the location of the binary fix-point and the total word length. Both the real and imaginary part share bit specifiers, and the total number of bits in anAPyCFixedis2 * bits. Only two of three bit specifiers need to be set to uniquely determine the complete fixed-point format.Note
For complex-valued fixed-point arrays, see
APyCFixedArray. For real-valued fixed-point formats, seeAPyFixedandAPyFixedArray.Word-length of fixed-point arithmetic operations using APyCFixed¶Operation
Result
int_bitsResult
frac_bitsa + bmax(a.int_bits, b.int_bits) + 1max(a.frac_bits, b.frac_bits)a - bmax(a.int_bits, b.int_bits) + 1max(a.frac_bits, b.frac_bits)a * ba.int_bits + b.int_bits + 1a.frac_bits + b.frac_bitsa / ba.int_bits + b.frac_bits + 1a.frac_bits + b.int_bits-aa.int_bits + 1a.frac_bits- Attributes:
bitsTotal number of bits.
frac_bitsNumber of fractional bits.
imagImaginary part.
int_bitsNumber of integer bits.
is_zeroTrue if the value equals zero, false otherwise.
leading_onesNumber of leading ones.
leading_signsNumber of leading sign-bits.
leading_zerosNumber of leading zeros.
realReal part.
trailing_zerosNumber of trailing zeros.
Methods
Change format of the complex-valued fixed-point number.
Retrieve complex conjugate.
Create a copy of the object.
Test if two fixed-point objects are exactly identical.
Constructor¶
Creation from other types¶
- from_complex(value: object, int_bits: int | None = None, frac_bits: int | None = None, bits: int | None = None) APyCFixed¶
Create an
APyCFixedobject from anint,float,complex,APyFixed,APyFloat, orAPyCFixed.The input is quantized using
QuantizationMode.RND_INFand overflow is handled using theOverflowMode.WRAPmode. Exactly two of the three bit-specifiers (bits, int_bits, frac_bits) must be set.- Parameters:
- Returns:
Examples
Complex-valued fixed-point fx_a, initialized from the complex number 1.234 + 0.4j, rounded to 1.25 + 0.5j as it is the closest representable number.
>>> import apytypes as apy >>> fx_a = apy.APyCFixed.from_complex(1.234 + 0.4j, int_bits=2, frac_bits=2) >>> fx_a APyCFixed((5, 2), bits=4, int_bits=2) >>> print(fx_a) (1.25+0.5j)
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) APyCFixed¶
Change format of the complex-valued fixed-point number.
This is the primary method for performing quantization and overflowing/saturation when dealing with APyTypes fixed-point numbers. The specified quantization and overflow are applied piecewise to the real and imaginary part.
Exactly two of three bit-specifiers (bits, int_bits, frac_bits) needs to 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:
Examples
>>> import apytypes as apy >>> fx = apy.fx(2.125 + 1.625j, int_bits=3, frac_bits=3)
# Truncation: 2.0 + 1.5j >>> fx.cast(int_bits=3, frac_bits=2, quantization=apy.QuantizationMode.TRN) APyCFixed((8, 6), bits=5, int_bits=3)
# Fixed-point rounding: 2.25 + 1.75j >>> fx.cast(int_bits=3, frac_bits=2, quantization=apy.QuantizationMode.RND) APyCFixed((9, 7), bits=5, int_bits=3)
# Two’s complement overflowing: -1.875 + 1.625j >>> fx.cast(int_bits=2, frac_bits=3, overflow=apy.OverflowMode.WRAP) APyCFixed((17, 13), bits=5, int_bits=2)
Get bit representation¶
- to_bits(self) tuple[int, int]¶
Retrieve underlying bit-pattern in a
tupleofint.Examples
Create complex-valued fixed-point number fx_a of value -5.75 + 2j and show its bit pattern (real, imag)
>>> import apytypes as apy >>> >>> fx_a = apy.fx(-5.75 + 2j, int_bits=4, frac_bits=4) >>> fx_a.to_bits() (164, 32)
Comparison¶
- is_identical(self, other: APyCFixed | APyCFixedArray) bool¶
Test if two fixed-point objects are exactly identical.
Two APyCFixed objects are considered exactly identical if, and only if, they represent the same fixed-point value, and have the exact same bit-specification (bits, int_bits, and frac_bits). This is a more restrictive test than
==, that only tests equality of the numerical fixed-point value.Examples
Complex-valued fixed-point fx_a and fx_b. They are equal, as they store the same value, but they are not identical as the differ in the int_bits specifier.
>>> import apytypes as apy >>> fx_a = apy.fx(2.0 + 3.0j, int_bits=3, frac_bits=3) >>> fx_b = apy.fx(2.0 + 3.0j, int_bits=4, frac_bits=3) >>> fx_a == fx_b True >>> fx_a.is_identical(fx_b) False
Other¶
Properties¶
Word length¶
Bit pattern information¶
- property is_zero¶
True if the value equals zero, false otherwise.
- property leading_ones¶
Number of leading ones.
Minimum of real and imaginary parts.
Added in version 0.5.
- Returns:
- property leading_signs¶
Number of leading sign-bits.
That is, the number of bits that is identical to the most significant bit.
Minimum of real and imaginary parts.
Added in version 0.5.
- Returns:
- property leading_zeros¶
Number of leading zeros.
Minimum of real and imaginary parts.
Added in version 0.5.
- Returns:
Real and imaginary part¶