<s> from . . libmp . backend import xrange <EOL> rowsep = '<STR_LIT:\n>' <EOL> colsep = '<STR_LIT:U+0020>' <EOL> class _matrix ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . __data = { } <EOL> self . _LU = None <EOL> convert = kwargs . get ( '<STR_LIT>' , self . ctx . convert ) <EOL> if not convert : <EOL> convert = lambda x : x <EOL> if isinstance ( args [ <NUM_LIT:0> ] , ( list , tuple ) ) : <EOL> if isinstance ( args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , ( list , tuple ) ) : <EOL> A = args [ <NUM_LIT:0> ] <EOL> self . __rows = len ( A ) <EOL> self . __cols = len ( A [ <NUM_LIT:0> ] ) <EOL> for i , row in enumerate ( A ) : <EOL> for j , a in enumerate ( row ) : <EOL> self [ i , j ] = convert ( a ) <EOL> else : <EOL> v = args [ <NUM_LIT:0> ] <EOL> self . __rows = len ( v ) <EOL> self . __cols = <NUM_LIT:1> <EOL> for i , e in enumerate ( v ) : <EOL> self [ i , <NUM_LIT:0> ] = e <EOL> elif isinstance ( args [ <NUM_LIT:0> ] , int ) : <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> self . __rows = self . __cols = args [ <NUM_LIT:0> ] <EOL> else : <EOL> if not isinstance ( args [ <NUM_LIT:1> ] , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . __rows = args [ <NUM_LIT:0> ] <EOL> self . __cols = args [ <NUM_LIT:1> ] <EOL> elif isinstance ( args [ <NUM_LIT:0> ] , _matrix ) : <EOL> A = args [ <NUM_LIT:0> ] . copy ( ) <EOL> self . __data = A . _matrix__data <EOL> self . __rows = A . _matrix__rows <EOL> self . __cols = A . _matrix__cols <EOL> for i in xrange ( A . __rows ) : <EOL> for j in xrange ( A . __cols ) : <EOL> A [ i , j ] = convert ( A [ i , j ] ) <EOL> elif hasattr ( args [ <NUM_LIT:0> ] , '<STR_LIT>' ) : <EOL> A = self . ctx . matrix ( args [ <NUM_LIT:0> ] . tolist ( ) ) <EOL> self . __data = A . _matrix__data <EOL> self . __rows = A . _matrix__rows <EOL> self . __cols = A . _matrix__cols <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def apply ( self , f ) : <EOL> """<STR_LIT>""" <EOL> new = self . ctx . matrix ( self . __rows , self . __cols ) <EOL> for i in xrange ( self . __rows ) : <EOL> for j in xrange ( self . __cols ) : <EOL> new [ i , j ] = f ( self [ i , j ] ) <EOL> return new <EOL> def __nstr__ ( self , n = None , ** kwargs ) : <EOL> res = [ ] <EOL> maxlen = [ <NUM_LIT:0> ] * self . cols <EOL> for i in range ( self . rows ) : <EOL> res . append ( [ ] ) <EOL> for j in range ( self . cols ) : <EOL> if n : <EOL> string = self . ctx . nstr ( self [ i , j ] , n , ** kwargs ) <EOL> else : <EOL> string = str ( self [ i , j ] ) <EOL> res [ - <NUM_LIT:1> ] . append ( string ) <EOL> maxlen [ j ] = max ( len ( string ) , maxlen [ j ] ) <EOL> for i , row in enumerate ( res ) : <EOL> for j , elem in enumerate ( row ) : <EOL> row [ j ] = elem . rjust ( maxlen [ j ] ) <EOL> res [ i ] = "<STR_LIT:[>" + colsep . join ( row ) + "<STR_LIT:]>" <EOL> return rowsep . join ( res ) <EOL> def __str__ ( self ) : <EOL> return self . __nstr__ ( ) <EOL> def _toliststr ( self , avoid_type = False ) : <EOL> """<STR_LIT>""" <EOL> typ = self . ctx . mpf <EOL> s = '<STR_LIT:[>' <EOL> for i in xrange ( self . __rows ) : <EOL> s += '<STR_LIT:[>' <EOL> for j in xrange ( self . __cols ) : <EOL> if not avoid_type or not isinstance ( self [ i , j ] , typ ) : <EOL> a = repr ( self [ i , j ] ) <EOL> else : <EOL> a = "<STR_LIT:'>" + str ( self [ i , j ] ) + "<STR_LIT:'>" <EOL> s += a + '<STR_LIT:U+002CU+0020>' <EOL> s = s [ : - <NUM_LIT:2> ] <EOL> s += '<STR_LIT>' <EOL> s = s [ : - <NUM_LIT:3> ] <EOL> s += '<STR_LIT:]>' <EOL> return s <EOL> def tolist ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ [ self [ i , j ] for j in range ( self . __cols ) ] for i in range ( self . __rows ) ] <EOL> def __repr__ ( self ) : <EOL> if self . ctx . pretty : <EOL> return self . __str__ ( ) <EOL> s = '<STR_LIT>' <EOL> s += self . _toliststr ( avoid_type = True ) + '<STR_LIT:)>' <EOL> return s <EOL> def __get_element ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> if key in self . __data : <EOL> return self . __data [ key ] <EOL> else : <EOL> return self . ctx . zero <EOL> def __set_element ( self , key , value ) : <EOL> '''<STR_LIT>''' <EOL> if value : <EOL> self . __data [ key ] = value <EOL> elif key in self . __data : <EOL> del self . __data [ key ] <EOL> def __getitem__ ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( key , int ) or isinstance ( key , slice ) : <EOL> if self . __rows == <NUM_LIT:1> : <EOL> key = ( <NUM_LIT:0> , key ) <EOL> elif self . __cols == <NUM_LIT:1> : <EOL> key = ( key , <NUM_LIT:0> ) <EOL> else : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> if isinstance ( key [ <NUM_LIT:0> ] , slice ) or isinstance ( key [ <NUM_LIT:1> ] , slice ) : <EOL> if isinstance ( key [ <NUM_LIT:0> ] , slice ) : <EOL> if ( key [ <NUM_LIT:0> ] . start is None or key [ <NUM_LIT:0> ] . start >= <NUM_LIT:0> ) and ( key [ <NUM_LIT:0> ] . stop is None or key [ <NUM_LIT:0> ] . stop <= self . __rows + <NUM_LIT:1> ) : <EOL> rows = xrange ( * key [ <NUM_LIT:0> ] . indices ( self . __rows ) ) <EOL> else : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> else : <EOL> rows = [ key [ <NUM_LIT:0> ] ] <EOL> if isinstance ( key [ <NUM_LIT:1> ] , slice ) : <EOL> if ( key [ <NUM_LIT:1> ] . start is None or key [ <NUM_LIT:1> ] . start >= <NUM_LIT:0> ) and ( key [ <NUM_LIT:1> ] . stop is None or key [ <NUM_LIT:1> ] . stop <= self . __cols + <NUM_LIT:1> ) : <EOL> columns = xrange ( * key [ <NUM_LIT:1> ] . indices ( self . __cols ) ) <EOL> else : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> else : <EOL> columns = [ key [ <NUM_LIT:1> ] ] <EOL> m = self . ctx . matrix ( len ( rows ) , len ( columns ) ) <EOL> for i , x in enumerate ( rows ) : <EOL> for j , y in enumerate ( columns ) : <EOL> m . __set_element ( ( i , j ) , self . __get_element ( ( x , y ) ) ) <EOL> return m <EOL> else : <EOL> if key [ <NUM_LIT:0> ] >= self . __rows or key [ <NUM_LIT:1> ] >= self . __cols : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> if key in self . __data : <EOL> return self . __data [ key ] <EOL> else : <EOL> return self . ctx . zero <EOL> def __setitem__ ( self , key , value ) : <EOL> if isinstance ( key , int ) or isinstance ( key , slice ) : <EOL> if self . __rows == <NUM_LIT:1> : <EOL> key = ( <NUM_LIT:0> , key ) <EOL> elif self . __cols == <NUM_LIT:1> : <EOL> key = ( key , <NUM_LIT:0> ) <EOL> else : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> if isinstance ( key [ <NUM_LIT:0> ] , slice ) or isinstance ( key [ <NUM_LIT:1> ] , slice ) : <EOL> if isinstance ( key [ <NUM_LIT:0> ] , slice ) : <EOL> if ( key [ <NUM_LIT:0> ] . start is None or key [ <NUM_LIT:0> ] . start >= <NUM_LIT:0> ) and ( key [ <NUM_LIT:0> ] . stop is None or key [ <NUM_LIT:0> ] . stop <= self . __rows + <NUM_LIT:1> ) : <EOL> rows = xrange ( * key [ <NUM_LIT:0> ] . indices ( self . __rows ) ) <EOL> else : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> else : <EOL> rows = [ key [ <NUM_LIT:0> ] ] <EOL> if isinstance ( key [ <NUM_LIT:1> ] , slice ) : <EOL> if ( key [ <NUM_LIT:1> ] . start is None or key [ <NUM_LIT:1> ] . start >= <NUM_LIT:0> ) and ( key [ <NUM_LIT:1> ] . stop is None or key [ <NUM_LIT:1> ] . stop <= self . __cols + <NUM_LIT:1> ) : <EOL> columns = xrange ( * key [ <NUM_LIT:1> ] . indices ( self . __cols ) ) <EOL> else : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> else : <EOL> columns = [ key [ <NUM_LIT:1> ] ] <EOL> if isinstance ( value , self . ctx . matrix ) : <EOL> if len ( rows ) == value . rows and len ( columns ) == value . cols : <EOL> for i , x in enumerate ( rows ) : <EOL> for j , y in enumerate ( columns ) : <EOL> self . __set_element ( ( x , y ) , value . __get_element ( ( i , j ) ) ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> value = self . ctx . convert ( value ) <EOL> for i in rows : <EOL> for j in columns : <EOL> self . __set_element ( ( i , j ) , value ) <EOL> else : <EOL> if key [ <NUM_LIT:0> ] >= self . __rows or key [ <NUM_LIT:1> ] >= self . __cols : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> value = self . ctx . convert ( value ) <EOL> if value : <EOL> self . __data [ key ] = value <EOL> elif key in self . __data : <EOL> del self . __data [ key ] <EOL> if self . _LU : <EOL> self . _LU = None <EOL> return <EOL> def __iter__ ( self ) : <EOL> for i in xrange ( self . __rows ) : <EOL> for j in xrange ( self . __cols ) : <EOL> yield self [ i , j ] <EOL> def __mul__ ( self , other ) : <EOL> if isinstance ( other , self . ctx . matrix ) : <EOL> if self . __cols != other . __rows : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> new = self . ctx . matrix ( self . __rows , other . __cols ) <EOL> for i in xrange ( self . __rows ) : <EOL> for j in xrange ( other . __cols ) : <EOL> new [ i , j ] = self . ctx . fdot ( ( self [ i , k ] , other [ k , j ] ) <EOL> for k in xrange ( other . __rows ) ) <EOL> return new <EOL> else : <EOL> new = self . ctx . matrix ( self . __rows , self . __cols ) <EOL> for i in xrange ( self . __rows ) : <EOL> for j in xrange ( self . __cols ) : <EOL> new [ i , j ] = other * self [ i , j ] <EOL> return new <EOL> def __rmul__ ( self , other ) : <EOL> if isinstance ( other , self . ctx . matrix ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> return self . __mul__ ( other ) <EOL> def __pow__ ( self , other ) : <EOL> if not isinstance ( other , int ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not self . __rows == self . __cols : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> n = other <EOL> if n == <NUM_LIT:0> : <EOL> return self . ctx . eye ( self . __rows ) <EOL> if n < <NUM_LIT:0> : <EOL> n = - n <EOL> neg = True <EOL> else : <EOL> neg = False <EOL> i = n <EOL> y = <NUM_LIT:1> <EOL> z = self . copy ( ) <EOL> while i != <NUM_LIT:0> : <EOL> if i % <NUM_LIT:2> == <NUM_LIT:1> : <EOL> y = y * z <EOL> z = z * z <EOL> i = i // <NUM_LIT:2> <EOL> if neg : <EOL> y = self . ctx . inverse ( y ) <EOL> return y <EOL> def __div__ ( self , other ) : <EOL> assert not isinstance ( other , self . ctx . matrix ) <EOL> new = self . ctx . matrix ( self . __rows , self . __cols ) <EOL> for i in xrange ( self . __rows ) : <EOL> for j in xrange ( self . __cols ) : <EOL> new [ i , j ] = self [ i , j ] / other <EOL> return new <EOL> __truediv__ = __div__ <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , self . ctx . matrix ) : <EOL> if not ( self . __rows == other . __rows and self . __cols == other . __cols ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> new = self . ctx . matrix ( self . __rows , self . __cols ) <EOL> for i in xrange ( self . __rows ) : <EOL> for j in xrange ( self . __cols ) : <EOL> new [ i , j ] = self [ i , j ] + other [ i , j ] <EOL> return new <EOL> else : <EOL> new = self . ctx . matrix ( self . __rows , self . __cols ) <EOL> for i in xrange ( self . __rows ) : <EOL> for j in xrange ( self . __cols ) : <EOL> new [ i , j ] += self [ i , j ] + other <EOL> return new <EOL> def __radd__ ( self , other ) : <EOL> return self . __add__ ( other ) <EOL> def __sub__ ( self , other ) : <EOL> if isinstance ( other , self . ctx . matrix ) and not ( self . __rows == other . __rows <EOL> and self . __cols == other . __cols ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return self . __add__ ( other * ( - <NUM_LIT:1> ) ) <EOL> def __neg__ ( self ) : <EOL> return ( - <NUM_LIT:1> ) * self <EOL> def __rsub__ ( self , other ) : <EOL> return - self + other <EOL> def __eq__ ( self , other ) : <EOL> return self . __rows == other . __rows and self . __cols == other . __cols and self . __data == other . __data <EOL> def __len__ ( self ) : <EOL> if self . rows == <NUM_LIT:1> : <EOL> return self . cols <EOL> elif self . cols == <NUM_LIT:1> : <EOL> return self . rows <EOL> else : <EOL> return self . rows <EOL> def __getrows ( self ) : <EOL> return self . __rows <EOL> def __setrows ( self , value ) : <EOL> for key in self . __data . copy ( ) : <EOL> if key [ <NUM_LIT:0> ] >= value : <EOL> del self . __data [ key ] <EOL> self . __rows = value <EOL> rows = property ( __getrows , __setrows , doc = '<STR_LIT>' ) <EOL> def __getcols ( self ) : <EOL> return self . __cols <EOL> def __setcols ( self , value ) : <EOL> for key in self . __data . copy ( ) : <EOL> if key [ <NUM_LIT:1> ] >= value : <EOL> del self . __data [ key ] <EOL> self . __cols = value <EOL> cols = property ( __getcols , __setcols , doc = '<STR_LIT>' ) <EOL> def transpose ( self ) : <EOL> new = self . ctx . matrix ( self . __cols , self . __rows ) <EOL> for i in xrange ( self . __rows ) : <EOL> for j in xrange ( self . __cols ) : <EOL> new [ j , i ] = self [ i , j ] <EOL> return new <EOL> T = property ( transpose ) <EOL> def conjugate ( self ) : <EOL> return self . apply ( self . ctx . conj ) <EOL> def transpose_conj ( self ) : <EOL> return self . conjugate ( ) . transpose ( ) <EOL> H = property ( transpose_conj ) <EOL> def copy ( self ) : <EOL> new = self . ctx . matrix ( self . __rows , self . __cols ) <EOL> new . __data = self . __data . copy ( ) <EOL> return new <EOL> __copy__ = copy <EOL> def column ( self , n ) : <EOL> m = self . ctx . matrix ( self . rows , <NUM_LIT:1> ) <EOL> for i in range ( self . rows ) : <EOL> m [ i ] = self [ i , n ] <EOL> return m <EOL> class MatrixMethods ( object ) : <EOL> def __init__ ( ctx ) : <EOL> ctx . matrix = type ( '<STR_LIT>' , ( _matrix , ) , { } ) <EOL> ctx . matrix . ctx = ctx <EOL> ctx . matrix . convert = ctx . convert <EOL> def eye ( ctx , n , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> A = ctx . matrix ( n , ** kwargs ) <EOL> for i in xrange ( n ) : <EOL> A [ i , i ] = <NUM_LIT:1> <EOL> return A <EOL> def diag ( ctx , diagonal , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> A = ctx . matrix ( len ( diagonal ) , ** kwargs ) <EOL> for i in xrange ( len ( diagonal ) ) : <EOL> A [ i , i ] = diagonal [ i ] <EOL> return A <EOL> def zeros ( ctx , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> m = n = args [ <NUM_LIT:0> ] <EOL> elif len ( args ) == <NUM_LIT:2> : <EOL> m = args [ <NUM_LIT:0> ] <EOL> n = args [ <NUM_LIT:1> ] <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % len ( args ) ) <EOL> A = ctx . matrix ( m , n , ** kwargs ) <EOL> for i in xrange ( m ) : <EOL> for j in xrange ( n ) : <EOL> A [ i , j ] = <NUM_LIT:0> <EOL> return A <EOL> def ones ( ctx , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> m = n = args [ <NUM_LIT:0> ] <EOL> elif len ( args ) == <NUM_LIT:2> : <EOL> m = args [ <NUM_LIT:0> ] <EOL> n = args [ <NUM_LIT:1> ] <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % len ( args ) ) <EOL> A = ctx . matrix ( m , n , ** kwargs ) <EOL> for i in xrange ( m ) : <EOL> for j in xrange ( n ) : <EOL> A [ i , j ] = <NUM_LIT:1> <EOL> return A <EOL> def hilbert ( ctx , m , n = None ) : <EOL> """<STR_LIT>""" <EOL> if n is None : <EOL> n = m <EOL> A = ctx . matrix ( m , n ) <EOL> for i in xrange ( m ) : <EOL> for j in xrange ( n ) : <EOL> A [ i , j ] = ctx . one / ( i + j + <NUM_LIT:1> ) <EOL> return A <EOL> def randmatrix ( ctx , m , n = None , min = <NUM_LIT:0> , max = <NUM_LIT:1> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not n : <EOL> n = m <EOL> A = ctx . matrix ( m , n , ** kwargs ) <EOL> for i in xrange ( m ) : <EOL> for j in xrange ( n ) : <EOL> A [ i , j ] = ctx . rand ( ) * ( max - min ) + min <EOL> return A <EOL> def swap_row ( ctx , A , i , j ) : <EOL> """<STR_LIT>""" <EOL> if i == j : <EOL> return <EOL> if isinstance ( A , ctx . matrix ) : <EOL> for k in xrange ( A . cols ) : <EOL> A [ i , k ] , A [ j , k ] = A [ j , k ] , A [ i , k ] <EOL> elif isinstance ( A , list ) : <EOL> A [ i ] , A [ j ] = A [ j ] , A [ i ] <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def extend ( ctx , A , b ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( A , ctx . matrix ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if A . rows != len ( b ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> A = A . copy ( ) <EOL> A . cols += <NUM_LIT:1> <EOL> for i in xrange ( A . rows ) : <EOL> A [ i , A . cols - <NUM_LIT:1> ] = b [ i ] <EOL> return A <EOL> def norm ( ctx , x , p = <NUM_LIT:2> ) : <EOL> r"""<STR_LIT>""" <EOL> try : <EOL> iter ( x ) <EOL> except TypeError : <EOL> return ctx . absmax ( x ) <EOL> if type ( p ) is not int : <EOL> p = ctx . convert ( p ) <EOL> if p == ctx . inf : <EOL> return max ( ctx . absmax ( i ) for i in x ) <EOL> elif p == <NUM_LIT:1> : <EOL> return ctx . fsum ( x , absolute = <NUM_LIT:1> ) <EOL> elif p == <NUM_LIT:2> : <EOL> return ctx . sqrt ( ctx . fsum ( x , absolute = <NUM_LIT:1> , squared = <NUM_LIT:1> ) ) <EOL> elif p > <NUM_LIT:1> : <EOL> return ctx . nthroot ( ctx . fsum ( abs ( i ) ** p for i in x ) , p ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def mnorm ( ctx , A , p = <NUM_LIT:1> ) : <EOL> r"""<STR_LIT>""" <EOL> A = ctx . matrix ( A ) <EOL> if type ( p ) is not int : <EOL> if type ( p ) is str and '<STR_LIT>' . startswith ( p . lower ( ) ) : <EOL> return ctx . norm ( A , <NUM_LIT:2> ) <EOL> p = ctx . convert ( p ) <EOL> m , n = A . rows , A . cols <EOL> if p == <NUM_LIT:1> : <EOL> return max ( ctx . fsum ( ( A [ i , j ] for i in xrange ( m ) ) , absolute = <NUM_LIT:1> ) for j in xrange ( n ) ) <EOL> elif p == ctx . inf : <EOL> return max ( ctx . fsum ( ( A [ i , j ] for j in xrange ( n ) ) , absolute = <NUM_LIT:1> ) for i in xrange ( m ) ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
