author | Francesco Abbate <francesco.bbt@gmail.com> | 2012年02月07日 22:11:22 +0100 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2012年02月07日 22:11:22 +0100 |
commit | e693e320e7cf0d0fc30468ba352aea8aeb8c93c8 (patch) | |
tree | 72779988ce3a5070379fbc65d77ad4747adf65ff | |
parent | 8a5bbe0044dc547e730310de4ab01210d0786677 (diff) | |
download | gsl-shell-e693e320e7cf0d0fc30468ba352aea8aeb8c93c8.tar.gz |
-rw-r--r-- | examples/3d.lua | 26 | ||||
-rw-r--r-- | examples/bessel.lua | 44 | ||||
-rw-r--r-- | examples/cspline-example.lua | 42 | ||||
-rw-r--r-- | examples/data/sige-sims-prof.csv | 316 | ||||
-rw-r--r-- | examples/eigensystems.lua | 39 | ||||
-rw-r--r-- | examples/fractals.lua | 234 | ||||
-rw-r--r-- | examples/interp.lua | 53 | ||||
-rw-r--r-- | examples/minimize.lua | 104 | ||||
-rw-r--r-- | examples/optical-disp.lua | 101 | ||||
-rw-r--r-- | examples/pre3d.lua | 195 | ||||
-rw-r--r-- | examples/qag-bessel.lua | 37 | ||||
-rw-r--r-- | examples/qr-lssolve.lua | 28 | ||||
-rw-r--r-- | examples/randist.lua | 17 | ||||
-rw-r--r-- | examples/sige.lua | 128 | ||||
-rw-r--r-- | examples/vandermonde.lua | 56 | ||||
-rw-r--r-- | examples/zernike.lua | 43 |
diff --git a/examples/3d.lua b/examples/3d.lua deleted file mode 100644 index 2ba4d878..00000000 --- a/examples/3d.lua +++ /dev/null @@ -1,26 +0,0 @@ - -use 'math' - -require 'plot3d' - -function demo1() - local x = |u,v| (1 + 1/2 * v *cos(u/2))*cos(u) - local y = |u,v| (1 + 1/2 * v *cos(u/2))*sin(u) - local z = |u,v| 1/2 * v * sin(u/2) - return graph.surfplot({x, y, z}, 0, -1, 2*pi, 1, {gridu= 60, gridv= 4, stroke= true}) -end - -function demo2() - local f = |x, y| exp(-(x^2+y^2))*sin(x) - return graph.plot3d(f, -4, -4, 4, 4, {gridx= 31, gridy= 31}) -end - -function demo3() - local f = |x, y| x^2 - y^2 - return graph.plot3d(f, -3, -3, 3, 3, {stroke= true}) -end - -echo([[ -demo1() - Plot the mobieus strip using a parametric rapresentation -demo2() - Plot example of function f(x,y) = exp(-(x^2+y^2))*sin(x) -demo3() - Plot example of function f(x,y) = x^2 - y^2]]) diff --git a/examples/bessel.lua b/examples/bessel.lua deleted file mode 100644 index 497cf6df..00000000 --- a/examples/bessel.lua +++ /dev/null @@ -1,44 +0,0 @@ - -function bessJ(x, n) - local f = |t| cos(n*t - x*sin(t)) -- we define the function to integrate - return 1/pi * integ {f= f, points={0, pi}} -end - -function bessJext(x,alpha) - local f1 = |t| cos(alpha*t - x*sin(t)) - local f2 = |t| exp(-x*sinh(t)-alpha*t) - local t1 = 1/pi * integ {f= f1, points={0, pi}} - local t2 = -sin(alpha*pi)/pi * integ {f= f2, points={0, '+inf'}} - return t1 + t2 -end - -function fact(n) - local p = n > 1 and n or 1 - for i=2,n-1 do p = p * i end - return p -end - -function bessJseriev1(x,alpha,nmax) - local a, m = 0, nmax - local t = (x/2)^2 - local sign = 1 - 2*(m % 2) - local c = sign / (fact(m) * fact(m + alpha)) - for k=0,m do - a = (a * t + c) - c = - c * (m-k) * (m-k+alpha) - end - return a * (x/2)^alpha -end - -function bessJserie(x, alpha, eps) - local a, c, p = 0, 1/fact(alpha), 1 - local t = (x/2)^2 - for n = 0, 100 do - a = a + c * p - local r = (n + 1) * (n + alpha + 1) - p = p * t - c = - c / r - if t / r < eps then break end - end - return a * (x/2)^alpha -end diff --git a/examples/cspline-example.lua b/examples/cspline-example.lua deleted file mode 100644 index 466e3042..00000000 --- a/examples/cspline-example.lua +++ /dev/null @@ -1,42 +0,0 @@ - -local function cubic(x, y, z, h) - local N = #x - 1 - return function(xe) - local i = N - for j=2,N+1 do - if x[j] >= xe then i = j-1; break end - end - return (z[i+1]*(xe - x[i])^3 + z[i]*(x[i+1] - xe)^3)/(6*h[i]) + (y[i+1]/h[i] - h[i]/6*z[i+1])*(xe-x[i]) + (y[i]/h[i] - h[i]/6*z[i])*(x[i+1]-xe) - end -end - -function cspline(x, y) - local N = #x - 1 - local h = ilist(|i| x[i+1] - x[i], N) - - local function mterm(i,j) - if i == 1 then - return (j == 1 and 1 or 0) - elseif i == N+1 then - return (j == N+1 and 1 or 0) - else - return j == i-1 and h[i-1] or (j == i and 2*(h[i-1]+h[i]) or (j == i+1 and h[i] or 0)) - end - end - - local M = new(N+1, N+1, mterm) - local b = new(N+1, 1, |i| (i > 1 and i < N+1) and 6*((y[i+1] - y[i])/h[i] - (y[i] - y[i-1])/h[i-1]) or 0) - - local z = solve(M, b) - - return cubic(x, y, z, h) -end - -x = {-1, -0.5, -0.3, 0, 0.3, 0.5, 1} -y = ilist(|i| exp(-x[i]*x[i]), #x) - -f = cspline(x, y) - -p = fxplot(f, -1, 1) -p.title = 'Cubic spline interpolation' -p:addline(ipath(sequence(function(j) return x[j], y[j] end, #x)), 'blue', {{'marker', size= 6}}) diff --git a/examples/data/sige-sims-prof.csv b/examples/data/sige-sims-prof.csv deleted file mode 100644 index 3fae93a6..00000000 --- a/examples/data/sige-sims-prof.csv +++ /dev/null @@ -1,316 +0,0 @@ -0.12578112,0.007534327 -0.21108096,0.001706499 -0.2963808,0.00563319 -0.38168064,0.01935592 -0.46698048,0.070880641 -0.55228032,0.232247176 -0.63758016,0.618301998 -0.72288,1.396899399 -0.80817984,2.710068207 -0.89347968,4.343824916 -0.97877952,6.592957537 -1.06407936,9.34387715 -1.1493792,12.13993142 -1.23467904,15.05125236 -1.31997888,17.87557765 -1.40527872,20.49686914 -1.49057856,23.27910988 -1.5758784,25.35734531 -1.66117824,27.30962652 -1.74647808,28.83990502 -1.83177792,30.61262474 -1.91707776,31.7257668 -2.0023776,32.51009803 -2.08767744,33.46343212 -2.17297728,33.90257536 -2.25827712,34.55431499 -2.34357696,34.6983783 -2.4288768,34.83917595 -2.51417664,34.83897888 -2.59947648,34.89707112 -2.68477632,34.89341888 -2.77007616,35.01108425 -2.855376,34.87707151 -2.94067584,34.85673517 -3.02597568,34.35292181 -3.11127552,34.63142049 -3.19657536,34.31501363 -3.2818752,33.83805786 -3.36717504,33.63711503 -3.45247488,33.48435053 -3.53777472,33.19862699 -3.62307456,32.95200494 -3.7083744,33.18439766 -3.79367424,33.00536467 -3.87897408,32.75420126 -3.96427392,32.45780014 -4.04957376,32.13684891 -4.1348736,32.13708062 -4.22017344,32.31349051 -4.30547328,31.99358342 -4.39077312,31.90218416 -4.47607296,31.91464243 -4.5613728,31.70087515 -4.64667264,31.68628107 -4.73197248,31.60100551 -4.81727232,31.73540562 -4.90257216,31.5475405 -4.987872,31.54377268 -5.07317184,31.43733865 -5.15847168,31.62030231 -5.24377152,31.42257929 -5.32907136,31.59652511 -5.4143712,31.43141682 -5.49967104,31.52179393 -5.58497088,31.56846011 -5.67027072,31.37400781 -5.75557056,31.38456563 -5.8408704,31.1831554 -5.92617024,31.39277661 -6.01147008,31.4056036 -6.09676992,31.11718208 -6.18206976,31.72895427 -6.2673696,31.55680634 -6.35266944,31.58709416 -6.43796928,31.36724327 -6.52326912,31.42415509 -6.60856896,31.31162416 -6.6938688,31.61097615 -6.77916864,31.4516802 -6.86446848,31.30832058 -6.94976832,31.29526418 -7.03506816,31.55105833 -7.120368,31.25895426 -7.20566784,31.03974574 -7.29096768,31.34239389 -7.37626752,31.36457186 -7.46156736,31.42870383 -7.5468672,31.59978781 -7.63216704,30.90525025 -7.71746688,31.49930862 -7.80276672,31.27069789 -7.88806656,31.51757286 -7.9733664,31.35516378 -8.05866624,31.36061994 -8.14396608,31.32807065 -8.22926592,31.28821583 -8.31456576,31.2743352 -8.3998656,31.6300672 -8.48516544,31.39150864 -8.57046528,31.12168878 -8.65576512,31.24728247 -8.74106496,31.51871112 -8.8263648,31.37113862 -8.91166464,31.47509159 -8.99696448,31.60737431 -9.08226432,31.60615768 -9.16756416,31.60879497 -9.252864,31.47216183 -9.33816384,31.43366781 -9.42346368,31.39915076 -9.50876352,31.4824224 -9.59406336,31.25524675 -9.6793632,31.59086343 -9.76466304,31.71230732 -9.84996288,31.43731424 -9.93526272,31.3865646 -10.02056256,31.45515888 -10.1058624,31.4726013 -10.19116224,31.40871853 -10.27646208,31.53552818 -10.36176192,31.71552188 -10.44706176,31.55302295 -10.5323616,31.39097576 -10.61766144,31.68558014 -10.70296128,31.64308313 -10.78826112,31.50172617 -10.87356096,31.53731461 -10.9588608,31.52890603 -11.04416064,31.48441595 -11.12946048,31.29171969 -11.21476032,31.51175869 -11.30006016,31.57576063 -11.38536,31.63919868 -11.47065984,31.49751954 -11.55595968,31.56262855 -11.64125952,31.48138802 -11.72655936,31.3232605 -11.8118592,31.52634999 -11.89715904,31.66664616 -11.98245888,31.35219869 -12.06775872,31.44483442 -12.15305856,31.37799794 -12.2383584,31.50211825 -12.32365824,31.488009 -12.40895808,31.83168451 -12.49425792,31.78670764 -12.57955776,31.82457651 -12.6648576,31.54878124 -12.75015744,31.67060774 -12.83545728,31.71996488 -12.92075712,31.55551337 -13.00605696,31.51790574 -13.0913568,31.68376711 -13.17665664,31.58999069 -13.26195648,31.84198503 -13.34725632,31.4597095 -13.43255616,31.60170552 -13.517856,31.67184419 -13.60315584,31.91048596 -13.68845568,31.55423077 -13.77375552,31.65175622 -13.85905536,31.73473355 -13.9443552,31.59577261 -14.02965504,31.70246225 -14.11495488,31.86705735 -14.20025472,31.70632569 -14.28555456,31.55937146 -14.3708544,31.6561642 -14.45615424,31.73626414 -14.54145408,31.70933816 -14.62675392,31.61879981 -14.71205376,31.63589141 -14.7973536,31.80513488 -14.88265344,31.65796279 -14.96795328,31.81495551 -15.05325312,31.60541932 -15.13855296,31.82062969 -15.2238528,31.68709916 -15.30915264,31.88599147 -15.39445248,31.83740949 -15.47975232,31.67727461 -15.56505216,31.92463013 -15.650352,31.99400628 -15.73565184,31.69720773 -15.82095168,31.62567427 -15.90625152,31.91007739 -15.99155136,31.78840561 -16.0768512,31.81031339 -16.16215104,31.74530076 -16.24745088,31.57281564 -16.33275072,31.78167166 -16.41805056,31.57610078 -16.5033504,31.86289319 -16.58865024,31.59789782 -16.67395008,31.57368065 -16.75924992,31.4744497 -16.84454976,31.61243652 -16.9298496,31.35104898 -17.01514944,31.32409259 -17.10044928,31.05302073 -17.18574912,30.80548929 -17.27104896,30.839382 -17.3563488,30.40273672 -17.44164864,29.73392167 -17.52694848,29.39702486 -17.61224832,28.44775178 -17.69754816,27.6537719 -17.782848,26.39631012 -17.86814784,25.36009075 -17.95344768,23.7925397 -18.03874752,22.33203876 -18.12404736,20.5098766 -18.2093472,19.02098494 -18.29464704,17.5010436 -18.37994688,15.850408 -18.46524672,14.13709213 -18.55054656,12.60783404 -18.6358464,11.29033286 -18.72114624,10.11198535 -18.80644608,8.824805867 -18.89174592,7.971035207 -18.97704576,6.932944886 -19.0623456,6.141476359 -19.14764544,5.408107987 -19.23294528,4.655191048 -19.31824512,4.145825679 -19.40354496,3.683419154 -19.4888448,3.282841817 -19.57414464,2.902033001 -19.65944448,2.573148596 -19.74474432,2.231779656 -19.83004416,1.984969672 -19.915344,1.756328242 -20.00064384,1.545244316 -20.08594368,1.398573938 -20.17124352,1.278183638 -20.25654336,1.038216283 -20.3418432,0.937069241 -20.42714304,0.870517362 -20.51244288,0.763840039 -20.59774272,0.702616891 -20.68304256,0.626493235 -20.7683424,0.53087818 -20.85364224,0.47161923 -20.93894208,0.442636209 -21.02424192,0.382485037 -21.10954176,0.357199923 -21.1948416,0.317188068 -21.28014144,0.299185971 -21.36544128,0.258730674 -21.45074112,0.237236125 -21.53604096,0.206347356 -21.6213408,0.185459979 -21.70664064,0.172694735 -21.79194048,0.146330608 -21.87724032,0.128528285 -21.96254016,0.141407642 -22.04784,0.104841938 -22.13313984,0.08843813 -22.21843968,0.082392865 -22.30373952,0.081870598 -22.38903936,0.079164536 -22.4743392,0.067344052 -22.55963904,0.068191041 -22.64493888,0.060489208 -22.73023872,0.055637043 -22.81553856,0.044183042 -22.9008384,0.040616857 -22.98613824,0.039375649 -23.07143808,0.038788498 -23.15673792,0.034474591 -23.24203776,0.034659307 -23.3273376,0.033874325 -23.41263744,0.027735193 -23.49793728,0.028891637 -23.58323712,0.024985582 -23.66853696,0.019774041 -23.7538368,0.018253866 -23.83913664,0.018175062 -23.92443648,0.022931047 -24.00973632,0.017614901 -24.09503616,0.01164021 -24.180336,0.010711196 -24.26563584,0.011808801 -24.35093568,0.011236438 -24.43623552,0.013408831 -24.52153536,0.011768502 -24.6068352,0.007470942 -24.69213504,0.009097366 -24.77743488,0.012360518 -24.86273472,0.012248849 -24.94803456,0.010634189 -25.0333344,0.009135265 -25.11863424,0.009641066 -25.20393408,0.01125926 -25.28923392,0.014871454 -25.37453376,0.007405334 -25.4598336,0.00641232 -25.54513344,0.003764811 -25.63043328,0.00590787 -25.71573312,0.010612797 -25.80103296,0.005866041 -25.8863328,0.006371751 -25.97163264,0.008563673 -26.05693248,0.007554274 -26.14223232,0.007978361 -26.22753216,0.005922984 -26.312832,0.007988376 -26.39813184,0.007529068 -26.48343168,0.006990182 -26.56873152,0.005926943 -26.65403136,0.006432239 -26.7393312,0.006929291 -26.82463104,0.005902356 -26.90993088,0.00535718 -26.99523072,0.003781971 diff --git a/examples/eigensystems.lua b/examples/eigensystems.lua deleted file mode 100644 index fc25b2e3..00000000 --- a/examples/eigensystems.lua +++ /dev/null @@ -1,39 +0,0 @@ - -local function vandermonde(x) - local n = #x - return new(n, n, |i,j| x[i]^(n-j)) -end - - -function demo1() - local m = new(4, 4, |i,j| 1/(i+j-1)) - echo 'Matrix:' - print(m) - local e, v = eigsv(m) - echo('Eigenvalues:') - print(ilist(|i| e[i], 4)) - - -- the following expression will give a diagonal matrix with the eigenvalues - --- along the diagonal - echo('Matrix diagonal form:') - print(prod(v, m*v)) -end - -function demo2() - local m = vandermonde {-1, -2, 3, 4} - echo 'Matrix:' - print(m) - - local e, v = eignsv(m) - echo 'Eigenvalues:' - print(ilist(|i| e[i], 4)) - - -- the following expression will give a diagonal matrix with the eigenvalues - --- along the diagonal - echo('Matrix diagonal form:') - print(inv(v) * m * v) -end - -echo([[ -demo1() - example of eigensystem solving for a real symmetrix matrix -demo2() - example of eigensystem solving for a real non-symmetrix matrix]]) diff --git a/examples/fractals.lua b/examples/fractals.lua deleted file mode 100644 index 71fdb224..00000000 --- a/examples/fractals.lua +++ /dev/null @@ -1,234 +0,0 @@ - -use 'stdlib' - -local function c_generator(n, n_angle, len_frac, g) - local exp, real, imag = complex.exp, complex.real, complex.imag - local w, r, k = ilist(|| 0, n+1), #g - - local s = len_frac^n - local sz = cnew(n_angle, 1, |k| s * exp(2i*pi*(k-1)/n_angle)) - - local sh = ilist(|k| g[k%r+1] - g[(k-1)%r+1], 0, r-1) - local a = (g[1]*n) % n_angle - - local z = 0 - return function() - if w[n+1] == 0 then - local j - z = z + sz[a+1] - for j=1,n+1 do - w[j] = (w[j] + 1) % r - a = (a + sh[w[j]+1]) % n_angle - if w[j] ~= 0 then - break - end - end - return real(z), imag(z) - end - end -end - -local function levyc(n) - local p = plot('Levy\'s C curve') - local c = ipath(c_generator(n, 4, 1/2, {-1,0,0,1})) - p:addline(c, 'red', {}, {{'rotate', angle= -pi/4}}) - p:addline(c, 'red', {}, {{'translate', x=1/sqrt(2), y=-1/sqrt(2)},{'rotate', angle= pi/4}}) - p.units = false - p:show() - return p -end - -function demo1() - local pl = plot() - - local t = path() - t:move_to(0,0) - t:line_to(1,0) - t:line_to(0.5,-sqrt(3)/2) - t:close() - - local v = ipath(c_generator(4, 6, 1/3, {0,1,-1,0})) - local c = rgba(0,0,0.7,0.2) - pl:add(v, c) - pl:add(v, c, {}, {{'translate', x=1, y=0}, {'rotate', angle=-2*pi/3}}) - pl:add(v, c, {}, {{'translate', x=0.5, y=-sqrt(3)/2}, - {'rotate', angle=-2*2*pi/3}}) - pl:add(t, c) - - c = rgb(0,0,0.7) - - pl:addline(v, c) - pl:addline(v, c, {}, {{'translate', x=1, y=0}, {'rotate', angle=-2*pi/3}}) - pl:addline(v, c, {}, {{'translate', x=0.5, y=-sqrt(3)/2}, - {'rotate', angle=-2*2*pi/3}}) - - pl.units = false - pl:show() -end - -demo2 = function(n) return levyc(n and n or 6) end - -function demo3() - local rdsd = sqrt(2)/2 - local ubox = rect(0, 0, 1, 1) - local cf - - local function pitag_tree(pl, x, y, th, ll, depth) - local col = cf(depth) - pl:add(ubox, col, {}, {{'translate', x= x, y= y}, - {'rotate', angle= th}, - {'scale', ll}}) - if depth > 0 then - x, y = x - ll*sin(th), y + ll*cos(th) - pitag_tree(pl, x, y, th + pi/4, ll*rdsd, depth-1) - x, y = x + ll*rdsd*cos(th+pi/4), y + ll*rdsd*sin(th+pi/4) - pitag_tree(pl, x, y, th - pi/4, ll*rdsd, depth-1) - end - end - - local depth = 12 - local cfgen = color_function('darkgreen', 1) - cf = |d| cfgen(1-d/depth) - local pl = plot() - pl.units = false - pitag_tree(pl, 0, 0, 0, 1, depth) - pl:show() -end - -function demo3bis(n) - n = n and n or 10 - local rdsd = sqrt(2)/2 - local m = new(2^(n+1)-1, 4) - - local function pitag_tree(x, y, th, ll, k, depth) - m:set(k, 1, x) - m:set(k, 2, y) - m:set(k, 3, th) - m:set(k, 4, depth) - if depth > 0 then - x, y = x - ll*sin(th), y + ll*cos(th) - k = pitag_tree(x, y, th + pi/4, ll*rdsd, k+1, depth-1) - x, y = x + ll*rdsd*cos(th+pi/4), y + ll*rdsd*sin(th+pi/4) - k = pitag_tree(x, y, th - pi/4, ll*rdsd, k+1, depth-1) - end - return k - end - - pitag_tree(0, 0, 0, 1, 1, n) - - local cfgen = color_function('darkgreen', 1) - - local pl = plot() - pl:show() - - for d=n, 0, -1 do - local dfact = rdsd^(n-d) - local box = rect(0, 0, dfact, dfact) - for k=1, 2^(n+1)-1 do - if m:get(k,4) == d then - local x, y, th = m:get(k,1), m:get(k,2), m:get(k,3) - local tr = {{'translate', x=x, y=y}, {'rotate', angle=th}} - pl:add(box, cfgen(1-d/n), {}, tr) - pl:add(box, cfgen(1-(d-1)/n), {{'stroke', width= 2.5*dfact}}, tr) - -- pl:addline(box, cfgen(1-(d-1)/n), {}, tr) - end - end - end -end - -function demo3ter(n) - n = n and n or 10 - local cf - - local function pitag_tree(pl, x, y, th, ll, depth) - local box = rect(0, 0, ll, ll) - local col = cf(depth) - pl:add(box, col, {}, {{'translate', x= x, y= y}, {'rotate', angle= th}}) - if depth > 0 then - x, y = x - ll*sin(th), y + ll*cos(th) - local a1 = th + atan2(12,16) - pitag_tree(pl, x, y, a1, ll*4/5, depth-1) - x, y = x + ll*4/5*cos(a1), y + ll*4/5*sin(a1) - pitag_tree(pl, x, y, th + atan2(-12,9), ll*3/5, depth-1) - end - end - - local cfgen = color_function('darkgreen', 1) - cf = |d| cfgen(1-d/n) - local pl = plot() - pl.units = false - pl:show() - pitag_tree(pl, 0, 0, 0, 1, n) -end - -function demo4(n) - local ubox = rect(0, 0, 1, 1) - n = n and n or 10 - local col, coln - - local function pitag_tree(pl, x, y, th, ll, depth) - if depth == 0 then - local tr = {{'translate', x= x, y= y}, {'rotate', angle= th}, - {'scale', ll}} - pl:add(ubox, col, {}, tr) - pl:add(ubox, coln, {{'stroke', width= 2.5*ll}}, tr) - end - if depth > 0 then - x, y = x - ll*sin(th), y + ll*cos(th) - local a1 = th + atan2(12,16) - pitag_tree(pl, x, y, a1, ll*4/5, depth-1) - x, y = x + ll*4/5*cos(a1), y + ll*4/5*sin(a1) - pitag_tree(pl, x, y, th + atan2(-12,9), ll*3/5, depth-1) - end - end - - local cfgen = color_function('darkgreen', 1) - - local pl = plot() - pl.sync = false - pl.clip = false - pl:show() - - for k=0, n do - col, coln = cfgen(k/n), cfgen((k+1)/n) - pitag_tree(pl, 0, 0, 0, 1, k) - pl:flush() - end -end - -function demo3q() - local cf - local llmt = 0.05 - local ta1, ta2 = atan2(12,16), atan2(-12,9) - - local function pitag_tree(pl, x, y, th, ll, k) - if ll < llmt then return end - - local box = rect(0, 0, ll, ll) - local tr = {{'translate', x= x, y= y}, {'rotate', angle= th}} - pl:add(box, cf(k), {}, tr) - pl:add(box, cf(k+1), {{'stroke', width= 2.5*ll}}, tr) - - x, y = x - ll*sin(th), y + ll*cos(th) - local a1 = th + ta1 - pitag_tree(pl, x, y, a1, ll*4/5, k+1) - x, y = x + ll*4/5*cos(a1), y + ll*4/5*sin(a1) - pitag_tree(pl, x, y, th + ta2, ll*3/5, k+1) - end - - local cfgen = color_function('darkgreen', 1) - - local pl = plot() - pl.units = false - pl:show() - - local n = ceil(log(llmt)/log(4/5)) - - cf = |k| cfgen(k/n) - pitag_tree(pl, 0, 0, 0, 1, 0) -end - -echo 'demo1() - Von Koch\'s curve' -echo 'demo2() - Levy\'s C curve' -echo 'demo3() - Pythagorean Tree (symmetric)' -echo 'demo4() - Pythagorean Tree (asymmetric)' diff --git a/examples/interp.lua b/examples/interp.lua deleted file mode 100644 index d0226e50..00000000 --- a/examples/interp.lua +++ /dev/null @@ -1,53 +0,0 @@ -use 'stdlib' - -function demo1() - - local w = window('v..') - local N = 8 - local xsmp = |k| 2*pi*(k-1)/N - local x, y = new(N+1, 1, xsmp), new(N+1, 1, |k| sin(xsmp(k))) - - local function interp_plot(tp) - local p = plot(string.format('%s interpolation', tp)) - p:add(xyline(x, y), 'black', {{'marker', size=5}}) - local ap = interp(x, y, tp) - p:addline(fxline(|x| ap:eval(x), 0, 2*pi), 'blue', {{'dash', 7, 3, 3, 3}}) - p:addline(fxline(|x| ap:deriv(x), 0, 2*pi)) - p:add(fxline(cos, 0, 2*pi), 'blue', {{'stroke', width=0.75}, {'dash', 7,3}}) - print(string.format('%s interp / integral between (%g, %g) = %g', tp, - 0, 2*pi, ap:integ(0, 2*pi))) - return p - end - - - local p = plot 'Akima Interpolation' - w:attach(interp_plot('akima'), '1') - w:attach(interp_plot('cspline'), '2') -end - -function demo2() - local N = 12 - local r = rng() - local xlmt = 5 - local xp = 0 - local gp = function() - local xn = xp - xp = xp + xlmt*(1 + rnd.gaussian(r, 0.3))/N - return xn - end - local x = new(N, 1, gp) - local y = new(N, 1, |k| x[k]^2*exp(-x[k])) - local p = plot 'Cubic Spline Interpolation' - p:show() - p:addline(xyline(x, y)) - p:add(xyline(x, y), 'black', {{'marker', size= 5}}) - local ap = interp(x, y, 'cspline') - local a, b = 0, x[N] - p:addline(fxline(|x| ap:eval(x), a, b), 'blue', {{'dash', 7, 3, 3, 3}}) - p:addline(fxline(|x| ap:deriv(x), a, b), 'green') - print(string.format('Integral between (%g, %g) = %g', a, b, ap:integ(a, b))) - return p, ap -end - -echo 'demo1() - Akima interpolation of simple sine data' -echo 'demo2() - Cubic Spline interpolation of a function with random sampling' diff --git a/examples/minimize.lua b/examples/minimize.lua deleted file mode 100644 index 2f33d1ac..00000000 --- a/examples/minimize.lua +++ /dev/null @@ -1,104 +0,0 @@ - -use 'stdlib' - -require 'contour' - -f = function(x, g) - local xc = vector {4.45, -1.2} - local y = x - xc - if g then set(g, 2*y) end - return prod(y, y)[1] - end - - -fex = function(x, g) - local x1, x2 = x[1], x[2] - local z = 4*x1^2 + 2*x2^2 + 4*x1*x2 + 2*x2 + 1 - local e = exp(x1) - if g then - g:set(1,1, e * (z + 8*x1 + 4*x2)) - g:set(2,1, e * (4*x2 + 4*x1 + 2)) - end - return e * z - end - -frosenbrock = function(x, g) - local x, y = x[1], x[2] - local v = 100*(y-x^2)^2 + (1-x)^2 - if (g) then - g:set(1,1, -4*100*(y-x^2)*x - 2*(1-x)) - g:set(2,1, 2*100*(y-x^2)) - end - return v - end - -local function cook(f) - local p = new(2,1) - return function(x,y) - p:set(1,1, x) - p:set(2,1, y) - return f(p) - end -end - -function contour_minimize(f, m, x1, y1, x2, y2, contour_options) - local p = contour(cook(f), x1, y1, x2, y2, contour_options) - local c = path(m.x[1], m.x[2]) - local cx, cy = m.x[1], m.x[2] - while m:step() == 'continue' do - if cx ~= m.x[1] or cy ~= m.x[2] then - c:line_to(m.x[1], m.x[2]) - cx, cy = m.x[1], m.x[2] - end - end - c:line_to(m.x[1], m.x[2]) - print(m.x[1], m.x[2], m.value) - - p:addline(c, 'black', {{'marker', size=5}}) - p:addline(c, 'red') - return p -end - -function demo1() - local w = window('v..') - - local x0 = vector {-1.2, 1.0} - local m = fmultimin(frosenbrock, 2) - m:set(x0, vector {0.5, 0.5}, 0.01^2/2) - - local copts = {levels= 12, show= false} - local p1 = contour_minimize(frosenbrock, m, -1.5, -0.5, 1.5, 2, copts) - p1.title = 'Rosenbrock function minimisation, algorithm w/o derivates' - w:attach(p1, 1) - - local m = fdfmultimin(frosenbrock, 2, "bfgs") - m:set(x0, 0.5) - - local p2 = contour_minimize(frosenbrock, m, -1.5, -0.5, 1.5, 2, copts) - p2.title = 'Rosenbrock function minimisation, BFGS solver' - w:attach(p2, 2) -end - -function demo2() - local x0 = vector {-1.2, 1.0} - local m = fmultimin(f, 2) - m:set(x0, vector {1, 1}, 0.01) - local p = contour_minimize(f, m, -2, -3, 8, 2) - p.title = 'Quadratic function minimisation' - return p -end - - -function demo3() - local x0 = vector {-0.5, 1.0} - local m = fdfmultimin(fex, 2, "bfgs") - m:set(x0, 0.5) - local p = contour_minimize(fex, m, -2, -2.5, 1, 1.5, - {gridx=30, gridy= 30, levels= 22}) - p.title = 'f(x,y) = exp(x) * (4 x^2 + 2 y^2 + 4 x y + 2 y + 1)' - return p -end - -echo "demo1() - minimization of Rosenbrock function with different algorithms" -echo "demo2() - minimization of quadratic function without derivates" -echo "demo3() - function minimization using BFGS solver" diff --git a/examples/optical-disp.lua b/examples/optical-disp.lua deleted file mode 100644 index 208145fa..00000000 --- a/examples/optical-disp.lua +++ /dev/null @@ -1,101 +0,0 @@ - -require 'disp' - -datadir = 'packages/optical-disp' - -local function dispfit(ref, m, ps, p0) - local nb = #ref - local function fithook(x, f, J) - for j = 1, #ps do m:apply(ps[j], x[j]) end - for k = 1, nb do - local lam, n = ref:sample(k) - if f then - local z = m:get_n(lam) - n - f:set(k, 1, z) - end - if J then - for j = 1, #ps do - local z = m:get_n_deriv(ps[j], lam) - J:set(k, j, z) - end - end - end - end - return cnlfsolver {fdf= fithook, n= nb, p0= vector(p0)} -end - -local function plotcfr(ref, m, title, get) - get = get and get or real - local lnr, lnm - for lam, nref in ref:samples() do - if not lnr then - lnr, lnm = path(lam, get(nref)), path(lam, get(m:get_n(lam))) - else - lnr:line_to(lam, get(nref)) - lnm:line_to(lam, get(m:get_n(lam))) - end - end - local pn = plot(title) - pn:addline(lnr, 'blue', {{'marker', size=5}}) - pn:addline(lnm, 'red', {{'dash', a=7, b=3}}) - pn:show() - return pn -end - -function demo1() - -- ho fit for silicon oxyde - local oxho = disp.ho {{nosc= 145, en= 15.78}} - - local ps, p0 = {'nosc:0', 'en:0'}, {170, 15.78} - - local sio2 = disp.load_nk (datadir .. '/thermal-sio2.nk') - - local fit = dispfit(sio2, oxho, ps, p0) - - fit:run() - - print('Resulting parameters:', tr(fit.p)) - print('Chi square:', fit.f:norm() / #sio2) - - plotcfr(sio2, oxho, 'Fit result, n vs wavelngth') -end - -function demo2() - -- ho fit for crystalline silicon - - local siho = disp.ho {{nosc= 186.5385, en= 10.4963, eg= 1.1442, phi= 0.0532}, - {nosc= 0.1788, en= 3.3766, eg= 0.5510, phi= -8.1241}, - {nosc= 0.6134, en= 4.7829, eg= 0.6031, phi= -6.5940}, - {nosc= 1.0210, en= 4.9724, eg= 0.6961, phi= -1.9735}, - {nosc= 0.5096, en= 3.3572, eg= 2.2388, phi= 4.7374}, - {nosc= 0.05, en= 5.45, eg= 0.17, phi= 4.2} - } - - local ps = {'nosc:0', - 'nosc:1', 'en:1', 'eg:1', 'phi:1', - 'nosc:2', 'en:2', 'eg:2', 'phi:2', - 'nosc:3', 'en:3', 'eg:3', 'phi:3', - 'nosc:4', 'en:4', 'eg:4', 'phi:4', - 'nosc:5', 'en:5', 'eg:5', 'phi:5'} - local p0 = {186, - 0.18, 3.37, 0.5, -8.12, - 0.6, 4.78, 0.55, -6.59, - 1.0, 4.972, 0.69, -1.97, - 0.5, 3.357, 2.23, 4.73, - 0.05, 5.45, 0.07, 4.2} - - local si_w = disp.load_nk (datadir .. '/si-woollam-b.nk') - - local fit = dispfit(si_w, siho, ps, p0) - - print('Initial Chi square:', fit.f:norm() / #si_w) - - fit:run(200) - - print('Resulting parameters:', tr(fit.p)) - print('Final Chi square:', fit.f:norm() / #si_w) - - plotcfr(si_w, siho, 'Fit result, n vs wavelngth') - plotcfr(si_w, siho, 'Fit result, k vs wavelngth', |z| -imag(z)) -end - diff --git a/examples/pre3d.lua b/examples/pre3d.lua deleted file mode 100644 index 31d38eef..00000000 --- a/examples/pre3d.lua +++ /dev/null @@ -1,195 +0,0 @@ - -use 'stdlib' - -local Pre3d = require 'pre3d/pre3d' -local ShapeUtils = require 'pre3d/pre3d_shape_utils' - -local function setTransform(ct, rx, ry, dz) - ct:reset() - ct:rotateZ(0) - ct:rotateY(ry) - ct:rotateX(rx) - ct:translate(0, 0, dz and -dz or -80) -end - -local function draw(renderer, shape) - renderer:bufferShape(shape) - renderer:drawBuffer() - renderer:emptyBuffer() -end - -function star(r) - local a, ae = 54*pi/180, 72*pi/180 - local li, hi = r*cos(a), r*sin(a) - local he = li*tan(ae) - local xv, yv = 0, - hi - he - local xb, yb = - li, - hi - local p = path(xv, yv) - p:line_to(xb, yb) - for k=1, 4 do - local th = 2*pi*k/5 - p:line_to(xv*cos(th) + yv*sin(th), yv*cos(th) - xv*sin(th)) - p:line_to(xb*cos(th) + yb*sin(th), yb*cos(th) - xb*sin(th)) - end - p:close() - return p -end - -function demo1() - local win = window() - win:layout('v(h..).') - - p1 = plot 'sin' - p1:addline(fxline(sin, 0, 2*pi), 'blue') - p2 = plot 'cos' - p2:addline(fxline(cos, 0, 2*pi), 'green') - - win:attach(p1, '2') - win:attach(p2, '1,1') - - local plt = canvas('Pre3D') - local a = 0.6 - plt:limits(-a, -a, a, a) - local r = rng.new() - local darkgray = graph.rgb(0.9, 0.9, 0.9) - local lightgray = graph.rgb(0.6, 0.6, 0.6) - for k = 1, 50 do - local x, y = (2*r:get()-1)*a*0.8, (2*r:get()-1)*a*0.8 - local d = rnd.gaussian(r, 0.015*a) + 0.03*a - local s = star(d) - plt:add(s, darkgray, {}, {{'translate', x=x, y=y}}) - plt:addline(s, lightgray, {}, {{'translate', x=x, y=y}}) - end - plt.units = false - plt.sync = false - plt:pushlayer() - - win:attach(plt, '1,2') - - local renderer = Pre3d.Renderer(plt) - -- shape = ShapeUtils.makeSphere(1, 12, 12) - local shape = ShapeUtils.makeOctahedron() - - ShapeUtils.linearSubdivideTri(shape) - ShapeUtils.forEachVertex(shape, - function(v, i, s) - -- TODO(deanm): inplace. - s.vertices[i] = Pre3d.Math.unitVector3d(v) - return false - end - ) - -- We need to rebuild the normals after extruding the vertices. - ShapeUtils.rebuildMeta(shape) - - renderer.draw_overdraw = false - renderer.draw_backfaces = false - renderer.fill_rgba = rgb(0x4A/255, 0x92/255, 0xBF/255) - renderer.fill_rgba_backside = rgb(0xBF/255, 0x92/255, 0x4A/255) - renderer.set_light_intensity = true - -- renderer.fill_rgba_alpha = 0.95 - renderer.stroke_rgba = rgb(0x66/255, 0x66/255, 0x66/255) - - renderer.camera.focal_length = 30; - - local N, tour = 256, 2*pi - for j=0, N do - local a = tour*j/N - setTransform(renderer.camera.transform, a, 0.15 * a) - draw(renderer, shape) - end -end - - -function demo2() - local win = window() - local plt = canvas 'Rotating Sphere' - plt:limits(-1, -1, 1, 1) - plt.sync = false - win:attach(plt, '') - - local renderer = Pre3d.Renderer(plt) - local shape = ShapeUtils.makeSphere(1, 12, 12) - - renderer.draw_overdraw = true - renderer.draw_backfaces = false - renderer.fill_rgba = rgb(0x4A/255, 0x92/255, 0xBF/255) - renderer.fill_rgba_backside = rgb(0xBF/255, 0x92/255, 0x4A/255) - renderer.set_light_intensity = true --- renderer.fill_rgba_alpha = 0.95 --- renderer.stroke_rgba = rgb(0x66/255, 0x66/255, 0x66/255) - - renderer.camera.focal_length = 40; - - local N, tour = 256, 2*pi - for j=0, N do - local a = tour*j/N - setTransform(renderer.camera.transform, a, 0.15 * a) - draw(renderer, shape) - end -end - -function demo3() - local win = window() - local plt = canvas 'Moebius strip' - plt:limits(-1, -1, 1, 1) - plt.sync = false - win:attach(plt, '') - - local renderer = Pre3d.Renderer(plt) - local x = |u,v| (1 + 1/2 * v *cos(u/2))*cos(u) - local y = |u,v| (1 + 1/2 * v *cos(u/2))*sin(u) - local z = |u,v| 1/2 * v * sin(u/2) - local shape = ShapeUtils.makeUVSurface(y, z, x, 0, -1, 2*pi, 1, 60, 4) - - renderer.draw_overdraw = false - renderer.draw_backfaces = true - renderer.fill_rgba = rgb(0x4A/255, 0x92/255, 0xBF/255) - renderer.fill_rgba_backside = rgb(0xBF/255, 0x92/255, 0x4A/255) - renderer.set_light_intensity = true - renderer.draw_overdraw = true - renderer.stroke_rgba = rgb(0.2, 0.2, 0.2) - - renderer.camera.focal_length = 40; - - local N, tour = 256, 2*pi - for j=0, N do - local a = tour*j/N - setTransform(renderer.camera.transform, -a, -0.15*a) - draw(renderer, shape) - end -end - -function demo4() - local win = window() - local plt = canvas 'Pre3D' - plt:limits(-0.1, -0.1, 0.5, 0.5) - plt.sync = false - win:attach(plt, '') - - local renderer = Pre3d.Renderer(plt) - local shape = ShapeUtils.makeXYFunction(|x,y| 1.2*exp(-x^2-y^2), -2, -2, 2, 2, 20, 20) - - renderer.draw_overdraw = false - renderer.draw_backfaces = true - renderer.fill_rgba = rgb(0x4A/255, 0x92/255, 0xBF/255) - renderer.fill_rgba_backside = rgb(0xBF/255, 0x92/255, 0x4A/255) - renderer.set_light_intensity = true - renderer.draw_overdraw = true --- renderer.stroke_rgba = rgb(0x66/255, 0x66/255, 0x66/255) - - renderer.camera.focal_length = 30; - - local N, tour = 256, 2*pi - for j=0, N do - local a = tour*j/N - setTransform(renderer.camera.transform, -a, -0.15*a) - draw(renderer, shape) - end -end - - -echo([[ -demo1() - multiple plots window with 3D animations on a fixed background -demo2() - rotating sphere -demo3() - rotating Moebius strip -demo4() - rotating surface from (x,y) function / Warning: memory & CPU hungry]]) diff --git a/examples/qag-bessel.lua b/examples/qag-bessel.lua deleted file mode 100644 index 4ed5261f..00000000 --- a/examples/qag-bessel.lua +++ /dev/null @@ -1,37 +0,0 @@ -use 'math' - -local template = require 'template' -local qag = template.load('qag', {limit=64, order=21}) -local qng = template.load('qng', {}) - -local epsabs, epsrel = 1e-6, 0.01 - -function bessel_gen(n, q) - local xs - local fint = function(t) return cos(n*t - xs*sin(t)) end - return function(x) - xs = x - return q(fint, 0, pi, epsabs, epsrel) / pi - end -end - -local J4 = bessel_gen(4, qag) -local J4b = bessel_gen(4, qng) - -w = graph.window 'v..' - -p1 = graph.plot('J4 bessel function') -p2 = graph.plot('J4 bessel function') -p1:addline(graph.fxline(J4, 0, 30*pi), 'red') -p2:addline(graph.fxline(J4b, 0, 8*pi), 'blue') -w:attach(p1, 1) -w:attach(p2, 2) - -local xold, xsmp = 0, 1 -for x=0, 12*pi, 0.001 do - local y = J4(x) - if x - xold > xsmp and y > -1 then - print(y) - xold = x - end -end diff --git a/examples/qr-lssolve.lua b/examples/qr-lssolve.lua deleted file mode 100644 index e707f627..00000000 --- a/examples/qr-lssolve.lua +++ /dev/null @@ -1,28 +0,0 @@ - -function demo1() - local x0, x1, n = 0, 12.5, 32 - local a, b = 0.55, -2.4 - local xsmp = |i| (i-1)/(n-1) * x1 - - local r = rng() - local x = new(n, 1, xsmp) - local y = new(n, 1, |i| a*xsmp(i) + b + rnd.gaussian(r, 0.4)) - - X = new(n,2, |i,j| j == 1 and a or b * xsmp(i)) - - qr = QR(X) - - xls, res = qr:lssolve(y) - - print('Linear fit coefficients: ', tr(xls)) - - p = plot() - p:addline(xyline(x, X * xls)) - p:add(xyline(x, y), 'blue', {{'stroke'}, {'marker', size=5}}) - p.title = 'Linear Fit' - p:show() - - return p -end - -echo 'demo1() - examples of linear regression using QR decomposition' diff --git a/examples/randist.lua b/examples/randist.lua deleted file mode 100644 index b89b998c..00000000 --- a/examples/randist.lua +++ /dev/null @@ -1,17 +0,0 @@ - -demo1 = - function() - local a, sig, n = 2, 2.44, 40 - local idx = |k| a + (k-1)*5*sig/n -- sampling function - local pdf = |x| pdf.gaussian_tail(x, a, sig) - local cdf = |x| cdf.gaussian_tail(x, a, sig) - local cdftest = |x| integ {f= pdf, points= {a, x}} - local p = plot 'Gaussian tail pdf / cdf' - p:addline(fxline(pdf, a, 5*sig)) - p:addline(fxline(cdf, a, 5*sig), 'blue') - p:addline(fxline(cdftest, a, 5*sig), 'green', {{'dash', 5, 5}}) - p:show() - return p - end - -print 'demo1() - gaussian tail distribution pdf and cdf plot' diff --git a/examples/sige.lua b/examples/sige.lua deleted file mode 100644 index d50344cb..00000000 --- a/examples/sige.lua +++ /dev/null @@ -1,128 +0,0 @@ - -function read_data(filename) - local f = io.open(filename, "r") - local t = {} - for ln in f:lines() do - local a, b = string.match(ln, "([0-9%.]+)%s*,%s*([0-9%.]+)") - t[#t+1] = {tonumber(a), tonumber(b)} - end - return matrix(t) -end - -function step_func(brs) - return function(x) - for p in brs:rows() do - if p:get(1,1) > x then return p:get(1,2) end - end - error 'x out of range in step function' - end -end - -function step_func_gauss(brs, sg) - return function(x) - local xinf, xsup = x - 5*sg, x + 5*sg - local xc = xinf - local parz = 0 - for p in brs:rows() do - local xi, yi = p:get(1,1), p:get(1,2) - if xi > xc then - local del = cdf.gaussian(xi-x, sg) - cdf.gaussian(xc-x, sg) - parz = parz + yi * del - end - xc = xi - if xc > xsup then break end - end - return parz - end -end - -dt = read_data('examples/data/sige-sims-prof.csv') -brs, sg0 = matrix {{2, 0}, {4, 36}, {18,32}, {100, 0}}, 0.8 - -mysf = step_func(brs) -mycf = step_func_gauss(brs, sg0) - -p = canvas 'SiGe Ge% profile' -p.units = true -p:limits(0, 0, 30, 44) -p:addline(xyline(dt:col(1), dt:col(2))) -p:show() -p.sync = false -p:pushlayer() - -function apply_param(brs, xp) - brs:set(1,1, xp[1]) - brs:set(2,1, xp[2]) - brs:set(2,2, xp[3]) - brs:set(3,1, xp[4]) - brs:set(3,2, xp[5]) -end - -function minf_gener(lbrs) - local lbrs = copy(lbrs) - local sigma - local function compute(xp, g) - if g then error 'cannot calc gradient' end - - if xp[1] > xp[2] or xp[2] > xp[4] then - return 0/0 - end - - if xp[3] < 0 or xp[5] < 0 or xp[3] > 42 then - return 0/0 - end - - apply_param(lbrs, xp) - sigma = xp[6] - local mycf = step_func_gauss(lbrs, xp[6]) - - local resid = 0 - for r in dt:rows() do - local xi, yi = r:get(1,1), r:get(1,2) - resid = resid + (yi - mycf(xi))^2 - end - - return resid - end - local function coeffs() - return lbrs, sigma - end - return compute, coeffs -end - -function do_minimize() - local f, coeffs = minf_gener(brs) - local m = fmultimin(f, 6) - - local x0 = vector {2, 4, 36, 18, 32, 0.8} - local dx = vector {0.8, 0.8, 6, 2, 6, 0.1} - - m:set(x0, dx, 0.3) - - while m:step() == 'continue' do - print(tr(m.x), m.value) - - p:clear() - local obrs, sg = coeffs() - local sf = step_func(obrs) - local cf = step_func_gauss(obrs, sg) - p:addline(fxline(sf, 0, 27), "green") - p:addline(fxline(cf, 0, 27), "blue") - p:flush() - end - - print('SUCCESS:', tr(m.x), m.value) - - return m.x -end - -xopt = do_minimize() --- xopt = vector {1.476, 2.66143, 42, 18.4212, 31.5867, 39.7966, 0.604194} - -apply_param(brs, xopt) - -p = plot'SiGe profile from SIMS data' -p:addline(xyline(dt:col(1), dt:col(2))) -p:addline(fxline(step_func(brs), 0, 27), "blue") -p:addline(fxline(step_func_gauss(brs, xopt[6]), 0, 27), "green") -p:show() diff --git a/examples/vandermonde.lua b/examples/vandermonde.lua deleted file mode 100644 index ff25d730..00000000 --- a/examples/vandermonde.lua +++ /dev/null @@ -1,56 +0,0 @@ - -function demo1() - local n = 7 - local r = num.rng() - local x = matrix.new(n, 1, |i| i) - local y = matrix.new(n, 1, |i| r:get()) - local m = matrix.new(n, n, |i,j| x[i]^(j-1)) - local us = matrix.solve(m, y) - - local poly_eval = |x| iter.isum(|i| us[i] * x^(i-1), #us) - - local p = graph.plot() - p:addline(graph.xyline(x, y), 'blue', {{'marker', size=5}}) - p:addline(graph.fxline(poly_eval, x[1], x[#x])) - p.title = 'Polynomial interpolation' - p.clip = false - p:show() - - return p -end - -function demo2() - local n = 7 - local r = num.rng() - local x = matrix.new(n, 1, |i| i) - local y = matrix.cnew(n, 1, |i| complex.new(r:get(), r:get())) - local m = matrix.new(n, n, |i,j| x[i]^(j-1)) - local us = matrix.solve(m, y) - - local poly_eval = |x| iter.isum(|i| us[i] * x^(i-1), #us) - - local pr, pi = graph.plot(), graph.plot() - - local rit = iter.sequence(function(i) return x[i], complex.real(y[i]) end, n) - local iit = iter.sequence(function(i) return x[i], complex.imag(y[i]) end, n) - pr:addline(graph.ipath(rit), 'blue', {{'marker', size=5}}) - pi:addline(graph.ipath(iit), 'green', {{'marker', size=5}}) - - pr:addline(graph.fxline(|x| complex.real(poly_eval(x)), x[1], x[#x])) - pi:addline(graph.fxline(|x| complex.imag(poly_eval(x)), x[1], x[#x]), 'magenta') - - pr.title = 'Polynomial interpolation, real part' - pr.clip = false - - pi.title = 'Imaginary part' - pi.clip = false - - local w = graph.window 'v..' - w:attach(pr, 2) - w:attach(pi, 1) - - return pr, pi -end - -echo 'demo1() - Polynomial interpolation using Vandermonde matrix' -echo 'demo2() - Complex valued polynomial interpolation using Vandermonde matrix' diff --git a/examples/zernike.lua b/examples/zernike.lua deleted file mode 100644 index 0fb3e71a..00000000 --- a/examples/zernike.lua +++ /dev/null @@ -1,43 +0,0 @@ - -local invf = |n| n >= 0 and 1/fact(n) or 0 - -function zerR(n, m, p) - local ip, im = (n+m)/2, (n-m)/2 - local z = 0 - for k=0, im do - local f = fact(n-k) * (invf(k) * invf(ip-k) * invf(im-k)) - if f > 0 then z = z + (-1)^k * f * p^(n-2*k) end - end - return z -end - --- function bfact(n, k) --- return (k < 0 or k > n) and 0 or choose(n,k) --- end - --- function zerR(n, m, p) --- local im = (n-m)/2 --- local z = 0 --- for k=0, im do --- z = z + (-1)^k * bfact(n-k, k) * bfact(n-2*k, im-k) * p^(n-2*k) --- end --- return z --- end - -function zernicke(n, m, p, phi, even) - local pf = even and cos(m*phi) or sin(-m*phi) - return zerR(n, m, p) * pf -end - -local Nlev = 15 -local levels = ilist(|k| (k - Nlev)/Nlev, 0, 2*Nlev) -local N, M = 8, 2 -local Rcut = 0.9 - -require 'contour' -local p = polar_contour(|x,y| zernicke(N, M, sqrt(x^2+y^2), atan2(y,x)), Rcut, {gridx= 81, gridy= 81, levels= 10}) -p:addline(circle(0, 0, Rcut), 'gray') -p.title = string.format('Zernike polynomial (N=%i, M=%i)', N, M) - - --- contour(|x,y| zernicke(N, M, sqrt(x^2+y^2), atan2(y,x)), -1, -1, 1, 1, {gridx= 61, gridy= 61, levels= levels}) |