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

Including overloading of basic algebra #48

Closed
fremling opened this issue Apr 30, 2021 · 10 comments
Closed

Including overloading of basic algebra #48

fremling opened this issue Apr 30, 2021 · 10 comments

Comments

@fremling
Copy link
Collaborator

Hi. Is there a particular reason why the library has chosen not to overload the basic arithmetic operator such as +,-,*,/?

Currently need to add some biolerpate code at the top to e able to things like

x=W"x"
y=W"y"
z=x+y
w=x-y


import Base.+
import Base.*
import Base.-
import Base./
+(a::MathLink.WSymbol,b::MathLink.WSymbol)=weval(W"Plus"(a,b))
+(a::MathLink.WExpr,b::MathLink.WSymbol)=weval(W"Plus"(a,b))
+(a::MathLink.WExpr,b::MathLink.WExpr)=weval(W"Plus"(a,b))
-(a::MathLink.WSymbol)=weval(W"Minus"(a))
-(a::MathLink.WExpr)=weval(W"Minus"(a))
-(a::MathLink.WSymbol,b::MathLink.WSymbol)=weval(W"Plus"(a,W"Minus"(b)))
-(a::MathLink.WExpr,b::MathLink.WSymbol)=weval(W"Plus"(a,W"Minus"(b)))
-(a::MathLink.WExpr,b::MathLink.WExpr)=weval(W"Plus"(a,W"Minus"(b)))
*(a::MathLink.WExpr,b::MathLink.WExpr)=weval(W"Times"(a,b))
*(a::MathLink.WSymbol,b::MathLink.WExpr)=weval(W"Times"(a,b))
*(a::MathLink.WExpr,b::MathLink.WSymbol)=weval(W"Times"(a,b))
*(a::MathLink.WSymbol,b::MathLink.WSymbol)=weval(W"Times"(a,b))
/(a::MathLink.WSymbol,b::MathLink.WSymbol)=weval(W"Times"(a, W"Power"(b, -1)))
/(a::MathLink.WExpr,b::Number)=weval(W"Times"(a, W"Power"(b, -1)))

@simonbyrne
Copy link
Member

I was originally trying to keep this as a backend package, closely mirroring WSTP/MathLink itself, and build another package as the frontend, but never got around to that.

This would also result in eager evaluation, which would give different semantics from the underlying Wolfram language.

@fremling
Copy link
Collaborator Author

fremling commented May 3, 2021

I see. I'm not an expert of the inner workings of mathematica, but I guess the , the weval could easily be dropped to avoid eager evaluation.

For new users of the package however, having the most basic operations +, -, *, / overloaded would still be quite useful and pedagogic.

@fremling
Copy link
Collaborator Author

After playing around with your package now for a while i have collected quite a bunch of auxiliary functions that make sense to me.
I'm attaching the files here since i'm not very familiar with the pull request architecture (and I'm also not sure where you would want the functions).

The files contain overloading of functions +,-,*,/,^ as well as a switch MLGreedyEval (default is false) to toggle greedy or delayed evluation.
There are also functions that augment the put function to handle julia fractions as well as complex numbers.

Further is there a function `W2Mstr that can translate the ML representation to a string that can be pasted directly into the mathematica editor (quite useful)

Further there is workaround that allows put to handle Int128 by performing a prime factorization (since it's currently not suported).

The functions also comes with some basic tests (see attachments in zip file).

I'd love to make this parts of the package, so if there is anything i can do to help please let me know.

MathLinkHeaders.zip

@simonbyrne
Copy link
Member

to be honest, my time is limited so that I really only have time to do bug fixes. Your best option might be to just create a new package (which is fairly simple in Julia), and add this as a dependency.

@simonbyrne
Copy link
Member

Unless of course someone wants to take over maintenance.

@fremling
Copy link
Collaborator Author

I think you are correct that my wished would better fit a new package. Let me therefore present MathLinkExtras, where this is all implemented. Hopefully it can make it al the way to be a full fledged julia package.

@simonbyrne
Copy link
Member

simonbyrne commented Sep 14, 2021

Thanks: I'm sure there are others that would find that useful!

Further there is workaround that allows put to handle Int128 by performing a prime factorization (since it's currently not suported).

It should be possible to handle this by passing it as a string, as we do for BigInt:

function put(link::Link, x::BigInt)
put(link, WInteger(string(x)))
end
function get(link::Link, ::Type{BigInt})
parse(BigInt, get(link, WInteger).value)
end

I'd be happy to add that, if you want to open a PR?

@fremling
Copy link
Collaborator Author

Hi, if it's that easy to mend that would be great.

I'd love to open a PR for it but I can't seem to install the package from the repository source.
If i try i get an error that "deps/deps.jl" is missing.

julia> using MathLink
[ Info: Precompiling MathLink [18c93696-a329-5786-9845-8443133fa0b4]
ERROR: LoadError: could not open file /home/fremling/...../MathLink.jl/deps/deps.jl
Stacktrace:
[1] include at ./boot.jl:317 [inlined]

@simonbyrne
Copy link
Member

Yeah, you have to Pkg.build() it manually when using the source.

@fremling
Copy link
Collaborator Author

Ah, silly me. Now it works :)
I also created a PR for the update. I think we can close this issue now :)

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