real(), frac(), view_as_real(), view_as_complex() and complex() in PyTorch
Tags: pytorch, real, frac, viewasreal
Published: 2024-05-18 16:40:14

*[My post](https://dev.to/hyperkai/conj-conjphysical-isconj-and-resolveconj-in-pytorch-1mha) explains [conj()](https://pytorch.org/docs/stable/generated/torch.conj.html), [conj_physical()](https://pytorch.org/docs/stable/generated/torch.conj_physical.html), [is_conj()](https://pytorch.org/docs/stable/generated/torch.is_conj.html) and [resolve_conj()](https://pytorch.org/docs/stable/generated/torch.resolve_conj.html).

[real()](https://pytorch.org/docs/stable/generated/torch.real.html) can get the 0D or more D tensor of zero or more real parts from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `real()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor.
- The 1st argument with `torch` is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).

```python
import torch

my_tensor = torch.tensor(4.7352+6.1584j)

torch.real(input=my_tensor)
# tensor(4.7352)

my_tensor = torch.tensor([4.7352+6.1584j,
                          2.3706-8.4339j,
                          -9.1648-3.0342j])
torch.real(input=my_tensor)
# tensor([4.7352,
#         2.3706,
#         -9.1648])

my_tensor = torch.tensor([[4.7352+6.1584j,
                           2.3706-8.4339j,
                           -9.1648-3.0342j],
                          [1.5885+7.2316j,
                           -6.4314+3.9501j,
                           5.7923-4.1148j]])
torch.real(input=my_tensor)
# tensor([[4.7352,
#          2.3706,
#          -9.1648],
#         [1.5885,
#          -6.4314,
#          5.7923]])

my_tensor = torch.tensor([[4, 2, -9],
                          [1, -6, 5]])
torch.real(input=my_tensor)
# tensor([[4, 2, -9],
#         [1, -6, 5]])

my_tensor = torch.tensor([[4.7352, 2.3706, -9.1648],
                          [1.5885, -6.4314, 5.7923]])
torch.real(input=my_tensor)
# tensor([[4.7352, 2.3706, -9.1648],
#         [1.5885, -6.4314, 5.7923]])

my_tensor = torch.tensor([[True, False, True],
                          [False, True, False]])
torch.real(input=my_tensor)
# tensor([[True, False, True],
#         [False, True, False]])
```

[frac()](https://pytorch.org/docs/stable/generated/torch.frac.html) can get the 0D or more D tensor of zero or more decimal parts from the a 0D or more D tensor of zero or more floating point numbers as shown below:

*Memos:
- `frac()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `float`).
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

my_tensor = torch.tensor(4.7352)

torch.frac(input=my_tensor)
my_tensor.frac()
# tensor(0.7352)

my_tensor = torch.tensor([4.7352, 2.3706, -9.1648])

torch.frac(input=my_tensor)
# tensor([0.7352, 0.3706, -0.1648])

my_tensor = torch.tensor([[4.7352, 2.3706, -9.1648],
                          [1.5885, -6.4314, 5.7923]])
torch.frac(input=my_tensor)
# tensor([[0.7352, 0.3706, -0.1648],
#         [0.5885, -0.4314, 0.7923]])
```

[view_as_real()](https://pytorch.org/docs/stable/generated/torch.view_as_real.html) can get the 0D or more D tensor of zero or more real and imaginary parts from the 0D or more D tensor of zero or more complex numbers as shown below:

*Memos:
- `view_as_real()` can be used with `torch` but not with a tensor.
- The 1st argument with `torch` is `input`(Required-Type:`tensor` of `complex`).

```python
import torch

my_tensor = torch.tensor(4.7352+6.1584j)

torch.view_as_real(input=my_tensor)
# tensor([4.7352, 6.1584])

my_tensor = torch.tensor([4.7352+6.1584j,
                          2.3706-8.4339j, 
                          -9.1648-3.0342j])
torch.view_as_real(input=my_tensor)
# tensor([[4.7352, 6.1584],
#         [2.3706, -8.4339],
#         [-9.1648, -3.0342]])

my_tensor = torch.tensor([[4.7352+6.1584j, 
                           2.3706-8.4339j, 
                           -9.1648-3.0342j],
                          [1.5885+7.2316j,
                           -6.4314+3.9501j,
                           5.7923-4.1148j]])
torch.view_as_real(input=my_tensor)
# tensor([[[4.7352, 6.1584],
#          [2.3706, -8.4339],
#          [-9.1648, -3.0342]],
#         [[1.5885, 7.2316],
#          [-6.4314, 3.9501],
#          [5.7923, -4.1148]]])
```

[view_as_complex()](https://pytorch.org/docs/stable/generated/torch.view_as_complex.html) can get the 0D or more D tensor of one or more complex numbers from the 1D or more D tensor of 2 or more even floating point numbers as shown below:

*Memos:
- `view_as_complex()` can be used with `torch` but not with a tensor.
- The 1st argument with `torch` is `input`(Required-Type:`tensor` of `float`).

```python
import torch

my_tensor = torch.tensor([4.7352, 6.1584])

torch.view_as_complex(input=my_tensor)
# tensor(4.7352+6.1584j)

my_tensor = torch.tensor([[4.7352, 6.1584],
                          [2.3706, -8.4339], 
                          [-9.1648, -3.0342]])
torch.view_as_complex(input=my_tensor)
# tensor([4.7352+6.1584j,
#         2.3706-8.4339j,
#         -9.1648-3.0342j])

my_tensor = torch.tensor([[4.7352, 6.1584], 
                          [2.3706, -8.4339], 
                          [-9.1648, -3.0342],
                          [1.5885, 7.2316],
                          [-6.4314, 3.9501],
                          [5.7923, -4.1148]])
torch.view_as_complex(input=my_tensor)
# tensor([4.7352+6.1584j,
#         2.3706-8.4339j,
#         -9.1648-3.0342j,
#         1.5885+7.2316j,
#         -6.4314+3.9501j,
#         5.7923-4.1148j])
```

[complex()](https://pytorch.org/docs/stable/generated/torch.complex.html) can get the 0D or more D tensor of zero or more complex numbers from two of the 0D or more D tensors of zero or more floating point numbers as shown below:

*Memos:
- `complex()` can be used with `torch` but not with a tensor.
- The 1st argument with `torch` is `real`(Required-Type:`tensor` of `float`).
- The 2st argument with `torch` or the 1st argument is `imag`(Required-Type:`tensor` of `float`).
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

tensor1 = torch.tensor(4.7352)
tensor2 = torch.tensor([6.1584, -8.4339, -3.0342])

torch.complex(real=tensor1, imag=tensor2)
# tensor([4.7352+6.1584j, 4.7352-8.4339j, 4.7352-3.0342j])

torch.complex(real=tensor2, imag=tensor1)
# tensor([6.1584+4.7352j, -8.4339+4.7352j, -3.0342+4.7352j])

tensor1 = torch.tensor([4.7352, 2.3706, -9.1648])
tensor2 = torch.tensor([[6.1584, -8.4339, -3.0342],
                        [7.2316, 3.9501, -4.1148]])
torch.complex(real=tensor1, imag=tensor2)
# tensor([[4.7352+6.1584j, 2.3706-8.4339j, -9.1648-3.0342j],
#         [4.7352+7.2316j, 2.3706+3.9501j, -9.1648-4.1148j]])

torch.complex(real=tensor2, imag=tensor1)
# tensor([[6.1584+4.7352j, -8.4339+2.3706j, -3.0342-9.1648j],
#         [7.2316+4.7352j, 3.9501+2.3706j, -4.1148-9.1648j]])
```