Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for native Julia functions? #28

Open
korsbo opened this issue Jul 18, 2018 · 3 comments
Open

Add support for native Julia functions? #28

korsbo opened this issue Jul 18, 2018 · 3 comments

Comments

@korsbo
Copy link
Owner

korsbo commented Jul 18, 2018

In Julia, one can inspect the expressions that build up a method with @code_lowered. The returned object does have all the information required to deduce what the method does. I have been playing around with latexifying simple julia functions. The code for this can be found in the function_support branch.

I have implemented rudimentary support for latexifying some simple functions:

using Latexify
gamma_pdf(t, n, r) = r^n*t^(n-1)*exp(-r*t)/gamma(n)

latexify(gamma_pdf, (1,1,1))

$\frac{r^{n} \cdot t^{n - 1} \cdot e^{ - r \cdot t}}{\Gamma\left( n \right)}$

(I need to pass a tuple or array of arguments to my gamma_pdf so that Julia can figure out which method to use)

A multi-line example:

function ode(du, u, p, t)
    du[1] = p[1] * u[2] 
    du[2] = - u[1]
end
latexify(ode, (1,1,1,1))

\begin{equation}
\begin{array}{l}
\textrm{du}\left[1\right] = p\left[1\right] \cdot u\left[2\right] \\
\textrm{du}\left[2\right] = - u\left[1\right] \\
\end{array}
\end{equation}

However, this is a bit of a house of cards in two repects: One is that the code is hard to read/understand, which makes the package harder to maintain. The other respect is that It breaks for any operation that is not explicitly defined in latexoperation.

For example:

f(n) = [i for i in 1:n]
latexify(f, (1))

will error.

Support for very simple stuff is achievable. Support for truly general functions should be theoretically possible, but I can't see it being feasible.

Does anyone have feedback on this/is anyone willing and able to implement a better version than I have done myself?

Also, is this worth working further on at all? It has already been a bit of a time-sink.

@korsbo
Copy link
Owner Author

korsbo commented Jul 5, 2019

I'm dropping this for the foreseeable future. The code that I once wrote was for Julia 0.6 and is no longer functional. This would take a long time and it would most likely never be as general as one would like.

@korsbo korsbo closed this as completed Jul 5, 2019
@korsbo
Copy link
Owner Author

korsbo commented May 5, 2020

The following works for later Julia versions.

using MacroTools
using Latexify

f(x, y, z) = x * x / (y + z)
c = @code_lowered f(1., 1., 1.)
exparr = [postwalk(x -> x isa Core.SlotNumber ? c.slotnames[x.id] : x, expr) for expr in c.code]
exparr = [postwalk(x -> x isa Core.GlobalRef ? x.name : x, expr) for expr in exparr]

for i = 1:length(exparr)
    exparr[i] = postwalk(x -> x isa Core.SSAValue ? exparr[x.id] : x, exparr[i])
end

ex = exparr[end].args[1]
latexify(ex)

I have not tested this for any more complicated functions yet.

@tecosaur
Copy link

Required changes to the above for current Julia versions:

using MacroTools: postwalk

ex = exparr[end-1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants