Skip to content

Commit

Permalink
Update wcall signature to be more type stable
Browse files Browse the repository at this point in the history
  • Loading branch information
eswagel committed Jul 29, 2024
1 parent e567886 commit 419e4cf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/Manifest.toml
.vscode/
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ For example, Mathematica excels at solving complicated equations and differentia

## Installation

To run SymbolicsMathLink.jl, both the [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl) package and the [MathLink.jl](https://github.com/JuliaInterop/MathLink.jl) package must be installed **and configured**. That means you must have an active subscription to Mathematica installed on your computer. See details in MathLink.jl for more information, but be aware that it takes some effort.
To run SymbolicsMathLink.jl, both the [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl) package and the [MathLink.jl](https://github.com/JuliaInterop/MathLink.jl) package must be installed **and configured**. That means you must have an active subscription to Mathematica or the free Wolfram Engine installed on your computer. See details in MathLink.jl for more information, but be aware that it takes some effort.

To install SymbolicsMathLink.jl, run the following command in the Julia REPL:

Expand Down Expand Up @@ -46,13 +46,13 @@ Additionally, piecewise functions are supported through `Symbolics.IfElse.ifelse

The package exports only the function `wcall`:
```julia
wcall(head::AbstractString, args...; returnJulia=true, kwargs...)
wcall([returnJulia::Val{bool},] head::AbstractString, args...; kwargs...)
```
`head` is the name of the Mathematica function to be called, for example: `Solve`, or `DSolve`, etc.
`head` is the name of the Mathematica function to be called, for example: `"Solve"`, or `"DSolve"`, etc.
`args` are the arguments to be passed to the `head` function in Mathematica. These will be converted automatically to MathLink objects.
`kwargs` are the keyword arguments to be passed to the `head` function in Mathematica. These will be converted automatically to MathLink objects.

If `returnJulia=true` (default), `wcall` converts the Mathematica result back to Julia Symbolics, and you don't need to worry at all about what's going on under the hood. If you want to manipulate the MathLink result further for any reason, set `returnJulia=false` and it will return the result as a MathLink object.
If `returnJulia` is `Val(true)` (default), `wcall` converts the Mathematica result back to Julia Symbolics, and you don't need to worry at all about what's going on under the hood. If you want to manipulate the MathLink result further for any reason, pass `Val(false)` as the first argument and it will return the result as a MathLink object.

## Use Case: Evaluating Long Functions

Expand All @@ -71,7 +71,7 @@ julia> wcall("ReplaceAll",hermite,[x,10])

## Contributing

Contributions to SymbolicsMathLink.jl are welcome! To contribute, please fork the repository and submit a pull request.
Contributions to SymbolicsMathLink.jl are welcome! To contribute, please submit a pull request.

## License

Expand Down
23 changes: 11 additions & 12 deletions src/wcall.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
function wcall(head::AbstractString, args...; returnJulia=true,kwargs...)
"""
wcall(head::AbstractString, args...; returnJulia=true, kwargs...)
"""
wcall([returnJulia::Val{bool},] head::AbstractString, args...; returnJulia=Val(true), kwargs...)
Calls a Mathematica function on arguments of Symbolics and other Julia types, converting those arguments to Mathematica, and then changing the result back to Julia.
# Arguments
- `returnJulia`: Optional. If `Val(false)`, the function will return a Mathematica MathLink object instead of converting back to Julia.
- `head::AbstractString`: The name of the Mathematica function to call.
- `args...`: The arguments to the Mathematica function. These can be Symbolics, Julia types, or Mathematica types.
- `returnJulia::Bool=true`: If `false`, the function will return a Mathematica MathLink object instead of converting back to Julia.
- `kwargs...`: Keyword arguments to the Mathematica function. These can be Symbolics, Julia types, or Mathematica types.
# Returns
Expand All @@ -25,15 +24,15 @@ function wcall(head::AbstractString, args...; returnJulia=true,kwargs...)
-1 - x
```
"""
return wcall(head, expr_to_mathematica.(args)...;returnJulia=returnJulia, kwargs...)
function wcall(::Val{false}, head::AbstractString, args...; kwargs...)
mathematica_result = wcall(head, args...; kwargs...)
return convert_to_julia(mathematica_result)
end
wcall(head::AbstractString, args::Vararg{Mtypes}; returnJulia=true, kwargs...) = begin
mathematica_result = weval(MathLink.WSymbol(head)(args...; kwargs...))
if returnJulia
return mathematica_to_expr(mathematica_result)
else
return mathematica_result
end

wcall(::Val{true}, head::AbstractString, args...; kwargs...)=begin
return weval(MathLink.WSymbol(head)(args...; kwargs...))
end

wcall(head::AbstractString, args...; kwargs...) = wcall(Val(true), head, args...; kwargs...)

export wcall

0 comments on commit 419e4cf

Please sign in to comment.