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

Conversion to julia expressions #16

Open
BeastyBlacksmith opened this issue Sep 3, 2019 · 8 comments
Open

Conversion to julia expressions #16

BeastyBlacksmith opened this issue Sep 3, 2019 · 8 comments

Comments

@BeastyBlacksmith
Copy link
Contributor

Being able to convert WExprs to julia Expr, such that you can define julia functions, that you derived using Mathematica functions would be very nice to have.

@simonbyrne
Copy link
Member

simonbyrne commented Sep 4, 2019

We need some sort of way to match up functions between Julia and Mathematica. This is obvious in some cases (e.g. sin => W"Sin"), but some functions differ in their usage or how certain arguments are handled (e.g. sum vs W"Sum").

@BeastyBlacksmith
Copy link
Contributor Author

Is there a way we could query the signatures of the Mathematica functions, such that we could build a mapping via codegen? ( Check, if a method with a matching signature exists and if not, define a bridge )

@simonbyrne
Copy link
Member

Not that I know of. MathLink only sees symbol names, it has no semantic knowledge.

This may be better handled in another package built on top of this.

@seadra
Copy link

seadra commented Aug 8, 2020

Even a version which doesn't translate function calls would be immensely useful

@musoke
Copy link

musoke commented Jul 6, 2023

@Lightup1
Copy link

Lightup1 commented Jul 24, 2023

Is it possible to convert a pure complex number in type of WExpr to a julia ComplexF64?
I recently used Meta.parse(W2Mstr(WExpr)) for real numbers and eval(Meta.parse(replace(W2Mstr(weval(WExpr)),"I"=>"im"))) for complex numbers. Function like pyconvert in PythonCall.jl is preferred.

@musoke
Copy link

musoke commented Jul 24, 2023

I extracted part of @AmplitudeGravity's demo above and extended it in a package here: https://github.com/musoke/WolframExpr.jl. It converts Mathematica/Wolfram expressions to Julia expressions and functions. It only knows how to translate a handful of functions out of the box, but enough for complex rational functions. You can tell it how other functions are defined.

julia> using MathLink, WolframExpr

julia> s = "(1 + G[x])/(1 + x + I*x^2)";

julia> f = string_to_function(s, [:G, :x]);

julia> g(x) = im * x + x^2;

julia> f(g, 1)
1.0 + 0.0im

@fremling
Copy link
Collaborator

Hi, based on ticked (#72) I took the liberty to add the feature (`W2Julia ) in into the 0.5.3 release of today (https://github.com/JuliaInterop/MathLink.jl/releases/tag/v0.5.3). It aims to convert MathLink objects to Julia style objects.

For instance it can convert the "Assumptions" to a dictionary. Below are some examples:

@test W2Julia(W`{1,2,3}`) == [1,2,3]
@test W2Julia(W`{1,a,3}`) == [1,W"a",3]
@test W2Julia(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
@test W2Julia(W`Association["team" -> "HOU", "lastName" -> "Ching"]`) == Dict( "team" => "HOU" , "lastName" => "Ching")
@test W2Julia(W`Association["team" -> {1,2,3}, "lastName" -> "Ching"]`) == Dict( "team" => [1,2,3] , "lastName" => "Ching")
@test W2Julia(W`{1,Association["team" -> {1,2,3}, "lastName" -> "Ching"]}`) == [1,Dict( "team" => [1,2,3] , "lastName" => "Ching")]

As this is almost at the embryo stage, feel free to report strange (or lacking) behavior i other tickets.

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

6 participants