#NEWLINE# def _write( self , stream , a , comment = '' , field = None , precision = None , symmetry = None ) : #NEWLINE# #INDENT# if ( isinstance ( a , list ) or isinstance ( a , ndarray ) or isinstance ( a , tuple ) or hasattr ( a , '__array__' ) ) : #NEWLINE# #INDENT# rep = self . FORMAT_ARRAY #NEWLINE# a = asarray ( a ) #NEWLINE# if ( len ( a . shape ) != 2 ) : #NEWLINE# #INDENT# raise ValueError ( 'Expected 2 dimensional array' ) #NEWLINE# #UNINDENT# ( rows , cols ) = a . shape #NEWLINE# if ( field is not None ) : #NEWLINE# #INDENT# if ( field == self . FIELD_INTEGER ) : #NEWLINE# #INDENT# a = a . astype ( 'i' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( field == self . FIELD_REAL ) : #NEWLINE# #INDENT# if ( a . dtype . char not in 'fd' ) : #NEWLINE# #INDENT# a = a . astype ( 'd' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( fmt == self . FIELD_COMPLEX ) : #NEWLINE# #INDENT# if ( a . dtype . char not in 'FD' ) : #NEWLINE# #INDENT# a = a . astype ( 'D' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( not isspmatrix ( a ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'unknown matrix type: %s' % type ( a ) ) ) #NEWLINE# #UNINDENT# rep = 'coordinate' #NEWLINE# ( rows , cols ) = a . shape #NEWLINE# #UNINDENT# typecode = a . dtype . char #NEWLINE# if ( precision is None ) : #NEWLINE# #INDENT# if ( typecode in 'fF' ) : #NEWLINE# #INDENT# precision = 8 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# precision = 16 #NEWLINE# #UNINDENT# #UNINDENT# if ( field is None ) : #NEWLINE# #INDENT# kind = a . dtype . kind #NEWLINE# if ( kind == 'i' ) : #NEWLINE# #INDENT# field = 'integer' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( kind == 'f' ) : #NEWLINE# #INDENT# field = 'real' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( kind == 'c' ) : #NEWLINE# #INDENT# field = 'complex' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( ( 'unexpected dtype kind ' + kind ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( symmetry is None ) : #NEWLINE# #INDENT# symmetry = self . _get_symmetry ( a ) #NEWLINE# #UNINDENT# self . __class__ . _validate_format ( rep ) #NEWLINE# self . __class__ . _validate_field ( field ) #NEWLINE# self . __class__ . _validate_symmetry ( symmetry ) #NEWLINE# stream . write ( asbytes ( '%%MatrixMarket matrix {0} {1} {2}\n' . format ( rep , field , symmetry ) ) ) #NEWLINE# for line in comment . split ( '\n' ) : #NEWLINE# #INDENT# stream . write ( asbytes ( ( '%%%s\n' % line ) ) ) #NEWLINE# #UNINDENT# template = self . _field_template ( field , precision ) #NEWLINE# if ( rep == self . FORMAT_ARRAY ) : #NEWLINE# #INDENT# stream . write ( asbytes ( ( '%i %i\n' % ( rows , cols ) ) ) ) #NEWLINE# if ( field in ( self . FIELD_INTEGER , self . FIELD_REAL ) ) : #NEWLINE# #INDENT# if ( symmetry == self . SYMMETRY_GENERAL ) : #NEWLINE# #INDENT# for j in range ( cols ) : #NEWLINE# #INDENT# for i in range ( rows ) : #NEWLINE# #INDENT# stream . write ( asbytes ( ( template % a [ ( i , j ) ] ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for j in range ( cols ) : #NEWLINE# #INDENT# for i in range ( j , rows ) : #NEWLINE# #INDENT# stream . write ( asbytes ( ( template % a [ ( i , j ) ] ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( field == self . FIELD_COMPLEX ) : #NEWLINE# #INDENT# if ( symmetry == self . SYMMETRY_GENERAL ) : #NEWLINE# #INDENT# for j in range ( cols ) : #NEWLINE# #INDENT# for i in range ( rows ) : #NEWLINE# #INDENT# aij = a [ ( i , j ) ] #NEWLINE# stream . write ( asbytes ( ( template % ( real ( aij ) , imag ( aij ) ) ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for j in range ( cols ) : #NEWLINE# #INDENT# for i in range ( j , rows ) : #NEWLINE# #INDENT# aij = a [ ( i , j ) ] #NEWLINE# stream . write ( asbytes ( ( template % ( real ( aij ) , imag ( aij ) ) ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( field == self . FIELD_PATTERN ) : #NEWLINE# #INDENT# raise ValueError ( 'pattern type inconsisted with dense format' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( ( 'Unknown field type %s' % field ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# coo = a . tocoo ( ) #NEWLINE# if ( symmetry != self . SYMMETRY_GENERAL ) : #NEWLINE# #INDENT# lower_triangle_mask = ( coo . row >= coo . col ) #NEWLINE# coo = coo_matrix ( ( coo . data [ lower_triangle_mask ] , ( coo . row [ lower_triangle_mask ] , coo . col [ lower_triangle_mask ] ) ) , shape = coo . shape ) #NEWLINE# #UNINDENT# stream . write ( asbytes ( ( '%i %i %i\n' % ( rows , cols , coo . nnz ) ) ) ) #NEWLINE# if ( field == self . FIELD_PATTERN ) : #NEWLINE# #INDENT# IJV = vstack ( ( coo . row , coo . col ) ) . T #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( field in [ self . FIELD_INTEGER , self . FIELD_REAL ] ) : #NEWLINE# #INDENT# IJV = vstack ( ( coo . row , coo . col , coo . data ) ) . T #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( field == self . FIELD_COMPLEX ) : #NEWLINE# #INDENT# IJV = vstack ( ( coo . row , coo . col , coo . data . real , coo . data . imag ) ) . T #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( ( 'Unknown field type %s' % field ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# IJV [ : , : 2 ] += 1 #NEWLINE# fmt = ( ( '%i' , '%i' ) + ( ( ( '%%.%dg' % precision ) , ) * ( IJV . shape [ 1 ] - 2 ) ) ) #NEWLINE# savetxt ( stream , IJV , fmt = fmt )