diff options
author | cloudygoose <cloudygooseg@gmail.com> | 2015-06-25 23:50:50 +0800 |
---|---|---|
committer | cloudygoose <cloudygooseg@gmail.com> | 2015-06-25 23:50:50 +0800 |
commit | d7e14b19816732d8efe6b586aaa493c2d924124c (patch) | |
tree | 98838784f721414b4e0d0dc0d6438de257a6c518 /nerv/matrix/init.lua | |
parent | d1c6d254e19ec31b501bd70f0a3adcc824ad8e56 (diff) |
[a little big]matrix dim added, now use m:dim() to determine whether vector or matrix
Diffstat (limited to 'nerv/matrix/init.lua')
-rw-r--r-- | nerv/matrix/init.lua | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/nerv/matrix/init.lua b/nerv/matrix/init.lua index 1a8925f..f230e9f 100644 --- a/nerv/matrix/init.lua +++ b/nerv/matrix/init.lua @@ -1,6 +1,7 @@ function nerv.Matrix:__tostring__() local ncol = self:ncol() local nrow = self:nrow() + local dim = self:dim() local strt = {} local fmt if self.fmt then @@ -8,12 +9,7 @@ function nerv.Matrix:__tostring__() else fmt = "%.8f " end - if nrow == 1 then - for col = 0, ncol - 1 do - table.insert(strt, string.format(fmt, self[col])) - end - table.insert(strt, "\n") - else + if (dim == 2) then for row = 0, nrow - 1 do local rp = self[row] for col = 0, ncol - 1 do @@ -21,6 +17,11 @@ function nerv.Matrix:__tostring__() end table.insert(strt, "\n") end + else + for col = 0, ncol - 1 do + table.insert(strt, string.format(fmt, self[col])) + end + table.insert(strt, "\n") end table.insert(strt, string.format( "[%s %d x %d]", self.__typename, nrow, ncol)) @@ -28,19 +29,19 @@ function nerv.Matrix:__tostring__() end -- gen: a function takes take indices of the matrix and return the generated --- all entrys in the matrix will be assigned by calling gen(i, j) +-- all entrys in the matrix will be assigned by calling gen(i, j), for a vector, gen(j) will be called. function nerv.Matrix:generate(gen) - if (self:nrow() == 1) then - for j = 0, self:ncol() - 1 do - self[j] = gen(j) - end - else + if (self:dim() == 2) then for i = 0, self:nrow() - 1 do local row = self[i] for j = 0, self:ncol() - 1 do row[j] = gen(i, j) end end + else + for j = 0, self:ncol() - 1 do + self[j] = gen(j) + end end end |