| 1 |
from math import * |
| 2 |
import array |
| 3 |
import sys |
| 4 |
import random |
| 5 |
|
| 6 |
import numarray as N |
| 7 |
import numarray.linear_algebra as la |
| 8 |
|
| 9 |
def eps_prologue(x0, y0, x1, y1, draw_box = False): |
| 10 |
print '%!PS-Adobe-3.0 EPSF' |
| 11 |
print '%%BoundingBox:', x0, y0, x1, y1 |
| 12 |
print '%%EndComments' |
| 13 |
print '%%EndProlog' |
| 14 |
print '%%Page: 1 1' |
| 15 |
if draw_box: |
| 16 |
print x0, y0, 'moveto', x0, y1, 'lineto', x1, y1, 'lineto', x1, y0, 'lineto closepath stroke' |
| 17 |
|
| 18 |
def eps_trailer(): |
| 19 |
print '%%EOF' |
| 20 |
|
| 21 |
|
| 22 |
def rk4(y, dydx, x, h, derivs): |
| 23 |
hh = h * .5 |
| 24 |
h6 = h * (1./6) |
| 25 |
xh = x + hh |
| 26 |
yt = [] |
| 27 |
for i in range(len(y)): |
| 28 |
yt.append(y[i] + hh * dydx[i]) |
| 29 |
dyt = derivs(xh, yt) |
| 30 |
for i in range(len(y)): |
| 31 |
yt[i] = y[i] + hh * dyt[i] |
| 32 |
dym = derivs(xh, yt) |
| 33 |
for i in range(len(y)): |
| 34 |
yt[i] = y[i] + h * dym[i] |
| 35 |
dym[i] += dyt[i] |
| 36 |
dyt = derivs(x + h, yt) |
| 37 |
for i in range(len(y)): |
| 38 |
y[i] += h6 * (dydx[i] + dyt[i] + 2 * dym[i]) |
| 39 |
|
| 40 |
def run_elastica_half(sp, k0, lam1, lam2, n): |
| 41 |
def mec_derivs(x, ys): |
| 42 |
th, k = ys[2], ys[3] |
| 43 |
dx, dy = cos(th), sin(th) |
| 44 |
return [dx, dy, k, lam1 * dx + lam2 * dy, k * k] |
| 45 |
ys = [0, 0, 0, k0, 0] |
| 46 |
xyk = [(ys[0], ys[1], ys[3])] |
| 47 |
n = max(1, int(sp * n)) |
| 48 |
h = float(sp) / n |
| 49 |
s = 0 |
| 50 |
for i in range(n): |
| 51 |
dydx = mec_derivs(s, ys) |
| 52 |
rk4(ys, dydx, s, h, mec_derivs) |
| 53 |
xyk.append((ys[0], ys[1], ys[3])) |
| 54 |
return xyk, ys[2], ys[4] |
| 55 |
|
| 56 |
def run_elastica(sm, sp, k0, lam1, lam2, n = 100): |
| 57 |
xykm, thm, costm = run_elastica_half(-sm, k0, -lam1, lam2, n) |
| 58 |
xykp, thp, costp = run_elastica_half(sp, k0, lam1, lam2, n) |
| 59 |
xyk = [] |
| 60 |
for i in range(1, len(xykm)): |
| 61 |
x, y, k = xykm[i] |
| 62 |
xyk.append((-x, y, k)) |
| 63 |
xyk.reverse() |
| 64 |
xyk.extend(xykp) |
| 65 |
x = xyk[-1][0] - xyk[0][0] |
| 66 |
y = xyk[-1][1] - xyk[0][1] |
| 67 |
th = thm + thp |
| 68 |
sth, cth = sin(thm), cos(thm) |
| 69 |
x, y = x * cth - y * sth, x * sth + y * cth |
| 70 |
result = [] |
| 71 |
maxk = 0 |
| 72 |
for xt, yt, kt in xyk: |
| 73 |
maxk = max(maxk, kt) |
| 74 |
result.append((xt, yt, kt)) |
| 75 |
cost = costm + costp |
| 76 |
return result, cost, x, y, th |
| 77 |
|
| 78 |
def run_mec_cos_pos(k, lam1, lam2, n = 1000): |
| 79 |
cost = 0 |
| 80 |
th = 0 |
| 81 |
x = 0 |
| 82 |
y = 0 |
| 83 |
dt = 1.0 / n |
| 84 |
result = [(0, 0)] |
| 85 |
for i in range(n): |
| 86 |
k1 = lam1 * cos(th) + lam2 * sin(th) |
| 87 |
|
| 88 |
cost += dt * k * k |
| 89 |
x += dt * cos(th) |
| 90 |
y += dt * sin(th) |
| 91 |
th += dt * k |
| 92 |
|
| 93 |
k += dt * k1 |
| 94 |
result.append((x, y)) |
| 95 |
return result, cost, x, y, th |
| 96 |
|
| 97 |
def run_mec_cos(k, lam1, lam2, n = 1000): |
| 98 |
resp, costp, xp, yp, thp = run_mec_cos_pos(k*.5, lam1*.25, lam2*.25, n) |
| 99 |
resm, costm, xm, ym, thm = run_mec_cos_pos(k*.5, lam1*-.25, lam2*.25, n) |
| 100 |
cost = (costp + costm) * .5 |
| 101 |
x, y = xp + xm, yp - ym |
| 102 |
th = thp + thm |
| 103 |
sth, cth = .5 * sin(thm), .5 * cos(thm) |
| 104 |
x, y = x * cth - y * sth, x * sth + y * cth |
| 105 |
result = [] |
| 106 |
for i in range(1, n): |
| 107 |
xt, yt = resm[n - i - 1] |
| 108 |
result.append((-xt * cth - yt * sth, -xt * sth + yt * cth)) |
| 109 |
for i in range(n): |
| 110 |
xt, yt = resp[i] |
| 111 |
result.append((xt * cth - yt * sth, xt * sth + yt * cth)) |
| 112 |
return result, cost, x, y, th |
| 113 |
|
| 114 |
def cross_prod(a, b): |
| 115 |
return [a[1] * b[2] - a[2] * b[1], |
| 116 |
a[2] * b[0] - a[0] * b[2], |
| 117 |
a[0] * b[1] - a[1] * b[0]] |
| 118 |
|
| 119 |
def solve_mec(constraint_fnl): |
| 120 |
delta = 1e-3 |
| 121 |
params = [pi, 0, 0] |
| 122 |
for i in range(20): |
| 123 |
k, lam1, lam2 = params |
| 124 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2) |
| 125 |
|
| 126 |
|
| 127 |
c1c, c2c, costc = constraint_fnl(cost, x, y, th) |
| 128 |
print '% constraint_fnl =', c1c, c2c, 'cost =', costc |
| 129 |
|
| 130 |
dc1s = [] |
| 131 |
dc2s = [] |
| 132 |
for j in range(len(params)): |
| 133 |
params1 = N.array(params) |
| 134 |
params1[j] += delta |
| 135 |
k, lam1, lam2 = params1 |
| 136 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2) |
| 137 |
c1p, c2p, costp = constraint_fnl(cost, x, y, th) |
| 138 |
params1 = N.array(params) |
| 139 |
params1[j] -= delta |
| 140 |
k, lam1, lam2 = params1 |
| 141 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2) |
| 142 |
c1m, c2m, costm = constraint_fnl(cost, x, y, th) |
| 143 |
dc1s.append((c1p - c1m) / (2 * delta)) |
| 144 |
dc2s.append((c2p - c2m) / (2 * delta)) |
| 145 |
xp = cross_prod(dc1s, dc2s) |
| 146 |
xp = N.divide(xp, sqrt(N.dot(xp, xp))) |
| 147 |
|
| 148 |
print '% dc1s =', dc1s |
| 149 |
print '% dc2s =', dc2s |
| 150 |
print '% xp =', xp |
| 151 |
|
| 152 |
|
| 153 |
params1 = N.array(params) |
| 154 |
for j in range(len(params)): |
| 155 |
params1[j] += delta * xp[j] |
| 156 |
k, lam1, lam2 = params1 |
| 157 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2) |
| 158 |
c1p, c2p, costp = constraint_fnl(cost, x, y, th) |
| 159 |
print '% constraint_fnl+ =', c1p, c2p, 'cost =', costp |
| 160 |
params1 = N.array(params) |
| 161 |
for j in range(len(params)): |
| 162 |
params1[j] -= delta * xp[j] |
| 163 |
k, lam1, lam2 = params1 |
| 164 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2) |
| 165 |
c1m, c2m, costm = constraint_fnl(cost, x, y, th) |
| 166 |
print '% constraint_fnl- =', c1m, c2m, 'cost =', costm |
| 167 |
d2cost = (costp + costm - 2 * costc) / (delta * delta) |
| 168 |
dcost = (costp - costm) / (2 * delta) |
| 169 |
|
| 170 |
print '% dcost =', dcost, 'd2cost =', d2cost |
| 171 |
if d2cost < 0: d2cost = .1 |
| 172 |
|
| 173 |
jm = N.array([dc1s, dc2s, [x * d2cost for x in xp]]) |
| 174 |
|
| 175 |
ji = la.inverse(jm) |
| 176 |
|
| 177 |
|
| 178 |
dp = N.dot(ji, [c1c, c2c, dcost]) |
| 179 |
print '% dp =', dp |
| 180 |
print '% [right]=', [c1c, c2c, dcost] |
| 181 |
params -= dp * .1 |
| 182 |
print '%', params |
| 183 |
sys.stdout.flush() |
| 184 |
return params |
| 185 |
|
| 186 |
def solve_mec_3constr(constraint_fnl, n = 30, initparams = None): |
| 187 |
delta = 1e-3 |
| 188 |
if initparams: |
| 189 |
params = N.array(initparams) |
| 190 |
else: |
| 191 |
params = [3.14, 0, 0] |
| 192 |
for i in range(n): |
| 193 |
k, lam1, lam2 = params |
| 194 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2) |
| 195 |
c1c, c2c, c3c = constraint_fnl(cost, x, y, th) |
| 196 |
print '% constraint_fnl =', c1c, c2c, c3c |
| 197 |
|
| 198 |
dc1s = [] |
| 199 |
dc2s = [] |
| 200 |
dc3s = [] |
| 201 |
for j in range(len(params)): |
| 202 |
params1 = N.array(params) |
| 203 |
params1[j] += delta |
| 204 |
k, lam1, lam2 = params1 |
| 205 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2) |
| 206 |
c1p, c2p, c3p = constraint_fnl(cost, x, y, th) |
| 207 |
params1 = N.array(params) |
| 208 |
params1[j] -= delta |
| 209 |
k, lam1, lam2 = params1 |
| 210 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2) |
| 211 |
c1m, c2m, c3m = constraint_fnl(cost, x, y, th) |
| 212 |
dc1s.append((c1p - c1m) / (2 * delta)) |
| 213 |
dc2s.append((c2p - c2m) / (2 * delta)) |
| 214 |
dc3s.append((c3p - c3m) / (2 * delta)) |
| 215 |
|
| 216 |
|
| 217 |
jm = N.array([dc1s, dc2s, dc3s]) |
| 218 |
|
| 219 |
ji = la.inverse(jm) |
| 220 |
|
| 221 |
dp = N.dot(ji, [c1c, c2c, c3c]) |
| 222 |
if i < n/2: scale = .25 |
| 223 |
else: scale = 1 |
| 224 |
params -= scale * dp |
| 225 |
print '%', params |
| 226 |
return params |
| 227 |
|
| 228 |
def mk_ths_fnl(th0, th1): |
| 229 |
def fnl(cost, x, y, th): |
| 230 |
actual_th0 = atan2(y, x) |
| 231 |
actual_th1 = th - actual_th0 |
| 232 |
print '% x =', x, 'y =', y, 'hypot =', hypot(x, y) |
| 233 |
return th0 - actual_th0, th1 - actual_th1, cost |
| 234 |
return fnl |
| 235 |
|
| 236 |
def mk_y_fnl(th0, th1, ytarg): |
| 237 |
def fnl(cost, x, y, th): |
| 238 |
actual_th0 = atan2(y, x) |
| 239 |
actual_th1 = th - actual_th0 |
| 240 |
return th0 - actual_th0, th1 - actual_th1, ytarg - hypot(x, y) |
| 241 |
return fnl |
| 242 |
|
| 243 |
def solve_mec_nested_inner(th0, th1, y, fnl): |
| 244 |
innerfnl = mk_y_fnl(th0, th1, y) |
| 245 |
params = [th0 + th1, 1e-6, 1e-6] |
| 246 |
params = solve_mec_3constr(innerfnl, 10, params) |
| 247 |
k, lam1, lam2 = params |
| 248 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2, 100) |
| 249 |
c1, c2, c3 = fnl(cost, x, y, th) |
| 250 |
return c3, params |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
def solve_mec_nested(th0, th1, fnl): |
| 255 |
R = .61803399 |
| 256 |
C = 1 - R |
| 257 |
ax, cx = .6, .85 |
| 258 |
bx = .5 * (ax + cx) |
| 259 |
|
| 260 |
x0, x3 = ax, cx |
| 261 |
if abs(cx - bx) > abs(bx - ax): |
| 262 |
x1, x2 = bx, bx + C * (cx - bx) |
| 263 |
else: |
| 264 |
x1, x2 = bx - C * (bx - ax), bx |
| 265 |
f1, p = solve_mec_nested_inner(th0, th1, x1, fnl) |
| 266 |
f2, p = solve_mec_nested_inner(th0, th1, x2, fnl) |
| 267 |
for i in range(10): |
| 268 |
print '%', x0, x1, x2, x3, ':', f1, f2 |
| 269 |
if f2 < f1: |
| 270 |
x0, x1, x2 = x1, x2, R * x2 + C * x3 |
| 271 |
f1 = f2 |
| 272 |
f2, p = solve_mec_nested_inner(th0, th1, x2, fnl) |
| 273 |
else: |
| 274 |
x1, x2, x3 = R * x1 + C * x0, x1, x2 |
| 275 |
f2 = f1 |
| 276 |
f1, p = solve_mec_nested_inner(th0, th1, x1, fnl) |
| 277 |
if f1 < f2: |
| 278 |
x = x1 |
| 279 |
else: |
| 280 |
x = x2 |
| 281 |
cc, p = solve_mec_nested_inner(th0, th1, x, fnl) |
| 282 |
return p |
| 283 |
|
| 284 |
def plot(xys): |
| 285 |
cmd = 'moveto' |
| 286 |
for xy in xys: |
| 287 |
print 306 + 200 * xy[0], 396 - 200 * xy[1], cmd |
| 288 |
cmd = 'lineto' |
| 289 |
print 'stroke' |
| 290 |
|
| 291 |
def mec_test(): |
| 292 |
th0, th1 = 0, pi / 4 |
| 293 |
fnl = mk_ths_fnl(th0, th1) |
| 294 |
params = solve_mec_nested(th0, th1, fnl) |
| 295 |
k, lam1, lam2 = params |
| 296 |
xys, cost, x, y, th = run_mec_cos(k, lam1, lam2, 1000) |
| 297 |
plot(xys) |
| 298 |
print '% run_mec_cos:', cost, x, y, th |
| 299 |
print '1 0 0 setrgbcolor' |
| 300 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2) |
| 301 |
print '%', xys |
| 302 |
plot(xys) |
| 303 |
print '% run_elastica:', cost, x, y, th |
| 304 |
print 'showpage' |
| 305 |
print '%', params |
| 306 |
|
| 307 |
def lenfig(): |
| 308 |
print '306 720 translate' |
| 309 |
th0, th1 = pi / 2, pi / 2 |
| 310 |
for i in range(1, 10): |
| 311 |
y = .1 * i + .003 |
| 312 |
fnl = mk_y_fnl(th0, th1, y) |
| 313 |
params = solve_mec_3constr(fnl) |
| 314 |
k, lam1, lam2 = params |
| 315 |
print 'gsave 0.5 dup scale -306 -396 translate' |
| 316 |
xys, cost, x, y, th = run_elastica(-2, 2, k, lam1, lam2, 100) |
| 317 |
print 'gsave [2] 0 setdash' |
| 318 |
plot(xys) |
| 319 |
print 'grestore' |
| 320 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2, 100) |
| 321 |
plot(xys) |
| 322 |
print 'grestore' |
| 323 |
print '% y =', y, 'params =', params |
| 324 |
if lam2 < 0: |
| 325 |
mymaxk = k |
| 326 |
else: |
| 327 |
mymaxk = sqrt(k * k + 4 * lam2) |
| 328 |
lam = abs(lam2) / (mymaxk * mymaxk) |
| 329 |
print '-200 0 moveto /Symbol 12 selectfont (l) show' |
| 330 |
print '/Times-Roman 12 selectfont ( = %.4g) show' % lam |
| 331 |
print '0 -70 translate' |
| 332 |
print 'showpage' |
| 333 |
|
| 334 |
def lenplot(figno, th0, th1): |
| 335 |
result = [] |
| 336 |
if figno in (1, 2): |
| 337 |
imax = 181 |
| 338 |
elif figno == 3: |
| 339 |
imax = 191 |
| 340 |
for i in range(1, imax): |
| 341 |
yy = .005 * i |
| 342 |
if figno in (1, 2) and i == 127: |
| 343 |
yy = 2 / pi |
| 344 |
if figno == 3 and i == 165: |
| 345 |
yy = .8270 |
| 346 |
fnl = mk_y_fnl(th0, th1, yy) |
| 347 |
params = solve_mec_3constr(fnl) |
| 348 |
k, lam1, lam2 = params |
| 349 |
xys, cost, x0, y0, th = run_elastica(-.5, .5, k, lam1, lam2, 100) |
| 350 |
if lam2 < 0: |
| 351 |
mymaxk = k |
| 352 |
else: |
| 353 |
mymaxk = sqrt(k * k + 4 * lam2) |
| 354 |
lam = abs(lam2) / (mymaxk * mymaxk) |
| 355 |
result.append((yy, lam, cost)) |
| 356 |
return result |
| 357 |
|
| 358 |
def lengraph(figno): |
| 359 |
if figno in (1, 2): |
| 360 |
eps_prologue(15, 47, 585, 543) |
| 361 |
th0, th1 = pi / 2, pi / 2 |
| 362 |
yscale = 900 |
| 363 |
ytic = .05 |
| 364 |
ymax = 10 |
| 365 |
elif figno == 3: |
| 366 |
eps_prologue(15, 47, 585, 592) |
| 367 |
th0, th1 = pi / 3, pi / 3 |
| 368 |
yscale = 500 |
| 369 |
ytic = .1 |
| 370 |
ymax = 10 |
| 371 |
x0 = 42 |
| 372 |
xscale = 7.5 * 72 |
| 373 |
y0 = 72 |
| 374 |
print '/Times-Roman 12 selectfont' |
| 375 |
print '/ss 1.5 def' |
| 376 |
print '/circle { ss 0 moveto currentpoint exch ss sub exch ss 0 360 arc } bind def' |
| 377 |
print '.5 setlinewidth' |
| 378 |
print x0, y0, 'moveto', xscale, '0 rlineto 0', yscale * ytic * ymax, 'rlineto', -xscale, '0 rlineto closepath stroke' |
| 379 |
for i in range(0, 11): |
| 380 |
print x0 + .1 * i * xscale, y0, 'moveto 0 6 rlineto stroke' |
| 381 |
print x0 + .1 * i * xscale, y0 + ytic * ymax * yscale, 'moveto 0 -6 rlineto stroke' |
| 382 |
print x0 + .1 * i * xscale, y0 - 12, 'moveto' |
| 383 |
print '(%.1g) dup stringwidth exch -.5 mul exch rmoveto show' % (.1 * i) |
| 384 |
for i in range(0, 11): |
| 385 |
print x0, y0 + ytic * i * yscale, 'moveto 6 0 rlineto stroke' |
| 386 |
print x0 + xscale, y0 + ytic * i * yscale, 'moveto -6 0 rlineto stroke' |
| 387 |
print x0 - 3, y0 + ytic * i * yscale - 3.5, 'moveto' |
| 388 |
print '(%.2g) dup stringwidth exch neg exch rmoveto show' % (ytic * i) |
| 389 |
print x0 + .25 * xscale, y0 - 24, 'moveto (chordlen / arclen) show' |
| 390 |
print x0 - 14, y0 + ytic * ymax * yscale + 10, 'moveto /Symbol 12 selectfont (l) show' |
| 391 |
if figno in (1, 2): |
| 392 |
print x0 + 2 / pi * xscale, y0 - 18, 'moveto' |
| 393 |
print '(2/p) dup stringwidth exch -.5 mul exch rmoveto show' |
| 394 |
print 'gsave [3] 0 setdash' |
| 395 |
print x0, y0 + .25 * yscale, 'moveto', xscale, '0 rlineto stroke' |
| 396 |
if figno == 3: |
| 397 |
print x0, y0 + .5 * yscale, 'moveto', xscale, '0 rlineto stroke' |
| 398 |
xinterest = .81153 |
| 399 |
print x0 + xinterest * xscale, y0, 'moveto 0', yscale * .5, 'rlineto stroke' |
| 400 |
print 'grestore' |
| 401 |
print '.75 setlinewidth' |
| 402 |
if 1: |
| 403 |
if figno in (1, 2): |
| 404 |
costscale = .01 * yscale |
| 405 |
elif figno == 3: |
| 406 |
costscale = .0187 * yscale |
| 407 |
lenresult = lenplot(figno, th0, th1) |
| 408 |
cmd = 'moveto' |
| 409 |
for x, y, cost in lenresult: |
| 410 |
print x0 + xscale * x, y0 + yscale * y, cmd |
| 411 |
cmd = 'lineto' |
| 412 |
print 'stroke' |
| 413 |
if figno in (2, 3): |
| 414 |
cmd = 'moveto' |
| 415 |
for x, y, cost in lenresult: |
| 416 |
print x0 + xscale * x, y0 + costscale * cost, cmd |
| 417 |
cmd = 'lineto' |
| 418 |
print 'stroke' |
| 419 |
cmd = 'moveto' |
| 420 |
for x, y, cost in lenresult: |
| 421 |
print x0 + xscale * x, y0 + costscale * x * cost, cmd |
| 422 |
cmd = 'lineto' |
| 423 |
print 'stroke' |
| 424 |
print '/Times-Roman 12 selectfont' |
| 425 |
if figno == 2: |
| 426 |
xlm, ylm = .75, 7 |
| 427 |
xls, yls = .42, 15 |
| 428 |
elif figno == 3: |
| 429 |
xlm, ylm = .6, 3 |
| 430 |
xls, yls = .37, 15 |
| 431 |
print x0 + xscale * xlm, y0 + costscale * ylm, 'moveto' |
| 432 |
print '(MEC cost) show' |
| 433 |
print x0 + xscale * xls, y0 + costscale * yls, 'moveto' |
| 434 |
print '(SIMEC cost) show' |
| 435 |
if figno == 1: |
| 436 |
minis = [(.05, 5, -5), |
| 437 |
(.26, -40, 10), |
| 438 |
(.4569466, -5, -10), |
| 439 |
(.55, 35, 12), |
| 440 |
|
| 441 |
(.6026, -60, 45), |
| 442 |
(.6046, -60, 15), |
| 443 |
(.6066, -60, -15), |
| 444 |
|
| 445 |
(.6213, -22, 10), |
| 446 |
(.6366198, 15, 22), |
| 447 |
(.66, 20, 10), |
| 448 |
(.9, 0, -10)] |
| 449 |
elif figno == 2: |
| 450 |
minis = [(.4569466, -5, -10), |
| 451 |
(.6366198, 15, 22)] |
| 452 |
elif figno == 3: |
| 453 |
minis = [(.741, 55, -10), |
| 454 |
(.81153, -30, 20), |
| 455 |
(.8270, 20, 30)] |
| 456 |
|
| 457 |
for yy, xo, yo in minis: |
| 458 |
fnl = mk_y_fnl(th0, th1, yy) |
| 459 |
params = solve_mec_3constr(fnl) |
| 460 |
k, lam1, lam2 = params |
| 461 |
if lam2 < 0: |
| 462 |
mymaxk = k |
| 463 |
else: |
| 464 |
mymaxk = sqrt(k * k + 4 * lam2) |
| 465 |
lam = abs(lam2) / (mymaxk * mymaxk) |
| 466 |
x = x0 + xscale * yy |
| 467 |
y = y0 + yscale * lam |
| 468 |
print 'gsave %g %g translate circle fill' % (x, y) |
| 469 |
print '%g %g translate 0.15 dup scale' % (xo, yo) |
| 470 |
print '-306 -396 translate' |
| 471 |
print '2 setlinewidth' |
| 472 |
if yy < .6 or yy > .61: |
| 473 |
s = 2 |
| 474 |
elif yy == .6046: |
| 475 |
s = 2.8 |
| 476 |
else: |
| 477 |
s = 5 |
| 478 |
xys, cost, x, y, th = run_elastica(-s, s, k, lam1, lam2, 100) |
| 479 |
print 'gsave [10] 0 setdash' |
| 480 |
plot(xys) |
| 481 |
print 'grestore 6 setlinewidth' |
| 482 |
xys, cost, x, y, th = run_elastica(-.5, .5, k, lam1, lam2, 100) |
| 483 |
plot(xys) |
| 484 |
print 'grestore' |
| 485 |
|
| 486 |
print 'showpage' |
| 487 |
eps_trailer() |
| 488 |
|
| 489 |
def draw_axes(x0, y0, xscale, yscale, xmax, ymax, nx, ny): |
| 490 |
print '.5 setlinewidth' |
| 491 |
print '/Times-Roman 12 selectfont' |
| 492 |
print x0, y0, 'moveto', xscale * xmax, '0 rlineto 0', yscale * ymax, 'rlineto', -xscale * xmax, '0 rlineto closepath stroke' |
| 493 |
xinc = (xmax * xscale * 1.) / nx |
| 494 |
yinc = (ymax * yscale * 1.) / ny |
| 495 |
for i in range(0, nx + 1): |
| 496 |
if i > 0 and i < nx + 1: |
| 497 |
print x0 + xinc * i, y0, 'moveto 0 6 rlineto stroke' |
| 498 |
print x0 + xinc * i, y0 + ymax * yscale, 'moveto 0 -6 rlineto stroke' |
| 499 |
print x0 + xinc * i, y0 - 12, 'moveto' |
| 500 |
print '(%.2g) dup stringwidth exch -.5 mul exch rmoveto show' % ((i * xmax * 1.) / nx) |
| 501 |
for i in range(0, ny + 1): |
| 502 |
if i > 0 and i < ny + 1: |
| 503 |
print x0, y0 + yinc * i, 'moveto 6 0 rlineto stroke' |
| 504 |
print x0 + xmax * xscale, y0 + yinc * i, 'moveto -6 0 rlineto stroke' |
| 505 |
print x0 - 3, y0 + yinc * i - 3.5, 'moveto' |
| 506 |
print '(%.2g) dup stringwidth exch neg exch rmoveto show' % ((i * ymax * 1.) / ny) |
| 507 |
|
| 508 |
|
| 509 |
def mecchord(): |
| 510 |
x0 = 72 |
| 511 |
y0 = 72 |
| 512 |
thscale = 150 |
| 513 |
chscale = 400 |
| 514 |
print '.5 setlinewidth' |
| 515 |
print '/ss 1.5 def' |
| 516 |
print '/circle { ss 0 moveto currentpoint exch ss sub exch ss 0 360 arc } bind def' |
| 517 |
draw_axes(x0, y0, thscale, chscale, 3.2, 1, 16, 10) |
| 518 |
print x0 + 1 * thscale, y0 - 28, 'moveto (angle) show' |
| 519 |
print 'gsave', x0 - 32, y0 + .25 * chscale, 'translate' |
| 520 |
print '90 rotate 0 0 moveto (chordlen / arclen) show' |
| 521 |
print 'grestore' |
| 522 |
|
| 523 |
cmd = 'moveto' |
| 524 |
for i in range(0, 67): |
| 525 |
s = i * .01 |
| 526 |
xys, cost, x, y, th = run_elastica(-s, s, 4, 0, -8, 100) |
| 527 |
|
| 528 |
if s > 0: |
| 529 |
ch = hypot(x, y) / (2 * s) |
| 530 |
else: |
| 531 |
ch = 1 |
| 532 |
print '%', s, x, y, th, ch |
| 533 |
print x0 + thscale * th / 2, y0 + ch * chscale, cmd |
| 534 |
cmd = 'lineto' |
| 535 |
print 'stroke' |
| 536 |
|
| 537 |
print 'gsave %g %g translate circle fill' % (x0 + thscale * th / 2, y0 + ch * chscale) |
| 538 |
print '-30 15 translate 0.25 dup scale' |
| 539 |
print '-306 -396 translate' |
| 540 |
print '2 setlinewidth' |
| 541 |
plot(xys) |
| 542 |
print 'grestore' |
| 543 |
|
| 544 |
print 'gsave [2] 0 setdash' |
| 545 |
cmd = 'moveto' |
| 546 |
for i in range(0, 151): |
| 547 |
th = pi * i / 150. |
| 548 |
if th > 0: |
| 549 |
ch = sin(th) / th |
| 550 |
else: |
| 551 |
ch = 1 |
| 552 |
print x0 + thscale * th, y0 + ch * chscale, cmd |
| 553 |
cmd = 'lineto' |
| 554 |
print 'stroke' |
| 555 |
print 'grestore' |
| 556 |
|
| 557 |
k0 = 4 * .4536 / (2 / pi) |
| 558 |
s = pi / (2 * k0) |
| 559 |
xys, cost, x, y, th = run_elastica(-s, s, k0, 0, 0, 100) |
| 560 |
th = pi |
| 561 |
ch = 2 / pi |
| 562 |
print 'gsave %g %g translate circle fill' % (x0 + thscale * th / 2, y0 + ch * chscale) |
| 563 |
print '30 15 translate 0.25 dup scale' |
| 564 |
print '-306 -396 translate' |
| 565 |
print '2 setlinewidth' |
| 566 |
plot(xys) |
| 567 |
print 'grestore' |
| 568 |
|
| 569 |
print x0 + 1.25 * thscale, y0 + .55 * chscale, 'moveto (MEC) show' |
| 570 |
print x0 + 2.3 * thscale, y0 + .35 * chscale, 'moveto (SIMEC) show' |
| 571 |
|
| 572 |
print 'showpage' |
| 573 |
|
| 574 |
def trymec(sm, sp): |
| 575 |
xys, thm, cost = run_elastica_half(abs(sm), 0, 1, 0, 10) |
| 576 |
xm, ym, km = xys[-1] |
| 577 |
if sm < 0: |
| 578 |
xm = -xm |
| 579 |
ym = -ym |
| 580 |
thm = -thm |
| 581 |
xys, thp, cost = run_elastica_half(abs(sp), 0, 1, 0, 10) |
| 582 |
xp, yp, kp = xys[-1] |
| 583 |
if sp < 0: |
| 584 |
xp = -xp |
| 585 |
yp = -yp |
| 586 |
thp = -thp |
| 587 |
chth = atan2(yp - ym, xp - xm) |
| 588 |
|
| 589 |
actual_th0 = chth - thm |
| 590 |
actual_th1 = thp - chth |
| 591 |
return actual_th0, actual_th1 |
| 592 |
|
| 593 |
def findmec_old(th0, th1): |
| 594 |
guess_ds = sqrt(6 * (th1 - th0)) |
| 595 |
guess_savg = 2 * (th1 + th0) / guess_ds |
| 596 |
sm = .5 * (guess_savg - guess_ds) |
| 597 |
sp = .5 * (guess_savg + guess_ds) |
| 598 |
|
| 599 |
for i in range(1): |
| 600 |
actual_th0, actual_th1 = trymec(sm, sp) |
| 601 |
guess_dth = 1./6 * (sp - sm) ** 2 |
| 602 |
guess_avth = .5 * (sp + sm) * (sp - sm) |
| 603 |
guess_th0 = .5 * (guess_avth - guess_dth) |
| 604 |
guess_th1 = .5 * (guess_avth + guess_dth) |
| 605 |
print sm, sp, actual_th0, actual_th1, guess_th0, guess_th1 |
| 606 |
|
| 607 |
def mecplots(): |
| 608 |
print '2 dup scale' |
| 609 |
print '-153 -296 translate' |
| 610 |
scale = 45 |
| 611 |
print '0.25 setlinewidth' |
| 612 |
print 306 - scale * pi/2, 396, 'moveto', 306 + scale * pi/2, 396, 'lineto stroke' |
| 613 |
print 306, 396 - scale * pi/2, 'moveto', 306, 396 + scale * pi/2, 'lineto stroke' |
| 614 |
print '/ss .5 def' |
| 615 |
print '/circle { ss 0 moveto currentpoint exch ss sub exch ss 0 360 arc } bind def' |
| 616 |
|
| 617 |
tic = .1 |
| 618 |
maj = 5 |
| 619 |
r0, r1 = 0, 81 |
| 620 |
|
| 621 |
for i in range(r0, r1, maj): |
| 622 |
sm = i * tic |
| 623 |
cmd = 'moveto' |
| 624 |
for j in range(i, r1): |
| 625 |
sp = j * tic + 1e-3 |
| 626 |
th0, th1 = trymec(sm, sp) |
| 627 |
print '%', sm, sp, th0, th1 |
| 628 |
print 306 + scale * th0, 396 + scale * th1, cmd |
| 629 |
cmd = 'lineto' |
| 630 |
print 'stroke' |
| 631 |
for j in range(r0, r1, maj): |
| 632 |
sp = j * tic + 1e-3 |
| 633 |
cmd = 'moveto' |
| 634 |
for i in range(0, j + 1): |
| 635 |
sm = i * tic |
| 636 |
th0, th1 = trymec(sm, sp) |
| 637 |
print '%', sm, sp, th0, th1 |
| 638 |
print 306 + scale * th0, 396 + scale * th1, cmd |
| 639 |
cmd = 'lineto' |
| 640 |
print 'stroke' |
| 641 |
|
| 642 |
for i in range(r0, r1, maj): |
| 643 |
sm = i * tic |
| 644 |
for j in range(i, r1, maj): |
| 645 |
sp = j * tic + 1e-3 |
| 646 |
th0, th1 = trymec(sm, sp) |
| 647 |
print 'gsave' |
| 648 |
print 306 + scale * th0, 596 + scale * th1, 'translate' |
| 649 |
print 'circle fill' |
| 650 |
uscale = 3 |
| 651 |
xys, thm, cost = run_elastica_half(abs(sm), 0, 1, 0, 100) |
| 652 |
x0, y0 = xys[-1][0], xys[-1][1] |
| 653 |
if sm < 0: |
| 654 |
x0, y0 = -x0, -y0 |
| 655 |
xys, thm, cost = run_elastica_half(abs(sp), 0, 1, 0, 100) |
| 656 |
x1, y1 = xys[-1][0], xys[-1][1] |
| 657 |
if sp < 0: |
| 658 |
x1, y1 = -x1, -y1 |
| 659 |
cmd = 'moveto' |
| 660 |
for xy in xys: |
| 661 |
print xy[0] * uscale, xy[1] * uscale, cmd |
| 662 |
cmd = 'lineto' |
| 663 |
print 'stroke' |
| 664 |
print '1 0 0 setrgbcolor' |
| 665 |
print x0 * uscale, y0 * uscale, 'moveto' |
| 666 |
print x1 * uscale, y1 * uscale, 'lineto stroke' |
| 667 |
print 'grestore' |
| 668 |
|
| 669 |
print 'showpage' |
| 670 |
|
| 671 |
def findmec(th0, th1): |
| 672 |
delta = 1e-3 |
| 673 |
if th0 < 0 and th0 - th1 < .1: |
| 674 |
sm = 5. |
| 675 |
sp = 6. |
| 676 |
else: |
| 677 |
sm = 3. |
| 678 |
sp = 5. |
| 679 |
params = [sm, sp] |
| 680 |
lasterr = 1e6 |
| 681 |
for i in range(25): |
| 682 |
sm, sp = params |
| 683 |
ath0, ath1 = trymec(sm, sp) |
| 684 |
c1c, c2c = th0 - ath0, th1 - ath1 |
| 685 |
|
| 686 |
err = c1c * c1c + c2c * c2c |
| 687 |
if 0: |
| 688 |
print '%findmec', sm, sp, ath0, ath1, err |
| 689 |
sys.stdout.flush() |
| 690 |
|
| 691 |
if err < 1e-9: |
| 692 |
return params |
| 693 |
if err > lasterr: |
| 694 |
return None |
| 695 |
lasterr = err |
| 696 |
|
| 697 |
|
| 698 |
dc1s = [] |
| 699 |
dc2s = [] |
| 700 |
for j in range(len(params)): |
| 701 |
params1 = N.array(params) |
| 702 |
params1[j] += delta |
| 703 |
sm, sp = params1 |
| 704 |
ath0, ath1 = trymec(sm, sp) |
| 705 |
c1p, c2p = th0 - ath0, th1 - ath1 |
| 706 |
|
| 707 |
params1 = N.array(params) |
| 708 |
params1[j] -= delta |
| 709 |
sm, sp = params1 |
| 710 |
ath0, ath1 = trymec(sm, sp) |
| 711 |
c1m, c2m = th0 - ath0, th1 - ath1 |
| 712 |
|
| 713 |
dc1s.append((c1p - c1m) / (2 * delta)) |
| 714 |
dc2s.append((c2p - c2m) / (2 * delta)) |
| 715 |
|
| 716 |
jm = N.array([dc1s, dc2s]) |
| 717 |
ji = la.inverse(jm) |
| 718 |
dp = N.dot(ji, [c1c, c2c]) |
| 719 |
|
| 720 |
if i < 4: |
| 721 |
scale = .5 |
| 722 |
else: |
| 723 |
scale = 1 |
| 724 |
params -= scale * dp |
| 725 |
if params[0] < 0: params[0] = 0. |
| 726 |
return params |
| 727 |
|
| 728 |
def mecrange(figtype): |
| 729 |
scale = 130 |
| 730 |
eps_prologue(50, 110, 570, 630) |
| 731 |
print -50, 0, 'translate' |
| 732 |
print '0.5 setlinewidth' |
| 733 |
thlmin, thlmax = -pi/2, 2.4 |
| 734 |
thrmin, thrmax = -2.2, pi / 2 + .2 |
| 735 |
print 306 + scale * thlmin, 396, 'moveto', 306 + scale * thlmax, 396, 'lineto stroke' |
| 736 |
print 306, 396 + scale * thrmin, 'moveto', 306, 396 + scale * thrmax, 'lineto stroke' |
| 737 |
|
| 738 |
print 'gsave [2] 0 setdash' |
| 739 |
print 306, 396 + scale * pi / 2, 'moveto' |
| 740 |
print 306 + scale * thlmax, 396 + scale * pi / 2, 'lineto stroke' |
| 741 |
print 306 + scale * thlmin, 396 - scale * pi / 2, 'moveto' |
| 742 |
print 306 + scale * thlmax, 396 - scale * pi / 2, 'lineto stroke' |
| 743 |
print 306 + scale * pi / 2, 396 + scale * thrmin, 'moveto' |
| 744 |
print 306 + scale * pi / 2, 396 + scale * thrmax, 'lineto stroke' |
| 745 |
print 'grestore' |
| 746 |
|
| 747 |
print 306 + 3, 396 + scale * thrmax - 10, 'moveto' |
| 748 |
print '/Symbol 12 selectfont (q) show' |
| 749 |
print 0, -2, 'rmoveto' |
| 750 |
print '/Times-Italic 9 selectfont (right) show' |
| 751 |
|
| 752 |
print 306 - 18, 396 + scale * pi / 2 - 4, 'moveto' |
| 753 |
print '/Symbol 12 selectfont (p/2) show' |
| 754 |
print 306 + scale * 2.2, 396 - scale * pi / 2 + 2, 'moveto' |
| 755 |
print '/Symbol 12 selectfont (-p/2) show' |
| 756 |
|
| 757 |
print 306 + scale * pi/2 + 2, 396 + scale * thrmax - 10, 'moveto' |
| 758 |
print '/Symbol 12 selectfont (p/2) show' |
| 759 |
|
| 760 |
print 306 + scale * 2.2, 396 + 6, 'moveto' |
| 761 |
print '/Symbol 12 selectfont (q) show' |
| 762 |
print 0, -2, 'rmoveto' |
| 763 |
print '/Times-Italic 9 selectfont (left) show' |
| 764 |
|
| 765 |
print '/ss 0.8 def' |
| 766 |
print '/circle { ss 0 moveto currentpoint exch ss sub exch ss 0 360 arc } bind def' |
| 767 |
cmd = 'moveto' |
| 768 |
for i in range(0, 201): |
| 769 |
th = (i * .005 - .75 )* pi |
| 770 |
rmin = 1.5 |
| 771 |
rmax = 2.5 |
| 772 |
for j in range(20): |
| 773 |
r = (rmin + rmax) * .5 |
| 774 |
th0 = r * cos(th) |
| 775 |
th1 = r * sin(th) |
| 776 |
if findmec(th0, th1) == None: |
| 777 |
rmax = r |
| 778 |
else: |
| 779 |
rmin = r |
| 780 |
r = (rmin + rmax) * .5 |
| 781 |
th0 = r * cos(th) |
| 782 |
th1 = r * sin(th) |
| 783 |
print '%', r, th, th0, th1 |
| 784 |
print 306 + scale * th0, 396 + scale * th1, cmd |
| 785 |
cmd = 'lineto' |
| 786 |
sys.stdout.flush() |
| 787 |
print 'stroke' |
| 788 |
sys.stdout.flush() |
| 789 |
|
| 790 |
for i in range(-11, 12): |
| 791 |
for j in range(-11, i + 1): |
| 792 |
th0, th1 = i * .196, j * .196 |
| 793 |
print '%', th0, th1 |
| 794 |
params = findmec(th0, th1) |
| 795 |
if params != None: |
| 796 |
sm, sp = params |
| 797 |
print 'gsave' |
| 798 |
print 306 + scale * th0, 396 + scale * th1, 'translate' |
| 799 |
uscale = 22 |
| 800 |
k0, lam1, lam2 = justify_mec(sm, sp) |
| 801 |
xys, cost, x, y, th = run_elastica(-.5, .5, k0, lam1, lam2) |
| 802 |
cmdm = 'moveto' |
| 803 |
dx = xys[-1][0] - xys[0][0] |
| 804 |
dy = xys[-1][1] - xys[0][1] |
| 805 |
ch = hypot(dx, dy) |
| 806 |
chth = atan2(dy, dx) |
| 807 |
if figtype == 'mecrange': |
| 808 |
print 'circle fill' |
| 809 |
s = uscale * sin(chth) / ch |
| 810 |
c = uscale * cos(chth) / ch |
| 811 |
h = -xys[0][0] * s + xys[0][1] * c |
| 812 |
for xy in xys: |
| 813 |
print xy[0] * c + xy[1] * s, h + xy[0] * s - xy[1] * c, cmdm |
| 814 |
cmdm = 'lineto' |
| 815 |
elif figtype == 'mecrangek': |
| 816 |
ds = 1. / (len(xys) - 1) |
| 817 |
sscale = 13. / ch |
| 818 |
kscale = 3 * ch |
| 819 |
print 'gsave .25 setlinewidth' |
| 820 |
print sscale * -.5, 0, 'moveto', sscale, 0, 'rlineto stroke' |
| 821 |
print 'grestore' |
| 822 |
for l in range(len(xys)): |
| 823 |
print sscale * (ds * l - 0.5), kscale * xys[l][2], cmdm |
| 824 |
cmdm = 'lineto' |
| 825 |
print 'stroke' |
| 826 |
print 'grestore' |
| 827 |
sys.stdout.flush() |
| 828 |
print 'showpage' |
| 829 |
eps_trailer() |
| 830 |
|
| 831 |
|
| 832 |
def justify_mec(sm, sp): |
| 833 |
sc = (sm + sp) * 0.5 |
| 834 |
xys, thc, cost = run_elastica_half(sc, 0, 1, 0, 100) |
| 835 |
print '% sc =', sc, 'thc =', thc |
| 836 |
k0, lam1, lam2 = xys[-1][2], cos(thc), -sin(thc) |
| 837 |
ds = sp - sm |
| 838 |
k0 *= ds |
| 839 |
lam1 *= ds * ds |
| 840 |
lam2 *= ds * ds |
| 841 |
return [k0, lam1, lam2] |
| 842 |
|
| 843 |
import sys |
| 844 |
figname = sys.argv[1] |
| 845 |
if len(figname) > 4 and figname[-4:] == '.pdf': figname = figname[:-4] |
| 846 |
if figname in ('lengraph1', 'lengraph2', 'lengraph3'): |
| 847 |
lengraph(int(figname[-1])) |
| 848 |
|
| 849 |
|
| 850 |
elif figname == 'mecchord': |
| 851 |
mecchord() |
| 852 |
elif figname in ('mecrange', 'mecrangek'): |
| 853 |
mecrange(figname) |
| 854 |
elif figname == 'mecplots': |
| 855 |
mecplots() |
| 856 |
elif figname == 'findmec': |
| 857 |
findmec(-.1, -.1) |
| 858 |
findmec(-.2, -.2) |
| 859 |
|