Skip to content

Commit

Permalink
Merge pull request #1098 from n0rbed/fix
Browse files Browse the repository at this point in the history
Fixed output of coeff(expression, x^0) or equivalent to coeff(expression, 1)
  • Loading branch information
ChrisRackauckas authored Mar 24, 2024
2 parents 807e00c + cf3b3ae commit c6cf755
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ julia> Symbolics.coeff(x^2 + y, x^2)
"""
function coeff(p, sym=nothing)
p, sym = value(p), value(sym)

if isequal(sym, 1)
sym = nothing
end

if issym(p) || isterm(p)
sym === nothing ? 0 : Int(isequal(p, sym))
elseif ispow(p)
Expand Down
6 changes: 6 additions & 0 deletions test/coeff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ e = x*y^2 + 2x + y^3*x^3
@test isequal(coeff(expand(((x + 1)^4 + x)^3), x^2), 93)
@test isequal(coeff(simplify((x^2 - 1) / (x - 1)), x), 1)
@test isequal(coeff(expand((x^(1//2) + y^0.5)^2), x), 1)


# issue #1098
@test isequal(coeff(x^2 + 1, x^0), 1)
@test isequal(coeff(e, x^0), 0)
@test isequal(coeff(a*x + 3, x^0), 3)

0 comments on commit c6cf755

Please sign in to comment.