From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/87544 Path: news.gmane.org!not-for-mail From: Hans Hagen Newsgroups: gmane.comp.tex.context Subject: Re: Simple command with variable number of arguments Date: Fri, 23 May 2014 20:00:39 +0200 Message-ID: <537F8CC7.9080008@wxs.nl> References: <9CFDA0D3-CC58-4A92-AE16-6FFE7B56D6EC@me.com> Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------050907090301010204090307" X-Trace: ger.gmane.org 1400868076 23282 80.91.229.3 (23 May 2014 18:01:16 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 23 May 2014 18:01:16 +0000 (UTC) To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Fri May 23 20:01:08 2014 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from balder.ntg.nl ([5.39.185.229]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WntmF-0003Ey-Pt for gctc-ntg-context-518@m.gmane.org; Fri, 23 May 2014 20:01:07 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by balder.ntg.nl (Postfix) with ESMTP id 4AA6C10252 for ; Fri, 23 May 2014 20:01:07 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at balder.ntg.nl Original-Received: from balder.ntg.nl ([127.0.0.1]) by localhost (balder.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id C3gzY9BONyDc for ; Fri, 23 May 2014 20:01:07 +0200 (CEST) Original-Received: from balder.ntg.nl (localhost [IPv6:::1]) by balder.ntg.nl (Postfix) with ESMTP id BB31910227 for ; Fri, 23 May 2014 20:01:01 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by balder.ntg.nl (Postfix) with ESMTP id D10EC101E8 for ; Fri, 23 May 2014 20:00:57 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at balder.ntg.nl Original-Received: from balder.ntg.nl ([127.0.0.1]) by localhost (balder.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 5USfKejiAXRj for ; Fri, 23 May 2014 20:00:56 +0200 (CEST) Original-Received: from filter1-ams.mf.surf.net (filter1-ams.mf.surf.net [192.87.102.69]) by balder.ntg.nl (Postfix) with ESMTP id 07546101E3 for ; Fri, 23 May 2014 20:00:47 +0200 (CEST) Original-Received: from smtp.ziggozakelijk.nl (D57D1DA2.static.ziggozakelijk.nl [213.125.29.162]) by filter1-ams.mf.surf.net (8.14.3/8.14.3/Debian-9.4) with ESMTP id s4NI0kFS029189 for ; Fri, 23 May 2014 20:00:46 +0200 X-Default-Received-SPF: pass (skip=loggedin (res=PASS)) x-ip-name=10.100.1.103; Original-Received: from [10.100.1.103] (unverified [10.100.1.103]) by pragma-net.nl (SurgeMail 6.5a2) with ESMTP id 3350-1713362 for ; Fri, 23 May 2014 20:00:44 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 In-Reply-To: <9CFDA0D3-CC58-4A92-AE16-6FFE7B56D6EC@me.com> X-Authenticated-User: hagen@controller-9 X-Bayes-Prob: 0.5 (Score 0, tokens from: ntg-context@ntg.nl, base:default, @@RPTN) X-CanIt-Geo: ip=213.125.29.162; country=NL; region=Provincie Overijssel; city=Zwolle; latitude=52.5058; longitude=6.0858; http://maps.google.com/maps?q=52.5058,6.0858&z=6 X-CanItPRO-Stream: uu:ntg-context@ntg.nl (inherits from uu:default, base:default) X-Canit-Stats-ID: 0NM5u0KKr - f8fc871e0534 - 20140523 (trained as not-spam) X-Scanned-By: CanIt (www . roaringpenguin . com) on 192.87.102.69 X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.14 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: ntg-context-bounces@ntg.nl Xref: news.gmane.org gmane.comp.tex.context:87544 Archived-At: This is a multi-part message in MIME format. --------------050907090301010204090307 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: quoted-printable 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 =85 \stopmatrix and matrix calculatio= ns, I wrote a lua code which did matrix calculations and writing with a l= ot of help from this list. > Using the code, I can write class materials easily. It is good enough f= or 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 a= n expert in Lua. A bit more readable: matrixOP.symMatrix =3D function(sym, x, y, nx ,ny) -- symMatrix("a", "m",= "n") local nx =3D nx or 2 local ny =3D ny or nx local function filled(i,y) local mrow =3D { } for j=3D1,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 =3D { } for j=3D1,nx do table.insert(mrow,"\\vdots") end table.insert(mrow,"\\ddots") table.insert(mrow,"\\vdots") return mrow end -- local mm =3D { } for i=3D1,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 =3D { left =3D [[\left(\,]], right =3D [[\,\right)]], } matrixOP.write =3D 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 =3D 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 =3D function(t, i, m) -- replace i-th row with m*(i-th r= ow) for k =3D 1, #t[i] do t[i][k] =3D m*t[i][k] end end matrixOP.rowMultSum =3D function(t, i, j, m) -- replace i-th row with i-t= h=20 row + m*(j-th row) for k =3D 1, #t[1] do t[i][k] =3D t[i][k] + m*t[j][k] end end (no need for further speedup as it't non-critical code with neglectable=20 runtime) More Luaish (local usage): matrixOP.transpose =3D function(m) -- transpose of a matrix local mT=3D{} for j =3D 1, #m[1] do local mTrow=3D{} for i=3D1, #m do local temp =3D m[i][j] table.insert(mTrow, temp) end table.insert(mT, mTrow) end return mT end Don't forget the local here: matrixOP.inner =3D function (u, v) -- inner product of two vectors local temp=3D0 if #u =3D=3D #v then for i=3D1, #u do temp =3D temp + u[i]*v[i] end end return temp end and here: matrixOP.product =3D function(m1, m2) -- product of two matrices local m3=3D{} if #m1[1] =3D=3D #m2 then for i =3D 1, #m1 do local mrow=3D{} for j=3D1, #m2[1] do local temp =3D 0 for k=3D1, #m1[1] do u =3D m1[i][k]*m2[k][j] temp =3D temp + u end table.insert(mrow, temp) end table.insert(m3, mrow) end end return m3 end less code: matrixOP.searchRow =3D function(m, i) local pr =3D #m + 1 for k=3Di+1, #m do if m[k][i] =3D=3D 0 then k =3D k+1 else pr =3D k break end end if pr <=3D #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=20 original table: matrixOP.upperTri =3D function(m) local temp =3D table.copy(m) for i=3D1, #temp-1 do local pivot =3D temp[i][i] if pivot =3D=3D 0 then local pRow =3D matrixOP.searchRow(temp, i) if pRow=3D=3D0 then break end matrixOP.rowChange(temp, i, pRow) sgn=3D(-1)*sgn end for k=3Di+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 =3D function(m) if #m =3D=3D#m[1]then local det =3D 1 local t =3D matrixOP.upperTri(m) for i=3D1,#t do det =3D 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=20 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=3D"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=3D{\left(\,},right=3D{\,\right)}] >> >> \startluacode >> document =3D document or {} >> document.matthias =3D 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 =3D document.matthias.lua_columnvec= tor 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 >> -------------- next part -------------- >> An HTML attachment was scrubbed... >> URL: >> >> ------------------------------ > > > > _______________________________________________________________________= ____________ > If your question is of interest to others as well, please add an entry = to the Wiki! > > maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-= context > webpage : http://www.pragma-ade.nl / http://tex.aanhet.net > archive : http://foundry.supelec.fr/projects/contextrev/ > wiki : http://contextgarden.net > _______________________________________________________________________= ____________ > --=20 ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl ----------------------------------------------------------------- --------------050907090301010204090307 Content-Type: application/x-tex; name="xtest.tex" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="xtest.tex" \starttext \startluacode local matrixOP = { } moduledata.matrixOP = matrixOP function matrixOP.symMatrix(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 matrixOP.fences = { left = [[\left(\,]], right = [[\,\right)]], } function matrixOP.write(t) context.startmatrix(matrixOP.fences) for _, r in ipairs(t) do for _, c in ipairs(r) do context.NC(c) end context.NR() end context.stopmatrix() end function matrixOP.writeDet(t) context.startmatrix(matrixOP.fences) for _, r in ipairs(t) do for _, c in ipairs(r) do context.NC(c) end context.NR() end context.stopmatrix() end function matrixOP.rowChange(t, i, j) -- interchange two rows(i-th, j-th) t[i] , t[j]= t[j], t[i] end function matrixOP.rowMult(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 function matrixOP.rowMult(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 function matrixOP.rowMultSum(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 function matrixOP.transpose(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 function matrixOP.inner(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 function matrixOP.product(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 function matrixOP.searchRow(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 function matrixOP.upperTri(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 function matrixOP.determinant(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 \stopluacode \ctxlua{moduledata.matrixOP.write(moduledata.matrixOP.symMatrix("a", "m", "n"))} \ctxlua{moduledata.matrixOP.write(moduledata.matrixOP.symMatrix("a", "m", "n",4,8))} \ctxlua{moduledata.matrixOP.write(moduledata.matrixOP.upperTri{{1,2,3},{3,4,5},{3,4,5},{3,4,5},{3,4,5}})} \stoptext --------------050907090301010204090307 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________ --------------050907090301010204090307--