| 1 |
|
| 2 |
|
| 3 |
tex = False |
| 4 |
|
| 5 |
if tex: |
| 6 |
mulsym = ' ' |
| 7 |
else: |
| 8 |
mulsym = ' * ' |
| 9 |
|
| 10 |
class Poly: |
| 11 |
def __init__(self, p0, coeffs): |
| 12 |
self.p0 = p0 |
| 13 |
self.coeffs = coeffs |
| 14 |
def eval(self, x): |
| 15 |
y = x ** self.p0 |
| 16 |
z = 0 |
| 17 |
for c in coeffs: |
| 18 |
z += y * c |
| 19 |
y *= x |
| 20 |
return z |
| 21 |
|
| 22 |
def add(poly0, poly1, nmax): |
| 23 |
lp0 = len(poly0.coeffs) |
| 24 |
lp1 = len(poly1.coeffs) |
| 25 |
p0 = min(poly0.p0, poly1.p0) |
| 26 |
n = min(max(poly0.p0 + lp0, poly1.p1 + lp1), nmax) - p0 |
| 27 |
if n <= 0: return Poly(0, []) |
| 28 |
coeffs = [] |
| 29 |
for i in range(n): |
| 30 |
c = 0 |
| 31 |
if i >= poly0.p0 - p0 and i < lp0 + poly0.p0 - p0: |
| 32 |
c += poly0.coeffs[i + p0 - poly0.p0] |
| 33 |
if i >= poly1.p0 - p0 and i < lp1 + poly1.p0 - p0: |
| 34 |
c += poly1.coeffs[i + p0 - poly1.p0] |
| 35 |
coeffs.append(c) |
| 36 |
return Poly(p0, coeffs) |
| 37 |
|
| 38 |
def pr(str): |
| 39 |
if tex: |
| 40 |
print str, '\\\\' |
| 41 |
else: |
| 42 |
print '\t' + str + ';' |
| 43 |
|
| 44 |
def prd(str): |
| 45 |
if tex: |
| 46 |
print str, '\\\\' |
| 47 |
else: |
| 48 |
print '\tdouble ' + str + ';' |
| 49 |
|
| 50 |
def polymul(p0, p1, degree, basename, suppress_odd = False): |
| 51 |
result = [] |
| 52 |
for i in range(min(degree, len(p0) + len(p1) - 1)): |
| 53 |
terms = [] |
| 54 |
for j in range(i + 1): |
| 55 |
if j < len(p0) and i - j < len(p1): |
| 56 |
t0 = p0[j] |
| 57 |
t1 = p1[i - j] |
| 58 |
if t0 != None and t1 != None: |
| 59 |
terms.append(t0 + mulsym + t1) |
| 60 |
if terms == []: |
| 61 |
result.append(None) |
| 62 |
else: |
| 63 |
var = basename % i |
| 64 |
if (j % 2 == 0) or not suppress_odd: |
| 65 |
prd(var + ' = ' + ' + '.join(terms)) |
| 66 |
result.append(var) |
| 67 |
return result |
| 68 |
|
| 69 |
def polysquare(p0, degree, basename): |
| 70 |
result = [] |
| 71 |
for i in range(min(degree, 2 * len(p0) - 1)): |
| 72 |
terms = [] |
| 73 |
for j in range((i + 1)/ 2): |
| 74 |
if i - j < len(p0): |
| 75 |
t0 = p0[j] |
| 76 |
t1 = p0[i - j] |
| 77 |
if t0 != None and t1 != None: |
| 78 |
terms.append(t0 + mulsym + t1) |
| 79 |
if len(terms) >= 1: |
| 80 |
if tex and len(terms) == 1: |
| 81 |
terms = ['2 ' + terms[0]] |
| 82 |
else: |
| 83 |
terms = ['2' + mulsym + '(' + ' + '.join(terms) + ')'] |
| 84 |
if (i % 2) == 0: |
| 85 |
t = p0[i / 2] |
| 86 |
if t != None: |
| 87 |
if tex: |
| 88 |
terms.append(t + '^2') |
| 89 |
else: |
| 90 |
terms.append(t + mulsym + t) |
| 91 |
if terms == []: |
| 92 |
result.append(None) |
| 93 |
else: |
| 94 |
var = basename % i |
| 95 |
prd(var + ' = ' + ' + '.join(terms)) |
| 96 |
result.append(var) |
| 97 |
return result |
| 98 |
|
| 99 |
def mkspiro(degree): |
| 100 |
if tex: |
| 101 |
us = ['u = 1'] |
| 102 |
vs = ['v ='] |
| 103 |
else: |
| 104 |
us = ['u = 1'] |
| 105 |
vs = ['v = 0'] |
| 106 |
if tex: |
| 107 |
tp = [None, 't_{11}', 't_{12}', 't_{13}', 't_{14}'] |
| 108 |
else: |
| 109 |
tp = [None, 't1_1', 't1_2', 't1_3', 't1_4'] |
| 110 |
if tex: |
| 111 |
prd(tp[1] + ' = k_0') |
| 112 |
prd(tp[2] + ' = \\frac{k_1}{2}') |
| 113 |
prd(tp[3] + ' = \\frac{k_2}{6}') |
| 114 |
prd(tp[4] + ' = \\frac{k_3}{24}') |
| 115 |
else: |
| 116 |
prd(tp[1] + ' = km0') |
| 117 |
prd(tp[2] + ' = .5 * km1') |
| 118 |
prd(tp[3] + ' = (1./6) * km2') |
| 119 |
prd(tp[4] + ' = (1./24) * km3') |
| 120 |
tlast = tp |
| 121 |
coef = 1. |
| 122 |
for i in range(1, degree - 1): |
| 123 |
tmp = [] |
| 124 |
tcoef = coef |
| 125 |
|
| 126 |
for j in range(len(tlast)): |
| 127 |
c = tcoef / (j + 1) |
| 128 |
if (j % 2) == 0 and tlast[j] != None: |
| 129 |
if tex: |
| 130 |
tmp.append('\\frac{%s}{%.0f}' % (tlast[j], 1./c)) |
| 131 |
else: |
| 132 |
if c < 1e-9: |
| 133 |
cstr = '%.16e' % c |
| 134 |
else: |
| 135 |
cstr = '(1./%d)' % int(.5 + (1./c)) |
| 136 |
tmp.append(cstr + ' * ' + tlast[j]) |
| 137 |
tcoef *= .5 |
| 138 |
if tmp != []: |
| 139 |
sign = ('+', '-')[(i / 2) % 2] |
| 140 |
var = ('u', 'v')[i % 2] |
| 141 |
if tex: |
| 142 |
if i == 1: pref = '' |
| 143 |
else: pref = sign + ' ' |
| 144 |
str = pref + (' ' + sign + ' ').join(tmp) |
| 145 |
else: |
| 146 |
str = var + ' ' + sign + '= ' + ' + '.join(tmp) |
| 147 |
if var == 'u': us.append(str) |
| 148 |
else: vs.append(str) |
| 149 |
if i < degree - 1: |
| 150 |
if tex: |
| 151 |
basename = 't_{%d%%d}' % (i + 1) |
| 152 |
else: |
| 153 |
basename = 't%d_%%d' % (i + 1) |
| 154 |
if i == 1: |
| 155 |
tnext = polysquare(tp, degree - 1, basename) |
| 156 |
t2 = tnext |
| 157 |
elif i == 3: |
| 158 |
tnext = polysquare(t2l, degree - 1, basename) |
| 159 |
elif (i % 2) == 0: |
| 160 |
tnext = polymul(tlast, tp, degree - 1, basename, True) |
| 161 |
else: |
| 162 |
tnext = polymul(t2l, t2, degree - 1, basename) |
| 163 |
t2l = tlast |
| 164 |
tlast = tnext |
| 165 |
coef /= (i + 1) |
| 166 |
if tex: |
| 167 |
pr(' '.join(us)) |
| 168 |
pr(' '.join(vs)) |
| 169 |
else: |
| 170 |
for u in us: |
| 171 |
pr(u) |
| 172 |
for v in vs: |
| 173 |
pr(v) |
| 174 |
|
| 175 |
if __name__ == '__main__': |
| 176 |
mkspiro(12) |