From: Jeong Dal <haksan@me.com>
To: ntg-context@ntg.nl
Subject: Re: Simple command with variable number of arguments
Date: Fri, 23 May 2014 22:48:15 +0900 [thread overview]
Message-ID: <9CFDA0D3-CC58-4A92-AE16-6FFE7B56D6EC@me.com> (raw)
In-Reply-To: <mailman.56.1400848757.2240.ntg-context@ntg.nl>
[-- Attachment #1: Type: text/plain, Size: 777 bytes --]
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
[-- Attachment #2: MatrixLuacode.tex --]
[-- Type: application/octet-stream, Size: 3510 bytes --]
\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
[-- Attachment #3: Type: text/plain, Size: 1748 bytes --]
>
> Message: 1
> Date: Fri, 23 May 2014 13:44:30 +0200
> From: luigi scarso <luigi.scarso@gmail.com>
> To: mailing list for ConTeXt users <ntg-context@ntg.nl>
> Subject: Re: [NTG-context] Simple command with variable number of
> arguments
> Message-ID:
> <CAG5iGsCadu33Hw=hPhmDe+Wp1B_FcPjN0CaXwJAso+vnQBJEtA@mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> On Fri, May 23, 2014 at 11:54 AM, Matthias Weber <matweber@indiana.edu>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: <http://www.ntg.nl/pipermail/ntg-context/attachments/20140523/569165d6/attachment-0001.html>
>
> ------------------------------
[-- Attachment #4: Type: text/plain, Size: 485 bytes --]
___________________________________________________________________________________
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
___________________________________________________________________________________
next parent reply other threads:[~2014-05-23 13:48 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <mailman.56.1400848757.2240.ntg-context@ntg.nl>
2014-05-23 13:48 ` Jeong Dal [this message]
2014-05-23 18:00 ` Hans Hagen
[not found] ` <537FA66D.3000608@wxs.nl>
2014-05-24 0:36 ` Jeong Dal
2014-05-24 6:55 ` Jeong Dal
2014-05-23 9:54 Matthias Weber
2014-05-23 10:08 ` Otared Kavian
2014-05-23 10:55 ` Matthias Weber
2014-05-23 10:56 ` Aditya Mahajan
2014-05-23 11:14 ` Matthias Weber
2014-05-23 11:37 ` Aditya Mahajan
2014-05-23 12:08 ` Matthias Weber
2014-05-23 11:44 ` luigi scarso
2014-05-23 12:12 ` Matthias Weber
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9CFDA0D3-CC58-4A92-AE16-6FFE7B56D6EC@me.com \
--to=haksan@me.com \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).