ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Re: using variable for subscript in math+lua
@ 2012-10-26  6:29 Jeong Dal
  0 siblings, 0 replies; 10+ messages in thread
From: Jeong Dal @ 2012-10-26  6:29 UTC (permalink / raw)
  To: ntg-context

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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 10+ messages in thread
* using variable for subscript in math+lua
@ 2012-10-25  0:38 Jeong Dal
  2012-10-25  1:02 ` Philipp Gesang
  0 siblings, 1 reply; 10+ messages in thread
From: Jeong Dal @ 2012-10-25  0:38 UTC (permalink / raw)
  To: ntg-context

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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2012-10-26  6:29 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-26  6:29 using variable for subscript in math+lua Jeong Dal
  -- strict thread matches above, loose matches on Subject: below --
2012-10-25  0:38 Jeong Dal
2012-10-25  1:02 ` Philipp Gesang
2012-10-25  7:52   ` Hans Hagen
2012-10-25  8:01     ` Philipp Gesang
2012-10-25  8:28       ` Hans Hagen
2012-10-25 18:02   ` Otared Kavian
2012-10-25 20:11     ` Hans Hagen
2012-10-25 21:27     ` Procházka Lukáš
2012-10-25 22:05     ` Philipp Gesang

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).