Re: [NTG-context] Simple command with variable number of arguments
Dear Luigi and listers, Last year, I need to write a text for a linear algebra class. Since there are many \startmatrix … \stopmatrix and matrix calculations, I wrote a lua code which did matrix calculations and writing with a lot of help from this list. Using the code, I can write class materials easily. It is good enough for my purpose. I am not good in Lua coding, so there may be many things to be checked for efficiency and for stability. However, I attached the code because there may be someone who need it. It is also good thing to return what I get from the list back. I hope that you enhance the code for better performance since you are an expert in Lua. I always thank to this list and to developers of ConTeXt. Best regards, Dalyoung
Message: 1 Date: Fri, 23 May 2014 13:44:30 +0200 From: luigi scarso
To: mailing list for ConTeXt users Subject: Re: [NTG-context] Simple command with variable number of arguments Message-ID: Content-Type: text/plain; charset="utf-8" On Fri, May 23, 2014 at 11:54 AM, Matthias Weber
wrote: Dear All,
I would like to define a command that expands
\vector{2,4} % or vector[2,4] if that?s easier
to
\startpmatrix \NC 2 \NR \NC 4 \NR \stoppmatrix
and more generally
\vector{2,4,1,7}
to
\startpmatrix \NC 2 \NR \NC 4 \NR \NC 1 \NR \NC 7 \NR \stoppmatrix
Any hints how to achieve this?
Thanks,
\definemathmatrix [pmatrix] [left={\left(\,},right={\,\right)}]
\startluacode document = document or {} document.matthias = document.matthias or {} local function lua_columnvector(a) context.startpmatrix() for i,v in ipairs(a) do context.NC() context(tostring(v)) context.NR() end context.stoppmatrix() end document.matthias.lua_columnvector = document.matthias.lua_columnvector or lua_columnvector \stopluacode
\def\columnvector#1{\ctxlua{document.matthias.lua_columnvector(#1)}}
\starttext \startformula \columnvector{{1,2,3}} %% watch the double { ! \stopformula
\stoptext
-- luigi
On 5/23/2014 3:48 PM, Jeong Dal wrote:
Dear Luigi and listers,
Last year, I need to write a text for a linear algebra class. Since there are many \startmatrix … \stopmatrix and matrix calculations, I wrote a lua code which did matrix calculations and writing with a lot of help from this list. Using the code, I can write class materials easily. It is good enough for my purpose. I am not good in Lua coding, so there may be many things to be checked for efficiency and for stability.
However, I attached the code because there may be someone who need it. It is also good thing to return what I get from the list back.
I hope that you enhance the code for better performance since you are an expert in Lua.
A bit more readable: matrixOP.symMatrix = function(sym, x, y, nx ,ny) -- symMatrix("a", "m", "n") local nx = nx or 2 local ny = ny or nx local function filled(i,y) local mrow = { } for j=1,nx do table.insert(mrow, string.formatters["%s_{%s%s}"](sym,i,j)) end table.insert(mrow,"\\cdots") table.insert(mrow,string.formatters["%s_{%s%s}"](sym,i,y)) return mrow end local function dummy() local mrow = { } for j=1,nx do table.insert(mrow,"\\vdots") end table.insert(mrow,"\\ddots") table.insert(mrow,"\\vdots") return mrow end -- local mm = { } for i=1,ny do table.insert(mm,filled(i,y)) end table.insert(mm,dummy()) table.insert(mm,filled(x,y)) return mm end Also, variable nx, ny: \ctxlua{matrixOP.write(matrixOP.symMatrix("a","m","n",4,8))} Maybe this too: local default = { left = [[\left(\,]], right = [[\,\right)]], } matrixOP.write = function (t) context.startmatrix(default) for _, r in ipairs(t) do for _, c in ipairs(r) do context.NC(c) end context.NR() end context.stopmatrix() end matrixOP.writeDet = function(t) context.startmatrix(default) for _, r in ipairs(t) do for _, c in ipairs(r) do context.NC(c) end context.NR() end context.stopmatrix() end And: matrixOP.rowMult = function(t, i, m) -- replace i-th row with m*(i-th row) for k = 1, #t[i] do t[i][k] = m*t[i][k] end end matrixOP.rowMultSum = function(t, i, j, m) -- replace i-th row with i-th row + m*(j-th row) for k = 1, #t[1] do t[i][k] = t[i][k] + m*t[j][k] end end (no need for further speedup as it't non-critical code with neglectable runtime) More Luaish (local usage): matrixOP.transpose = function(m) -- transpose of a matrix local mT={} for j = 1, #m[1] do local mTrow={} for i=1, #m do local temp = m[i][j] table.insert(mTrow, temp) end table.insert(mT, mTrow) end return mT end Don't forget the local here: matrixOP.inner = function (u, v) -- inner product of two vectors local temp=0 if #u == #v then for i=1, #u do temp = temp + u[i]*v[i] end end return temp end and here: matrixOP.product = function(m1, m2) -- product of two matrices local m3={} if #m1[1] == #m2 then for i = 1, #m1 do local mrow={} for j=1, #m2[1] do local temp = 0 for k=1, #m1[1] do u = m1[i][k]*m2[k][j] temp = temp + u end table.insert(mrow, temp) end table.insert(m3, mrow) end end return m3 end less code: matrixOP.searchRow = function(m, i) local pr = #m + 1 for k=i+1, #m do if m[k][i] == 0 then k = k+1 else pr = k break end end if pr <= #m then return pr else return 0 end end Are you sure this is ok? I've added a copy as you might want to keep the original table: matrixOP.upperTri = function(m) local temp = table.copy(m) for i=1, #temp-1 do local pivot = temp[i][i] if pivot == 0 then local pRow = matrixOP.searchRow(temp, i) if pRow==0 then break end matrixOP.rowChange(temp, i, pRow) sgn=(-1)*sgn end for k=i+1, #temp do matrixOP.rowMultSum(temp, k, i,-temp[k][i]/temp[i][i]) end end return temp end So then, watch how we use the return value of this one: matrixOP.determinant = function(m) if #m ==#m[1]then local det = 1 local t = matrixOP.upperTri(m) for i=1,#t do det = det * t[i][i] end return det else return 0 end end
I always thank to this list and to developers of ConTeXt.
Based on the attached I can make a more efficient version that we can then add to the distribution (maybe you need more than this?) Hans
Best regards,
Dalyoung
Message: 1 Date: Fri, 23 May 2014 13:44:30 +0200 From: luigi scarso
To: mailing list for ConTeXt users Subject: Re: [NTG-context] Simple command with variable number of arguments Message-ID: Content-Type: text/plain; charset="utf-8" On Fri, May 23, 2014 at 11:54 AM, Matthias Weber
wrote: Dear All,
I would like to define a command that expands
\vector{2,4} % or vector[2,4] if that?s easier
to
\startpmatrix \NC 2 \NR \NC 4 \NR \stoppmatrix
and more generally
\vector{2,4,1,7}
to
\startpmatrix \NC 2 \NR \NC 4 \NR \NC 1 \NR \NC 7 \NR \stoppmatrix
Any hints how to achieve this?
Thanks,
\definemathmatrix [pmatrix] [left={\left(\,},right={\,\right)}]
\startluacode document = document or {} document.matthias = document.matthias or {} local function lua_columnvector(a) context.startpmatrix() for i,v in ipairs(a) do context.NC() context(tostring(v)) context.NR() end context.stoppmatrix() end document.matthias.lua_columnvector = document.matthias.lua_columnvector or lua_columnvector \stopluacode
\def\columnvector#1{\ctxlua{document.matthias.lua_columnvector(#1)}}
\starttext \startformula \columnvector{{1,2,3}} %% watch the double { ! \stopformula
\stoptext
-- luigi
participants (2)
-
Hans Hagen
-
Jeong Dal