Question
This is a python quesion about matrix inversion techniques using cramer's rule an so on: (You just have to fill out the origional code, don't
This is a python quesion about matrix inversion techniques using cramer's rule an so on: (You just have to fill out the origional code, don't have to write it on your own)
1. First, let's implement inversion by Cramer's rule as we derived in class. Fill in the following code in the function invCramers below: (Find the fill in here)
def minor(A, i, j):
"""Return the i,j'th minor of A"""
n = A.shape[0]
# list(range(i)) produces a list [0, 1, ..., i-1] # addition of lists concatenates so rows = [0, 1, ..., i-1, i+1, ..., n] rows = list(range(i)) + list(range(i+1, n)) cols = list(range(j)) + list(range(j+1, n)) # this uses 'fancy indexing'. We will discuss indexing in more detail # in a later computational lecture. subMatrix = A[rows, :][:, cols] return det(subMatrix)
def invCramers(A): """ Invert the square matrix A using Cramer's rule and built in determinant function. Cramer's rule: (A^{-1})_{ij} = (-1)^{i+j} M_{ji} / det(A) where M_{ji} is the ji'th minor. Returns the inverse of A. """ # assume that the matrix is n x n and get n from the shape n = A.shape[0] Ainv = zeros_like(A) #Fill in here ! return Ainv
2. Next let's check this works by inverting the test matrix A (given below) and verifying that the inverse it returns multiplied by A gives back the identity.
A = array([[1, 3,2], [5,11,7], [0, 2,9]], dtype=float)
Test here.
3. Let's implement Gauss-Jordan inversion . Note: don't worry about the swap operation (which is only needed if a diagonal entry is zero at some step). Fill in the following code: (You just need to fill out this origional code, don't have to write it yourself)
def invGaussJordan(A): n = A.shape[0] # make a copy to work on. this will be updated as we do elementary operations B = A.copy() # make a matrix which will eventually become Ainv (the RHS of the equation) Ainv = eye(n) # step through the columns: for i in range(n): # Goal: transform into the unit vector column e_j k = B[i,i] # multiply row i by 1/k in order to make B[i,i] = 1 B[i] = # FILL HERE Ainv[i] = # AND HERE # zero out other rows in column i # by subtracting multiples of row i for j in range(n): if j != i: # zero out row j # FILL HERE return Ainv
4. Check your Gauss-Jordan inversion works on the test matrix A.
5. Let's check how these compare to the numpy inversion function. Compute the inverse of A with 'inv'.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started