author | Francesco Abbate <francesco.bbt@gmail.com> | 2010年01月02日 21:39:18 +0000 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2010年01月02日 21:39:18 +0000 |
commit | 8e38a45bad58c0e8f53ed76e8bf6f8bf1bd7044f (patch) | |
tree | 9645747e390810e4a77e403b0a35bc0192b1a6ad /examples | |
parent | 0480a5aa50ce6e588621e88545671f200196fc55 (diff) | |
download | gsl-shell-8e38a45bad58c0e8f53ed76e8bf6f8bf1bd7044f.tar.gz |
-rw-r--r-- | examples/fft.lua | 16 | ||||
-rw-r--r-- | examples/fractals.lua | 5 | ||||
-rw-r--r-- | examples/hermite.lua | 27 | ||||
-rw-r--r-- | examples/nlinfit.lua | 53 | ||||
-rw-r--r-- | examples/ode-example.lua | 44 | ||||
-rw-r--r-- | examples/plot.lua | 31 |
diff --git a/examples/fft.lua b/examples/fft.lua index 4cde48cf..dd510f34 100644 --- a/examples/fft.lua +++ b/examples/fft.lua @@ -6,21 +6,15 @@ function demo1() local ncut = 16 local sq = new(n, 1, |i| i < n/3 and 0 or (i < 2*n/3 and 1 or 0)) - local sqf = sq:copy() - - local fmgeti = function(m, i) - local v = m:get(i+1,1) - if v then return i+1, i+1, v end - end local p = plot() - p:add_line(igpath(fmgeti, sq, 0), 'black') + p:add_line(ipath(iter(function(i) return i, sq:get(i,1) end, 1, n)), 'black') - fft(sqf) - for k=ncut, n/2 do sqf:set(k,0) end - fft_inv(sqf) + fft(sq) + for k=ncut, n/2 do sq:set(k,0) end + fft_inv(sq) - p:add_line(igpath(fmgeti, sqf, 0), 'red') + p:add_line(ipath(iter(function(i) return i, sq:get(i,1) end, 1, n)), 'red') p:show() return p diff --git a/examples/fractals.lua b/examples/fractals.lua index a2236e80..4543a203 100644 --- a/examples/fractals.lua +++ b/examples/fractals.lua @@ -44,8 +44,9 @@ end function levyc(n) local p = plot() local c = ipath(c_generator(n, 4, 1/2, {-1,0,0,1})) - p:add(c, 'red', {{'stroke'}}, {{'rotate', angle= -pi/4}}) - p:add(c, 'red', {{'stroke'}}, {{'translate', x=1/sqrt(2), y=-1/sqrt(2)},{'rotate', angle= pi/4}}) + p:add(c, 'red', {{'stroke', width=0.7}}, {{'rotate', angle= -pi/4}}) + p:add(c, 'red', {{'stroke', width=0.7}}, + {{'translate', x=1/sqrt(2), y=-1/sqrt(2)},{'rotate', angle= pi/4}}) p:show() return p end diff --git a/examples/hermite.lua b/examples/hermite.lua new file mode 100644 index 00000000..59a7bbed --- /dev/null +++ b/examples/hermite.lua @@ -0,0 +1,27 @@ +
+function hermiteLp(n,x)
+ return 1/sqrt(fact(n) *2^n*sqrt(pi)) * exp(-x*x/2) * (-4)^(n/2) * fact(n/2) * laguerre(n/2, -1/2, x^2)
+end
+
+p = fxplot(|x| hermiteLp(4, x), -12, 12, 'darkgreen')
+p:add_line(fxline(|x| hermiteLp(2, x), -12, 12), 'blue')
+p:add_line(fxline(|x| hermiteLp(50, x), -12, 12, 1024), 'magenta')
+
+function hermiteU(n,x)
+ return 1/sqrt(fact(n) *2^n*sqrt(pi)) * exp(-x*x/2) * 2^n * hypergU((1-n)/2, 3/2, x^2)
+end
+
+function hermiteFp(n,x)
+ return 1/sqrt(fact(n) *2^n*sqrt(pi)) * exp(-x*x/2) * (-1)^(n/2) * fact(n)/fact(n/2) * hyperg1F1(-n/2, 1/2, x^2)
+end
+
+
+p2 = fxplot(|x| hermiteFp(4, x), -12, 12, 'darkgreen')
+p2:add_line(fxline(|x| hermiteFp(2, x), -12, 12), 'blue')
+p2:add_line(fxline(|x| hermiteFp(50, x), -12, 12, 1024), 'magenta')
+
+
+-- p3 = plot()
+-- p3:add_line(fxline(|x| hermiteU(4, x), -12, 12), 'darkgreen')
+-- p3:add_line(fxline(|x| hermiteU(2, x), -12, 12), 'blue')
+-- p3:add_line(fxline(|x| hermiteU(50, x), -12, 12, 1024), 'magenta')
diff --git a/examples/nlinfit.lua b/examples/nlinfit.lua index 6e8a0084..83bef8e9 100644 --- a/examples/nlinfit.lua +++ b/examples/nlinfit.lua @@ -83,56 +83,3 @@ function demo2() until status ~= 'continue' print_state (s) end - -function demo2plot() - local x0, x1, n = 0, 1.5, 50 - local P = vector {1.55, -3.1} - local model = - {f = |p, x| p[1] * exp(p[2] * x), - J = function(p, x) - local e = exp(p[2] * x) - return e, x * p[1] * e - end} - local err_g = function(p) - local r = rng() - return || p[1] * 0.03 * rnd.gaussian(r) - end - local err = err_g(P) - local xs = |i| x0 + (i-1) * (x1 - x0) / n - local xy = new(n, 2, |i,j| j == 1 and xs(i) or model.f(P, xs(i))+err()) - local function expf(p, f, J) - for k=1, n do - local x = xs(k) - if f then f:set(k, 1, model.f(p, x) - xy:get(k,2)) end - if J then - local d1, d2 = model.J(p, x) - J:set(k, 1, d1) - J:set(k, 2, d2) - end - end - end - local function print_state(s) - print ("x: ", s.x:row_print()) - print ("chi square: ", prod(s.f, s.f)[1]) - end - s = solver {fdf= expf, n= n, p= 2, x0= vector {3.5, -2.5}} - repeat - print_state (s) - local status = s:iterate() - until status ~= 'continue' - print_state (s) - - local fmget = function(m, i) - local a, b = m:get(i+1,1), m:get(i+1,2) - if a then return i+1, a, b end - end - - pts = igpath(fmget, xy, 0) - ln_result = ipath(sample(|t| model.f(s.x, t), x0, x1, 128)) --- ln_theory = ipath(sample(|t| model.f(P, t), x0, x1, 128)) - p = plot() - p:add(pts, 'black', {{'stroke'}, {'marker', size=5}}) - p:add_line(ln_result, 'red') - p:show() - return p -end diff --git a/examples/ode-example.lua b/examples/ode-example.lua index 89061979..fa1c837f 100644 --- a/examples/ode-example.lua +++ b/examples/ode-example.lua @@ -19,6 +19,27 @@ -- require 'igsl' +require 'draw' + +function ode_lines(s, t0, y0, t1, tstep) + local r = y0:dims() + local p = {} + for k=1,r do p[k] = path(t0, y0[k]) end + for t, y in s:iter(t0, y0, t1, tstep) do + for k=1,r do p[k]:line_to(t, y[k]) end + end + return p +end + +function code_lines(s, t0, y0, t1, tstep) + local r = y0:dims() + local p = {} + for k=1,r do p[2*k-1] = path(t0, real(y0[k])); p[2*k] = path(t0, imag(y0[k])) end + for t, y in s:iter(t0, y0, t1, tstep) do + for k=1,r do p[2*k-1]:line_to(t, real(y[k])); p[2*k]:line_to(t, imag(y[k])) end + end + return p +end function demo1() local mu = 10 @@ -30,12 +51,11 @@ function demo1() local s = ode {f = odef, n= 2, eps_abs= 1e-6} - local t0, t1 = 0, 100 + local t0, t1 = 0, 50 local y0 = vector {1,0} - for t, y in s:iter(t0, y0, t1) do - print(t, y:row_print()) - end + local ln = ode_lines(s, t0, y0, t1) + return plot_lines(ln) end @@ -57,16 +77,15 @@ function demo2() local s = ode {f = odef, df= odedf, n= 2, eps_abs= 1e-6, method='bsimp'} - local t0, t1 = 0, 100 + local t0, t1 = 0, 50 local y0 = vector {1,0} - for t, y in s:iter(t0, y0, t1) do - print(t, y:row_print()) - end + local ln = ode_lines(s, t0, y0, t1) + return plot_lines(ln) end function demo3() - local m = cmatrix {{4i, 0},{-0.3, 3i}} + local m = cmatrix {{4i, 0},{-0.6, 8.2i}} local myf = function(t, y, f) set(f, cmul(m, y)) @@ -79,10 +98,9 @@ function demo3() local s = code {f= myf, df= mydf, n= 2, method='bsimp'} - local t0, t1 = 0, 5 + local t0, t1 = 0, 16 local y0 = cvector {1,0} - for t, y in s:iter(t0, y0, t1, 0.05) do - print(t, y:row_print()) - end + local ln = code_lines(s, t0, y0, t1, 0.04) + return plot_lines(ln) end diff --git a/examples/plot.lua b/examples/plot.lua index 5af5caa3..6e372863 100644 --- a/examples/plot.lua +++ b/examples/plot.lua @@ -3,10 +3,7 @@ require 'draw' function demo1() local f = |t| exp(-0.3*t) * sin(2*pi*t) - p = plot() - p:add_line(fxline(f, 0, 15, 512), 'red') - p:show() - return p + return fxplot(f, 0, 15,'red') end function demo2() @@ -37,7 +34,10 @@ function vonkoch(n) sy[k] = s * sqrt(3)/2 * sy[k] end + local first = true + return function() + if first then first = false; return x, y end if w[n+1] == 0 then x, y = x + sx[a+1], y + sy[a+1] for k=1,n+1 do @@ -53,8 +53,27 @@ function vonkoch(n) end function demo3() - pl = plot() - pl:add_line(ipath(vonkoch(4)), 'blue') + 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(vonkoch(4)) + 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:add(v, c, {{'stroke'}}) + pl:add(v, c, {{'stroke'}}, {{'translate', x=1, y=0}, {'rotate', angle=-2*pi/3}}) + pl:add(v, c, {{'stroke'}}, {{'translate', x=0.5, y=-sqrt(3)/2}, {'rotate', angle=-2*2*pi/3}}) + pl:show() return pl end |