# Load Complex Numbers using NumPy loadtxt()

In this tutorial, you’ll learn how to load complex numbers from various formats using NumPy `loadtxt` function.

Whether your data is in a standard complex format, separated into real and imaginary columns, or enclosed in custom ways, NumPy can handle it all.

Table of Contents

## Load Standard Complex Format

Suppose you have a dataset of complex numbers stored in a standard format like `1+2j`, and you want to load this data into a NumPy array.

Here’s how you can do it:

```import numpy as np

# Sample file path
file_path = 'complex_numbers.txt'
complex_data = np.loadtxt(file_path, dtype=complex)
print(complex_data)
```

Output:

```[1.+2.j 3.+4.j 5.+6.j]
```

Here, `np.loadtxt` is used to read the file `complex_numbers.txt`, which contains complex numbers in the format `1+2j`.

The `dtype=complex` argument tells NumPy to interpret the loaded data as complex numbers.

The result is a NumPy array of complex numbers, each represented as `a+bj`, where `a` is the real part and `b` is the imaginary part.

## Load Separated Real and Imaginary Columns

In some cases, the real and imaginary parts of complex numbers are separated into different columns.

NumPy’s `loadtxt` function can handle this format as well:

```import numpy as np
file_path = 'separated_complex_numbers.txt'

# Load the real and imaginary parts
real_part, imag_part = np.loadtxt(file_path, unpack=True)
complex_data = real_part + 1j*imag_part
print(complex_data)
```

Output:

```[1.+2.j 3.+4.j 5.+6.j]
```

In this script, `np.loadtxt` reads two columns of data from `separated_complex_numbers.txt`.

The `unpack=True` argument splits these columns into two separate arrays: `real_part` and `imag_part`.

These arrays are then combined to form an array of complex numbers using the expression `real_part + 1j*imag_part`, where `1j` represents the imaginary unit in Python.

## Load Custom Formats

Handling custom formats of complex numbers, such as using ‘i’ instead of ‘j’ for the imaginary part and omitting the sign for the imaginary part, requires a bit more work in Python with NumPy.

You can’t directly use `np.loadtxt` for these formats, but there’s a straightforward workaround.

Here’s how to do it:

```import numpy as np
file_path = 'custom_complex_numbers.txt'
def convert_to_complex(number_str):
number_str = number_str.replace('i', 'j').replace('+-', '-').replace('+', '+')
return complex(number_str)
complex_data = np.loadtxt(file_path, converters={0: convert_to_complex})
print(complex_data)
```

Output:

```[1.+2.j 3.-4.j 5.+6.j]
```

The function `convert_to_complex` replaces ‘i’ with ‘j’ and ensures that the sign between the real and imaginary parts is correctly formatted.

Then, `np.loadtxt` is used with the `converters` parameter to apply this function to each entry in the file.

## Load String Enclosed Complex Numbers

Sometimes complex numbers are stored in text files with each number enclosed in quotes, like `"1+2j"`.

Here’s how you can load such complex numbers using NumPy’s `loadtxt` function:

```import numpy as np
file_path = 'string_enclosed_complex_numbers.txt'
def convert_enclosed_complex(number_str):
return complex(number_str.strip('"'))
complex_data = np.loadtxt(file_path, converters={0: convert_enclosed_complex}, dtype=complex)
print(complex_data)
```

Output:

```[1.+2.j 3.+4.j 5.+6.j]
```

In this example, a custom function `convert_enclosed_complex` is used to strip the quotes and convert the string to a complex number.

The `np.loadtxt` function then applies this converter to each element in the file.

## Load Parentheses Enclosed Complex Numbers

Loading complex numbers that are enclosed in parentheses, such as `(1+2j)`, requires removing the parentheses.

```import numpy as np
file_path = 'parentheses_enclosed_complex_numbers.txt'
def convert_parentheses_complex(number_str):
return complex(number_str.strip('()'))
complex_data = np.loadtxt(file_path, converters={0: convert_parentheses_complex}, dtype=complex)
print(complex_data)
```

Output:

```[1.+2.j 3.+4.j 5.+6.j]
```

This script uses a custom converter function, `convert_parentheses_complex`, which strips off the parentheses from each string before converting it to a complex number.

The `np.loadtxt` function is then used with the `converters` parameter to process each line in the text file and transform them into complex numbers in the desired format.

## Loading Only the Real or Imaginary Part

Here’s how to selectively load just one part of the complex data:

```import numpy as np
file_path = 'complex_data.txt'
complex_data = np.loadtxt(file_path, dtype=complex)
real_parts = complex_data.real
print("Real Parts:", real_parts)
imaginary_parts = complex_data.imag
print("Imaginary Parts:", imaginary_parts)
```

Output:

```Real Parts: [1. 3. 5.]
Imaginary Parts: [2. 4. 6.]
```

In this code, after loading the complex numbers into `complex_data`, the `.real` and `.imag` properties of the NumPy array are used to extract the real and imaginary parts respectively.