Skip to content

Commit

Permalink
Fix coeff dict duplicate variable and function name issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenszhu committed Sep 17, 2024
1 parent 0c15b58 commit d04b925
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,9 @@ ratio(x::Integer,y::Integer) = iszero(rem(x,y)) ? div(x,y) : x//y
ratio(x::Rat,y::Rat) = x//y
function maybe_intcoeff(x)
if ismul(x)
coeff = coeff(x)
if coeff isa Rational && isone(denominator(coeff))
_Mul(symtype(x), coeff.num, dict(x); metadata = x.metadata)
coefficient = coeff(x)
if coefficient isa Rational && isone(denominator(coefficient))
_Mul(symtype(x), coefficient.num, dict(x); metadata = x.metadata)
else
x
end
Expand Down Expand Up @@ -566,16 +566,16 @@ $(SIGNATURES)
Any Muls inside an Add should always have a coeff of 1
and the key (in Add) should instead be used to store the actual coefficient
"""
function makeadd(sign, coeff, xs...)
function makeadd(sign, coefficient, xs...)
d = Dict{BasicSymbolic, Any}()
for x in xs
if isadd(x)
coeff += coeff(x)
coefficient += coeff(x)
_merge!(+, d, dict(x), filter = _iszero)
continue
end
if x isa Number
coeff += x
coefficient += x
continue
end
if ismul(x)
Expand All @@ -591,17 +591,17 @@ function makeadd(sign, coeff, xs...)
d[k] = v
end
end
coeff, d
coefficient, d
end

function makemul(coeff, xs...; d = Dict{BasicSymbolic, Any}())
function makemul(coefficient, xs...; d = Dict{BasicSymbolic, Any}())
for x in xs
if ispow(x) && x.impl.exp isa Number
d[x.impl.base] = x.impl.exp + get(d, x.impl.base, 0)
elseif x isa Number
coeff *= x
coefficient *= x
elseif ismul(x)
coeff *= coeff(x)
coefficient *= coeff(x)
_merge!(+, d, dict(x), filter = _iszero)
else
v = 1 + get(d, x, 0)
Expand All @@ -612,7 +612,7 @@ function makemul(coeff, xs...; d = Dict{BasicSymbolic, Any}())
end
end
end
coeff, d
coefficient, d
end

unstable_pow(a, b) = a isa Integer && b isa Integer ? (a // 1)^b : a^b
Expand Down Expand Up @@ -1229,13 +1229,13 @@ function +(a::SN, b::SN)
return _Add(
add_t(a, b), coeff(a) + coeff(b), _merge(+, dict(a), dict(b), filter = _iszero))
elseif isadd(a)
coeff, dict = makeadd(1, 0, b)
return _Add(add_t(a, b), coeff(a) + coeff, _merge(+, dict(a), dict, filter = _iszero))
coefficient, dictionary = makeadd(1, 0, b)
return _Add(add_t(a, b), coeff(a) + coefficient, _merge(+, dict(a), dictionary, filter = _iszero))
elseif isadd(b)
return b + a
end
coeff, dict = makeadd(1, 0, a, b)
_Add(add_t(a, b), coeff, dict)
coefficient, dictionary = makeadd(1, 0, a, b)
_Add(add_t(a, b), coefficient, dictionary)
end
function +(a::Number, b::SN)
if isconst(b)
Expand Down Expand Up @@ -1354,9 +1354,9 @@ function ^(a::SN, b)
elseif b isa Number && b < 0
_Div(1, a^(-b))
elseif ismul(a) && b isa Number
coeff = unstable_pow(coeff(a), b)
coefficient = unstable_pow(coeff(a), b)
_Mul(promote_symtype(^, symtype(a), symtype(b)),
coeff, mapvalues((k, v) -> b * v, dict(a)))
coefficient, mapvalues((k, v) -> b * v, dict(a)))
else
_Pow(a, b)
end
Expand Down

0 comments on commit d04b925

Please sign in to comment.