<s> from __future__ import absolute_import , print_function , division <EOL> import numpy <EOL> try : <EOL> import scipy . sparse <EOL> imported_scipy = True <EOL> except ImportError : <EOL> imported_scipy = False <EOL> import theano <EOL> from theano import gof <EOL> from six import string_types <EOL> def _is_sparse ( x ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( x , ( scipy . sparse . spmatrix , numpy . ndarray , tuple , list ) ) : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , x ) <EOL> return isinstance ( x , scipy . sparse . spmatrix ) <EOL> class SparseType ( gof . Type ) : <EOL> """<STR_LIT>""" <EOL> if imported_scipy : <EOL> format_cls = { '<STR_LIT>' : scipy . sparse . csr_matrix , <EOL> '<STR_LIT>' : scipy . sparse . csc_matrix , <EOL> '<STR_LIT>' : scipy . sparse . bsr_matrix } <EOL> dtype_set = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> ndim = <NUM_LIT:2> <EOL> Variable = None <EOL> Constant = None <EOL> def __init__ ( self , format , dtype ) : <EOL> if not imported_scipy : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> dtype = str ( dtype ) <EOL> if dtype in self . dtype_set : <EOL> self . dtype = dtype <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' % <EOL> dtype , list ( self . dtype_set ) ) <EOL> assert isinstance ( format , string_types ) <EOL> if format in self . format_cls : <EOL> self . format = format <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' % <EOL> format , list ( self . format_cls . keys ( ) ) ) <EOL> def filter ( self , value , strict = False , allow_downcast = None ) : <EOL> if isinstance ( value , self . format_cls [ self . format ] ) and value . dtype == self . dtype : <EOL> return value <EOL> if strict : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( value , value . dtype , self . dtype ) ) <EOL> if allow_downcast : <EOL> sp = self . format_cls [ self . format ] ( value , dtype = self . dtype ) <EOL> else : <EOL> sp = self . format_cls [ self . format ] ( value ) <EOL> if str ( sp . dtype ) != self . dtype : <EOL> raise NotImplementedError ( "<STR_LIT>" % <EOL> ( self . dtype , str ( sp . dtype ) ) ) <EOL> if sp . format != self . format : <EOL> raise NotImplementedError ( ) <EOL> return sp <EOL> @ staticmethod <EOL> def may_share_memory ( a , b ) : <EOL> if _is_sparse ( a ) and _is_sparse ( b ) : <EOL> return ( SparseType . may_share_memory ( a , b . data ) or <EOL> SparseType . may_share_memory ( a , b . indices ) or <EOL> SparseType . may_share_memory ( a , b . indptr ) ) <EOL> if _is_sparse ( b ) and isinstance ( a , numpy . ndarray ) : <EOL> a , b = b , a <EOL> if _is_sparse ( a ) and isinstance ( b , numpy . ndarray ) : <EOL> if ( numpy . may_share_memory ( a . data , b ) or <EOL> numpy . may_share_memory ( a . indices , b ) or <EOL> numpy . may_share_memory ( a . indptr , b ) ) : <EOL> return True <EOL> return False <EOL> def make_variable ( self , name = None ) : <EOL> return self . Variable ( self , name = name ) <EOL> def __eq__ ( self , other ) : <EOL> return ( type ( self ) == type ( other ) and other . dtype == self . dtype and <EOL> other . format == self . format ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . dtype ) ^ hash ( self . format ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( str ( self . dtype ) , str ( self . format ) ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( str ( self . dtype ) , str ( self . format ) ) <EOL> def values_eq_approx ( self , a , b , eps = <NUM_LIT> ) : <EOL> if not scipy . sparse . issparse ( a ) or not scipy . sparse . issparse ( b ) : <EOL> return False <EOL> diff = abs ( a - b ) <EOL> if diff . nnz == <NUM_LIT:0> : <EOL> return True <EOL> return max ( diff . data ) < eps <EOL> def values_eq ( self , a , b ) : <EOL> return scipy . sparse . issparse ( a ) and scipy . sparse . issparse ( b ) and abs ( a - b ) . sum ( ) == <NUM_LIT:0.0> <EOL> def is_valid_value ( self , a ) : <EOL> return scipy . sparse . issparse ( a ) and ( a . format == self . format ) <EOL> def get_shape_info ( self , obj ) : <EOL> obj = self . filter ( obj ) <EOL> assert obj . indices . dtype == '<STR_LIT>' <EOL> assert obj . indptr . dtype == '<STR_LIT>' <EOL> return ( obj . shape , obj . data . size , <EOL> obj . indices . size , obj . indptr . size , obj . nnz ) <EOL> def get_size ( self , shape_info ) : <EOL> return ( shape_info [ <NUM_LIT:1> ] * numpy . dtype ( self . dtype ) . itemsize + <EOL> ( shape_info [ <NUM_LIT:2> ] + shape_info [ <NUM_LIT:3> ] ) * numpy . dtype ( '<STR_LIT>' ) . itemsize ) <EOL> theano . compile . register_view_op_c_code ( <EOL> SparseType , <EOL> """<STR_LIT>""" , <EOL> <NUM_LIT:1> ) </s>
