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

Exercise - Vectors and Matrices

This document introduces some basic linear algebra concepts like vectors and matrices. It shows how to represent vectors and matrices using NumPy and perform common operations like addition, subtraction, scalar multiplication, dot products, and matrix multiplication in Python.

Uploaded by

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

Exercise - Vectors and Matrices

This document introduces some basic linear algebra concepts like vectors and matrices. It shows how to represent vectors and matrices using NumPy and perform common operations like addition, subtraction, scalar multiplication, dot products, and matrix multiplication in Python.

Uploaded by

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

Introduction to Linear Algebra

Vectors

Representing vectors in Python

[]
2

a=
[]
3
4
;

b=
[ ]
2
−1
;

c=
[ ] −3
−3
;

d=
[ ]
−2
3
;

e= 3
5

Import numpy library

In [ ]:

→ →
By using the numpy arrays, represent the vectors a to e

In [ ]:

Print the values of some of the vectors to make sure they match the definition:

In [ ]:

→ →
Use NumPy's built-in function linalg.norm to compute the length of the vectors a and e :

In [ ]:

Vector Operations

Adding, subtracting and multiplying vectors by using code

→ →
Add the vectors a and b :

In [ ]:

Print the result of the addition:

In [ ]:


→ →
Subtract vector b from a :

In [ ]:


Multiply the vector a by some number (scalar):

In [ ]:

Multiplying vectors elementwise

→ →
Multiply vectors a and b elementwise:

In [ ]:

Computing the dot product by using code

→ →
Compute the dot product of a and b by multipling two vectors elementwise and summing the multiples. For
summing, use the NumPy function np.sum :

In [ ]:


Apply the last operaton to compute the length of the vector a instead of using the function linalg.norm :

In [ ]:

Matrices
This is part of a broader convention of orderly arranging numbers in a rectangular, table-like structure called a
matrix. A variable containing a vector is represented with a small letter and an arrow above it. A variable
containing a matrix is conventionally represented with a capital letter. For example, M is a matrix containing 12
numbers arranged in a particular way.

[ ]
1 3 −2 4
M3 × 4 = 2 3 −1 −1 3 rows × 4 columns

−1 1 3 4

Elementary Matrix Operations in Python

Let us define matrices A


,B
and C
as follows:
[ ] [ ]
−1 1
4 2 −1 5
A= 2 1
−2 −3
3
1
−3 ;
4
B=
3
4
−3
−2
; C= [ ]
1
3 −1
3
; D= [ ]
2
−1 2
1

5 5

Use NumPy's data structure called array to represent matrix A:

In [ ]:

Here, we defined the other matrices:

In [26]:
B = np.array([[-1, 1],
[ 3, -3],
[ 4, -2],
[ 5, 5]])

In [27]:
C = np.array([[-1, 3],
[ 3, -1]])

In [28]:
D = np.array([[ 2, 1],
[-1, 2]])

Print the variables containing the matrices to make sure they match the definition:

In [ ]:

Matrix Addition and Subtraction in Python

Add matrices A
and B
and analyse the result:

In [ ]:

Add the matrices C


and D
:

In [ ]:

Add the matrix A


to itself:

In [ ]:
Multiplying a Matrix by a Scalar in Python

Multiply the matrix A


by 5:

In [ ]:

Multiply the matrix A


by -5:

In [ ]:

Matrix Multiplication in Python

Compute the matrix product A × B


, by using the NumPy command np.dot , and analyse the result:

In [ ]:

Compute the matrix product B × A


, and analyse the result:

In [ ]:

Compute the matrix products B × C


and B × D
:

In [ ]:

Computing matrix products C × D


and D × C
, and analyse the result:

In [ ]:

You might also like