0% found this document useful (0 votes)
20 views

Session 11 Lecture 2

Uploaded by

darayir140
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)
20 views

Session 11 Lecture 2

Uploaded by

darayir140
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/ 5

Linear Algebra

Linear algebra is the branch of mathematics that deals with vector spaces.

Vectors
Abstractly, vectors are objects that can be added together (to form new vectors) and that can be multiplied by
scalars (i.e., numbers), also to form new vectors.

For example, if you have the heights, weights, and ages of a large number of people, you can treat your data as
three-dimensional vectors (height, weight, age) . If you’re teaching a class with four exams, you can treat
student grades as four- dimensional vectors (exam1, exam2, exam3, exam4) .

In [1]:

1 # Vector addition
2 def vector_add(v, w):
3
4 print("v = ", v)
5 print("w = ", w)
6
7 return print("v + w = ",[v_i + w_i
8 for v_i, w_i in zip(v, w)])
9
10 l1 = [1, 2, 1, 4, 5]
11 l2 = [6, 5, 7, 4, 9]
12 vector_add(l1,l2)

v = [1, 2, 1, 4, 5]
w = [6, 5, 7, 4, 9]
v + w = [7, 7, 8, 8, 14]
In [3]:

1 # Python code to demonstrate the working of


2 # zip()
3
4 # initializing lists
5 name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ]
6 roll_no = [ 4, 1, 3, 2 ]
7 marks = [ 40, 50, 60, 70 ]
8
9 # using zip() to map values
10 mapped = zip(name, roll_no, marks)
11
12
13 # converting values to print as set
14
15 # printing resultant values
16 print ("The zipped result is : ",end="")
17 print (set(mapped))
18 #print(list(mapped))
19

The zipped result is : {('Manjeet', 4, 40), ('Shambhavi', 3, 60), ('N


ikhil', 1, 50), ('Astha', 2, 70)}

In [2]:

1 def dot(v, w):


2
3 print("v = ", v)
4 print("w = ", w)
5
6 return print("v1 * w2 + v2 *w2 = ",sum(v_i * w_i
7 for v_i, w_i in zip(v, w)))
8
9 l1 = [1, 2]
10 l2 = [6, 5]
11 dot(l1,l2)

v = [1, 2]
w = [6, 5]
v1 * w2 + v2 *w2 = 16

In [4]:

1 def scalar_multiply(c, v):


2 """c is a number, v is a vector"""
3 print("v = ", v)
4 print("c = ",c)
5 return print("c * v = ",[c * v_i for v_i in v])
6
7 scalar_multiply(3,[1,2,3])

v = [1, 2, 3]
c = 3
c * v = [3, 6, 9]

Matrices
A matrix is a two-dimensional collection of numbers. We will represent matrices as list s of list s, with each inner
list having the same size and representing a row of the matrix.

If A is a matrix, then A [i] [j] is the element in the ith row and the jth column. Per mathematical convention, we
will typically use capital letters to represent matrices.
For example:

In [5]:

1 A = [[1, 2, 3], # A has 2 rows and 3 columns


2 [4, 5, 6]]

In [6]:

1 print(A)

[[1, 2, 3], [4, 5, 6]]

In [7]:

1 B = [[1, 2], # B has 3 rows and 2 columns


2 [3, 4],
3 [5, 6]]

In [8]:

1 B

Out[8]:

[[1, 2], [3, 4], [5, 6]]

In [9]:

1 c = []

In [10]:

1 def shape(A):
2 num_rows = len(A)
3 num_cols = len(A[0]) # number of elements in first row
4 return num_rows, num_cols
5
6

In [11]:

1 shape(A)

Out[11]:

(2, 3)
In [ ]:

1 # A basic code for matrix input from user


2
3 R = int(input("Enter the number of rows:"))
4 C = int(input("Enter the number of columns:"))
5
6 # Initialize matrix
7 matrix = []
8 print("Enter the entries row wise:")
9
10 # For user input
11 # A for loop for row entries
12 for i in range(R):
13 a =[]
14 # A for loop for column entries
15 for j in range(C):
16 a.append(int(input()))
17 matrix.append(a)
18
19 # For printing the matrix
20 print()
21 for i in range(R):
22 for j in range(C):
23 print(matrix[i][j], end = " ")
24 print()
25
In [ ]:

1 mat1 = [[10, 13, 44],


2 [11, 2, 3],
3 [5, 3, 1]]
4
5 print("Matrix 1:")
6 print("-"*9)
7 for i in mat1:
8 print(i)
9
10 mat2 = [[7, 16, -6],
11 [9, 20, -4],
12 [-1, 3 , 27]]
13
14 print("="*15)
15 print("Matrix 2:")
16 print("-"*9)
17 for i in mat2:
18 print(i)
19
20 mat3 = [[0,0,0],
21 [0,0,0],
22 [0,0,0]]
23 matrix_length = len(mat1)
24
25 #To Add mat1 and mat2 matrices
26 for i in range(len(mat1)):
27 for k in range(len(mat2)):
28 mat3[i][k] = mat1[i][k] + mat2[i][k]
29
30 print("="*15)
31 print("Matrix 1 * Matrix 2:")
32 print("-"*21)
33 #To Print the matrix
34 for i in mat3:
35 print(i)

In [ ]:

You might also like