# 1 - Importing cmpx


```python
import cmpx
```


```python
cmpx.__version__
```




    '0.8.4'



# 2 - Instanciate a Complex class


```python
number = cmpx.Complex(re=1, im=3.2, restore=True)
```


```python
number
```




    1 + 3.2j




```python
type(number)
```




    cmpx.number.Complex



### As you can see, whenever you create a new object you have to give:
- re: real part of complex number.
- im: imaginary part of complex number.
- restore: this argument is used whenever you want to restore the last number you had to avoid assigning None to your object, sometimes operations are not allowed like dividing by zero so Python interpreter assign None whenever this happened.
### Default values of the constructor are as follows:
- re = 0
- im = 0
- restore = True


```python
number = cmpx.Complex()
number
```




    0



## There's another way to create a complex number


```python
number = cmpx.Complex.fromComplex(1 - 1j)
number
```




    1.0 - j



- As you can see, you can put directly the complex number as you know of, but keep in mind that the imaginary part must be always multiplied by a coefficient as above.


```python
number = cmpx.Complex.fromComplex(2 - 2j)
number
```




    2.0 - 2.0j



## To avoid the long wrtinig you can import directly the Complex class


```python
from cmpx import Complex

number = Complex(1,3)
number
```




    1 + 3j



# 3 - Mathematical operations
- There's a lot of operations you can do!


```python
num1 = cmpx.Complex.fromComplex(1 - 2j)
num2 = cmpx.Complex.fromComplex(-2 + 1j)
```

## Addition


```python
result = num1 + num2
result
```




    -1.0 - j



## Substraction


```python
result = num1 - num2
result
```




    3.0 - 3.0j



## Multiplication


```python
result = num1 * num2
result
```




    5.0j



## Division


```python
result = num1 / num2
result
```




    -0.8 + 0.6j




```python
result = num1 // num2
result
```




    -1.0



- You can also affect directly to the number by unary operators (+=, -=, *=, /=, //=).
- You can also make operations with non instanciated complex numbers as follows.


```python
num1 += 1
num1
```




    2.0 - 2.0j




```python
num1 *= 1 - 3j
num1
```




    -4.0 + 10.0j




```python
num1 /= 0
num1
```

    Float division by zero
    Restoring last number





    -4.0 + 10.0j



- Oh oh! you saw what happened there? as we spoke about it above, the restore field is playing a major factor here, because by default restore is True and because of that it will restore the last number you had.
- Also, you can make restore=False if you don't want to recover the last number.

## Module


```python
num2.mod()
```




    2.23606797749979



## Conjugate 


```python
num2.con()
```




    -2.0 - j



# 4 - Comparisons
- You can also do comparison between complex numbers based on their modules.


```python
num1 = cmpx.Complex(1, 2)
num2 = cmpx.Complex(-2, -3)
```


```python
if num1 > num2:
    print('({}) > ({})'.format(num1, num2))
```


```python
if num1 >= num2:
    print('({}) >= ({})'.format(num1, num2))
```


```python
if num1 < num2:
    print('({}) < ({})'.format(num1, num2))
```

    (1 + 2j) < (-2 - 3j)



```python
if num1 <= num2:
    print('({}) <= ({})'.format(num1, num2))
```

    (1 + 2j) <= (-2 - 3j)



```python
if num1 == num2:
    print('({}) == ({})'.format(num1, num2))
```


```python
if num1 != num2:
    print('({}) != ({})'.format(num1, num2))
```

    (1 + 2j) != (-2 - 3j)


- You can also do comparisons with non instanciated numbers as follows.


```python
if num1 > (1 - 0.5j):
    print('({}) > ({})'.format(num1, (1-0.5j)))
```

    (1 + 2j) > ((1-0.5j))


# 5 - Solving linear and second degree equations
- You can solve first or second degree equation using solve function, it takes three arguments and those are the coefficients (a, b and c) :
\begin{equation}
ax^2 + bx + c = 0
\end{equation}
- solve function returns a tuple of solutions which they are Complex objects.


```python
from cmpx.equations import solve
```


```python
solutions = solve(-1, 2.5, -4)
```


```python
solutions
```




    (1.25 + 1.5612494995995996j, 1.25 - 1.5612494995995996j)




```python
type(solutions[0])
```




    cmpx.number.Complex



894032da6090a406990048f5f199264b8e7d0a98
ipynb
Jupyter Notebook
cmpx tutorial.ipynb
Omar-Belghaouti/PythonComplex
4f286ee4a4c8c042a02a5a2e92d063377c15c713
MIT
2019-09-10T20:35:44.000Z
2021-09-30T11:14:25.000Z
cmpx tutorial.ipynb
Omar-Belghaouti/PythonComplex
4f286ee4a4c8c042a02a5a2e92d063377c15c713
MIT
cmpx tutorial.ipynb
Omar-Belghaouti/PythonComplex
4f286ee4a4c8c042a02a5a2e92d063377c15c713
MIT
Qwen/Qwen-72B
1. YES
2. YES