ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] Combining python and lua
       [not found] <990d094b-4733-492b-83f2-8014d51658c5@Spark>
@ 2023-10-25 15:45 ` Alexandre Christe
  2023-10-25 19:17   ` [NTG-context] " Bruce Horrocks
  2023-10-25 19:52   ` Aditya Mahajan
  0 siblings, 2 replies; 4+ messages in thread
From: Alexandre Christe @ 2023-10-25 15:45 UTC (permalink / raw)
  To: Hans Hagen via ntg-context


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

Dear list,

I’d like to know the best approach to make this a bit more usable. I’m trying to typeset a table with sin/cos/tan values. I’d like to be able to convert the angles in degrees into radians (with math notation) and to compute the trigonometric function leading to sin(pi/4) = 1/2, or cos(pi/6) = sqrt(3)/2 and display it in math mode.

Here’s my current MWE, where I fail to convert the angles in radians and fail to display the result in symbolic form.

\starttext

\startluacode
local angleList = {0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 240, 270, 300, 315, 330, 360}

context.bTABLE()
 context.bTR()
 context.bTD() context("$x$") context.eTD()
 context.bTD() context("$0$") context.eTD()
 for _, a in ipairs(angleList) do
 context.bTD() context("$\letterpercent.0f$",a) context.eTD()
 end
 context.eTR()
 context.bTR()
 context.bTD() context("$\\cos(x)$") context.eTD()
 for _, a in ipairs(angleList) do
 context.bTD() context("$\letterpercent.3f$",math.cos(math.rad(a))) context.eTD()
 end
 context.eTR()
 context.bTR()
 context.bTD() context("$\\sin(x)$") context.eTD()
 for _, a in ipairs(angleList) do
 context.bTD() context("$\letterpercent.3f$",math.sin(math.rad(a))) context.eTD()
 end
 context.eTR()
 context.bTR()
 context.bTD() context("$\\tan(x)$") context.eTD()
 for _, a in ipairs(angleList) do
 if a == 90 or a == 270 then
 context.bTD() context("$-$") context.eTD()
 else
 context.bTD() context("$\letterpercent.3f$",math.tan(math.rad(a))) context.eTD()
 end
 end
 context.eTR()
context.eTABLE()
\stopluacode

\stoptext

\stoptext

One possible way would be to use Python and the t-filter module, like this

\usemodule[filter]
%It depends on your OS. In mine it's python3
\defineexternalfilter
 [pythontyping]
 [filtercommand={python3 \externalfilterinputfile \space > \externalfilteroutputfile},
 output={\externalfilterbasefile.tex},
 cache=yes,
 readcommand=\typefile,
 spacebefore=medium,
 spaceafter=medium]
%We copy settings to save typing
\defineexternalfilter[pythonformula][pythontyping]
%But instead of typing, we input a formula
\setupexternalfilter[pythonformula]
 [readcommand=\samplefile] %\input should also work

\starttext

Convert angle to radians:
$\startpythonformula
 from sympy import *
 print(latex(pi/180*60))
 \stoppythonformula$

Compute trigonometric function:
$\startpythonformula
 from sympy import *
 print(latex(cos(pi/3)))
 \stoppythonformula$

\stoptext

However, I haven’t managed so far to combine things (lua and Python) in an automatic way.

How should I proceed?

A. Christe

[-- Attachment #1.2: Type: text/html, Size: 3710 bytes --]

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

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

* [NTG-context] Re: Combining python and lua
  2023-10-25 15:45 ` [NTG-context] Combining python and lua Alexandre Christe
@ 2023-10-25 19:17   ` Bruce Horrocks
  2023-10-25 19:52   ` Aditya Mahajan
  1 sibling, 0 replies; 4+ messages in thread
From: Bruce Horrocks @ 2023-10-25 19:17 UTC (permalink / raw)
  To: ntg-context mailing list

On 25 Oct 2023, at 16:45, Alexandre Christe <aleks.christe@gmail.com> wrote:
> 
> Dear list,
> 
> I’d like to know the best approach to make this a bit more usable. I’m trying to typeset a table with sin/cos/tan values. I’d like to be able to convert the angles in degrees into radians (with math notation) and to compute the trigonometric function leading to sin(pi/4) = 1/2, or cos(pi/6) = sqrt(3)/2 and display it in math mode.

You can use templates to subtitute values into a string and then send that out to be typeset.
For example: if you want to turn "sin(pi/4) = 1/2" to 

  \startformula
  \sin{\frac{\pi}{4}} = \frac{1}{2}
  \stopformula

Then use this Lua code:

  local myTrigTemplate = [[
    \startformula
    \%fname%{\frac{\pi}{%divisor%}} = \frac{%numerator%}{%denominator%}
    \stopformula
  ]]

  context.templates[myTrigTemplate] {
    fname = "sin",
    divisor = 4,
    numerator = 1,
    denominator = 2
  }

By combining them (using the result of a simple template as the parameter value into a larger template) you can create a function similar to Python's latex() that handles the trig functions you are using. That is the easy bit.

Converting the angle into radians and then getting the result as a fraction of pi requires you to implement a continued fraction algorithm to determine numerator and denominator.

Extending that to determine whether to use sqrt(3)/2 instead of some other fractional approximation is another significant step up in complexity.

I can send you some code that does that - but it's written in HP-71 BASIC so you'll have to adapt it. :-)

The real question is: do you really want to be able to generate an expression from *any* initial angle value or just those in your table? If it's just those in your table then I think it would be much, much less effort to extend 'angleList' with accompanying lists holding the values to substitute into the template. Or even just typeset the whole table by hand (prepare a list of the expressions and use editor macros to ConTeXtify them).

