Skip to content

Commit

Permalink
feat: Complex numbers
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
musoke committed Jul 24, 2023
1 parent 9eef20c commit e506851
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/WolframExpr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ julia> wexpr_to_expr(W`1. + 1`)
```
"""
function wexpr_to_expr(symb::MathLink.WSymbol)
Symbol(symb.name)
if symb.name == "I"
return :im
else
Symbol(symb.name)
end
end

function wexpr_to_expr(num::Number)
Expand Down Expand Up @@ -139,6 +143,9 @@ function wexpr_to_expr(expr::MathLink.WExpr)::Expr
return Expr(:call, :^, map(wexpr_to_expr, expr.args)...)
elseif expr.head.name == "Rational"
return Expr(:call, ://, map(wexpr_to_expr, expr.args)...)
elseif expr.head.name == "Complex"
@assert length(expr.args) == 2
return Expr(:call, :Complex, map(wexpr_to_expr, expr.args)...)
else
return Expr(:call, Symbol(expr.head.name), map(wexpr_to_expr, expr.args)...)
end
Expand Down
6 changes: 6 additions & 0 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ using WolframExpr

@test string_to_expr("1.+1") == :(1.0 + 1)

@test string_to_expr("I") == :im
@test string_to_expr("1 + 2 I") == :(1 + 2im)
@test eval(string_to_expr("Complex[1, 2]")) == eval(:(1 + 2im))

f = string_to_function("1.+1", [])
@test f() === 2.0

f = string_to_function("1.+2I", [])
@test f() === 1.0 + 2.0im

f = WolframExpr.string_to_function("x+y", [:x, :y])
@test f(1.2, 2) === 3.2

Expand Down

0 comments on commit e506851

Please sign in to comment.