SymPy.jl
SymPy.jl copied to clipboard
Multiplying NumberSymbols turns them into their numerical approximation
When a NumberSymbol such as pi
is multiplied by a number or negated, it is converted to a Float64:
julia> typeof(pi)
Irrational{:π}
julia> typeof(pi*2)
Float64
For comparison, this is the result in python:
>>> type(pi)
<class 'sympy.core.numbers.Pi'>
>>> type(2*pi)
<class 'sympy.core.mul.Mul'>
This breaks any symbolic computation that involves such constants and their properties, for instance:
julia> i = symbols(:i, integer=True)
i
julia> sin(i*pi)
0
julia> sin(i*-pi)
-sin(3.14159265358979⋅i)
Yes, this is true. You have to be wary of eager conversion to floating point before a value is converted to a symbolic value. In this case you can use PI
(which is the same as PI=Sym(pi)
) and the symbolic value will force others numbers to promote to symbolic values.
(As an example. Even this 1/2*PI
will be an issue as 1/2
will be a floating point number. To avoid this you can use rationals, as in 1//2 * pi
, or reexpress, as in PI/2
.
Hope that clarifies.