Regards,
—
Bruce Horrocks
Hampshire, UK

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

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

* [NTG-context] Re: Combining python and lua
  2023-10-25 15:45 ` [NTG-context] Combining python and lua Alexandre Christe
  2023-10-25 19:17   ` [NTG-context] " Bruce Horrocks
@ 2023-10-25 19:52   ` Aditya Mahajan
  2023-10-26  9:21     ` Alexandre Christe
  1 sibling, 1 reply; 4+ messages in thread
From: Aditya Mahajan @ 2023-10-25 19:52 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 1451 bytes --]

On Wed, 25 Oct 2023, Alexandre Christe wrote:

> Dear list,
> 
> I’d like to know the best approach to make this a bit more usable. I’m trying to typeset a table with sin/cos/tan values. I’d like to be able to convert the angles in degrees into radians (with math notation) and to compute the trigonometric function leading to sin(pi/4) = 1/2, or cos(pi/6) = sqrt(3)/2 and display it in math mode.
> 
> Here’s my current MWE, where I fail to convert the angles in radians 

there is math.sind, cosd, etc. for computing sin for angle in degrees. The implementation is l-math.lua is:

    local pipi = 2*math.pi/360

    function math.sind(d) return sin(d*pipi) end
    function math.cosd(d) return cos(d*pipi) end
    function math.tand(d) return tan(d*pipi) end

> and fail to display the result in symbolic form.

Context doesn't do symbolic math. You can try checking if one of the symbolic math libraries in lua provides all the features that you want.

> One possible way would be to use Python and the t-filter module, like this

This can work as follows. Take the array of angles, and convert them to the desired format in python and write them to a temp file. Read that temp file in lua and typeset it as you want.

Another option is an old proof of concept by Luigi: https://github.com/bastibe/lunatic-python which allowed two-way communication between python and luatex. Not sure if it still works.

Aditya

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

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

* [NTG-context] Re: Combining python and lua
  2023-10-25 19:52   ` Aditya Mahajan
@ 2023-10-26  9:21     ` Alexandre Christe
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandre Christe @ 2023-10-26  9:21 UTC (permalink / raw)
  To: mailing list for ConTeXt users


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

Hi,

Thanks for your inputs Aditya and Bruce.

I’m still having some issues when trying to include the output of the filter module within lua.

MNWE:

\defineexternalfilter
 [sympy]
 [filtercommand={/Users/aleks/miniconda3/bin/python3 \externalfilterinputfile\space > \externalfilteroutputfile},
 bufferbefore=init,
 output={\externalfilterbasefile.out},
 readcommand=\typefile,
 read=no,]

\startbuffer[init]
from sympy import *
\stopbuffer

\starttext

\startluacode
 local deg2rad = [[
 %\startsympy

 print(latex(rad(%angleVal%)))
 %\stopsympy
	]]

	local calcTrigFunction = [[
 \startsympy
 from sympy import *

 print(latex(%fname%(rad(%angleVal%))))
 \stopsympy
	]]

	local angleList = {0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 240, 270, 300, 315, 330, 360}

	for _, a in ipairs(angleList) do
 context.templates[calcTrigFunction] {
 fname = "sin",
 angleVal = a
 }
	end

\stopluacode

\stoptext

In particular, it fails as soon as  \startsympy and \stopsympy are part of the template. Should I protect these?

A. Christe
Le 25 oct. 2023 à 21:56 +0200, Aditya Mahajan <adityam@umich.edu>, a écrit :
> On Wed, 25 Oct 2023, Alexandre Christe wrote:
>
> > Dear list,
> >
> > I’d like to know the best approach to make this a bit more usable. I’m trying to typeset a table with sin/cos/tan values. I’d like to be able to convert the angles in degrees into radians (with math notation) and to compute the trigonometric function leading to sin(pi/4) = 1/2, or cos(pi/6) = sqrt(3)/2 and display it in math mode.
> >
> > Here’s my current MWE, where I fail to convert the angles in radians
>
> there is math.sind, cosd, etc. for computing sin for angle in degrees. The implementation is l-math.lua is:
>
> local pipi = 2*math.pi/360
>
> function math.sind(d) return sin(d*pipi) end
> function math.cosd(d) return cos(d*pipi) end
> function math.tand(d) return tan(d*pipi) end
>
> > and fail to display the result in symbolic form.
>
> Context doesn't do symbolic math. You can try checking if one of the symbolic math libraries in lua provides all the features that you want.
>
> > One possible way would be to use Python and the t-filter module, like this
>
> This can work as follows. Take the array of angles, and convert them to the desired format in python and write them to a temp file. Read that temp file in lua and typeset it as you want.
>
> Another option is an old proof of concept by Luigi: https://github.com/bastibe/lunatic-python which allowed two-way communication between python and luatex. Not sure if it still works.
>
> Aditya
>
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
>
> maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
> webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
> archive : https://github.com/contextgarden/context
> wiki : https://wiki.contextgarden.net
> ___________________________________________________________________________________

[-- Attachment #1.2: Type: text/html, Size: 4621 bytes --]

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2023-10-26  9:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <990d094b-4733-492b-83f2-8014d51658c5@Spark>
2023-10-25 15:45 ` [NTG-context] Combining python and lua Alexandre Christe
2023-10-25 19:17   ` [NTG-context] " Bruce Horrocks
2023-10-25 19:52   ` Aditya Mahajan
2023-10-26  9:21     ` Alexandre Christe

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