Friday 15 June 2012

Unknown Error in Python when solving a matrix using a custom class -



Unknown Error in Python when solving a matrix using a custom class -

i wrote class represent matrix in python, , many different operations can performed on it. part, works, every 1 time in while i'll specific error:

traceback (most recent phone call last): file "c:\users\philip\documents\coding\python\matrix_testing.py", line 30, in <module> b.rref() file "c:\python27\lib\matrix.py", line 193, in rref copy[ ] = self._rowadd( self._rowmultiply( copy[ id ], -copy[ ][ pivot_column ] ), copy[ ] ) typeerror: list indices must integers, not nonetype

this unusual, , i'm unsure how arrive @ nonetype index. inserting print statements nil explain. here's 1 type of matrix reproduces error:

b = [ [ 1, -2, -3, 6, -2 ], [ 0, 0, 1, -1, -3 ], [ 0, 0, 0, 0, -5 ], [ 0, 0, 0, 0, 0 ], ] b = matrix( b ) b.addcolumn( [ 0, 0, 0, 0 ] ) b.show() b.rref() b.show()

and here's matrix class:

class matrixerror(exception): ''' base of operations class handling errors ''' pass class invalidmatrix(matrixerror): ''' handling errors invalid matrix sizes. attributes: msg -- error message m -- row count n -- column count ''' # def __init__( self, msg, m, n ): # self.msg = msg # self.m = m # self.n = n pass class nonmatrixobject( matrixerror ): ''' handling operations on non-matrices. attributes: msg -- error message invalidtype -- type of wrong object ''' ''' def __init__( self, msg, invalidtype ): self.msg = msg self.invalidtype = invalidtype ''' pass class matrix: ''' class used create representation of matrix, on different operations can performed, such matrix addition, subtraction, , multiplication, solutions reduced row matrix, , row matrix forms. ''' def __init__( self, initmatrix = [] ): ''' check if matrix valid size. if valid, sets matrix.''' valid = true if len( initmatrix ) != 0: in range( len( initmatrix ) ): curr = len( initmatrix[ ] ) prev = len( initmatrix[ - 1 ] ) if != 0: if ( curr != prev ) or ( prev == 0 ): valid = false break if valid: self._matrix = initmatrix self._m = len( initmatrix ) if self._m == 0: self._n = 0 else: self._n = len( initmatrix[ 0 ] ) else: try: raise invalidmatrix( 'error: length of rows inconsistent, ', prev, curr ) except invalidmatrix e: print( '%s matrix contains row of length %d , of length %d' % ( e.msg, e.m, e.n ) ) # matrix generation def generatematrix( self, m, n, v ): '''generates m*n matrix values v''' temp = [] in range( m ): temprow = [] i2 in range( n ): temprow.append( v ) temp.append( temprow ) self._matrix = temp def identitymatrix( self, n ): '''generates n identity matrix''' imatrix = [] self._m = n self._n = n in range( n ): row = [] u in range( n ): if u == i: row.append( 1 ) else: row.append( 0 ) imatrix.append( row ) self._matrix = imatrix # methods solving rre form , re form def _rowadd( self, row1, row2 ): '''internal use: adds 1 row another''' i, amount in enumerate( row2 ): row1[ ] += amount homecoming row1 def _rowdivide( self, row, divisor ): '''internal use: divides row value''' row = [ ( float( value ) / divisor ) value in row ] #print( row ) homecoming row def _rowmultiply( self, row, factor ): '''internal use: multiplies row factor''' row = [ ( value * factor ) value in row ] homecoming row def _isinre( self, row = none ): '''internal use: checks if matrix in eow echelon form. broken currently''' if row , ( row in range( len( self._matrix ) ) ): re-create = [ self._matrix[ row ] ] else: re-create = self._matrix verdict = true i, value in enumerate( re-create ): i2, value2 in enumerate( value ): if ( i2 == ) , ( value2 != 1 ): verdict = false elif ( i2 < ) , ( value2 != 0 ): verdict = false homecoming verdict def isinrre( self ): pass def _findpivot( self, row ): '''internal use: finds column id of pivot position in row''' id, value in enumerate( row ): if value != 0: homecoming id homecoming none def _checkblank( self, row ): '''internal use: checks if row blank''' value in row: if value != 0: homecoming false homecoming true # solving def ref( self ): '''converts matrix row echelon form''' if self._n >= self._m: re-create = self._matrix copy.sort() copy.reverse() id in range( len( re-create ) ): pivot_column = self._findpivot( copy[ id ] ) if pivot_column == none: go on pivot = copy[ id ][ pivot_column ] copy[ id ] = self._rowdivide( copy[ id ], pivot ) in range( id + 1, len( re-create ) ): if not self._checkblank( copy[ ] ): addend = self._rowmultiply( copy[ id ], -copy[ ][ pivot_column ] ) copy[ ] = self._rowadd( addend , copy[ ] ) copy.sort() copy.reverse() self._matrix = re-create else: try: raise invalidmatrix( 'error: cannot convert matrix, ', self._m, self._n ) except invalidmatrix e: print( '%s%dx%d invalid size' % ( e.msg, e.m, e.n ) ) def rref( self ): '''converts matrix reduced row echelon form''' if not self._n >= self._m: try: raise invalidmatrix( 'error: cannot convert matrix, ', self._m, self._n ) except invalidmatrix e: print( '%s%dx%d invalid size' % ( e.msg, e.m, e.n ) ) else: self.ref() re-create = self._matrix id in reversed( range( len( re-create ) ) ): pivot_column = self._findpivot( copy[ id ] ) if not pivot_column: pass in reversed( range( id ) ): if not self._checkblank( copy[ ] ): copy[ ] = self._rowadd( self._rowmultiply( copy[ id ], -copy[ ][ pivot_column ] ), copy[ ] ) self._matrix = re-create def solve( self ): '''solves , returns x in formula ax=b.''' re-create = matrix( self._matrix ) if copy._m + 1 == copy._n: copy.rref() answ = [] row in copy._matrix: answ.append( row[ -1 ] ) homecoming answ # methods outside matrix utilize def factormultiply( self, factor ): if type( factor ) int: new = [] row in self._matrix: new.append( self._rowmultiply( row, factor ) ) self._matrix = new def addrow( self, row ): '''adds row matrix''' if len( row ) == self._n: self._matrix.append( row ) else: try: raise invalidmatrix( 'error: row not right size', row, self._n ) except invalidmatrix e: print( '%s (%d).' % ( e.msg, e.n ) ) def addcolumn( self, column ): if len( column ) == self._m: in range( self._m ): self._matrix[ ].append( column[ ] ) def display( self ): '''displays total value of matrix''' row in self._matrix: print( row ) print( '\n' ) def show( self ): '''displays value of matrix rounded 2 decimal places''' row in self._matrix: printed = '[ ' value in row: if value == 0: printed += '0.00 ' else: printed += '%0.2f ' % value print( printed + ' ]' ) def augment( self, other ): new = self._matrix if other._m == self._m: in range( self._m ): new[ ] = self._matrix[ ] + other._matrix[ ] self._matrix = new # overloading operators def __add__( self, other ): try: dupe = self._matrix if ( self._n == other._n ) , ( self._m == other._m ): in range( self._m ): id, amount in enumerate( other._matrix[ ] ): dupe[ ][ id ] += amount homecoming matrix( dupe ) except: raise( nonmatrixobject( 'error: cannot add together matrix object non-matrix type ', type( other ) ) ) def __mul__( self, other ): try: if self._n == other._m: new = [] in range( self._m ): row = [] i2 in range( other._n ): sum = 0 i3 in range( self._n ): factor1 = self._matrix[ ][ i3 ] factor2 = other._matrix[ i3 ][ i2 ] addend = factor1 * factor2 sum += addend row.append( sum ) new.append( row ) homecoming matrix( new ) else: raise invalidmatrix( 'matrix sizes not compatible multiplication' ) except: raise nonmatrixobject( 'cannot multiply matrix object non-matrix type \'%s\'' % type( other ) )

so in rref phone call _findpivot can homecoming none. check if pivot_column none , if pass. in python, pass keyword nothing. error happens when seek access copy[ ][ pivot_column ]. did intend utilize continue skip iteration instead of pass?

python matrix

No comments:

Post a Comment