Skip to content

Commit

Permalink
proj: Add Gauss Jordan
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoZeke committed Oct 30, 2017
1 parent acba078 commit 85633b6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ guiNumKnife is an attempt to ease the burden of undergraduate B.Tech coursework,
- Add Max Iterations for secant
- Hide and enable UI elements per method
- Use Naive Gauss Elimination Again
- Unbreak pivoting for Gauss Jordan
- Show steps for everything
- Plot

Expand Down
9 changes: 9 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ def calcLASGEP(self):
self.outTextLA.append("\nThe vector b is \n" + str(b.reshape((-1, 1))))
self.outTextLA.append("\nThe X vector is \n" + str(classAns.x.reshape((-1,1))))

def calcLASGJ(self):
A = np.matrix(np.loadtxt(StringIO(self.inpTextLA.toPlainText())))
b = np.fromstring(self.inpVecB.text(),sep=' ')
classAns = las.gaussJordan(A,b)
self.outTextLA.append("<b> Gauss Jordan Method </b><br>")
self.outTextLA.append("The matrix A is \n" + str(A))
self.outTextLA.append("\nThe vector b is \n" + str(b.reshape((-1, 1))))
self.outTextLA.append("\nThe X vector is \n" + str(classAns.reshape((-1,1))))

def calcLASGS(self):
A = np.matrix(np.loadtxt(StringIO(self.inpTextLA.toPlainText())))
b = np.fromstring(self.inpVecB.text(),sep=' ')
Expand Down
35 changes: 33 additions & 2 deletions modules/las.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def _elimination(self):

def _backsub(self):
# Back Substitution

self.x = np.zeros(self.n)
for k in range(self.n - 1, -1, -1):
self.x[k] = (self.b[k] - np.dot(self.A[k, k + 1:], self.x[k + 1:])) / self.A[k, k]
Expand Down Expand Up @@ -120,4 +119,36 @@ def gaussSeidel(A, b, itrMax = 1000):

old_x = copy.deepcopy(x)

return x,k
return x,k


def gaussJordan(A,b):
"""
Returns the vector x such that Ax=b.
A is assumed to be an n by n matrix and b an n-element vector.
"""
n,m = A.shape
# should put in some checks of our assumptions, e.g. n == m
C = np.zeros((n,m+1),float)
C[:,0:n],C[:,n] = A, b

for j in range(n):
# First, do partial pivoting.
p = j # the current diagonal element is pivot by default
# look for alternate pivot by searching for largest element in column
for i in range(j+1,n):
if abs(C[i,j]) > abs(C[p,j]): p = i
if abs(C[p,j]) < 1.0e-16:
print("matrix is (likely) singular")
return b
# swap rows to get largest magnitude element on the diagonal (BROKEN)
# C[p,:],C[j,:] = copy(C[j,:]),copy(C[p,:])
# Now, do scaling and elimination.
pivot = C[j,j]
C[j,:] = C[j,:] / pivot
for i in range(n):
if i == j: continue
C[i,:] = C[i,:] - C[i,j]*C[j,:]
I,x = C[:,0:n],C[:,n]
return x
5 changes: 4 additions & 1 deletion testUI.ui
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Input a &lt;span style=&quot; text-decoration: underline;&quot;&gt;single variable&lt;/span&gt; function in terms of &lt;span style=&quot; font-weight:600;&quot;&gt;x&lt;/span&gt;.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="placeholderText">
<string>1000</string>
<string/>
</property>
</widget>
</item>
Expand Down Expand Up @@ -416,6 +416,9 @@
<property name="text">
<string/>
</property>
<property name="placeholderText">
<string>1000</string>
</property>
</widget>
</item>
</layout>
Expand Down

0 comments on commit 85633b6

Please sign in to comment.