author | Francesco Abbate <francesco.bbt@gmail.com> | 2012年12月17日 17:37:36 +0100 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2012年12月17日 17:37:36 +0100 |
commit | c912cffd0e942d77f998488f7b7746d0aa30f052 (patch) | |
tree | 1df71f6f82157ce82c3a287ac8a8fc813f411adb /gdt-plot.lua | |
parent | 38bac83e37c24e5be164edad90650076247b2717 (diff) | |
download | gsl-shell-c912cffd0e942d77f998488f7b7746d0aa30f052.tar.gz |
-rw-r--r-- | gdt-plot.lua | 35 |
diff --git a/gdt-plot.lua b/gdt-plot.lua index a1308c4d..8d624474 100644 --- a/gdt-plot.lua +++ b/gdt-plot.lua @@ -1,4 +1,5 @@ local concat = table.concat +local select = select local function collate(ls) return concat(ls, ' ') @@ -257,7 +258,41 @@ local function gdt_table_reduce(t_src, jxs, jys, jes) return t end +local function count_args(...) + return select('#', ...) +end + +local function set_elements(X, P, i, ...) + for k = 1, P do + local v = select(k, ...) + X:set(i, k, v) + end +end + +local function gdt_table_linfit(t, f, jy) + local N, M = t:dim() + local name = {} + for k = 1, M do + name[k] = t:get_header(k) + end + local row = {} + for k = 1, M do + row[name[k]] = t:get(1, k) + end + local P = count_args(f(row)) + + local X, Y = matrix.alloc(N, P), matrix.alloc(N, 1) + for i = 1, N do + for k = 1, M do row[name[k]] = t:get(i, k) end + set_elements(X, P, i, f(row)) + Y:set(i, 1, t:get(i, jy)) + end + + return num.linfit(X, Y) +end + gdt.barplot = gdt_table_barplot gdt.plot = gdt_table_lineplot gdt.xyplot = gdt_table_xyplot gdt.reduce = gdt_table_reduce +gdt.lm = gdt_table_linfit |