Re: [NTG-context] using variable for subscript in math+lua
![](https://secure.gravatar.com/avatar/ecffc5c1eb6692198d091ee98e921822.jpg?s=120&d=mm&r=g)
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
participants (1)
-
Jeong Dal