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

Change name W2Julia => W2JuliaStrucht, and First version ofW2JuliaExpr #88

Merged
merged 4 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,27 @@ julia> W2Mstr(W`b/(c^(a+c))`)



## W2Julia - Conversion to Julia structures
W2Julia is designed primarily to convert wolfram structures to Julia structures. This includes conversions of Mathematica lists to Julia vectors and Mathematica associations to Julia dictionaries.
## W2JuliaStruct - Conversion to Julia structures
`W2JuliaStruct` is designed primarily to convert wolfram structures to Julia structures. This includes conversions of Mathematica lists to Julia vectors and Mathematica associations to Julia dictionaries.

Some examples or tests that will evaluate to true:

```julia
using Test
@test W2Julia(W`{1,2,3}`) == [1,2,3]
@test W2Julia([1,2,3]) == [1,2,3]
@test W2Julia(W`{1,2,3}`) == [1,2,3]
@test W2Julia(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
@test W2Julia([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
@test W2JuliaStruct([1,2,3]) == [1,2,3]
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
@test W2JuliaStruct(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
@test W2JuliaStruct([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]

@test W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
@test W2JuliaStruct(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)

@test W2Julia(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")
@test W2JuliaStruct(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")

@test W2Julia(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C")
@test W2JuliaStruct(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C")
```

W2Julia does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present.
`W2JuliaStruct` does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present.


## LateX printing in JuPyter Notebooks
Expand Down
3 changes: 2 additions & 1 deletion src/MathLink.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ include("extras.jl")
include("eval.jl")
include("display.jl")
include("operators.jl")
include("W2Julia.jl")
include("W2JuliaStruct.jl")
include("W2JuliaExpr.jl")
end
72 changes: 0 additions & 72 deletions src/W2Julia.jl

This file was deleted.

28 changes: 28 additions & 0 deletions src/W2JuliaExpr.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


export W2JuliaExpr

"""
Converts MathLink `WExpr`essions to Julia `Expr`essions
"""
function W2JuliaExpr(wexpr::WExpr)
####Check if the operator has an alias
if haskey(funcDict, wexpr.head.name)
####Use the alias
Operator=funcDict[wexpr.head.name]
else
####Use the name directly as a symbol (but in lowercase)
Operator=Symbol(lowercase(wexpr.head.name))
end
return Expr(:call,Operator,[W2JuliaExpr(arg) for arg in wexpr.args]...)
end
W2JuliaExpr(wexpr::WSymbol) = Symbol(wexpr.name)
W2JuliaExpr(wexpr::Number) = wexpr


###Dictionary with known funcitons and their translations
funcDict=Dict("Plus"=>:+,
"Minus"=>:-,
"Power"=>:^,
"Times"=>:*,
)
72 changes: 72 additions & 0 deletions src/W2JuliaStruct.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@


export W2JuliaStruct

"""
W2JuliaStruct is designed primarily to convert wolfram structures to Julia structures. This includes conversions of Mathematica lists to Julia vectors and Mathematica associations to Julia dictionaries.

Some examples or tests that will evaluate to true:

using Test
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
@test W2JuliaStruct([1,2,3]) == [1,2,3]
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
@test W2JuliaStruct(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
@test W2JuliaStruct([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]

@test W2JuliaStruct(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)


@test W2JuliaStruct(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")

@test W2JuliaStruct(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C")


W2JuliaStruct does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present.

"""
W2JuliaStruct(X::Vector) = [ W2JuliaStruct(x) for x in X]
function W2JuliaStruct(X::Dict)
NewDict = Dict()
for key in keys(X)
NewDict[key] = W2JuliaStruct(X[key])
end
return NewDict
end



W2JuliaStruct(X::Number) = X
W2JuliaStruct(X::String) = X
W2JuliaStruct(X::MathLink.WSymbol) = X
function W2JuliaStruct(X::MathLink.WExpr)
if X.head == W"List"
return W2JuliaStruct.(X.args)
elseif X.head == W"Association"
W2JuliaStructAccociation(X)
else
return X
end
end


function W2JuliaStructAccociation(Asso::MathLink.WExpr)
if Asso.head != W"Association"
error("Not an Association")
end
##Wprint(Asso)
if Asso.head != W"Association"
error("Not an association")
end
D=Dict()
for Rule in Asso.args
if Rule.head != W"Rule"
error("not a rule")
end
if length(Rule.args) != 2
error("Bad rule")
end
D[Rule.args[1]]=W2JuliaStruct(Rule.args[2])
end
return D
end
44 changes: 32 additions & 12 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,49 @@ using Test
import MathLink: WExpr, WSymbol


@testset "W2Julia" begin
@testset "W2JuliaExpr" begin
###Test of a simple MathLink to Julia converter. It converts MathLink expressions to the correcsponding Julia constructions
@testset "Variables" begin
@test W2JuliaExpr(W"a") == :a
@test W2JuliaExpr(W`a`) == :a
@test W2JuliaExpr(W"a"+W"b") == :(a+b)
@test W2JuliaExpr(W`sin`) == :sin
@test W2JuliaExpr(W`a+b`) == :(a+b)
@test W2JuliaExpr(W`a*b`) == :(a*b)
@test W2JuliaExpr(W`Sin[a]`) == :(sin(a))
@test W2JuliaExpr(W`Sin[a+b]`) == :(sin(a+b))
@test W2JuliaExpr(W`Cos[a^b]`) == :(cos(a^b))
@test W2JuliaExpr(W`a/b`) == :(a*(b^-1))
@test W2JuliaExpr(W`a^b`) == :(a^b)
@test W2JuliaExpr(W`Exp[a]`) == :(exp(a))
end
end



@testset "W2JuliaStruct" begin
###Test of a simple MathLink to Julia converter. It converts MathLink expressions to the correcsponding Julia constructions
@testset "Lists to Lists" begin
@test W2Julia(W`{1,2,3}`) == [1,2,3]
@test W2Julia([1,2,3]) == [1,2,3]
@test W2Julia([1,2.2,3]) == [1,2.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([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
@test W2JuliaStruct([1,2,3]) == [1,2,3]
@test W2JuliaStruct([1,2.2,3]) == [1,2.2,3]
@test W2JuliaStruct(W`{1,a,3}`) == [1,W"a",3]
@test W2JuliaStruct(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
@test W2JuliaStruct([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]

end
@testset "Association to Dict" begin
@test W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
@test W2JuliaStruct(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)

@test W2Julia(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")
@test W2JuliaStruct(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")
end
@testset "Association and List Dict" begin
@test W2Julia(W`Association["A" -> {1,2,3}, "B" -> "C"]`) == Dict( "A" => [1,2,3] , "B" => "C")
@test W2JuliaStruct(W`Association["A" -> {1,2,3}, "B" -> "C"]`) == Dict( "A" => [1,2,3] , "B" => "C")

@test W2Julia(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C")
@test W2JuliaStruct(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C")


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

end

Expand Down
Loading