Skip to content

Commit 26ee669

Browse files
authored
Merge pull request #60 from fremling/master
Included the W2Mstr function
2 parents 50c8b29 + 98d0ad9 commit 26ee669

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,43 @@ julia> P12 * [W"a" W"b" ; W`a+b` 2] == [ W"b" 2-W"b" ; W"a" W"b"]
132132
true
133133
```
134134

135+
136+
## W2Mstr - Mathematica conversion
137+
Sometimes one wants to be able to read the Julia MathLink expressions back into Mathematica. For that purpose, `W2Mstr` is also supplied. This implementation is currently quite defensive with parentheses, which gives a more verbose output than necessary. Here are a few examples
138+
139+
```julia
140+
julia> W2Mstr(W`x`)
141+
"x"
142+
143+
julia> W2Mstr(W"Sin"(W"x"))
144+
"Sin[x]"
145+
146+
julia> W2Mstr(weval(W`a + c + v`))
147+
"(a + c + v)"
148+
149+
julia> W2Mstr(weval(W`a^(b+c)`))
150+
"(a^(b + c))"
151+
152+
julia> W2Mstr(weval(W`e+a^(b+c)`))
153+
"((a^(b + c)) + e)"
154+
155+
julia> W2Mstr(W"a"+W"c"+W"v"+W"Sin"(2 +W"x" + W"Cos"(W"q")))
156+
"(a + c + v + Sin[(2 + x + Cos[q])])"
157+
158+
julia> W2Mstr(im*2)
159+
"(2*I)"
160+
161+
julia> W2Mstr(weval(W"Complex"(W"c",W"b")))
162+
"(c+b*I)"
163+
164+
julia> W2Mstr(W"c"+im*W"b")
165+
"(((1*I)*b) + c)"
166+
167+
julia> W2Mstr(W`b/(c^(a+c))`)
168+
"(b*((c^(a + c))^-1))"
169+
```
170+
171+
135172
## LateX printing in JuPyter Notebooks
136173
Printing in Juypter notebooks is by defaults done in latex.
137174
This can be turned off with the command `MathLink.set_texOutput(false)`

src/display.jl

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,116 @@ function Base.show(io,::MIME"text/latex",x::MathLink.WExpr)
9393
end
9494

9595

96+
export Wprint
97+
98+
####..............
99+
100+
function Wprint(WExpr)
101+
weval(W"Print"(WExpr))
102+
return
103+
end
104+
105+
export W2Mstr
106+
107+
###function that prints the W-form back to Mathematica Input-language
108+
109+
W2Mstr(x::Number) = "$x"
110+
W2Mstr(z::Complex) = W2Mstr(WComplex(z))
111+
W2Mstr(x::Rational) = "($(x.num)/$(x.den))"
112+
function W2Mstr(x::Array)
113+
Dim = length(size(x))
114+
Str="{"
115+
for j in 1:size(x)[1]
116+
if j>1
117+
Str*=","
118+
end
119+
if Dim == 1
120+
Str*=W2Mstr(x[j])
121+
elseif Dim == 2
122+
Str*=W2Mstr(x[j,:])
123+
else
124+
###Arrays with larger dimension: $Dim != 1,2 not implemented yet
125+
end
126+
end
127+
Str*="}"
128+
return Str
129+
end
130+
131+
W2Mstr(x::MathLink.WSymbol) = x.name
132+
function W2Mstr(x::MathLink.WExpr)
133+
#println("W2Mstr::",x.head.name)
134+
if x.head.name == "Plus"
135+
Str = W2Mstr_PLUS(x.args)
136+
elseif x.head.name == "Times"
137+
Str = W2Mstr_TIMES(x.args)
138+
elseif x.head.name == "Power"
139+
Str = W2Mstr_POWER(x.args)
140+
elseif x.head.name == "Complex"
141+
Str = W2Mstr_COMPLEX(x.args)
142+
else
143+
Str=x.head.name*"["
144+
for j in 1:length(x.args)
145+
if j>1
146+
Str*=","
147+
end
148+
Str*=W2Mstr(x.args[j])
149+
end
150+
Str*="]"
151+
end
152+
return Str
153+
end
154+
155+
function W2Mstr_PLUS(x::Union{Array,Tuple})
156+
#println("W2Mstr_PLUS:",x)
157+
Str="("
158+
for j in 1:length(x)
159+
if j>1
160+
Str*=" + "
161+
end
162+
Str*=W2Mstr(x[j])
163+
end
164+
Str*=")"
165+
end
166+
167+
function W2Mstr_TIMES(x::Union{Array,Tuple})
168+
#println("W2Mstr_TIMES:",x)
169+
Str="("
170+
for j in 1:length(x)
171+
if j>1
172+
Str*="*"
173+
end
174+
Str*=W2Mstr(x[j])
175+
end
176+
Str*=")"
177+
end
178+
179+
180+
function W2Mstr_POWER(x::Union{Array,Tuple})
181+
#println("W2Mstr_POWER:",x)
182+
if length(x) != 2
183+
error("Power takes two arguments")
184+
end
185+
Str="("*W2Mstr(x[1])*"^"*W2Mstr(x[2])*")"
186+
end
187+
188+
189+
190+
function W2Mstr_COMPLEX(x::Union{Tuple,Array})
191+
#println("W2Mstr_COMPLEX:",x)
192+
if length(x) != 2
193+
error("Complex takes two arguments")
194+
end
195+
if x[1] == 0
196+
###Imaginary
197+
Str="("*W2Mstr(x[2])*"*I)"
198+
elseif x[2] == 0
199+
### Real
200+
###Complex
201+
Str=W2Mstr(x[1])
202+
else
203+
###Complex
204+
Str="("*W2Mstr(x[1])*"+"*W2Mstr(x[2])*"*I)"
205+
end
206+
end
207+
96208

test/runtests.jl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,71 @@ import MathLink: WExpr, WSymbol
55

66

77

8+
@testset "W2Mstr" begin
9+
###Test of a naive MathLink to Mathematica converter function (to resuts can be copied into mathematica directly"
10+
11+
@testset "Basic Algebra" begin
12+
@test W2Mstr(W"a") == "a"
13+
@test W2Mstr(W"x") == "x"
14+
@test W2Mstr(W"x"+W"y") == "(x + y)"
15+
@test W2Mstr(W`Sqrt[a + b]`) == "Sqrt[(a + b)]"
16+
@test W2Mstr(W`Pow[x,2]`) == "Pow[x,2]"
17+
@test W2Mstr(W`x^2`) == "(x^2)"
18+
@test W2Mstr(W`a+b`) == "(a + b)"
19+
@test W2Mstr(weval(W`a + c + v`)) == "(a + c + v)"
20+
@test W2Mstr(2) == "2"
21+
@test W2Mstr(W`x`) == "x"
22+
@test W2Mstr(W"Sin"(W"x")) == "Sin[x]"
23+
@test W2Mstr(W`Sin[x]`) == "Sin[x]"
24+
25+
end
26+
@testset "Nested functions" begin
27+
@test W2Mstr(weval(W`a + c*b + v`)) == "(a + (b*c) + v)"
28+
@test W2Mstr(weval(W`(a + c)*(b + v)`)) == "((a + c)*(b + v))"
29+
@test W2Mstr(weval(W`a^(b+c)`)) == "(a^(b + c))"
30+
@test W2Mstr(weval(W`a^2`)) == "(a^2)"
31+
@test W2Mstr(weval(W`e+a^(b+c)`)) == "((a^(b + c)) + e)"
32+
@test W2Mstr(weval(W`a + c + v + Sin[2 + x + Cos[q]]`)) == "(a + c + v + Sin[(2 + x + Cos[q])])"
33+
set_GreedyEval(true)
34+
@test W2Mstr(W"a"+W"c"+W"v"+W"Sin"(2 +W"x" + W"Cos"(W"q"))) == "(a + c + v + Sin[(2 + x + Cos[q])])"
35+
set_GreedyEval(false)
36+
@test W2Mstr(W"a"+W"c"+W"v"+W"Sin"(2 +W"x" + W"Cos"(W"q"))) == "(((a + c) + v) + Sin[((2 + x) + Cos[q])])"
37+
38+
@test W2Mstr(W`Sqrt[x+Sin[y]+z^(3/2)]`) == "Sqrt[(x + Sin[y] + (z^(3*(2^-1))))]"
39+
40+
end
41+
42+
@testset "Complex values" begin
43+
@test W2Mstr(weval(W`2*I`)) == "(2*I)"
44+
@test W2Mstr(weval(W`2/I`)) == "(-2*I)"
45+
@test W2Mstr(W`2 + 0*I`) == "(2 + (0*I))"
46+
@test W2Mstr(W"Complex"(W"c",0)) == "c"
47+
@test W2Mstr(weval(W"Complex"(W"c",0))) == "c"
48+
@test W2Mstr(weval(W"Complex"(W"c",W"b"))) == "(c+b*I)"
49+
@test W2Mstr(im) == "(1*I)"
50+
@test W2Mstr(2*im) == "(2*I)"
51+
end
52+
53+
@testset "Factions" begin
54+
@test W2Mstr(W`3/4`) == "(3*(4^-1))"
55+
@test W2Mstr(3//4) == "(3/4)"
56+
@test W2Mstr(W`b/c`) == "(b*(c^-1))"
57+
@test W2Mstr(W`b/(c^(a+c))`) == "(b*((c^(a + c))^-1))"
58+
@test W2Mstr(W`(b^2)/(c^3)`) == "((b^2)*((c^3)^-1))"
59+
@test W2Mstr(weval(W`(b^2)/(c^3)`)) == "((b^2)*(c^-3))"
60+
end
61+
62+
@testset "Lists & Arrays" begin
63+
@test W2Mstr([W`x`,W`a`]) == "{x,a}"
64+
@test W2Mstr([W`x`]) == "{x}"
65+
@test W2Mstr([W`x` W`y`; W`z` W`x`]) == "{{x,y},{z,x}}"
66+
end
67+
68+
end
69+
70+
71+
72+
873
@testset "integers" begin
974
w = W"Factorial"(30)
1075
@test_throws MathLink.MathLinkError weval(Int, w)

0 commit comments

Comments
 (0)