In this assignment we want to solve the linear equation:
where
With the same matrix
where
Once we have the factorization of
In this assignment, we use the object-oriented programming pattern. Given the input matrix in the constructor, we decompose it and store the factorization and permutation into class members. The class can have a method solve to solve different equations of
We use the following class skeleton to implement:
import numpy as np
class LUSolver:
def __init__(self, input_matrix: np.ndarray):
"""
Constructor of the class. It takes the input matrix and decompose it into LU factorization.
Store the factorization and permutation into class members.
"""
pass
def solve(self, b: np.ndarray) -> np.ndarray:
"""
Solve the linear equation with the input matrix and the given vector b.
"""
passTo implement the class, you need two scipy functions:
scipy.linalg.lu_factor
and
scipy.linalg.lu_solve.
Have a look at their documentations about how to use them.
You should check the following conditions in the constructor. If a condition is not satisfied, you should raise an error with a meaningful message.
input_matrixshould be an instance ofnp.ndarray.input_matrixshould be a square matrix.input_matrixshould have thedtypeofnp.float64.- The size of
input_matrixshould be at least1. input_matrixshould not contain anyinf.input_matrixshould not contain anynan.input_matrixshould not be singular.
You should check the following conditions in the solve method. If a condition is not satisfied, you should raise an error with a meaningful message.
bshould be an instance ofnp.ndarray.bshould be a one-dimensional vector.bshould have thedtypeofnp.float64.- The size of
bshould be the same as the size of the input matrix. bshould not contain anyinf.bshould not contain anynan.
Same as Assignment 2, you should test the class with happy flow and all error conditions. In the happy flow, try to test with manually created input in the test script, and pre-defined input in a json file.