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

implement fallback conj to fix dot #233

Merged
merged 3 commits into from
Oct 1, 2021
Merged
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
11 changes: 8 additions & 3 deletions src/numerics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ function N(b::BasicType)
out = evalf(b)
imag(out) == Basic(0.0) ? real(out) : out
end


## Conversions SymEngine -> Julia

## Conversions SymEngine -> Julia
function as_numer_denom(x::Basic)
a, b = Basic(), Basic()
ccall((:basic_as_numer_denom, libsymengine), Nothing, (Ref{Basic}, Ref{Basic}, Ref{Basic}), a, b, x)
Expand All @@ -175,6 +175,11 @@ imag(x::BasicType{Val{:RealMPFR}}) = Basic(0)
imag(x::BasicType{Val{:Rational}}) = Basic(0)
imag(x::SymEngine.BasicType) = throw(InexactError())

# Because of the definitions above, `real(x) == x` for `x::Basic`
# such as `x = symbols("x")`. Thus, it is consistent to define the
# fallback
Base.conj(x::Basic) = 2 * real(x) - x

## define convert(T, x) methods leveraging N()
convert(::Type{Float64}, x::Basic) = convert(Float64, N(evalf(x, 53, true)))
convert(::Type{BigFloat}, x::Basic) = convert(BigFloat, N(evalf(x, precision(BigFloat), true)))
Expand Down Expand Up @@ -203,7 +208,7 @@ isless(x::Basic, y::Basic) = isless(N(x), N(y))


## These should have support in symengine-wrapper, but currently don't
trunc(x::Basic, args...) = Basic(trunc(N(x), args...))
trunc(x::Basic, args...) = Basic(trunc(N(x), args...))
trunc(::Type{T},x::Basic, args...) where {T <: Integer} = convert(T, trunc(x,args...))

ceil(x::Basic) = Basic(ceil(N(x)))
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,12 @@ end
@test_throws DomainError sin(zoo)
@test_throws DomainError sin(oo)
@test_throws DomainError subs(sin(log(y - y/x)), x => 1)

# Some basic checks for complex numbers
@testset "Complex numbers" begin
for T in (Int, Float64, BigFloat)
j = one(T) * IM
@test j == imag(j) * IM
@test conj(j) == -j
end
end
7 changes: 5 additions & 2 deletions test/test-dense-matrix.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Test
using SymEngine
import LinearAlgebra: lu, det, zeros
import LinearAlgebra: lu, det, zeros, dot
CDenseMatrix = SymEngine.CDenseMatrix

@vars x
@vars x y

# constructors
A = [x 1 2; 3 x 4; 5 6 x]
Expand Down Expand Up @@ -44,3 +44,6 @@ out = M \ b

@test SymEngine.dense_matrix_eye(2,2,0) == Basic[1 0; 0 1]

# dot product
@test dot(x, x) == x^2
@test dot([1, x, 0], [y, -2, 1]) == y - 2x