Minor changes in contour.lua and examples. - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrancesco Abbate <francesco.bbt@gmail.com>2010年05月07日 21:07:19 +0000
committerFrancesco Abbate <francesco.bbt@gmail.com>2010年05月07日 21:07:19 +0000
commit9eac0bd04aa4ffcb3adcbde01f0443078166e73c (patch)
treee322da92e9a773a216ee764c01172e3f7744caa3
parent726be2a870d0a57d1f2ed4f6a8ba4fc8a6df4e3b (diff)
downloadgsl-shell-9eac0bd04aa4ffcb3adcbde01f0443078166e73c.tar.gz
Minor changes in contour.lua and examples.
Diffstat
-rw-r--r--contour.lua 44
-rw-r--r--draw.lua 17
-rw-r--r--examples/contour-ex.lua 72
-rw-r--r--examples/contour.lua 53
-rw-r--r--examples/hpcontour-ex.lua 10
-rw-r--r--examples/minimize.lua 2
-rw-r--r--hpcontour.lua 13
-rw-r--r--igsl.lua 7
8 files changed, 103 insertions, 115 deletions
diff --git a/contour.lua b/contour.lua
index 4c49c4ed..706ba92a 100644
--- a/contour.lua
+++ b/contour.lua
@@ -21,6 +21,15 @@
local M = {}
local insert = table.insert
+local color = color_function('redyellow', 0.8)
+
+local function reverse(ls)
+ local k, n = 1, #ls
+ while k < n do
+ ls[k], ls[n] = ls[n], ls[k]
+ k, n = k+1, n-1
+ end
+end
local function root_solve(f, z0, x, y, dx0, dy0, z_eps)
local dx, dy = dx0, dy0
@@ -511,6 +520,10 @@ local function grid_create(f, left, right, nx, ny, nlevels_or_levels)
local inner = find_inner_level(s0, level)
local status = run(curve, segment_copy(s0), si0)
if status == 'boundary' then
+ reverse(curve)
+ segment_invert(s0)
+ run(curve, s0, si0)
+
if #curve < 2 then error 'curve with points less than two points' end
local a, b = curve[1], curve[#curve]
local aid = add_node(a, id, level)
@@ -622,30 +635,10 @@ local function grid_create(f, left, right, nx, ny, nlevels_or_levels)
local function grid_find_curves()
- local function my_bord_iter(level)
- local fiter = grid_iter_intersects(level)
- return function()
- for s, si in fiter do
- local msi, lsi = bord_main_index(si)
- if msi then
- if msi >= 2 then segment_invert(s) end
- return s, si
- end
- end
- end
- end
-
for level=0, nlevels do
- for s, _ in my_bord_iter(level) do
- local id = curve_next_id()
- local curve = curve_join(s, level, id)
- curve_register(curve)
- end
-
for s, _ in grid_iter_intersects(level) do
local id = curve_next_id()
local curve = curve_join(s, level, id)
- if not curve.closed then error 'expecting a closed curve' end
curve_register(curve)
end
end
@@ -717,11 +710,6 @@ local function grid_create(f, left, right, nx, ny, nlevels_or_levels)
end
end
- local function color(a)
- -- return rgba(0.9, 0.9 - 0.9*a, 0, 0.8)
- return rgba(0.91 - 0.565*a, 0.898 - 0.753*a, 0.85 - 0.25*a, 0.8)
- end
-
local function curve_draw(pl, id)
local ln = path()
curve_add_path(ln, id, 'acw')
@@ -751,12 +739,12 @@ local function grid_create(f, left, right, nx, ny, nlevels_or_levels)
end
end
- local function grid_draw_lines(pl, color)
+ local function grid_draw_lines(pl, col)
local ln = path()
for id = 1, #curves do
curve_add_path(ln, id, 'cw')
end
- pl:addline(ln, color)
+ pl:addline(ln, col)
end
return {
@@ -765,7 +753,7 @@ local function grid_create(f, left, right, nx, ny, nlevels_or_levels)
draw_lines = grid_draw_lines}
end
-function M.plot(f, a, b, ngridx, ngridy, nlevels)
+function contour(f, a, b, ngridx, ngridy, nlevels)
ngridx = ngridx and ngridx or 40
ngridy = ngridy and ngridy or 40
nlevels = nlevels and nlevels or 10
diff --git a/draw.lua b/draw.lua
index 66f1c7cf..34ebd32d 100644
--- a/draw.lua
+++ b/draw.lua
@@ -75,6 +75,23 @@ function rainbow(n)
return mcolors[q+1] .. bcolors[(n-1) % p + 1]
end
+local color_schema = {
+ bluish = {0.91, 0.898, 0.85, 0.345, 0.145, 0.6},
+ redyellow = {0.9, 0.9, 0, 0.9, 0, 0},
+ darkgreen = {0.9, 0.9, 0, 0, 0.4, 0}
+}
+-- local s, e = {1, 1, 1}, {0.1, 0.7, 0.2}
+-- local s, e = {0.9, 0.9, 0}, {0, 0.4, 0}
+-- local s, e = {0, 0.9, 0.9}, {0, 0, 0.7}
+
+function color_function(schema, alpha)
+ local c = color_schema[schema]
+ return function(a)
+ return rgba(c[1] + a*(c[4]-c[1]),
+ c[2] + a*(c[5]-c[2]),
+ c[3] + a*(c[6]-c[3]), alpha)
+ end
+end
function plot_lines(ln)
local p = plot()
diff --git a/examples/contour-ex.lua b/examples/contour-ex.lua
deleted file mode 100644
index 3a131783..00000000
--- a/examples/contour-ex.lua
+++ /dev/null
@@ -1,72 +0,0 @@
-
-
-fex = function(x1, x2)
- return 4*x1^2 + 2*x2^2 + 4*x1*x2 + 2*x2 + 1
- end
-
-frosenbrock = function(x, y)
- return 100*(y-x^2)^2 + (1-x)^2
- end
-
-local function fnorm(x, y, s)
- local x0, y0 = s[2], s[3]
- local sx, sy = s[4], s[5]
- return exp(-(x-x0)^2/(2*sx^2) - (y-y0)^2/(2*sy^2))
-end
-
-fpeaksmake = function(ls)
- return function(x, y)
- local z = 0
- for j, s in ipairs(ls) do
- z = z + s[1] * fnorm(x, y, s)
- end
- return z
- end
- end
-
-fpeaksslopemake = function(ls, sx, sy)
- return function(x, y)
- local z = 0
- for j, s in ipairs(ls) do
- z = z + s[1] * fnorm(x, y, s) + sx*x + sy*y
- end
- return z
- end
- end
-
-flin = function(x,y) return x + 2*y end
-flinm = function(x,y) return x - 2*y end
-fsqr = function(x,y) return x^2 + y^2 end
-
-fsincos = function(sx,sy)
- return function(x,y)
- return cos(x)+cos(y) + sx*x + sy*y
- end
- end
-
--- ftwopeaks = fpeaksmake {{10, 0, 0, 1, 1}, {5, 1, 1, 0.45, 0.15}}
--- ftwopeaks = fpeaksmake {{6, 0, 0, 1, 1}, {-5, 1.5, 1, 0.45, 0.15}}
-ftwopeaks = fpeaksmake {{-6, 0, 0, 1, 1}, {5, 1.5, 1, 1.45, 1.15}}
-ftwopeaksslp = fpeaksslopemake({{6, 0, 0, 1, 1}, {-5, 1.5, 1, 1.45, 1.15}}, 0.3, 0.2)
-fthreepeaks = fpeaksmake {{6, 0, 0, 1, 1}, {-5, 1.5, 1, 1.45, 1.15}, {1.5, 2, -2, 0.5, 0.8}}
-
-contour(fex, {-2, -2.5}, {1, 1}, 30, 30, 16)
--- contour(ftwopeaksslp, {-3, -2}, {3, 2}, 40, 40, 9)
-contour(ftwopeaks, {-4, -4}, {5, 4}, 40, 40, 10)
--- contour(fthreepeaks, {-4, -4}, {5, 4}, 40, 40, 15)
-contour(fsincos(0.1,0.3), {0, 0}, {14, 14}, 40, 40, 9)
-contour(fsincos(0.1,0.3), {-14, -14}, {14, 14}, 80, 80, 8)
-contour(fsincos(0,0), {0, 0}, {14, 14})
-contour(flin, {-4, -4}, {4, 4})
-contour(fsqr, {-4, -4}, {4, 4})
--- contour(flin, {-4, -4}, {4, 4}, 8, 16, 24)
--- contour(flinm, {-4, -4}, {4, 4}, 8, 16, 24)
-
-zlev, nlev = {}, 9
-for k=1,nlev do
- table.insert(zlev, frosenbrock(1, 1 - 2 * (k/nlev)^2))
-end
-contour(frosenbrock, {-1.5, -0.5}, {1.5, 2}, 80, 80, zlev)
-
--- NICE PLOT WITH THREE PEAKS
-contour(fpeaksmake {{6, 0, 0, 1, 1}, {-5, 1.5, 1, 1.45, 1.15}, {4, 2, -2, 0.8, 0.8}}, {-2, -4}, {5, 4}, 40, 40, 9)
diff --git a/examples/contour.lua b/examples/contour.lua
new file mode 100644
index 00000000..81bbe278
--- /dev/null
+++ b/examples/contour.lua
@@ -0,0 +1,53 @@
+
+require 'contour'
+
+fex = function(x1, x2)
+ return 4*x1^2 + 2*x2^2 + 4*x1*x2 + 2*x2 + 1
+ end
+
+frosenbrock = function(x, y)
+ return 100*(y-x^2)^2 + (1-x)^2
+ end
+
+local function fnorm(x, y, s)
+ local x0, y0 = s[2], s[3]
+ local sx, sy = s[4], s[5]
+ return exp(-(x-x0)^2/(2*sx^2) - (y-y0)^2/(2*sy^2))
+end
+
+makepeaks = function(ls)
+ return function(x, y)
+ local z = 0
+ for j, s in ipairs(ls) do
+ z = z + s[1] * fnorm(x, y, s)
+ end
+ return z
+ end
+ end
+
+fsqr = function(x,y) return x^2 + y^2 end
+
+fsincos = function(sx,sy)
+ return function(x,y)
+ return cos(x)+cos(y) + sx*x + sy*y
+ end
+ end
+
+
+contour(fsqr, {-4, -4}, {4, 4})
+
+contour(fex, {-2, -2.5}, {1, 1})
+
+contour(fsincos(0.1, 0.3), {0, 0}, {4*pi, 4*pi})
+contour(fsincos(0, 0), {0, 0}, {4*pi, 4*pi})
+
+
+f3ps = makepeaks {{6, 0, 0, 1, 1},
+ {-5, 1.5, 1, 1.45, 1.15},
+ {4, 2, -2, 0.8, 0.8}}
+
+contour(f3ps, {-2, -4}, {5, 4})
+
+local N = 7
+contour(frosenbrock, {-1.5, -0.5}, {1.5, 2}, 80, 80,
+ ilist(function(k) return frosenbrock(1, 1 - 2 * (k/N)^2) end, 1, N))
diff --git a/examples/hpcontour-ex.lua b/examples/hpcontour-ex.lua
index 7623afcb..568a24a7 100644
--- a/examples/hpcontour-ex.lua
+++ b/examples/hpcontour-ex.lua
@@ -1,5 +1,5 @@
-local contour = require 'hpcontour'
+require 'hpcontour'
fex = function(x, g)
local x1, x2 = x[1], x[2]
@@ -104,12 +104,12 @@ ftwopeaks = fpeaksmake {{-6, 0, 0, 1, 1}, {5, 1.5, 1, 1.45, 1.15}}
ftwopeaksslp = fpeaksslopemake({{6, 0, 0, 1, 1}, {-5, 1.5, 1, 1.45, 1.15}}, 0.3, 0.2)
fthreepeaks = fpeaksmake {{6, 0, 0, 1, 1}, {-5, 1.5, 1, 1.45, 1.15}, {1.5, 2, -2, 0.5, 0.8}}
--- contour.plot(fex, {-2, -2.5}, {1, 0.5}, 20, 20, 25)
+hpcontour(fex, {-2, -2.5}, {1, 0.5}, 20, 20, 16)
-- contour.plot(ftwopeaksslp, {-3, -2}, {3, 2}, 40, 40, 9)
-- contour.plot(ftwopeaks, {-4, -4}, {5, 4}, 40, 40, 10)
-- contour.plot(fthreepeaks, {-4, -4}, {5, 4}, 40, 40, 15)
-- contour.plot(fsincos(0.1,0.3), {0, 0}, {14, 14}, 20, 20, 13)
-contour.plot(fsincos(0.1,0.3), {-14, -14}, {14, 14}, 20, 20, 10)
+--hpcontour(fsincos(0.1,0.3), {-14, -14}, {14, 14}, 20, 20, 10)
-- contour.plot(fsincos(0,0), {0, 0}, {4*pi, 4*pi}, 60, 60, 10)
-- contour.plot(flin, {-4, -4}, {4, 4})
-- contour.plot(fsqr, {-4, -4}, {4, 4})
@@ -122,8 +122,8 @@ for k=1,nlev do
pze:set(2,1, 1 - 2 * (k/nlev)^2)
table.insert(zlev, frosenbrock(pze))
end
-contour.plot(frosenbrock, {-1.5, -0.5}, {1.5, 2}, 20, 20, zlev)
+--contour.plot(frosenbrock, {-1.5, -0.5}, {1.5, 2}, 20, 20, zlev)
-contour.plot(fpeaksmake {{6, 0, 0, 1, 1}, {-5, 1.5, 1, 1.45, 1.15}, {4, 2, -2, 0.8, 0.8}}, {-2, -4}, {5, 4}, 20, 20, 9)
+hpcontour(fpeaksmake {{6, 0, 0, 1, 1}, {-5, 1.5, 1, 1.45, 1.15}, {4, 2, -2, 0.8, 0.8}}, {-2, -4}, {5, 4}, 20, 20, 10)
-- NICE PLOT WITH THREE PEAKS
-- contour.plot(fpeaksmake {{6, 0, 0, 1, 1}, {-5, 1.5, 1, 1.45, 1.15}, {4, 2, -2, 0.8, 0.8}}, {-4, -4}, {5, 4}, 20, 20, 9)
diff --git a/examples/minimize.lua b/examples/minimize.lua
index f6faf10e..7d02c761 100644
--- a/examples/minimize.lua
+++ b/examples/minimize.lua
@@ -1,3 +1,5 @@
+require 'contour'
+
f = function(x, g)
local xc = vector {4.45, -1.2}
local y = x - xc
diff --git a/hpcontour.lua b/hpcontour.lua
index 10e9ec76..2387bfa4 100644
--- a/hpcontour.lua
+++ b/hpcontour.lua
@@ -22,6 +22,8 @@ local plcurve = require 'plcurve'
local M = {}
+local color = color_function('redyellow', 0.8)
+
local insert = table.insert
local function reverse(ls)
@@ -886,11 +888,6 @@ local function grid_create(f, left, right, nx, ny, nlevels_or_levels)
end
end
- local function color(a)
- -- return rgba(0.9, 0.9 - 0.9*a, 0, 0.8)
- return rgba(0.91 - 0.565*a, 0.898 - 0.753*a, 0.85 - 0.25*a, 0.8)
- end
-
local function curve_draw(pl, id)
local ln = path()
curve_add_path(ln, id, 'acw')
@@ -926,12 +923,12 @@ local function grid_create(f, left, right, nx, ny, nlevels_or_levels)
end
end
- local function grid_draw_lines(pl, color)
+ local function grid_draw_lines(pl, col)
local ln = path()
for id = 1, #curves do
curve_add_path(ln, id, 'cw')
end
- pl:addline(ln, color)
+ pl:addline(ln, col)
end
return {
@@ -941,7 +938,7 @@ local function grid_create(f, left, right, nx, ny, nlevels_or_levels)
draw_lines = grid_draw_lines}
end
-function M.plot(f, a, b, ngridx, ngridy, nlevels)
+function hpcontour(f, a, b, ngridx, ngridy, nlevels)
ngridx = ngridx and ngridx or 20
ngridy = ngridy and ngridy or 20
nlevels = nlevels and nlevels or 12
diff --git a/igsl.lua b/igsl.lua
index 23f43de4..c1863149 100644
--- a/igsl.lua
+++ b/igsl.lua
@@ -288,5 +288,8 @@ function sampmodel(f, xs)
return A
end
-local _contour = require 'contour'
-contour = _contour.plot
+function ilist(f, i0, i1)
+ local ls = {}
+ for k= i0, i1 do ls[#ls+1] = f(k) end
+ return ls
+end
generated by cgit v1.2.3 (git 2.46.0) at 2025年09月29日 19:08:58 +0000

AltStyle によって変換されたページ (->オリジナル) /