In: Computer Science
Write the following Python script:
Write a function called linsolve() that will be useful in categorizing and solving linear algebra problems. The function will have up to three parameters:
• A required 2D array representing the coefficient matrix of a linear algebra equation,
• A required 1D or 2D array representing the right-side constants of a linear algebra equations, and
• An optional parameter used to determine which condition number to use in determining the condition of the system. The default case for this is the integer 2.
The returns for this function will depend on whether there is a unique solution to the system. If there is, the three returns should be, in order:
• A 1D or 2D array representing the solution array of the linear system,
• A float representing the determinant of the coefficient matrix, and
• A float representing the condition number of the coefficient matrix.
If there is no unique solution, the function should return None for the solution array, 0 for the determinant, and -1 for the condition number; the function should also print out “No solution.”
These task could be solved using numpy :
Here is a code snippet in Python for above task attached with screenshot of output
import numpy as np
def linsolve(A,B,condition):
a=np.array(A)#matrix A into array
b=np.array(B)#matrix B into array
det=np.linalg.det(A)#det for storing determinant
if det==0:
print("Infinite Solution")
return "None",0,-1
else:
x=np.linalg.solve(a,b)
return x,det,1
m_list=[[6,1,1],[2,2,2],[2,8,7]]
x,determinant,cond=linsolve(m_list,[1,2,3],2)
print(x)
print(determinant)
print(cond)
OUTPUT'S SCREENSHOT
1.
2.