From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/87530 Path: news.gmane.org!not-for-mail From: Jeong Dal Newsgroups: gmane.comp.tex.context Subject: Re: Simple command with variable number of arguments Date: Fri, 23 May 2014 22:48:15 +0900 Message-ID: <9CFDA0D3-CC58-4A92-AE16-6FFE7B56D6EC@me.com> References: Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="Boundary_(ID_vHIB3OUT8QR/Rc3ieLMXkw)" X-Trace: ger.gmane.org 1400852934 24316 80.91.229.3 (23 May 2014 13:48:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 23 May 2014 13:48:54 +0000 (UTC) To: ntg-context@ntg.nl Original-X-From: ntg-context-bounces@ntg.nl Fri May 23 15:48:49 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 1Wnpq3-0007wb-0N for gctc-ntg-context-518@m.gmane.org; Fri, 23 May 2014 15:48:47 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by balder.ntg.nl (Postfix) with ESMTP id 74C0410225 for ; Fri, 23 May 2014 15:48:46 +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 AaTBgCkFah02 for ; Fri, 23 May 2014 15:48:46 +0200 (CEST) Original-Received: from balder.ntg.nl (localhost [IPv6:::1]) by balder.ntg.nl (Postfix) with ESMTP id 0C439101FB for ; Fri, 23 May 2014 15:48:42 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by balder.ntg.nl (Postfix) with ESMTP id 1B668101E8 for ; Fri, 23 May 2014 15:48:38 +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 wAKih9g7rEvZ for ; Fri, 23 May 2014 15:48:36 +0200 (CEST) Original-Received: from filter2-til.mf.surf.net (filter2-til.mf.surf.net [194.171.167.218]) by balder.ntg.nl (Postfix) with ESMTP id 99590101E3 for ; Fri, 23 May 2014 15:48:27 +0200 (CEST) Original-Received: from nk11p03mm-asmtp001.mac.com (nk11p03mm-asmtp001.mac.com [17.158.232.236]) by filter2-til.mf.surf.net (8.14.3/8.14.3/Debian-9.4) with ESMTP id s4NDmP5G015852 for ; Fri, 23 May 2014 15:48:26 +0200 Original-Received: from [10.0.1.3] (unknown [1.235.92.77]) by nk11p03mm-asmtp001.mac.com (Oracle Communications Messaging Server 7u4-27.08(7.0.4.27.7) 64bit (built Aug 22 2013)) with ESMTPSA id <0N6100BR350G2T20@nk11p03mm-asmtp001.mac.com> for ntg-context@ntg.nl; Fri, 23 May 2014 13:48:19 +0000 (GMT) In-reply-to: X-Mailer: Apple Mail (2.1874) X-MANTSH: 1TEIXREEbG1oaGkdHB0lGUkdDRl5PWBoaHxEKTEMXGx0EGx8SBBscHwQdHhAbHho fGhEKWE0XSxEKbX4XGhEKTFkXGxobGxEKWUkXEQpZXhdoY3kRCkNOF0sbHhpiTk0caBl1Whl4c wceZBsZHhJjbhEKWFwXGQQaBBsbB01OHxgYGBlLBRsdBBsfEgQbHB8EHR4QGx4aHxsRCl5ZF2F eenMTEQpMRhdia2sRCkNaFxsEGBkfBBMYBB0dEQpCRRdua3kcGkdtWVNpAREKQk4XbHBgeUAdY lJpGmIRCkJMF2RcTWZTUmFYTlltEQpCbBdheGtgTUFtXUlnXREKQkAXYxxDHhMTWnBLG3wRCkJ YF2gaT0JebEVcTHNEEQpwZxdsfWdZRkdmehJ4YhEKcGgXZ05bZ2tgbBpiWVMRCnBoF21FG39bB Rh6fh1eEQpwaBduH1wSTh1uGHljRREKcGgXa0JpcGdHWGwTW0ERCnBoF2hibxlPfHIaTRgFEQp waxduRlNQT1JgY2hQaREKcEsXYxhLUGlsYkVYf1wRCnBnF2BBSxNyZ3pGZFlYEQpwZxdhHEESQ XoeXE5lBREKcGcXZGNYbBhkXWVkQUgRCnB/F29LSVpzTnhNeRITEQpwXxdhGQFvfUNoYwVyBRE KcGwXaGYZZ01zRnoBR2cR X-CLX-Spam: false X-CLX-Score: 1011 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.11.96,1.0.14,0.0.0000 definitions=2014-05-23_05:2014-05-23,2014-05-23,1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 suspectscore=1 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1402240000 definitions=main-1405230175 X-Bayes-Prob: 0.0001 (Score 0, tokens from: ntg-context@ntg.nl, base:default, @@RPTN) X-CanIt-Geo: ip=17.158.232.236; country=US; region=California; city=Cupertino; latitude=37.3042; longitude=-122.0946; http://maps.google.com/maps?q=37.3042,-122.0946&z=6 X-CanItPRO-Stream: uu:ntg-context@ntg.nl (inherits from uu:default, base:default) X-Canit-Stats-ID: 0TM5pMqC3 - 14bde73241a1 - 20140523 (trained as not-spam) X-Scanned-By: CanIt (www . roaringpenguin . com) on 194.171.167.218 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:87530 Archived-At: --Boundary_(ID_vHIB3OUT8QR/Rc3ieLMXkw) Content-type: text/plain; charset=euc-kr Content-transfer-encoding: quoted-printable Dear Luigi and listers, Last year, I need to write a text for a linear algebra class. Since there are many \startmatrix =A1=A6 \stopmatrix and matrix = calculations, I wrote a lua code which did matrix calculations and = writing with a lot of help from this list.=20 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.=20= 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 --Boundary_(ID_vHIB3OUT8QR/Rc3ieLMXkw) Content-type: application/octet-stream; name=MatrixLuacode.tex Content-transfer-encoding: 7bit Content-disposition: attachment; filename=MatrixLuacode.tex \startenvironment MatrixLuacode \startbuffer[luaMatrix] \startluacode matrixOP = matrixOP or {} -- recommended: use namespace matrixOP.symMatrix = function(sym, x, y) -- symMatrix("a", "m", "n") local entry local mrow={} local mm={} for i=1, 2 do -- first two rows mrow={} for j=1, 2 do entry=sym.."_{"..i..j.."}" table.insert(mrow, entry) end entry="\\cdots" table.insert(mrow, entry) entry=sym.."_{"..i..y.."}" table.insert(mrow, entry) table.insert(mm,mrow) end entry = {"\\vdots","\\vdots","\\ddots","\\vdots"} --3rd row table.insert(mm,entry) mrow={} for j=1, 2 do -- last row entry=sym.."_{"..x..j.."}" table.insert(mrow, entry) end entry="\\cdots" table.insert(mrow, entry) entry=sym.."_{"..x..y.."}" table.insert(mrow, entry) table.insert(mm,mrow) return mm end matrixOP.write = function (t) context.startmatrix{left = "\\left(\\,", right = "\\,\\right)"} 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{left = "\\left|\\ ", right = "\\ \\right|"} for _, r in ipairs(t) do for _, c in ipairs(r) do context.NC(c) end context.NR() end context.stopmatrix() end matrixOP.rowChange = function(t, i, j) -- interchange two rows(i-th, j-th) t[i] , t[j]= t[j], t[i] end matrixOP.rowMult = function(t, i, m) -- replace i-th row with m*(i-th row) local len len = #(t[i]) for k = 1, len 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) local len len = #(t[1]) for k = 1, len do t[i][k] = t[i][k] + m*t[j][k] end end matrixOP.transpose = function(m) -- transpose of a matrix local temp local mT={} local mTrow for j = 1, #m[1] do mTrow={} for i=1, #m do temp = m[i][j] table.insert(mTrow, temp) end table.insert(mT, mTrow) end return mT end matrixOP.inner = function (u, v) -- inner product of two vectors temp=0 if #u == #v then for i=1, #u do temp = temp + u[i]*v[i] end end return temp end matrixOP.product = function(m1, m2) -- product of two matrices local temp local m3={} local mrow if #m1[1] == #m2 then for i = 1, #m1 do mrow={} for j=1, #m2[1] do 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 matrixOP.searchRow = function(m, i) local pv, pr pr = #m + 1 for k=i+1, #m do pv=m[k][i] if pv == 0 then k = k+1 else pr = k break end end if pr <= #m then return pr else return 0 end end matrixOP.upperTri = function(m) local pivot, pRow, multiple local temp temp = m for i=1, #temp-1 do pivot = temp[i][i] if pivot == 0 then pRow = matrixOP.searchRow(temp, i) if pRow==0 then -- context("Singular Matrix") break end matrixOP.rowChange(temp, i, pRow) sgn=(-1)*sgn end for k=i+1, #temp do multiple = -temp[k][i]/temp[i][i] matrixOP.rowMultSum(temp, k, i,multiple) end end return temp end matrixOP.determinant = function(m) local det, sgn if #m ==#m[1] then det=1 sgn=1 matrixOP.upperTri(m) for i = 1, #m do det = det*m[i][i] end det = sgn*det else det = 0 end -- context(det) return det end \stopluacode \stopbuffer \stopenvironment --Boundary_(ID_vHIB3OUT8QR/Rc3ieLMXkw) Content-type: text/plain; CHARSET=US-ASCII Content-transfer-encoding: 7BIT > > 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 > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > > ------------------------------ --Boundary_(ID_vHIB3OUT8QR/Rc3ieLMXkw) 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 ___________________________________________________________________________________ --Boundary_(ID_vHIB3OUT8QR/Rc3ieLMXkw)--