Basic NumPy

Problem

Given a matrix

A and a vector
b
, use NumPy to calculate the projection
A(AA)1Ab
and the solution
(AA)1Ab
of the least squares problem.

Thought

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.

import numpy as np

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

A = np.array([ [-1, -1], [1, 0], [0, 1] ])

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.

  1. matrix addision: A + B
  2. matrix multiplication: A.dot(B)
  3. transpose: A.T
  4. inverse: np.linalg.inv(A)

Sample answer

Run the SageMath code below or simply click here.

import numpy as np A = np.array([ [-1, -1], [1, 0], [0, 1] ]) b = np.array([1,0,0]) ATAinv = np.linalg.inv(A.T.dot(A)) print("projection") print(A.dot(ATAinv).dot(A.T).dot(b)) print("least square problem") print(ATAinv.dot(A.T).dot(b))

This note can be found at Course website > Learning resources.