1
#ADD STRING MATRIX AND NUM MATRIX Fraction(3).limit_denominator(10)from fractions import Fraction
#ONLY WORKS FOR SQUARE ONES RIGHT NOW!
from fractions import Fraction

def make1(nm,x):
    if nm[x][x]!=1:
        print("Divide R1 by ",Fraction(nm[x][x]).limit_denominator(10))
        tempr = multiply(nm[x],1/nm[x][x])
        nm[x] = tempr
    return nm

def convert(n):
    try:
        return float(n)
    except ValueError:
        num, denom = n.split('/')
        return float(num) / float(denom)

def convertm(m):
    lm = len(m)
    lx = len(m[0])
    tempn = [0]*lx
    temps = [[]]*lm
    print(temps)
    cnt = 0
    for x in m:
        tempn = x
        for n in x:
            temps[cnt].append(str(Fraction(n).limit_denominator(10)))
            print(n)
        cnt+=1
        print(temps)

def mprint(matrix):
    s = [[str(e) for e in row] for row in matrix]
    lens = [max(map(len, col)) for col in zip(*s)]
    fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
    table = [fmt.format(*row) for row in s]
    print('\n'.join(table))

def subtract(r1,r2): #r1-r2
    tempr = [0]*len(r1)
    for x in range (0,len(r1)):
        tempr[x] = r1[x]-r2[x]
    return tempr

def multiply(r1,n):
    tempr = [0]*len(r1)
    for x in range (0,len(r1)):
        tempr[x] = r1[x]*n
    return tempr

def ans(nm):
    end = len(nm[0])
    cnt = 0
    for x in nm:
        cnt+=1
        print("X",cnt,"=",x[end-1])

equ = int(input("How many equasions are in the linear system? "))
#unk = int(input("How many unkowns are in the linear system? "))
nm = [0] * equ
sm = [0] * equ
for x in range (0,equ):
    tempinput = input("Please enter line "+str(x+1)+" of the matrix: ")
    templist = [convert(n) for n in tempinput.split()]
    nm[x] = templist
    sm[x] = tempinput.split()

mprint(nm)

nm = make1(nm,0)

mprint(nm)

for p in range (0,equ-1):

    for x in range (p,equ-1):
        print("Subtract ",Fraction(nm[x+1][p]).limit_denominator(10),"*",p+1,"by",p+2)
        tempr = multiply(nm[p],nm[x+1][p])
        nm[x+1] = subtract(tempr,nm[x+1])
        print("FIRST X: ",x,"P",x)                
        mprint(nm)
        nm = make1(nm,p+1)
        mprint(nm)

#GOIN BACK
for p in range (0,equ-1):
    for x in range (0,equ-(p+1)):
        print("Subtract ",x,"by",Fraction(nm[x][2-p]).limit_denominator(10),"*",3)
        tempr = multiply(nm[2-p],nm[x][2-p])
        nm[x]= subtract(nm[x],tempr)
        print("SECOND X: ",x,"P",x)

    mprint(nm)

ans(nm)

##or x in range (0,equ):
  #  print()

#g = nm[1][0]-1
#print("")
#tempr = multiply(nm[0],g/nm[0][0])
#nm[0]=tempr
#tempr = subtract(nm[1],nm[0])
#nm[0] = tempr

Pastebin of my code

Ok so where my actual problem is in the unimplemented (because I couldn't get it working) def convertm. What this is supposed to do is take the matrix with numbers (nm) and take every value and convert it into a string as fractions (x/x) if needed and store it in the matrix of strings (sm). Here is that segment of code I am referencing...

def convertm(m):
    lm = len(m)
    lx = len(m[0])
    tempn = [0]*lx
    temps = [[]]*lm
    print(temps)
    cnt = 0
    for x in m:
        tempn = x
        for n in x:
            temps[cnt].append(str(Fraction(n).limit_denominator(10)))
            print(n)
        cnt+=1
        print(temps)

I added some prints in order to try and test what the heck was going on during it. I am getting an output of just the last row being repeated through all rows. I think I don't have a return statement currently just because I have been trying get this to work. Ok so for an example if an array is imported that is...

[ [1,2,3],

[4,5,6],

[7,8,9] ]

It will output (set temps to)

[ ['7','8','9'],

['7','8','9'],

['7','8','9'] ]

I want it to output (set temps to)

[ ['1','2','3'],

['4','5','6'],

['7','8','9'] ]

Also I am using Python 3.3.1

(probably should upgrade to 3.3.3 but that is not what we are discussing!) I have absolutely no idea why it is doing this and any little bit of help would very appreciated!

THANK YOU

I also apologize if this formatting is horrible I am new to this and I copy/pasted this from another forum I am very desperate to know what is going on here.

2
  • 1
    I would remove the huge block of code at the top. I completely ignored it to answer the question. It will turn most users off from attempting to answer the question. Commented Feb 5, 2014 at 2:50
  • Generally, you'll get the best results when asking a question if you strip your code down to the bare minimum necessary to run it and produce the error. Commented Feb 5, 2014 at 2:55

1 Answer 1

1

The line

temps = [[]]*lm

makes a list of list, where each sublist points to the same list in memory. So, if you modify one, you modify them all. This is why you are seeing the behavior you are seeing.

Change it to

temps = [[] for _ in range(lm)] # xrange on python2

to get different sublists.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked thank you so much for the help! This solved a lot of frustration I have been going through!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.