<s> tex = False <EOL> if tex : <EOL> mulsym = '<STR_LIT:U+0020>' <EOL> else : <EOL> mulsym = '<STR_LIT>' <EOL> class Poly : <EOL> def __init__ ( self , p0 , coeffs ) : <EOL> self . p0 = p0 <EOL> self . coeffs = coeffs <EOL> def eval ( self , x ) : <EOL> y = x ** self . p0 <EOL> z = <NUM_LIT:0> <EOL> for c in coeffs : <EOL> z += y * c <EOL> y *= x <EOL> return z <EOL> def add ( poly0 , poly1 , nmax ) : <EOL> lp0 = len ( poly0 . coeffs ) <EOL> lp1 = len ( poly1 . coeffs ) <EOL> p0 = min ( poly0 . p0 , poly1 . p0 ) <EOL> n = min ( max ( poly0 . p0 + lp0 , poly1 . p1 + lp1 ) , nmax ) - p0 <EOL> if n <= <NUM_LIT:0> : return Poly ( <NUM_LIT:0> , [ ] ) <EOL> coeffs = [ ] <EOL> for i in range ( n ) : <EOL> c = <NUM_LIT:0> <EOL> if i >= poly0 . p0 - p0 and i < lp0 + poly0 . p0 - p0 : <EOL> c += poly0 . coeffs [ i + p0 - poly0 . p0 ] <EOL> if i >= poly1 . p0 - p0 and i < lp1 + poly1 . p0 - p0 : <EOL> c += poly1 . coeffs [ i + p0 - poly1 . p0 ] <EOL> coeffs . append ( c ) <EOL> return Poly ( p0 , coeffs ) <EOL> def pr ( str ) : <EOL> if tex : <EOL> print str , '<STR_LIT>' <EOL> else : <EOL> print '<STR_LIT:\t>' + str + '<STR_LIT:;>' <EOL> def prd ( str ) : <EOL> if tex : <EOL> print str , '<STR_LIT>' <EOL> else : <EOL> print '<STR_LIT>' + str + '<STR_LIT:;>' <EOL> def polymul ( p0 , p1 , degree , basename , suppress_odd = False ) : <EOL> result = [ ] <EOL> for i in range ( min ( degree , len ( p0 ) + len ( p1 ) - <NUM_LIT:1> ) ) : <EOL> terms = [ ] <EOL> for j in range ( i + <NUM_LIT:1> ) : <EOL> if j < len ( p0 ) and i - j < len ( p1 ) : <EOL> t0 = p0 [ j ] <EOL> t1 = p1 [ i - j ] <EOL> if t0 != None and t1 != None : <EOL> terms . append ( t0 + mulsym + t1 ) <EOL> if terms == [ ] : <EOL> result . append ( None ) <EOL> else : <EOL> var = basename % i <EOL> if ( j % <NUM_LIT:2> == <NUM_LIT:0> ) or not suppress_odd : <EOL> prd ( var + '<STR_LIT>' + '<STR_LIT>' . join ( terms ) ) <EOL> result . append ( var ) <EOL> return result <EOL> def polysquare ( p0 , degree , basename ) : <EOL> result = [ ] <EOL> for i in range ( min ( degree , <NUM_LIT:2> * len ( p0 ) - <NUM_LIT:1> ) ) : <EOL> terms = [ ] <EOL> for j in range ( ( i + <NUM_LIT:1> ) / <NUM_LIT:2> ) : <EOL> if i - j < len ( p0 ) : <EOL> t0 = p0 [ j ] <EOL> t1 = p0 [ i - j ] <EOL> if t0 != None and t1 != None : <EOL> terms . append ( t0 + mulsym + t1 ) <EOL> if len ( terms ) >= <NUM_LIT:1> : <EOL> if tex and len ( terms ) == <NUM_LIT:1> : <EOL> terms = [ '<STR_LIT>' + terms [ <NUM_LIT:0> ] ] <EOL> else : <EOL> terms = [ '<STR_LIT:2>' + mulsym + '<STR_LIT:(>' + '<STR_LIT>' . join ( terms ) + '<STR_LIT:)>' ] <EOL> if ( i % <NUM_LIT:2> ) == <NUM_LIT:0> : <EOL> t = p0 [ i / <NUM_LIT:2> ] <EOL> if t != None : <EOL> if tex : <EOL> terms . append ( t + '<STR_LIT>' ) <EOL> else : <EOL> terms . append ( t + mulsym + t ) <EOL> if terms == [ ] : <EOL> result . append ( None ) <EOL> else : <EOL> var = basename % i <EOL> prd ( var + '<STR_LIT>' + '<STR_LIT>' . join ( terms ) ) <EOL> result . append ( var ) <EOL> return result <EOL> def mkspiro ( degree ) : <EOL> if tex : <EOL> us = [ '<STR_LIT>' ] <EOL> vs = [ '<STR_LIT>' ] <EOL> else : <EOL> us = [ '<STR_LIT>' ] <EOL> vs = [ '<STR_LIT>' ] <EOL> if tex : <EOL> tp = [ None , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> tp = [ None , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if tex : <EOL> prd ( tp [ <NUM_LIT:1> ] + '<STR_LIT>' ) <EOL> prd ( tp [ <NUM_LIT:2> ] + '<STR_LIT>' ) <EOL> prd ( tp [ <NUM_LIT:3> ] + '<STR_LIT>' ) <EOL> prd ( tp [ <NUM_LIT:4> ] + '<STR_LIT>' ) <EOL> else : <EOL> prd ( tp [ <NUM_LIT:1> ] + '<STR_LIT>' ) <EOL> prd ( tp [ <NUM_LIT:2> ] + '<STR_LIT>' ) <EOL> prd ( tp [ <NUM_LIT:3> ] + '<STR_LIT>' ) <EOL> prd ( tp [ <NUM_LIT:4> ] + '<STR_LIT>' ) <EOL> tlast = tp <EOL> coef = <NUM_LIT:1.> <EOL> for i in range ( <NUM_LIT:1> , degree - <NUM_LIT:1> ) : <EOL> tmp = [ ] <EOL> tcoef = coef <EOL> for j in range ( len ( tlast ) ) : <EOL> c = tcoef / ( j + <NUM_LIT:1> ) <EOL> if ( j % <NUM_LIT:2> ) == <NUM_LIT:0> and tlast [ j ] != None : <EOL> if tex : <EOL> tmp . append ( '<STR_LIT>' % ( tlast [ j ] , <NUM_LIT:1.> / c ) ) <EOL> else : <EOL> if c < <NUM_LIT> : <EOL> cstr = '<STR_LIT>' % c <EOL> else : <EOL> cstr = '<STR_LIT>' % int ( <NUM_LIT> + ( <NUM_LIT:1.> / c ) ) <EOL> tmp . append ( cstr + '<STR_LIT>' + tlast [ j ] ) <EOL> tcoef *= <NUM_LIT> <EOL> if tmp != [ ] : <EOL> sign = ( '<STR_LIT:+>' , '<STR_LIT:->' ) [ ( i / <NUM_LIT:2> ) % <NUM_LIT:2> ] <EOL> var = ( '<STR_LIT:u>' , '<STR_LIT:v>' ) [ i % <NUM_LIT:2> ] <EOL> if tex : <EOL> if i == <NUM_LIT:1> : pref = '<STR_LIT>' <EOL> else : pref = sign + '<STR_LIT:U+0020>' <EOL> str = pref + ( '<STR_LIT:U+0020>' + sign + '<STR_LIT:U+0020>' ) . join ( tmp ) <EOL> else : <EOL> str = var + '<STR_LIT:U+0020>' + sign + '<STR_LIT>' + '<STR_LIT>' . join ( tmp ) <EOL> if var == '<STR_LIT:u>' : us . append ( str ) <EOL> else : vs . append ( str ) <EOL> if i < degree - <NUM_LIT:1> : <EOL> if tex : <EOL> basename = '<STR_LIT>' % ( i + <NUM_LIT:1> ) <EOL> else : <EOL> basename = '<STR_LIT>' % ( i + <NUM_LIT:1> ) <EOL> if i == <NUM_LIT:1> : <EOL> tnext = polysquare ( tp , degree - <NUM_LIT:1> , basename ) <EOL> t2 = tnext <EOL> elif i == <NUM_LIT:3> : <EOL> tnext = polysquare ( t2l , degree - <NUM_LIT:1> , basename ) <EOL> elif ( i % <NUM_LIT:2> ) == <NUM_LIT:0> : <EOL> tnext = polymul ( tlast , tp , degree - <NUM_LIT:1> , basename , True ) <EOL> else : <EOL> tnext = polymul ( t2l , t2 , degree - <NUM_LIT:1> , basename ) <EOL> t2l = tlast <EOL> tlast = tnext <EOL> coef /= ( i + <NUM_LIT:1> ) <EOL> if tex : <EOL> pr ( '<STR_LIT:U+0020>' . join ( us ) ) <EOL> pr ( '<STR_LIT:U+0020>' . join ( vs ) ) <EOL> else : <EOL> for u in us : <EOL> pr ( u ) <EOL> for v in vs : <EOL> pr ( v ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> mkspiro ( <NUM_LIT:12> ) </s>
