Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coeff for non-integer powers fails #26565

Open
NAThompson opened this issue May 5, 2024 · 0 comments
Open

coeff for non-integer powers fails #26565

NAThompson opened this issue May 5, 2024 · 0 comments

Comments

@NAThompson
Copy link

NAThompson commented May 5, 2024

To reproduce:

from sympy import * 
i = symbols('i', integer=True)
α = symbols('α', real=True)
x = symbols('x', real=True, positive=True)
c = symbols('c', real=True)
f = c*pow(x, i + α)
expr = simplify(collect(diff(f, x), x))
print(latex(expr)) # works as expected
co = expr.coeff(x, i + α - 1)

Visual inspection of the print allows us to easily determine the result should be (i+α)c, but instead we get:

Traceback (most recent call last):
  File "~/lib/python3.12/site-packages/sympy/utilities/misc.py", line 555, in as_int
    return operator.index(n)
           ^^^^^^^^^^^^^^^^^
TypeError: 'Add' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "reproduce.py", line 9, in <module>
    co = expr.coeff(x, i + α - 1)
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/lib/python3.12/site-packages/sympy/core/expr.py", line 1461, in coeff
    n = as_int(n)
        ^^^^^^^^^
  File "~/lib/python3.12/site-packages/sympy/utilities/misc.py", line 557, in as_int
    raise ValueError('%s is not an integer' % (n,))
ValueError: i + α - 1 is not an integer

Now, I concede that this is probably just a feature request, but nonetheless I figured I'd see if someone put the assert in there just for safety, even though real-valued arguments should work just fine.

Other information:

$ pip show sympy
Version: 1.12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant