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

Notebook Linear Algebra

Linear algebra is the branch of mathematics that deals with vector spaces. Vectors are objects that can be added together and multiplied by scalars to form new vectors. Matrices are two-dimensional collections of numbers represented as lists of lists, with each inner list having the same size and representing a row of the matrix. Basic operations on vectors and matrices like addition, multiplication, and shape are demonstrated through examples.

Uploaded by

darayir140
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Notebook Linear Algebra

Linear algebra is the branch of mathematics that deals with vector spaces. Vectors are objects that can be added together and multiplied by scalars to form new vectors. Matrices are two-dimensional collections of numbers represented as lists of lists, with each inner list having the same size and representing a row of the matrix. Basic operations on vectors and matrices like addition, multiplication, and shape are demonstrated through examples.

Uploaded by

darayir140
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Linear Algebra

May 27, 2021

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

1.1 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) .

[1]: # Vector addition


def vector_add(v, w):

print("v = ", v)
print("w = ", w)

return print("v + w = ",[v_i + w_i


for v_i, w_i in zip(v, w)])

l1 = [1, 2, 1, 4, 5]
l2 = [6, 5, 7, 4, 9]
vector_add(l1,l2)

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

[3]: # Python code to demonstrate the working of


# zip()

# initializing lists
name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ]
roll_no = [ 4, 1, 3, 2 ]
marks = [ 40, 50, 60, 70 ]

# using zip() to map values

1
mapped = zip(name, roll_no, marks)

# converting values to print as set

# printing resultant values


print ("The zipped result is : ",end="")
print (set(mapped))
#print(list(mapped))

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


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

[2]: def dot(v, w):

print("v = ", v)
print("w = ", w)

return print("v1 * w2 + v2 *w2 = ",sum(v_i * w_i


for v_i, w_i in zip(v, w)))

l1 = [1, 2]
l2 = [6, 5]
dot(l1,l2)

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

[4]: def scalar_multiply(c, v):


"""c is a number, v is a vector"""
print("v = ", v)
print("c = ",c)
return print("c * v = ",[c * v_i for v_i in v])

scalar_multiply(3,[1,2,3])

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

1.2 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:

2
[5]: A = [[1, 2, 3], # A has 2 rows and 3 columns
[4, 5, 6]]

[6]: print(A)

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

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


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

[8]: B

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

[9]: c = []

[10]: def shape(A):


num_rows = len(A)
num_cols = len(A[0]) # number of elements in first row
return num_rows, num_cols

[11]: shape(A)

[11]: (2, 3)

[ ]: # A basic code for matrix input from user

R = int(input("Enter the number of rows:"))


C = int(input("Enter the number of columns:"))

# Initialize matrix
matrix = []
print("Enter the entries row wise:")

# For user input


# A for loop for row entries
for i in range(R):
a =[]
# A for loop for column entries
for j in range(C):
a.append(int(input()))
matrix.append(a)

# For printing the matrix


print()
for i in range(R):

3
for j in range(C):
print(matrix[i][j], end = " ")
print()

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


[11, 2, 3],
[5, 3, 1]]

print("Matrix 1:")
print("-"*9)
for i in mat1:
print(i)

mat2 = [[7, 16, -6],


[9, 20, -4],
[-1, 3 , 27]]

print("="*15)
print("Matrix 2:")
print("-"*9)
for i in mat2:
print(i)

mat3 = [[0,0,0],
[0,0,0],
[0,0,0]]
matrix_length = len(mat1)

#To Add mat1 and mat2 matrices


for i in range(len(mat1)):
for k in range(len(mat2)):
mat3[i][k] = mat1[i][k] + mat2[i][k]

print("="*15)
print("Matrix 1 * Matrix 2:")
print("-"*21)
#To Print the matrix
for i in mat3:
print(i)

[ ]:

You might also like