Page 1 of 1

# Seeking help with a relatively simple program (Beginner).

Posted 22 January 2015 - 02:27 PM

Hello </dream.in.code> forum! I am a Python and coding beginner. I am working on a program that takes in a matrix A and performs Gaussian elimination on it until it is reduced to only numbers above the diagonal, then back-substitutes to find the value of the last entry in the solutions vector and uses that to solve the rest of the matrix. I am trying to add pivoting capabilities to it.
The gaussian elimination and back-substitution codes were running fine, but I can't get it to work all together yet. If anybody has useful advice, tips or tricks, I would be so grateful.
Note: This is not an assignment for school. I'm just trying to learn coding. It's my first coding language and I don't really know a ton about computers. I didn't write this code either, I found it in a book that was written by my department head. The pivoting code block was taken from another book. I'm going through this as an exercise. Trying to learn!!! Thanks in advance.
Ok, here it is:

```import numpy
def bobcatpivotLU(A,f):
n = f.size
#Begin by check for compatible matrix and rhs sizes
#A can only be a square matrix...?
#Note: == and != do opposite things.
if (A.shape != n or A.shape != n):
print "Error! Incompatible array and vector sizes."
return f
#Loop through the columns of the matrix.
#Eliminate lower triangular part.
for i in range(0,n-1):
#Loop through the rows below the diagonal for each column.
for j in range(i+1,n):
if A[i,i] == 0:
#PIVOTING CODE BLOCK STARTS HERE:
#Used to simply give a message saying "Zero on the diagonal! Must pivot!"
def pivot(A): # Basic row pivoting
m = len(A); n = len(A)
for j in range(min(n,m)):  # for each column on the main diag
if(A[j][j]==0): # Find a non-zero pivot and swap rows
thecolumn = [A[k][j] for k in range(j,m)]
ipivot = thecolumn.index(max(thecolumn))
temp = A[j]; A[j] = A[ipivot]; A[ipivot] = temp
for i in range(j+1,m):
c = A[i][j]/A[j][j]   # Ratio of (i,j) elt by (j,j) (diagonal) elt
A[i] = [A[i][k]-c*A[j][k] for k in range(n)]
return A
#END PIVOTING PORTION OF CODE, THAT I TRIED UNSUCCESSFULLY TO INSERT INTO MY WORKING CODE.
return f
m = A[j,i]/A[i,i]
A[j,:] = A[j,:] - m*A[i,:]
f[j] = f[j] - m*f[i]
return A,f

def bobcatpivotBS(A,f):
n = f.size
#Begin by check for compatible matrix and rhs sizes
if (A.shape != n or A.shape != n):
print "Error! Incompatible array and vector sizes."
return f
#initialize the solution vector, x, to zero.
x = numpy.zeros((n,1))
#Solve for last entry first.
x[n-1] = f[n-1]/A[n-1,n-1]
#loop from the end to the beginning
for i in range(n-2,-1,-1):
sum = 0
# for all known x values, sum and move to rhs
for j in range(i+1,n):
sum = sum + A[i,j]*x[j]
x[i] = (f[i] - sum)/A[i,i]
return x

```

Please go easy on me I'm a beginner.
I have a very beginner-ey question too actually: What does the return function do? When it says "return A" or "return f" what does it do?
THANK YOU!!!

This post has been edited by andrewsw: 22 January 2015 - 04:54 PM
Reason for edit:: Removed the word Python from topic title

Is This A Good Question/Topic? 0

## Replies To: Seeking help with a relatively simple program (Beginner).

### #2 andrewsw Reputation: 6834
• Posts: 28,366
• Joined: 12-December 12

## Re: Seeking help with a relatively simple program (Beginner).

Posted 22 January 2015 - 04:51 PM

To be honest there seems a large discrepancy between not understanding what return does and attempting to modify this fairly detailed code. (You say the code is relatively simple, but if you consider it so then I would expect you to have covered the return statement at an earlier point.)

Do you understand the code? What have you attempted?

The return statement leaves the current function, optionally returning a value (or None if no expression list is supplied).

This post has been edited by andrewsw: 22 January 2015 - 04:53 PM

### #3 jon.kiparsky Reputation: 11836
• Posts: 20,061
• Joined: 19-March 11

## Re: Seeking help with a relatively simple program (Beginner).

Posted 22 January 2015 - 05:21 PM

Perhaps if you start with a more specific question, it'll be a bit easier to help you. What are you having trouble with right now? That'll help us get you moving forward. Without that, we don't even know where to start.

## Re: Seeking help with a relatively simple program (Beginner).

Posted 22 January 2015 - 05:24 PM

I'm a beginning coder who also doesn't know much about computers.
I do understand what the code does and what parts of the code are doing it. I know linear algebra pretty well and I work with calculations a lot.

I'm sorry if I'm asking a totally beginner-level question here. I hope I'm not in the wrong place. Maybe I should start somewhere simpler but it seems crackable to me and I thought I might learn a lot especially with the indexing of i and j - the things I feel most fuzzy about.

I also can see where the syntax of the inserted code differs from my code (the A[j][j] vs. A[j,j] thing). I have fixed that and tried running it but it didn't work.

I have also tried renaming the variables m and n, p and q in the inserted code.

I'm sorry if my question is kind of dumb. I am trying to learn. I feel like if I figure out this code it will help me learn a lot, especially the indexing and the conditional statements parts.
Thanks.

Ok, no worries. I'll be back with specific (better) questions when I am back on my computer (which has Python on it) and I can work on this. Sorry for being vague.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }