Anda di halaman 1dari 7

#Ex03 Numpy Familiarization part 2

#201312555
#Andrade, Ken
#10-02-16
""""
Contains the following:
Class
ken
close
Functions
checker
tanspose_and_det
interchangedRowsandDet
interchangedColumnsandDet
rowMultipliedAndDet
rowsAddedAndDet

""""
import numpy as np
class ken:

def checker(self,A):
""" Checking ig the no. of equations are equal to no. of unknowns
Calls all other functions after checking if the matrix is determined

Args:
A(array): first parameter

Returns:
Coefficient matrix A if determined
Raises close class if the matrix is not determined
"""
if len(A)!=(len(A[0])-1):
try:
raise close ("No. of unknowns and equations are not equal!")
except close:
print "No. of unknowns and equations are not equal!"

else:
self.con=A[:,(len(A)):]
self.var= np.delete(A,-1,1)
self.sol=np.linalg.solve(self.var,self.con)
print "Properties of determinant"
print "Matrix A:\n",self.var
self.tanspose_and_det(self.var)
self.interchangedRowsandDet(0,1,self.var)
self.interchangedColumnsandDet(0,1,self.var)
self.rowMultipliedAndDet(0,2,self.var)
self.rowsAddedAndDet(0,1,0.5,self.var)

def tanspose_and_det(self,A):
"""Transpose the matrix by using numpy built-in-function
and solve for its determinant

Args:
A(array): First parameter

Prints:
An array that is the transpose of the matrix
The determinant of the new matrix

"""
self.matrix=A.transpose()
self.det=np.linalg.det(A)
print "Transposed Matrix A:\n",self.matrix
print"Determinant A:\n",self.det

def interchangedRowsandDet(self,i,k,A):
"""Interchanges i-th and k-th row and solve for its determinant

Args:
i(int): first parameter
k(int): second parameter
A(array): third parameter

Prints:
An array with intercnhaged i-th and k-th row
The determinant of the new matrix

"""
self.new=A
self.new[[k,i],:]=self.new[[i,k],:]
self.det=np.linalg.det(self.new)
print "\nInterchange rows",i+1,"and",k+1
print self.new
print "Determinant A:\n",self.det

def interchangedColumnsandDet(self,col_i,col_k,A):
"""Interchanges i-th and k-th column and solve for its determinant

Args:
col_i(int): first parameter
col_k(int): second parameter
A(array): third parameter

Prints:
An array with intercnhaged i-th and k-th column
The determinant of the new matrix

"""

self.new_col=A
self.new_col[:,[col_k,col_i]]=self.new_col[:,[col_i,col_k]]
self.det=np.linalg.det(self.new)
print "\nInterchange columns",col_i+1,"and",col_k+1
print self.new_col
print "Determinant A:\n",self.det

def rowMultipliedAndDet(self,i,k,A):
"""Multiplies the i-ith row by a scalar k

Args:
i(int): first parameter
k(int): second parameter
A(array): third parameter

Prints:
An array with row i multiplied by k
The determinant of the new matrix

"""
self.array=A
for n in range(len(self.array[0])):
self.array[i][n]=k*self.array[i][n]

self.det=np.linalg.det(self.array)

print "\nRow",i+1,"Multiplied by",k


print self.array
print "Determinant A:\n",self.det

def rowsAddedAndDet(self,i,j,k,A):
"""Multiplies the i-th row by a scalar k and add to j-th row of the matrix

Args:
i(int): first parameter
k(int): second parameter
j(int): third parameter
A(array): fourth parameter

Prints:
An array replacing jth row with row i multiplied by k
added to the origanal elements of row j
The determinant of the new matrix

"""
self.array=A
for n in range(len(self.array[0])):
self.array[j][n]=k*self.array[i][n]+self.array[j][n]
self.det=np.linalg.det(self.array)

print "\nNew Row 2 is Row",i+1,"Multiplied by",k,"Added to row",j+1

print self.array
print "Determinant A:\n",self.det

class close(Exception):
"""
Raises the program to close if the function is not satisfied
Else, the program will continue to run
"""
pass

if __name__=="__main__" :
#Calls class that contains the functions
k=ken()
check=close(Exception)
print "Solving a Linear System"
c=np.loadtxt("C:\Users\kenkenapc\Desktop\ge122.csv", delimiter=',', dtype=float)
k.checker(c)

Anda mungkin juga menyukai