BASICS OF
SCIENTIFIC
PROGRAMMING
Computational Methods in Nuclear Physics
NS 1003
BASICS OF SCIENTIFIC
PROGRAMMING
Simple operations using complex
numbers
Solve
𝑥2 - 4 = 0
Solving quadratics
𝑥2 - 4 = 0
𝑥2 = 4
x= ± 4
x=±2
Solve
𝑥2 + 1 = 0
Solving quadratics
𝑥2 + 1 = 0
𝑥 2 = -1 ;no real solutions
In order to solve this equation we define a new number,
i = −𝟏 or 𝒊𝟐 = -1 where i is an imaginary number.
Then; 𝑥 2 = -1
x = ± −1
x=±i
All complex numbers (z) can be written as; z = x + iy.
THE IMAGINARY NUMBER (I)
−1 = i i 2 = −1
It's any
number
you can
imagine
7
i 2 = −1
i = i i = −i
3 2
i = i i = −1 −1 = 1
4 2 2
i = i i = 1 i = i
5 4
...
8
USING I
Now we can handle quantities that
occasionally show up in mathematical
solutions
−a = −1 a = i a
What about
−49 −18
9
−49 −18
i.7
7i
10
COMPLEX NUMBERS
Complex numbers consist of two separate parts:
real part
imaginary part
3
3 + 4i −6 + i 4.5 + i 2 6
2
Visualization
• The Cartesian form of a complex number z is;
Z = x +yi
• Where x and y are both real numbers and i is
known as the imaginary unit
12
COMPLEX PLANE
y
Imaginary Axis
Real Axis x
13
FORM OF COMPLEX NUMBER
Imaginary Axis
( x, y )
z = x + iy r
z
Real Axis
14
COMPLEX NUMBERS IN PYTHON
The basic imaginary unit is equal to the square root of -1.
This is represented in python by letters j.
Python has a built-in module that you can use for
mathematical tasks for complex numbers.
“cmath”.
Complex numbers have their uses in many
applications related to mathematics and python
provides useful tools to handle and manipulate
them.
Example 1:
An complex number is represented by “ x + yi “.
Python converts the real numbers x and y into
complex using the function complex(x,y).
import cmath
x=5
y=3
z = complex(x,y)
print (z)
5+3j
Example 2
Consider the complex number 3 + 2i
1. Use the python function complex to create the
complex number 3 + 2i
2. We can check its type, and confirm that it’s
a complex number:
Floating-point
numbers can be used to create
complex numbers:
z = 3.14 + 2.71j
type(z)
3. The real part can be accessed.
x=3
y=2
z = complex(x,y);
print ("The real part of complex number is : ",end="")
print (z.real)
4. The imaginary part can be be accessed.
x=3
y=2
z = complex(x,y);
print ("The imaginary part of complex number is : ",end="")
print (z.imag)
Magnitude of the complex number
• The magnitude is the distance a complex number z lies from
the origin of the complex plane.
The magnitude of 3 + 2i is:
the square root of (32 + 22) = 13
|3 + 2i| = 131/2
5. When a complex number is passed as an argument
to abs() function, it returns the magnitude of the
complex number.
z = 3 + 2j
print('Absolute value of 3 + 2j is:', abs(z))
Argument/Phase of the complex number
• The argument of a complex number is the angle inclined from
the real axis in the direction of the complex number
represented on the complex plane
Find the argument of the complex number 2 + 2√3i.
6. Argument/ Phase of complex number
Geometrically, the phase of a complex number is
the angle between the positive real axis and the
vector representing complex number.
This is also known as argument of complex number.
Phase is returned using phase(), which takes complex
number as argument.
The range of phase lies from -pi to +pi.
Conjugate of the complex number
A complex conjugate of a complex number is another complex
number that has the same real part as the original complex
number and the imaginary part has the same magnitude but
opposite sign
7. Use the Python function .conjugate() to get
the complex conjugate of the
complex number.
ARITHMETIC OPERATIONS OF
COMPLEX NUMBERS
We can plug complex numbers
into arithmetic expressions and call many of
the built-in functions on them.
1) Addition/subtraction: combine all real parts
together and all imaginary parts together
2) Multiplication: expand first and then combine
real and imaginary parts together
3) Division: to get a real number in the
denominator, we multiply the top and bottom of
the fraction by the complex conjugate
33
Addition
The sum of two or more complex numbers is
equivalent to adding their real and imaginary parts
component-wise:
z1 = 2 + 3j
z2 = 4 + 5j
z1 + z2
(6+8j)
Python automatically promotes operands to
the complex data type when you add values of mixed
numeric types:
z = 2 + 3j
z + 7 # Add complex to integer
(9+3j)
Subtraction
Subtractingcomplex numbers is analogous to
adding them, which means you can also
apply it element-wise:
z1 = 2 + 3j
z2 = 4 + 5j
z1 - z2
(-2-2j)
Multiplication
The
product of two or more complex
numbers gets more interesting:
z1 = 2 + 3j
z2 = 4 + 5j
z1 * z2
(-7+22j)
Multiplication of Two Complex Numbers
37
Solve z1.z2
z1 = 2 + 3j
z2 = 4 + 5j
Division
z1 = 2 + 3j
z2 = 4 + 5j
DIVISION OF TWO COMPLEX NUMBERS
Multiply numerator and denominator by the
conjugate of the denominator
3i 3i 5 + 2i
=
5 − 2i 5 − 2i 5 + 2i
15i + 6i 2
=
25 − 4i 2
−6 + 15i 6 15
= =− + i
29 29 29
40
z1 = 2 + 3j
z2 = 4 + 5j
Find z1/z2
Note that complex numbers don’t
support floor division, also known as integer
division:
z1 // z2 ---------Error
z1 // 3.14 ---------Error
Example 3:
−16 −49
43
Exponentiation
We can raise complex numbers to a power
using the binary exponentiation operator
(**) or the built-in pow().
z = 3 + 2j
Find 𝑧 2
z = 3 + 2j
z**2
(5+12j)
pow(z, 2)
(5+12j)
Example 4
Let a=5+3i and b=2-7i
Perform the arithmetic operations with a
and b.
Example 5:
a = 3 + 2i and b = 4 + 5i.
Find the;
Magnitude of a
Angle of a
Real part of a
Imaginary part of a
Conjugate of a
𝑎
a+b, a –b, 𝑏, a×b, 𝑎2