Skip to content

Commit 1ba9c4c

Browse files
authored
Merge pull request #233 from ranocha/hr/dot
implement fallback `conj` to fix `dot`
2 parents d85847d + 3ac2d66 commit 1ba9c4c

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/numerics.jl

+8-3
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ function N(b::BasicType)
151151
out = evalf(b)
152152
imag(out) == Basic(0.0) ? real(out) : out
153153
end
154-
155154

156-
## Conversions SymEngine -> Julia
155+
156+
## Conversions SymEngine -> Julia
157157
function as_numer_denom(x::Basic)
158158
a, b = Basic(), Basic()
159159
ccall((:basic_as_numer_denom, libsymengine), Nothing, (Ref{Basic}, Ref{Basic}, Ref{Basic}), a, b, x)
@@ -175,6 +175,11 @@ imag(x::BasicType{Val{:RealMPFR}}) = Basic(0)
175175
imag(x::BasicType{Val{:Rational}}) = Basic(0)
176176
imag(x::SymEngine.BasicType) = throw(InexactError())
177177

178+
# Because of the definitions above, `real(x) == x` for `x::Basic`
179+
# such as `x = symbols("x")`. Thus, it is consistent to define the
180+
# fallback
181+
Base.conj(x::Basic) = 2 * real(x) - x
182+
178183
## define convert(T, x) methods leveraging N()
179184
convert(::Type{Float64}, x::Basic) = convert(Float64, N(evalf(x, 53, true)))
180185
convert(::Type{BigFloat}, x::Basic) = convert(BigFloat, N(evalf(x, precision(BigFloat), true)))
@@ -203,7 +208,7 @@ isless(x::Basic, y::Basic) = isless(N(x), N(y))
203208

204209

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

209214
ceil(x::Basic) = Basic(ceil(N(x)))

test/runtests.jl

+9
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,12 @@ end
246246
@test_throws DomainError sin(zoo)
247247
@test_throws DomainError sin(oo)
248248
@test_throws DomainError subs(sin(log(y - y/x)), x => 1)
249+
250+
# Some basic checks for complex numbers
251+
@testset "Complex numbers" begin
252+
for T in (Int, Float64, BigFloat)
253+
j = one(T) * IM
254+
@test j == imag(j) * IM
255+
@test conj(j) == -j
256+
end
257+
end

test/test-dense-matrix.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using Test
22
using SymEngine
3-
import LinearAlgebra: lu, det, zeros
3+
import LinearAlgebra: lu, det, zeros, dot
44
CDenseMatrix = SymEngine.CDenseMatrix
55

6-
@vars x
6+
@vars x y
77

88
# constructors
99
A = [x 1 2; 3 x 4; 5 6 x]
@@ -44,3 +44,6 @@ out = M \ b
4444

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

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

0 commit comments

Comments
 (0)