ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* 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

* Re: using variable for subscript in math+lua
  2012-10-25  0:38 using variable for subscript in math+lua Jeong Dal
@ 2012-10-25  1:02 ` Philipp Gesang
  2012-10-25  7:52   ` Hans Hagen
  2012-10-25 18:02   ` Otared Kavian
  0 siblings, 2 replies; 10+ messages in thread
From: Philipp Gesang @ 2012-10-25  1:02 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2467 bytes --]

···<date: 2012-10-25, Thursday>···<from: Jeong Dal>···

> 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

[-- Attachment #1.2: Type: application/pgp-signature, Size: 198 bytes --]

[-- Attachment #2: 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
___________________________________________________________________________________

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

* Re: using variable for subscript in math+lua
  2012-10-25  1:02 ` Philipp Gesang
@ 2012-10-25  7:52   ` Hans Hagen
  2012-10-25  8:01     ` Philipp Gesang
  2012-10-25 18:02   ` Otared Kavian
  1 sibling, 1 reply; 10+ messages in thread
From: Hans Hagen @ 2012-10-25  7:52 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________


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

* Re: using variable for subscript in math+lua
  2012-10-25  7:52   ` Hans Hagen
@ 2012-10-25  8:01     ` Philipp Gesang
  2012-10-25  8:28       ` Hans Hagen
  0 siblings, 1 reply; 10+ messages in thread
From: Philipp Gesang @ 2012-10-25  8:01 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 313 bytes --]

···<date: 2012-10-25, Thursday>···<from: Hans Hagen>···

> 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

[-- Attachment #1.2: Type: application/pgp-signature, Size: 198 bytes --]

[-- Attachment #2: 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
___________________________________________________________________________________

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

* Re: using variable for subscript in math+lua
  2012-10-25  8:01     ` Philipp Gesang
@ 2012-10-25  8:28       ` Hans Hagen
  0 siblings, 0 replies; 10+ messages in thread
From: Hans Hagen @ 2012-10-25  8:28 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On 25-10-2012 10:01, Philipp Gesang wrote:
> ···<date: 2012-10-25, Thursday>···<from: Hans Hagen>···
>
>> 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
___________________________________________________________________________________


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

* Re: using variable for subscript in math+lua
  2012-10-25  1:02 ` Philipp Gesang
  2012-10-25  7:52   ` Hans Hagen
@ 2012-10-25 18:02   ` Otared Kavian
  2012-10-25 20:11     ` Hans Hagen
                       ` (2 more replies)
  1 sibling, 3 replies; 10+ messages in thread
From: Otared Kavian @ 2012-10-25 18:02 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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 <Philipp.Gesang@alumni.uni-heidelberg.de> wrote:

> ···<date: 2012-10-25, Thursday>···<from: Jeong Dal>···
> 
>> 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
___________________________________________________________________________________


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

* Re: using variable for subscript in math+lua
  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
  2 siblings, 0 replies; 10+ messages in thread
From: Hans Hagen @ 2012-10-25 20:11 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Otared Kavian

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
___________________________________________________________________________________


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

* Re: using variable for subscript in math+lua
  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
  2 siblings, 0 replies; 10+ messages in thread
From: Procházka Lukáš @ 2012-10-25 21:27 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Thu, 25 Oct 2012 20:02:22 +0200, Otared Kavian <otared@gmail.com> 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
___________________________________________________________________________________

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

* Re: using variable for subscript in math+lua
  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
  2 siblings, 0 replies; 10+ messages in thread
From: Philipp Gesang @ 2012-10-25 22:05 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 6651 bytes --]

···<date: 2012-10-25, Thursday>···<from: Otared Kavian>···

> 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 <Philipp.Gesang@alumni.uni-heidelberg.de> wrote:
> 
> > ···<date: 2012-10-25, Thursday>···<from: Jeong Dal>···
> > 
> >> 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
> ___________________________________________________________________________________

-- 
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments

[-- Attachment #1.2: Type: application/pgp-signature, Size: 198 bytes --]

[-- Attachment #2: 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
___________________________________________________________________________________

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

* 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

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-25  0:38 using variable for subscript in math+lua 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
2012-10-26  6:29 Jeong Dal

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