Skip to content

Commit

Permalink
Added to help to the Julia file.
Browse files Browse the repository at this point in the history
  • Loading branch information
fremling committed Mar 9, 2024
1 parent 93430d7 commit dbe8c2b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,31 @@ 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.

Some examples or tests that will evaluate to true:

```
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 W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
@test W2Julia(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")
```

W2Julia 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
Printing in Jupyter notebooks is, by default, done in latex.
This can be turned off with the command `MathLink.set_texOutput(false)`
Expand Down
23 changes: 23 additions & 0 deletions src/W2Julia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@

export W2Julia

"""
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.
Some examples or tests that will evaluate to true:
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 W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
@test W2Julia(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")
W2Julia does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present.
"""
W2Julia(X::Vector) = [ W2Julia(x) for x in X]
function W2Julia(X::Dict)
NewDict = Dict()
Expand Down
9 changes: 6 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import MathLink: WExpr, WSymbol

@testset "W2Julia" 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]
Expand All @@ -19,10 +18,14 @@ import MathLink: WExpr, WSymbol
@testset "Association to Dict" begin
@test W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)

@test W2Julia(W`Association["team" -> "HOU", "lastName" -> "Ching"]`) == Dict( "team" => "HOU" , "lastName" => "Ching")
@test W2Julia(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")
end
@testset "Association and List Dict" begin
@test W2Julia(W`Association["team" -> {1,2,3}, "lastName" -> "Ching"]`) == Dict( "team" => [1,2,3] , "lastName" => "Ching")
@test W2Julia(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 W2Julia(W`{1,Association["team" -> {1,2,3}, "lastName" -> "Ching"]}`) == [1,Dict( "team" => [1,2,3] , "lastName" => "Ching")]

end
Expand Down

0 comments on commit dbe8c2b

Please sign in to comment.