# Complex numbers Source: https://www.tensorplay.cn/docs/complex_numbers.html TensorPlay supports complex tensors — elements stored as a real and an imaginary part. Complex dtypes are tensorplay.complex64 and tensorplay.complex128. ``` import tensorplay as tp x = tp.tensor([1 + 2j, 3 - 4j]) print(x.dtype) # tensorplay.complex64 print(x.real) # the real part print(x.imag) # the imaginary part ``` ## Creating complex tensors tensorplay.complex builds a complex tensor from separate real and imaginary tensors, and tensorplay.polar builds one from magnitude and angle: ``` re = tp.tensor([1.0, 3.0]) im = tp.tensor([2.0, -4.0]) z = tp.complex(re, im) # tensor([1.+2.j, 3.-4.j]) mag = tp.tensor([2.0, 1.0]) ang = tp.tensor([0.0, 1.5708]) p = tp.polar(mag, ang) # magnitude, angle ``` ## Reading and converting tensorplay.real and tensorplay.imag extract the real and imaginary parts. conj returns the complex conjugate and resolve_conj produces a tensor whose conjugation is applied; view_as_real reinterprets a complex tensor as a float tensor with an extra trailing dimension of size 2, while view_as_complex removes that pairing: ``` z = tp.tensor([1 + 2j, 3 + 4j]) print(tp.conj(z)) # [1.-2.j, 3.-4.j] print(tp.view_as_real(z).shape) # (2, 2) — the trailing 2 is (real, imag) ``` tensorplay.is_complex reports whether a tensor has a complex dtype. | tensorplay.complex |Constructs a complex tensor with its real part equal to real and its imaginary part equal to imag. | | --- | --- | | tensorplay.polar | Constructs a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with absolute value abs and angle angle. | | tensorplay.view_as_complex | Returns a view of input as a complex tensor. | | tensorplay.view_as_real | Returns a view of input as a real tensor. | | tensorplay.is_complex | Returns True if the data type of input is a complex data type i.e., one of tensorplay.complex64, and tensorplay.complex128. | | tensorplay.conj | Returns a view of input with a flipped conjugate bit. | | tensorplay.resolve_conj | Returns a new tensor with materialized conjugation if input's conjugate bit is set to True, else returns input. | | tensorplay.real | Returns a new tensor containing real values of the self tensor. | | tensorplay.imag | Returns a new tensor containing imaginary values of the self tensor. | ## Where to go next - [Tensors](/docs/tensorplay.html) — the full tensor API. - [Type information](/docs/type_info.html) — the limits of a complex dtype.