Skip to content

Commit 11fa187

Browse files
authored
Merge pull request #88 from fremling/master
Change name W2Julia => W2JuliaStrucht, and First version ofW2JuliaExpr
2 parents 002f058 + f6942e9 commit 11fa187

File tree

6 files changed

+145
-96
lines changed

6 files changed

+145
-96
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,27 @@ julia> W2Mstr(W`b/(c^(a+c))`)
164164

165165

166166

167-
## W2Julia - Conversion to Julia structures
168-
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.
167+
## W2JuliaStruct - Conversion to Julia structures
168+
`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.
169169

170170
Some examples or tests that will evaluate to true:
171171

172172
```julia
173173
using Test
174-
@test W2Julia(W`{1,2,3}`) == [1,2,3]
175-
@test W2Julia([1,2,3]) == [1,2,3]
176-
@test W2Julia(W`{1,2,3}`) == [1,2,3]
177-
@test W2Julia(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
178-
@test W2Julia([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]
174+
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
175+
@test W2JuliaStruct([1,2,3]) == [1,2,3]
176+
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
177+
@test W2JuliaStruct(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
178+
@test W2JuliaStruct([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]
179179

180-
@test W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
180+
@test W2JuliaStruct(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
181181

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

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

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

189189

190190
## LateX printing in JuPyter Notebooks

src/MathLink.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ include("extras.jl")
2929
include("eval.jl")
3030
include("display.jl")
3131
include("operators.jl")
32-
include("W2Julia.jl")
32+
include("W2JuliaStruct.jl")
33+
include("W2JuliaExpr.jl")
3334
end

src/W2Julia.jl

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/W2JuliaExpr.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
export W2JuliaExpr
4+
5+
"""
6+
Converts MathLink `WExpr`essions to Julia `Expr`essions
7+
"""
8+
function W2JuliaExpr(wexpr::WExpr)
9+
####Check if the operator has an alias
10+
if haskey(funcDict, wexpr.head.name)
11+
####Use the alias
12+
Operator=funcDict[wexpr.head.name]
13+
else
14+
####Use the name directly as a symbol (but in lowercase)
15+
Operator=Symbol(lowercase(wexpr.head.name))
16+
end
17+
return Expr(:call,Operator,[W2JuliaExpr(arg) for arg in wexpr.args]...)
18+
end
19+
W2JuliaExpr(wexpr::WSymbol) = Symbol(wexpr.name)
20+
W2JuliaExpr(wexpr::Number) = wexpr
21+
22+
23+
###Dictionary with known funcitons and their translations
24+
funcDict=Dict("Plus"=>:+,
25+
"Minus"=>:-,
26+
"Power"=>:^,
27+
"Times"=>:*,
28+
)

src/W2JuliaStruct.jl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
3+
export W2JuliaStruct
4+
5+
"""
6+
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.
7+
8+
Some examples or tests that will evaluate to true:
9+
10+
using Test
11+
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
12+
@test W2JuliaStruct([1,2,3]) == [1,2,3]
13+
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
14+
@test W2JuliaStruct(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
15+
@test W2JuliaStruct([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]
16+
17+
@test W2JuliaStruct(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
18+
19+
20+
@test W2JuliaStruct(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")
21+
22+
@test W2JuliaStruct(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C")
23+
24+
25+
W2JuliaStruct does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present.
26+
27+
"""
28+
W2JuliaStruct(X::Vector) = [ W2JuliaStruct(x) for x in X]
29+
function W2JuliaStruct(X::Dict)
30+
NewDict = Dict()
31+
for key in keys(X)
32+
NewDict[key] = W2JuliaStruct(X[key])
33+
end
34+
return NewDict
35+
end
36+
37+
38+
39+
W2JuliaStruct(X::Number) = X
40+
W2JuliaStruct(X::String) = X
41+
W2JuliaStruct(X::MathLink.WSymbol) = X
42+
function W2JuliaStruct(X::MathLink.WExpr)
43+
if X.head == W"List"
44+
return W2JuliaStruct.(X.args)
45+
elseif X.head == W"Association"
46+
W2JuliaStructAccociation(X)
47+
else
48+
return X
49+
end
50+
end
51+
52+
53+
function W2JuliaStructAccociation(Asso::MathLink.WExpr)
54+
if Asso.head != W"Association"
55+
error("Not an Association")
56+
end
57+
##Wprint(Asso)
58+
if Asso.head != W"Association"
59+
error("Not an association")
60+
end
61+
D=Dict()
62+
for Rule in Asso.args
63+
if Rule.head != W"Rule"
64+
error("not a rule")
65+
end
66+
if length(Rule.args) != 2
67+
error("Bad rule")
68+
end
69+
D[Rule.args[1]]=W2JuliaStruct(Rule.args[2])
70+
end
71+
return D
72+
end

test/runtests.jl

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,49 @@ using Test
44
import MathLink: WExpr, WSymbol
55

66

7-
@testset "W2Julia" begin
7+
@testset "W2JuliaExpr" begin
8+
###Test of a simple MathLink to Julia converter. It converts MathLink expressions to the correcsponding Julia constructions
9+
@testset "Variables" begin
10+
@test W2JuliaExpr(W"a") == :a
11+
@test W2JuliaExpr(W`a`) == :a
12+
@test W2JuliaExpr(W"a"+W"b") == :(a+b)
13+
@test W2JuliaExpr(W`sin`) == :sin
14+
@test W2JuliaExpr(W`a+b`) == :(a+b)
15+
@test W2JuliaExpr(W`a*b`) == :(a*b)
16+
@test W2JuliaExpr(W`Sin[a]`) == :(sin(a))
17+
@test W2JuliaExpr(W`Sin[a+b]`) == :(sin(a+b))
18+
@test W2JuliaExpr(W`Cos[a^b]`) == :(cos(a^b))
19+
@test W2JuliaExpr(W`a/b`) == :(a*(b^-1))
20+
@test W2JuliaExpr(W`a^b`) == :(a^b)
21+
@test W2JuliaExpr(W`Exp[a]`) == :(exp(a))
22+
end
23+
end
24+
25+
26+
27+
@testset "W2JuliaStruct" begin
828
###Test of a simple MathLink to Julia converter. It converts MathLink expressions to the correcsponding Julia constructions
929
@testset "Lists to Lists" begin
10-
@test W2Julia(W`{1,2,3}`) == [1,2,3]
11-
@test W2Julia([1,2,3]) == [1,2,3]
12-
@test W2Julia([1,2.2,3]) == [1,2.2,3]
13-
@test W2Julia(W`{1,a,3}`) == [1,W"a",3]
14-
@test W2Julia(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
15-
@test W2Julia([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]
30+
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
31+
@test W2JuliaStruct([1,2,3]) == [1,2,3]
32+
@test W2JuliaStruct([1,2.2,3]) == [1,2.2,3]
33+
@test W2JuliaStruct(W`{1,a,3}`) == [1,W"a",3]
34+
@test W2JuliaStruct(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
35+
@test W2JuliaStruct([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]
1636

1737
end
1838
@testset "Association to Dict" begin
19-
@test W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
39+
@test W2JuliaStruct(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
2040

21-
@test W2Julia(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")
41+
@test W2JuliaStruct(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")
2242
end
2343
@testset "Association and List Dict" begin
24-
@test W2Julia(W`Association["A" -> {1,2,3}, "B" -> "C"]`) == Dict( "A" => [1,2,3] , "B" => "C")
44+
@test W2JuliaStruct(W`Association["A" -> {1,2,3}, "B" -> "C"]`) == Dict( "A" => [1,2,3] , "B" => "C")
2545

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

2848

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

3151
end
3252

0 commit comments

Comments
 (0)