Given a matrix and a vector , use NumPy to calculate the projection and the solution of the least squares problem.
NumPy is a Python package for handling arrays. You may use Colab or Sage Cell to run Python code.
First, we have to import the package so that Python knows that you are going to use functions in the package.
Here np
is a shorthand for numpy
, which can be any name, but we follow the convention from the NumPy community.
Next, we have to construct an array (or a matrix).
Here A
is the name of your matrix, which can be a different name. In Python, a structure like [1, 2, 3]
is called a list
. Inside np.array(...)
, we see the structure of [ [...], ... [...] ]
, which is a list of lists. This is because a matrix can be viewed as a list of rows, and each row can be viewed as a list.
Finally, we introduce some functions.
A + B
A.dot(B)
A.T
np.linalg.inv(A)
Run the SageMath code below or simply click here.
This note can be found at Course website > Learning resources.