<s> import numpy as np <EOL> import sympy <EOL> from sympy import Matrix , integrate , simplify , trigsimp , solve , collect <EOL> def mintegrate ( m , var , l1 , l2 , mname , sufix , norm = False , do_simplify = False , <EOL> conds = '<STR_LIT:none>' ) : <EOL> print ( '<STR_LIT>' . format ( <EOL> mname = mname , var = var ) ) <EOL> m = Matrix ( m ) <EOL> sympy . var ( '<STR_LIT>' ) <EOL> if norm : <EOL> subs = { var : ( l2 - l1 ) * AAA + l1 } <EOL> m = m . subs ( subs ) <EOL> m *= ( l2 - l1 ) <EOL> for ( i , j ) , ki in np . ndenumerate ( m ) : <EOL> tmp = '<STR_LIT>' . format ( mname = mname , <EOL> sufix = sufix , i = i , j = j , var = var ) <EOL> try : <EOL> if norm : <EOL> ki = integrate ( ki , ( AAA , <NUM_LIT:0> , <NUM_LIT:1> ) , conds = conds ) <EOL> else : <EOL> ki = integrate ( ki , ( var , l1 , l2 ) , conds = conds ) <EOL> print ( '<STR_LIT>' . format ( tmp = tmp ) ) <EOL> except : <EOL> print ( '<STR_LIT>' . format ( tmp = tmp ) ) <EOL> m [ i , j ] = ki <EOL> for ( i , j ) , ki in np . ndenumerate ( m ) : <EOL> tmp = '<STR_LIT>' . format ( mname = mname , <EOL> sufix = sufix , i = i , j = j , var = var ) <EOL> try : <EOL> if do_simplify : <EOL> ki = simplify ( ki ) <EOL> else : <EOL> ki = trigsimp ( ki ) <EOL> except : <EOL> print ( '<STR_LIT>' . format ( tmp = tmp ) ) <EOL> ki = simplify ( ki ) <EOL> print ( '<STR_LIT>' . format ( tmp = tmp ) ) <EOL> m [ i , j ] = ki <EOL> filename = '<STR_LIT>' . format ( mname = mname , <EOL> sufix = sufix , var = var ) <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> def myprint ( sth ) : <EOL> lines . append ( str ( sth ) . strip ( ) + '<STR_LIT:\n>' ) <EOL> myprint ( '<STR_LIT>' + mname + '<STR_LIT>' + filename ) <EOL> for ( i , j ) , v in np . ndenumerate ( m ) : <EOL> if v : <EOL> myprint ( mname + '<STR_LIT>' . format ( i , j , v ) ) <EOL> return m <EOL> def dbl_mintegrate ( m , a , a1 , a2 , b , b1 , b2 , mname , sufix , <EOL> norm1 = False , norm2 = False , do_simplify = False , conds = '<STR_LIT:none>' ) : <EOL> m = mintegrate ( m , a , a1 , a2 , mname , sufix , norm = norm1 , <EOL> do_simplify = do_simplify , conds = conds ) <EOL> m = mintegrate ( m , b , b1 , b2 , mname , sufix , norm = norm2 , <EOL> do_simplify = do_simplify , conds = conds ) <EOL> return m <EOL> def mprint_mathematica ( m , mname , sufix ) : <EOL> from mathematica_printer import print_mathematica <EOL> filename = '<STR_LIT>' . format ( mname , sufix ) <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( print_mathematica ( m ) + '<STR_LIT:\n>' ) <EOL> def mprint ( m , mname , sufix = '<STR_LIT>' , header = None ) : <EOL> if sufix : <EOL> filename = '<STR_LIT>' . format ( mname = mname , sufix = sufix ) <EOL> else : <EOL> filename = '<STR_LIT>' . format ( mname = mname ) <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> if header : <EOL> f . write ( header ) <EOL> def myprint ( sth ) : <EOL> f . write ( str ( sth ) . strip ( ) + '<STR_LIT:\n>' ) <EOL> if sufix : <EOL> myprint ( '<STR_LIT>' . format ( mname = mname , sufix = sufix ) ) <EOL> else : <EOL> myprint ( '<STR_LIT>' . format ( mname = mname ) ) <EOL> for ( i , j ) , v in np . ndenumerate ( m ) : <EOL> if v : <EOL> myprint ( '<STR_LIT>' . format ( <EOL> mname = mname , v = v , i = i , j = j ) ) <EOL> def mprint_as_sparse ( m , mname , sufix , numeric = False , use_cse = False , <EOL> header = None , print_file = True , collect_for = None ) : <EOL> if use_cse : <EOL> subs , m_list = sympy . cse ( m ) <EOL> for i , v in enumerate ( m_list ) : <EOL> m [ i ] = v <EOL> filename = '<STR_LIT>' . format ( mname = mname , sufix = sufix ) <EOL> ls = [ ] <EOL> if header : <EOL> ls . append ( header ) <EOL> if use_cse : <EOL> ls . append ( '<STR_LIT>' ) <EOL> num = <NUM_LIT:10> <EOL> for i , sub in enumerate ( subs [ : : num ] ) : <EOL> ls . append ( '<STR_LIT>' + '<STR_LIT:U+002CU+0020>' . join ( <EOL> map ( str , [ j [ <NUM_LIT:0> ] for j in subs [ num * i : num * ( i + <NUM_LIT:1> ) ] ] ) ) ) <EOL> ls . append ( '<STR_LIT>' ) <EOL> for sub in subs : <EOL> ls . append ( '<STR_LIT>' . format ( * sub ) ) <EOL> if not numeric : <EOL> ls . append ( '<STR_LIT>' . format ( mname = mname , sufix = sufix ) ) <EOL> num = len ( [ i for i in list ( m ) if i ] ) <EOL> ls . append ( '<STR_LIT>' . format ( <EOL> mname = mname , sufix = sufix , num = num ) ) <EOL> for ( i , j ) , v in np . ndenumerate ( m ) : <EOL> if v : <EOL> ls . append ( '<STR_LIT>' ) <EOL> ls . append ( '<STR_LIT>' . format ( mname = mname , i = i ) ) <EOL> ls . append ( '<STR_LIT>' . format ( mname = mname , j = j ) ) <EOL> if collect_for != None : <EOL> v = collect ( v , collect_for , evaluate = False ) <EOL> ls . append ( '<STR_LIT>' . format ( mname = mname ) ) <EOL> for k , expr in v . items ( ) : <EOL> ls . append ( '<STR_LIT>' . format ( k = k ) ) <EOL> ls . append ( '<STR_LIT>' . format ( expr = k * expr ) ) <EOL> else : <EOL> ls . append ( '<STR_LIT>' . format ( mname = mname , v = v ) ) <EOL> else : <EOL> ls . append ( '<STR_LIT>' . format ( mname = mname , sufix = sufix ) ) <EOL> num = len ( [ i for i in list ( m ) if i ] ) <EOL> ls . append ( '<STR_LIT>' . format ( <EOL> mname = mname , sufix = sufix , num = num ) ) <EOL> ls . append ( '<STR_LIT:#>' ) <EOL> ls . append ( '<STR_LIT>' ) <EOL> ls . append ( '<STR_LIT:#>' ) <EOL> for ( i , j ) , v in np . ndenumerate ( m ) : <EOL> if v : <EOL> ls . append ( '<STR_LIT>' ) <EOL> ls . append ( '<STR_LIT>' . format ( mname = mname , v = v ) ) <EOL> ls . append ( '<STR_LIT:#>' ) <EOL> ls . append ( '<STR_LIT>' ) <EOL> ls . append ( '<STR_LIT:#>' ) <EOL> for ( i , j ) , v in np . ndenumerate ( m ) : <EOL> if v : <EOL> ls . append ( '<STR_LIT>' ) <EOL> ls . append ( '<STR_LIT>' ) <EOL> ls . append ( '<STR_LIT>' . format ( i = i ) ) <EOL> ls . append ( '<STR_LIT>' . format ( j = j ) ) <EOL> ls . append ( '<STR_LIT>' ) <EOL> string = '<STR_LIT:\n>' . join ( ls ) <EOL> if print_file : <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( sting ) <EOL> return string <EOL> def old_vdiff ( x , vector ) : <EOL> x = np . array ( x ) <EOL> shape = x . shape <EOL> ans = [ np . array ( [ e . diff ( vi ) for e in x . ravel ( ) ] ) for vi in vector ] <EOL> ans = [ a . reshape ( shape ) for a in ans ] <EOL> return np . array ( ans ) . swapaxes ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> def vdiff ( x , vector ) : <EOL> x = np . array ( x ) <EOL> shape = x . shape <EOL> ans = [ ] <EOL> for vi in vector : <EOL> if vi . is_Symbol : <EOL> tmp = [ ] <EOL> for e in x . ravel ( ) : <EOL> tmp . append ( e . diff ( vi ) ) <EOL> else : <EOL> subs = { } <EOL> new_var = sympy . var ( '<STR_LIT>' ) <EOL> for s in vi . free_symbols : <EOL> subs [ s ] = solve ( new_var - vi , s ) [ <NUM_LIT:0> ] <EOL> tmp = [ ] <EOL> for e in x . ravel ( ) : <EOL> e = e . subs ( subs ) <EOL> e = e . diff ( new_var ) <EOL> e = e . subs ( { new_var : vi } ) <EOL> tmp . append ( e . diff ( new_var ) ) <EOL> ans . append ( np . array ( tmp ) ) <EOL> ans = [ a . reshape ( shape ) for a in ans ] <EOL> return np . array ( ans ) . swapaxes ( <NUM_LIT:0> , <NUM_LIT:1> ) </s>
