# SymPy coeff() different behavior depending on how it is called

**URL:** <https://discourse.julialang.org/t/sympy-coeff-different-behavior-depending-on-how-it-is-called/64549>\
**Category:** General Usage\
**Tags:** sympy\
**Created:** [July 13, 2021, 6:18am UTC](https://discourse.julialang.org/t/sympy-coeff-different-behavior-depending-on-how-it-is-called/64549 "2021-07-13T06:18:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsgg](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hsgg](https://discourse.julialang.org/u/hsgg)\
**Post date:** [July 13, 2021, 6:18am UTC](https://discourse.julialang.org/t/sympy-coeff-different-behavior-depending-on-how-it-is-called/64549/1 "2021-07-13T06:18:29Z")

</div>

I am trying to get the coefficients from several SymPy expressions. Since there are a lot of different combinations, I’d like to loop over them with a variable `n`. However that fails when `n=-1`. Here is an example, where the first two attempts are explicit and work fine, and the last with the exponent written as a variable fails:

```julia
julia> using SymPy

julia> @syms x
(x,)

julia> p = 1/x
1
─
x

julia> p.coeff(1/x) # Yay, this works!
1

julia> p.coeff(x^(-1)) # This also works!
1

julia> n = -1; p.coeff(x^n) # Crash, bang, catastrophy.
ERROR: DomainError with -1:
Cannot raise an integer x to a negative power -1.
Convert input to float.
Stacktrace:
 [1] throw_domerr_powbysq(#unused#::Sym, p::Int64)
   @ Base ./intfuncs.jl:241
 [2] power_by_squaring(x_::Sym, p::Int64)
   @ Base ./intfuncs.jl:265
 [3] ^(x::Sym, p::Int64)
   @ Base ./intfuncs.jl:290
 [4] top-level scope
   @ REPL[6]:1

```

To me this smells like some compiler inference stuff… meaning I have no clue what I’m talking about. Even just an explanation of what is going on would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [July 13, 2021, 8:02am UTC](https://discourse.julialang.org/t/sympy-coeff-different-behavior-depending-on-how-it-is-called/64549/2 "2021-07-13T08:02:33Z")

</div>

I think `x^(-1)` gets replaced by the compiler with `Base.literal_pow(^, x, Val(-1))` which is probably being overloaded by `SymPy` to have different behaviour from `^`. This should probably be considered a bug.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [July 13, 2021, 10:25am UTC](https://discourse.julialang.org/t/sympy-coeff-different-behavior-depending-on-how-it-is-called/64549/3 "2021-07-13T10:25:15Z")

</div>

The issue here is `x^(-1)` is treated by the parser first; `x^n` dispatches to a generic implementation that errors. You can see the same behavior with `x=2`, it isn’t SymPy related.

Using a symbolic power, as in `x^Sym(n)`, would stay within SymPy, and works as desired.
