Skip to content

Commit 9a7aa19

Browse files
add real, conj and imag to "on construction" simplifying
1 parent 130cf14 commit 9a7aa19

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/methods.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const monadic = [deg2rad, rad2deg, transpose, conj, asind, log1p, acsch,
99
abs2, cosh, sin, cos, atan, cospi, cbrt, acosd, acoth, acotd,
1010
asecd, exp, acot, sqrt, sind, sinpi, asech, log2, tan, exp10,
1111
sech, coth, asin, cotd, cosd, sinh, abs, csc, tanh, secd,
12-
atand, sec, acscd, cot, exp2, expm1, atanh, real, gamma,
12+
atand, sec, acscd, cot, exp2, expm1, atanh, real, imag, gamma,
1313
loggamma, erf, erfc, erfcinv, erfi, erfcx, dawson, digamma,
1414
trigamma, invdigamma, polygamma, airyai, airyaiprime, airybi,
1515
airybiprime, besselj0, besselj1, bessely0, bessely1]
@@ -97,7 +97,7 @@ promote_symtype(::typeof(rem2pi), T::Type{<:Number}, mode) = T
9797
Base.rem2pi(x::Symbolic{<:Number}, mode::Base.RoundingMode) = term(rem2pi, x, mode)
9898

9999
for f in monadic
100-
if f in [real]
100+
if f in [real, imag, conj]
101101
continue
102102
end
103103
@eval promote_symtype(::$(typeof(f)), T::Type{<:Number}) = promote_type(T, Real)
@@ -111,8 +111,15 @@ for f in [identity, one, zero, *, +, -]
111111
@eval promote_symtype(::$(typeof(f)), T::Type{<:Number}) = T
112112
end
113113

114+
# All of these need to be `Sym` to overwrite the function definitions of
115+
# the L78, that are made with the type `Sym <: Symbolic`
116+
# @number_methods(Sym, term(f, a), term(f, a, b), skipbasics)
114117
promote_symtype(::typeof(Base.real), T::Type{<:Number}) = Real
115-
Base.real(s::Symbolic{<:Number}) = islike(s, Real) ? s : term(real, s)
118+
Base.real(s::Sym{<:Real}) = s
119+
promote_symtype(::typeof(Base.conj), T::Type{<:Number}) = T
120+
Base.conj(s::Sym{<:Real}) = s
121+
promote_symtype(::typeof(Base.imag), T::Type{<:Number}) = Real
122+
Base.imag(s::Sym{<:Real}) = zero(symtype(s))
116123

117124
## Booleans
118125

src/simplify_rules.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ let
5353
@rule(zero(~x) => zero(symtype(~x)))
5454
@rule(conj(~x::_isreal) => ~x)
5555
@rule(real(~x::_isreal) => ~x)
56+
@rule(imag(~x::_isreal) => zero(symtype(~x)))
5657
@rule(ifelse(~x::is_literal_number, ~y, ~z) => ~x ? ~y : ~z)
5758
]
5859

test/rulesets.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ end
2020
@syms a::Integer b c d x::Real y::Number
2121
@eqtest simplify(conj(x)) == x
2222
@eqtest simplify(real(x)) == x
23+
@eqtest simplify(imag(x)) == 0
2324
@eqtest simplify(x - y) == x + -1*y
2425
@eqtest simplify(x - sin(y)) == x + -1*sin(y)
2526
@eqtest simplify(-sin(x)) == -1*sin(x)

0 commit comments

Comments
 (0)