Skip to content

Commit

Permalink
Merge pull request #621 from JuliaHomotopyContinuation/conversion_bug
Browse files Browse the repository at this point in the history
Fix the bug in convert(type, Expression) from #615
  • Loading branch information
PBrdng authored Feb 14, 2025
2 parents 1638f9c + f89da5a commit 9f32884
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/model_kit/symengine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -701,19 +701,29 @@ function Base.convert(::Type{BigInt}, c::Basic)
end

function Base.convert(::Type{BigFloat}, c::Basic)
a = BigFloat()
ccall(
(:real_mpfr_get, libsymengine),
Nothing,
(Ref{BigFloat}, Ref{ExpressionRef}),
a,
c,
)
return a
if class(c) == :Integer
c1 = convert(Int, c)
return convert(BigFloat, c1)
else
a = BigFloat()
ccall(
(:real_mpfr_get, libsymengine),
Nothing,
(Ref{BigFloat}, Ref{ExpressionRef}),
a,
c,
)
return a
end
end

function Base.convert(::Type{Float64}, c::Basic)
return ccall((:real_double_get_d, libsymengine), Cdouble, (Ref{ExpressionRef},), c)
if class(c) == :Integer
c1 = convert(Int, c)
return convert(Float64, c1)
else
return ccall((:real_double_get_d, libsymengine), Cdouble, (Ref{ExpressionRef},), c)
end
end

function Base.real(x::Basic)
Expand Down
24 changes: 24 additions & 0 deletions test/model_kit/symbolic_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,28 @@
H_any = Homotopy(convert(Vector{Any}, h), [x, y, z], t)
@test H_any isa Homotopy
end

@testset "Convert" begin

@var x

s = x + 1 - x
@test convert(Int, s) == 1
@test convert(Int32, s) == Int32(1)
@test convert(Int64, s) == Int64(1)
@test convert(BigInt, s) == BigInt(1)
@test convert(BigFloat, s) == BigFloat(1)
@test convert(Float64, s) == Float64(1)
@test convert(ComplexF64, s) == ComplexF64(1)

r = x + 1.0 - x
@test convert(Float64, r) == Float64(1)
@test convert(ComplexF64, r) == ComplexF64(1)

u = x + BigFloat(1.0) - x
@test convert(BigFloat, u) == BigFloat(1.0)

t = x + (1.0 + 0.0im) - x
@test convert(ComplexF64, t) == ComplexF64(1)
end
end

0 comments on commit 9f32884

Please sign in to comment.