Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Numerical Operations on Vectors and Matrices | Linear Algebra
Mathematics for Data Analysis and Modeling
course content

Course Content

Mathematics for Data Analysis and Modeling

Mathematics for Data Analysis and Modeling

1. Basic Mathematical Concepts and Definitions
2. Linear Algebra
3. Mathematical Analysis

bookNumerical Operations on Vectors and Matrices

Numerical operations on vectors

In Python, numerical operations on vectors can be performed using various libraries such as NumPy. NumPy provides efficient and convenient functions for vectorized operations, making it easy to perform calculations on vectors.

Here are some common numerical operations on vectors, along with examples in Python:

Addition

Adding two vectors together element-wise.

123456
import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) c = a + b print(c)
copy

Subtraction

Subtracting one vector from another element-wise.

123456
import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) c = a - b print(c)
copy

Scalar Multiplication

Multiplying a vector by a scalar value.

123456
import numpy as np a = np.array([1, 2, 3]) b = 2 c = a * b print(c)
copy

Dot Product

Computing the dot product between two vectors.
The dot product of two vectors is a mathematical operation that yields a scalar value. The dot product of two vectors u and v is calculated by taking the sum of the products of their corresponding components:

u · v = u₁ * v₁ + u₂ * v₂ + u₃ * v₃ + ... + uₙ * vₙ

12345678
import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) c = np.dot(a, b) # Dot Product = 1*4 + 2*5 + 3*6 print(c)
copy

Operations on matrices

Now let's consider numerical operations on matrices.

Addition and subtraction

Matrices and vectors of the same shape can be added or subtracted element-wise.

123456789
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) S = A + B D = A - B print(S) print(D)
copy

Scalar Multiplication and Division

Each element of a matrix or vector can be multiplied or divided by a scalar value. Example in Python:

1234567891011
import numpy as np A = np.array([[1, 2], [3, 4]]) scalar = 2 B = scalar * A # Scalar multiplication C = A / scalar # Scalar division print(B) print(C)
copy

Matrix Multiplication

Multiplication of two matrices is a binary operation combining two matrices to produce a new matrix. To compute the result of the multiplication of two matrices, the number of columns in the first matrix must equal the number of rows in the second matrix.
The dot product of matrices is calculated by taking the dot product of corresponding rows from the first matrix and columns from the second matrix.
The resulting matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix. The process of matrix multiplication can be visualized as follows:

Example

12345678910111213141516171819202122232425262728
import numpy as np # Define matrices A and B A = np.array([[1, 2, 3], [4, 5, 6]]) # Matrix A with shape `(2, 3)` B = np.array([[7, 8], [9, 10], [11, 12]]) # Matrix B with shape `(3, 2)` # Perform dot product of matrices A and B C = np.dot(A, B) # Resulting matrix C with shape `(2, 2)` # Print the resulting matrix C print("Resulting matrix C (A dot B):") print(C) # Element-wise calculation of `C[0, 0]` # c11 = 1*7 + 2*9 + 3*11 print("C[0, 0] = 1*7 + 2*9 + 3*11 =", C[0, 0]) # Element-wise calculation of `C[0, 1]` # c12 = 1*8 + 2*10 + 3*12 print("C[0, 1] = 1*8 + 2*10 + 3*12 =", C[0, 1]) # Element-wise calculation of `C[1, 0]` # c21 = 4*7 + 5*9 + 6*11 print("C[1, 0] = 4*7 + 5*9 + 6*11 =", C[1, 0]) # Element-wise calculation of `C[1, 1]` # c22 = 4*8 + 5*10 + 6*12 print("C[1, 1] = 4*8 + 5*10 + 6*12 =", C[1, 1])
copy

Note

Pay attention that matrix multiplication operation is not commutative. In general case, A * B != B * A.

What will be the result of multiplication matrix with shape `(4, 5)` by matrix with shape `(3, 4)`?

What will be the result of multiplication matrix with shape (4, 5) by matrix with shape (3, 4)?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1
some-alt