Re: [NTG-context] using variable for subscript in math+lua
Thanks to all who gave a nice solutions. To write a big matrix of unknowns A=(a_{ij}) is not a frequent case. In math, there are many occasions to write matrices which need some typing work like \NC 2 \NC 3 \NC 4 \NR So if we have a function which write a matrix when the data is given as a form of table in Lua. There are other benefits. Sometimes, we may have to write a series matrices which are the results of the some row operations in matrix. We can also handle it conveniently because we can do some calculations in Lua. If there is a way to convey some table data from ConTeXt to Lua, it would be more convenient. Here is an example which I did by your help. I hope that you experts can make it more useful. Thank you again. Best regards, Dalyoung \starttext \startluacode matrixfunction = matrixfunction or {} -- recommended: use namespace matrixfunction.print_matrixA = function () context.startmatrix{left = "\\left(\\,", right = "\\,\\right)"} local schema1 = "a_{%d%d}" -- %d: prints integer part local schema2 = "a_{%d%s}" local schema3 = "a_{%s%d}" for i=1, 3 do for j=1, 3 do context.NC() context(schema1, i, j) end context.NC() context("\\cdots") context.NC() context(schema2, i, "n") context.NR() end for j = 1, 3 do context.NC() context("\\vdots") end context.NC() context("\\ddots") context.NC() context("\\vdots") context.NR() for i=1, 3 do context.NC() context(schema3, "m", i) end context.NC() context("\\cdots") context.NC() context("a_{mn}") context.NR() context.stopmatrix() end matrixfunction.print_matrix = 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 matrixfunction.rowChange = function(t, i, j) -- interchange two rows(i-th, j-th) t[i] , t[j]= t[j], t[i] end matrixfunction.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 matrixfunction.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 \stopluacode \startformula\startluacode matrixfunction.print_matrixA() \stopluacode\stopformula \startformula\startalign \startluacode local mat = {{0, 2, 4, -4, 1},{0, 0, 2, 3 , 4}, {2, 2, -6, 2, 4 }, {2,0 , -6, 9, 7}} context.NC("\\text{Given matrix}\\quad\\Rightarrow\\quad") context.NC() matrixfunction.print_matrix(mat) context.NR() context.NC() context("{\\bf r}_1 \\leftrightarrow {\\bf r}_3 \\quad\\Rightarrow\\quad") matrixfunction.rowChange(mat, 1, 3) context.NC() matrixfunction.print_matrix(mat) context.NR() context.NC() context("\\frac{1}{2}{\\bf r}_1 \\rightarrow {\\bf r}_1 \\quad\\Rightarrow\\quad") matrixfunction.rowMult(mat, 1, 1/2) context.NC() matrixfunction.print_matrix(mat) context.NR() context.NC() context("-2{\\bf r}_1 + {\\bf r}_4 \\rightarrow {\\bf r}_4 \\quad\\Rightarrow\\quad") matrixfunction.rowMultSum(mat, 4, 1, -2) context.NC() matrixfunction.print_matrix(mat) context.NR() context.NC() context("{\\bf r}_2 \\leftrightarrow {\\bf r}_3 \\quad\\Rightarrow\\quad") matrixfunction.rowChange(mat, 2, 3) context.NC() matrixfunction.print_matrix(mat) context.NR() \stopluacode \stopalign\stopformula \stoptext ___ 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 ___
Re: [NTG-context] using variable for subscript in math+lua
· > Hi Philipp, > > Thanks to you, Dalyoung and Hans for the nice example using luacode. > I was wonedring how would you modify your code in order to be able to change > the name of the matrix if necessary. For instance, how to modify your code in > such a way that upon saying >\ctxlua{document.print_matrix(b,3)} > one gets a 3x3 matrix whos coefficients are b_{11},…b_{33}. This may be overkill but you can just wrap the matrix command in some TeX macro: · % macros=mkvi \unprotect \startluacode local context = context commands.matrix_with_symbol = function (sym, x, y) if not y then y = x end -- default to square context.startmatrix{ left = "\\left(\\,", right = "\\,\\right)" } local schema = sym .. "_{%d%d}" -- %d: prints integer part for i=1, x do for j=1, y do context.NC() context(schema, i, j) end context.NR() end context.stopmatrix() end \stopluacode \unexpanded\def\printsymbolmatrix{% \dotripleempty\print_symbol_matrix_indeed% } \def\print_symbol_matrix_indeed[#first][#second][#num]{% \bgroup \def\sym{a}%% set default symbol to print, could be done in Lua \def\y{nil}% \ifsecondargument \ifthirdargument \edef\sym{#first}% \edef\x{#second}% \edef\y{#num}% \else \doifnumberelse{#first}% {\edef \x{#first}\edef\y{#second}}% {\edef\sym{#first}\edef\x{#second}}% \fi \else \edef\x{#first}% \fi \ctxcommand{matrix_with_symbol(\!!bs\sym\!!es, \x, \y)}% \egroup% } \protect \starttext \startformula \printsymbolmatrix[3]%% symbol is “a”, matrix is square \stopformula \startformula \printsymbolmatrix[4][8]%% symbol is “a”, matrix is 4x8 \stopformula \startformula \printsymbolmatrix[b][8]%% symbol is “b”, matrix is square \stopformula \startformula \printsymbolmatrix[c][5][3]%% symbol is “c”, matrix is 5x3 \stopformula \stoptext \endinput · Now the symbol will default to “a” but you can specify another one. (Btw. I think the Lua part could be done as easily with dorecurse.) Regards Philipp > > Thanks in advance: OK > > On 25 oct. 2012, at 03:02, Philipp Gesang > wrote: > > > · > > > >> Dear Hans, Lucas, Wolfgang, Aditya > >> > >> Now, I write a matrix using \startluacode by the helps of you. > >> Also, I can do some operations in matrices which reduced my typing job. > >> In this code, I have to give all the entries of a matrix as a table. It is > >> good to use in many cases. > >> > >> I have one more question. If the given array is as the following and it > >> works well. > >> > >> However, there might be a better way to do job using "for" iteration. > >> I tired to use "i, j" in several ways but it doesn't work. > > > > Inside a string the variables are just bytes. To print to a > > string you can interpolate with string.format() [1]. > > > > [1] http://www.lua.org/manual/5.1/manual.html#pdf-string.format > > > > · > > \startluacode > > document = document or { } -- recommended: use namespace > > document.print_matrix = function (x, y) > >if not y then y = x end -- default to square > >context.startmatrix{left = "\\left(\\,", right = "\\,\\right)"} > >local schema = "a_{%d%d}" -- %d: prints integer part > >for i=1, x do > > for j=1, y do > >--- use the template defined above to print the > >--- row and column > >context.NC(string.format(schema, i, j)) > > end > > context.NR() > >end > >context.stopmatrix() > > end > > \stopluacode > > > > \starttext > > \startformula > >\ctxlua{document.print_matrix(3)} > > \stopformula > > \startformula > >\ctxlua{document.print_matrix(9,4)} > > \stopformula > > \startformula > >\ctxlua{document.print_matrix(9)} > > \stopformula > > \stoptext > > \endinput > > · > > > >> > >> Is there a way to do such a job using "for" iteration? > >> > >> Thank you for reading. > >> > >> Best regards, > >> > >> Dalyoung > >> ___ > >> 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 > >> ___ > > > > -- > > () ascii ribbon campaign - against html e-mail > > /\ www.asciiribbon.org - against proprietary attachments > > __
Re: [NTG-context] using variable for subscript in math+lua
On Thu, 25 Oct 2012 20:02:22 +0200, Otared Kavian wrote: Hi Philipp, Thanks to you, Dalyoung and Hans for the nice example using luacode. I was wonedring how would you modify your code in order to be able to change the name of the matrix if necessary. For instance, how to modify your code in such a way that upon saying \ctxlua{document.print_matrix(b,3)} one gets a 3x3 matrix whos coefficients are b_{11},…b_{33}. Thanks in advance: OK Something like this? · document.print_matrix = function (x, y, name) local name = name or "a" if not y then y = x end -- default to square context.startmatrix{left = "\\left(\\,", right = "\\,\\right)"} local schema = name .. "_{%d%d}" -- %d: prints integer part for i=1, x do for j=1, y do --- use the template defined above to print the --- row and column context.NC(string.format(schema, i, j)) end context.NR() end context.stopmatrix() end · Call with: \ctxlua{document.print_matrix(3, 3, "b")} Best regards, Lukas ___ 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 ___
Re: [NTG-context] using variable for subscript in math+lua
On 25-10-2012 20:02, Otared Kavian wrote: Hi Philipp, Thanks to you, Dalyoung and Hans for the nice example using luacode. I was wonedring how would you modify your code in order to be able to change the name of the matrix if necessary. For instance, how to modify your code in such a way that upon saying \ctxlua{document.print_matrix(b,3)} one gets a 3x3 matrix whos coefficients are b_{11},…b_{33}. if the two of you can come up with some specs we can make a small module if needed Hans - 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 - ___ 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 ___
Re: [NTG-context] using variable for subscript in math+lua
Hi Philipp, Thanks to you, Dalyoung and Hans for the nice example using luacode. I was wonedring how would you modify your code in order to be able to change the name of the matrix if necessary. For instance, how to modify your code in such a way that upon saying \ctxlua{document.print_matrix(b,3)} one gets a 3x3 matrix whos coefficients are b_{11},…b_{33}. Thanks in advance: OK On 25 oct. 2012, at 03:02, Philipp Gesang wrote: > · > >> Dear Hans, Lucas, Wolfgang, Aditya >> >> Now, I write a matrix using \startluacode by the helps of you. >> Also, I can do some operations in matrices which reduced my typing job. >> In this code, I have to give all the entries of a matrix as a table. It is >> good to use in many cases. >> >> I have one more question. If the given array is as the following and it >> works well. >> >> However, there might be a better way to do job using "for" iteration. >> I tired to use "i, j" in several ways but it doesn't work. > > Inside a string the variables are just bytes. To print to a > string you can interpolate with string.format() [1]. > > [1] http://www.lua.org/manual/5.1/manual.html#pdf-string.format > > · > \startluacode > document = document or { } -- recommended: use namespace > document.print_matrix = function (x, y) >if not y then y = x end -- default to square >context.startmatrix{left = "\\left(\\,", right = "\\,\\right)"} >local schema = "a_{%d%d}" -- %d: prints integer part >for i=1, x do > for j=1, y do >--- use the template defined above to print the >--- row and column >context.NC(string.format(schema, i, j)) > end > context.NR() >end >context.stopmatrix() > end > \stopluacode > > \starttext > \startformula >\ctxlua{document.print_matrix(3)} > \stopformula > \startformula >\ctxlua{document.print_matrix(9,4)} > \stopformula > \startformula >\ctxlua{document.print_matrix(9)} > \stopformula > \stoptext > \endinput > · > >> >> Is there a way to do such a job using "for" iteration? >> >> Thank you for reading. >> >> Best regards, >> >> Dalyoung >> ___ >> 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 >> ___ > > -- > () ascii ribbon campaign - against html e-mail > /\ www.asciiribbon.org - against proprietary attachments > ___ > 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 > ___ ___ 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 ___
Re: [NTG-context] using variable for subscript in math+lua (and math+MetaFun)
Dear Hans, The structure of your code is very interesting. MP code) inside ConTeXt) inside Lua) inside ConTeXt) again! It is really useful. Thank you. Best regards, Dalyoung > > play with code like this > > \starttext > > \startluacode >context.startMPcode() >for i=1,10 do >for j=1,10 do >context('draw (%scm,%scm) withpen pencircle scaled 2mm > ;',i,j) >context('draw > thetextext.top("\\tt%s,%s",(%scm,%scm+2mm)) ;',i,j,i,j) >end >end >context.stopMPcode() > \stopluacode > > \stoptext > ___ 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 ___
Re: [NTG-context] using variable for subscript in math+lua (and math+MetaFun)
On 25-10-2012 13:59, Jeong Dal wrote: > Dear Gesang and Hans, > > Your solution is working nicely. > I may use lua to simplify my other work. > > I'd like to ask you one more thing. > > I tried to write a matrix using MetaFun to add some graphics as following: > > label(btex $a_{11}$ etex, ( , )); > label(btex $a_{12}$ etex, ( , )); > label(btex $a_{13}$ etex, ( , )); > … > label(btex $a_{3,3}$ etex, ( , )); > > Since the same command is repeated, I tested to use "for" iteration. > But I couldn't succeed. It is OK to use the variable as a text but I couldn't > find a way to use a variable as a subscript. > > For example, > > for i=1 unto 3: label(btex decimal(i) etex, (x,y)); endfor; > > is OK. But, > > for i=1 upto 3: label(btex $a_{i}$ etex, (x,y)); endfor; > or > for i=1 upto 3: label(btex $a_{deciaml(i)}$ etex, (x,y)); endfor; > > are not working as I wanted. > > Is there a way to use variable as a subscript in "label"? play with code like this \starttext \startluacode context.startMPcode() for i=1,10 do for j=1,10 do context('draw (%scm,%scm) withpen pencircle scaled 2mm ;',i,j) context('draw thetextext.top("\\tt%s,%s",(%scm,%scm+2mm)) ;',i,j,i,j) end end context.stopMPcode() \stopluacode \stoptext - 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 - ___ 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 ___
Re: [NTG-context] using variable for subscript in math+lua (and math+MetaFun)
Dear Gesang and Hans, Your solution is working nicely. I may use lua to simplify my other work. I'd like to ask you one more thing. I tried to write a matrix using MetaFun to add some graphics as following: label(btex $a_{11}$ etex, ( , )); label(btex $a_{12}$ etex, ( , )); label(btex $a_{13}$ etex, ( , )); … label(btex $a_{3,3}$ etex, ( , )); Since the same command is repeated, I tested to use "for" iteration. But I couldn't succeed. It is OK to use the variable as a text but I couldn't find a way to use a variable as a subscript. For example, for i=1 unto 3: label(btex decimal(i) etex, (x,y)); endfor; is OK. But, for i=1 upto 3: label(btex $a_{i}$ etex, (x,y)); endfor; or for i=1 upto 3: label(btex $a_{deciaml(i)}$ etex, (x,y)); endfor; are not working as I wanted. Is there a way to use variable as a subscript in "label"? Thank you again. Best regards, Dalyoung ___ 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 ___
Re: [NTG-context] using variable for subscript in math+lua
On 25-10-2012 10:01, Philipp Gesang wrote: · On 25-10-2012 03:02, Philipp Gesang wrote: context.NC(string.format(schema, i, j)) context.NC(schema, i, j) Right, but then if you encounter this the first time you won’t know what to google. actually it's wrong ... only the bare context(fmt,...) supports a format .. this is what i meant: context.NC() context(schema, i, j) the thing is that NC does not take an argument (some column separators have an optional) Hans - 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 - ___ 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 ___
Re: [NTG-context] using variable for subscript in math+lua
· > On 25-10-2012 03:02, Philipp Gesang wrote: > > > context.NC(string.format(schema, i, j)) > > context.NC(schema, i, j) Right, but then if you encounter this the first time you won’t know what to google. Regards Philipp pgpVIf0TmiH7I.pgp Description: PGP signature ___ 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 ___
Re: [NTG-context] using variable for subscript in math+lua
On 25-10-2012 03:02, Philipp Gesang wrote: context.NC(string.format(schema, i, j)) context.NC(schema, i, j) - 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 - ___ 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 ___
Re: [NTG-context] using variable for subscript in math+lua
· > Dear Hans, Lucas, Wolfgang, Aditya > > Now, I write a matrix using \startluacode by the helps of you. > Also, I can do some operations in matrices which reduced my typing job. > In this code, I have to give all the entries of a matrix as a table. It is > good to use in many cases. > > I have one more question. If the given array is as the following and it works > well. > > However, there might be a better way to do job using "for" iteration. > I tired to use "i, j" in several ways but it doesn't work. Inside a string the variables are just bytes. To print to a string you can interpolate with string.format() [1]. [1] http://www.lua.org/manual/5.1/manual.html#pdf-string.format · \startluacode document = document or { } -- recommended: use namespace document.print_matrix = function (x, y) if not y then y = x end -- default to square context.startmatrix{left = "\\left(\\,", right = "\\,\\right)"} local schema = "a_{%d%d}" -- %d: prints integer part for i=1, x do for j=1, y do --- use the template defined above to print the --- row and column context.NC(string.format(schema, i, j)) end context.NR() end context.stopmatrix() end \stopluacode \starttext \startformula \ctxlua{document.print_matrix(3)} \stopformula \startformula \ctxlua{document.print_matrix(9,4)} \stopformula \startformula \ctxlua{document.print_matrix(9)} \stopformula \stoptext \endinput · > > Is there a way to do such a job using "for" iteration? > > Thank you for reading. > > Best regards, > > Dalyoung > ___ > 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 > ___ -- () ascii ribbon campaign - against html e-mail /\ www.asciiribbon.org - against proprietary attachments pgpyR7Q10pCRx.pgp Description: PGP signature ___ 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 ___
[NTG-context] using variable for subscript in math+lua
Dear Hans, Lucas, Wolfgang, Aditya Now, I write a matrix using \startluacode by the helps of you. Also, I can do some operations in matrices which reduced my typing job. In this code, I have to give all the entries of a matrix as a table. It is good to use in many cases. I have one more question. If the given array is as the following and it works well. \starttext \startluacode printMatrix = 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 \stopluacode \startformula \startluacode local mat={{"a_{11}","a_{12}","a_{13}"},{"a_{21}","a_{22}","a_{23}"},{"a_{31}","a_{32}","a_{33}"}} printMatrix(mat) \stopluacode \stopformula \stoptext % However, there might be a better way to do job using "for" iteration. I tired to use "i, j" in several ways but it doesn't work. function printMatrixA() context.startmatrix{left = "\\left(\\,", right = "\\,\\right)"} for i=1,3 do for j=1, 3 do context.NC("a_{ij}") end context.NR() end context.stopmatrix() end %%% Is there a way to do such a job using "for" iteration? Thank you for reading. Best regards, Dalyoung ___ 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 ___