Skip to content

Commit bbbf2d9

Browse files
add iscall() to the functions real() conj() and imag(); add adjoint(::Symbolic{Number}) method; tests for real and complex syms
1 parent df8e84d commit bbbf2d9

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/methods.jl

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,36 @@ for f in [identity, one, zero, *, +, -]
160160
end
161161

162162
promote_symtype(::typeof(Base.real), T::Type{<:Number}) = Real
163-
Base.real(s::Symbolic{<:Number}) = islike(s, Real) ? s : term(real, s)
164163
promote_symtype(::typeof(Base.conj), T::Type{<:Number}) = T
165-
Base.conj(s::Symbolic{<:Number}) = islike(s, Real) ? s : term(conj, s)
166164
promote_symtype(::typeof(Base.imag), T::Type{<:Number}) = Real
167-
Base.imag(s::Symbolic{<:Number}) = islike(s, Real) ? zero(symtype(s)) : term(imag, s)
165+
function Base.real(s::Symbolic{<:Number})
166+
if iscall(s)
167+
f = operation(s)
168+
args = map(real, arguments(s))
169+
return f(args...)
170+
else
171+
islike(s, Real) ? s : term(real, s)
172+
end
173+
end
174+
function Base.conj(s::Symbolic{<:Number})
175+
if iscall(s)
176+
f = operation(s)
177+
args = map(conj, arguments(s))
178+
return f(args...)
179+
else
180+
islike(s, Real) ? s : term(conj, s)
181+
end
182+
end
183+
function Base.imag(s::Symbolic{<:Number})
184+
if iscall(s)
185+
f = operation(s)
186+
args = map(imag, arguments(s))
187+
return f(args...)
188+
else
189+
islike(s, Real) ? zero(symtype(s)) : term(imag, s)
190+
end
191+
end
192+
Base.adjoint(s::Symbolic{<:Number}) = conj(s)
168193

169194
## Booleans
170195

test/basics.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,14 @@ end
354354
end
355355
@test repr(sin(x) + sin(x)) == "sin(x) + sin(x)"
356356
end
357+
358+
@testset "real and complex" begin
359+
@syms x y z::Real
360+
@test isequal(conj(x*y*z), conj(x)*conj(y)*z)
361+
@test isequal(conj(x*y*z + y), conj(x)*conj(y)*z + conj(y))
362+
@test isequal(real(x*y*z + y), real(x)*real(y)*z + real(y))
363+
@test isequal(imag(x*y*z + y), imag(y))
364+
@test isequal(imag(x*y*z + y), imag(y))
365+
@test isequal(conj(exp(1im*x)*y), conj(exp(1im*x))*conj(y))
366+
@test isequal(adjoint(x*y*z), conj(x)*conj(y)*z)
367+
end

0 commit comments

Comments
 (0)