0% found this document useful (0 votes)
31 views6 pages

1.1 1. Complex Arithmetic in Python

The document discusses complex arithmetic and matrix manipulation in Python. It provides examples of defining complex numbers, performing arithmetic operations on complex numbers such as addition and multiplication, and extracting the real and imaginary parts. It also demonstrates defining matrices using NumPy, basic matrix operations like addition, subtraction and multiplication, and accessing elements of arrays and matrices.

Uploaded by

RagaviSowmya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

1.1 1. Complex Arithmetic in Python

The document discusses complex arithmetic and matrix manipulation in Python. It provides examples of defining complex numbers, performing arithmetic operations on complex numbers such as addition and multiplication, and extracting the real and imaginary parts. It also demonstrates defining matrices using NumPy, basic matrix operations like addition, subtraction and multiplication, and accessing elements of arrays and matrices.

Uploaded by

RagaviSowmya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2021-01-04

January 8, 2021

1 Complex Arithmetic
1.1 1. Complex Arithmetic in python
1.1.1 1.1 Defining of Complex number
EXAMPLE 1:
[2]: u=3j+5
[3]: u
[3]: (5+3j)
EXAMPLE 2:
[4]: v=2+3j
[5]: v
[5]: (2+3j)
EXAMPLE 3:
[6]: p=3
q=5
r=p+1j*q
[7]: r
[7]: (3+5j)
EXAMPLE 4:
[8]: s=2
t=6
z=complex(s,t)
[9]: z
[9]: (2+6j)

1.1.2 1.2 Addition and subtraction of two complex numbers


EXAMPPLE 1:
[10]: m=1+2j
n=5+6j

1
w=m+n
[11]: w
[11]: (6+8j)
EXAMPLE 2:
[12]: a=2+3j
b=5+6j
a+b
[12]: (7+9j)
EXAMPLE 3:
[13]: a-b
[13]: (-3-3j)

1.1.3 1.3 Multiplication and Division of two numbers


EXAMPLE 1:
[14]: a*b
[14]: (-8+27j)
EXAMPLE 2:
[15]: u*v
[15]: (1+21j)
EXAMPLE 3:
[16]: r*z
[16]: (-24+28j)
EXAMPLE 4:
[17]: a/b
[17]: (0.4590163934426229+0.04918032786885245j)
EXAMPLE 5:
[18]: u/v
[18]: (1.4615384615384617-0.6923076923076924j)
EXAMPLE 6:
[19]: r/z
[19]: (0.8999999999999999-0.2j)

1.1.4 1.4 Extracting real and Imaginery part from a complex number
EXAMPLE 1:
[23]: w.real

2
[23]: 6.0
[24]: w.imag
[24]: 8.0
EXAMPLE 2:
[21]: z.real
[21]: 2.0
[25]: z.imag
[25]: 6.0
EXAMPLE 3:
[26]: r.real
[26]: 3.0
[28]: r.imag
[28]: 5.0
EXAMPLE 4:
[29]: h=9-5j
h.real
[29]: 9.0
[30]: h.imag
[30]: -5.0

1.1.5 1.5 Complex Conjugate


EXAMPLE 1:
[32]: h.conjugate()
[32]: (9+5j)
EXAMPLE 2:
[33]: w.conjugate()
[33]: (6-8j)
EXAMPLE 3:
[34]: z.conjugate()
[34]: (2-6j)

1.1.6 1.6 Complex Functions in Python


The ‘cmath’ module defines functions that take a complex number as argument and return a com-
plex number as result.
[36]: from cmath import *

3
EXAMPLE 1:
[37]: r1=sin(8j)
[38]: r1
[38]: 1490.4788257895502j
EXAMPLE 2:
[39]: r2=cos(4j)
[40]: r2
[40]: (27.308232836016487-0j)
Note: eˆ(i) = cos + i sin
EXAMPLE 1:
[41]: f1=3
exp(1j*f1)
[41]: (-0.9899924966004454+0.1411200080598672j)
[42]: cos(f1)+1j*sin(f1)
[42]: (-0.9899924966004454+0.1411200080598672j)
EXAMPLE 2:
[43]: f2=8
exp(1j*f2)
[43]: (-0.14550003380861354+0.9893582466233818j)
[44]: cos(f2)+1j*sin(f2)
[44]: (-0.14550003380861354+0.9893582466233818j)
EXAMPLE 3: Find 1
[45]: sqrt(-1)
[45]: 1j

1.2 2 Matrix Manipulation in Python


In python matrix can be implemented as a 2D list/2D array. The module ‘numpy’ is imported to
do matrix multiplication.

1.2.1 2.1 Defining Matrices


EXAMPLE 1:
[48]: import numpy as np
A=np.array([[1,2],[3,4]])
print(A)

[[1 2]
[3 4]]
EXAMPLE 2:

4
[50]: B=np.array([[12,10],[-13,-4]])
print(B)

[[ 12 10]
[-13 -4]]

1.2.2 2.2 Basic Operations on Matrices


2.2.1 Addition of two matrices add() : This function is used to perform elementwise matrix
addition
EXAMPLE :
[51]: print(np.add(A,B))

[[ 13 12]
[-10 0]]

2.2.2 Subtraction of two matrices subtract() : This funtion is used to perform elementwise matrix
subtraction
EXAMPLE :
[52]: print(np.subtract(A,B))

[[-11 -8]
[ 16 8]]

2.2.3 Multiplication of two matrices multiply() : This function is used to perform elementwise
matrix multiplication.
EXAMPLE :
[53]: print(np.multiply(A,B))

[[ 12 20]
[-39 -16]]

dot() : This function is used to compute the dot multiplication.


[54]: print(np.dot(A,B))

[[-14 2]
[-16 14]]

2.2.4 Division of two matrices divide() : This command performs elementwise matrix division.
EXAMPLE :
[55]: print(np.divide(A,B))

[[ 0.08333333 0.2 ]
[-0.23076923 -1. ]]

2.2.5 Transpose of a Matrix EXAMPLE 1:

5
[56]: print(A.transpose())

[[1 3]
[2 4]]

EXAMPLE 2:
[57]: print(B.transpose())

[[ 12 -13]
[ 10 -4]]

2.2.6 Access of elements in array


[58]: import numpy as np
[59]: N=np.array([1,2,20,30])
print("N[0]=",N[0])
print("N[1]=",N[1])
print("N[2]=",N[2])
print("N[3]=",N[3])

N[0]= 1
N[1]= 2
N[2]= 20
N[3]= 30

1. Access Rows EXAMPLE :


[60]: M=np.array([[2,10],[-1,-4]])
print(M[0])

[ 2 10]

2. Access Columns EXAMPLE :


[61]: N=np.array([[2,10],[-1,-4]])
print("First Column",M[:,0])
print("Second Column",N[:,1])

First Column [ 2 -1]


Second Column [10 -4]

[ ]:

You might also like