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

Define Base.print for WExpr #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ julia> W"Sin"
W"Sin"

julia> sin1 = W"Sin"(1.0)
W"Sin(1.0)"
W"Sin"(1.0)

julia> sinx = W"Sin"(W"x")
W"Sin"(W"x")

julia> int = W"Integrate"(sinx, (W"x", 0, 1))
W"Integrate"(W"Sin"(W"x"), (W"x", 0, 1))

julia> println(int)
Integrate[Sin[x], {x, 0, 1}]
```

To parse an expression in the Wolfram Language, you can use the `W` cmd macro (note the backticks):
Expand All @@ -44,7 +50,7 @@ julia> weval(sin1)
julia> weval(sinx)
W"Sin"(W"x")

julia> weval(W"Integrate"(sinx, (W"x", 0, 1)))
julia> weval(int)
W"Plus"(1, W"Times"(-1, W"Cos"(1)))
```

Expand Down
21 changes: 21 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ struct WExpr
args
end

function Base.print(io::IO, w::WExpr)
print(io, w.head)
print(io, "[")
isfirst = true
for arg in w.args
if !isfirst
print(io, ", ")
else
isfirst = false
end
if typeof(arg) <: Union{AbstractVector, Tuple}
print(io, "{")
join(io, arg, ", ")
print(io, "}")
else
print(io, arg)
end
end
print(io, ']')
end

function Base.show(io::IO, w::WExpr)
show(io, w.head)
print(io, '(')
Expand Down