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

Addition of array of polynomials with scalar raises ValueError when comparing exponents. #101

Open
JulVandenBroeck opened this issue Jul 4, 2023 · 8 comments

Comments

@JulVandenBroeck
Copy link

JulVandenBroeck commented Jul 4, 2023

Hello jonathf!

I have been using your modules in my research for a while now and I love them. I have stumbled upon a problem I can't manage to solve, however.

I have an array of polynomials (with 2 elements in the example) and want to add 1 to all of them. Running the following code:

import numpoly
qs = numpoly.variable(2)
qs+1

Raises the following error:

Traceback (most recent call last):
  File "/home/quest/juvdnbro/repos/gpc/src/schrodinger1d/test.py", line 3, in <module>
    qs+1
  File "/home/quest/juvdnbro/.local/lib/python3.10/site-packages/numpoly/baseclass.py", line 242, in __array_ufunc__
    return numpoly.UFUNC_COLLECTION[ufunc](*inputs, **kwargs)
  File "/home/quest/juvdnbro/.local/lib/python3.10/site-packages/numpoly/array_function/add.py", line 60, in add
    return simple_dispatch(
  File "/home/quest/juvdnbro/.local/lib/python3.10/site-packages/numpoly/dispatch.py", line 84, in simple_dispatch
    inputs = numpoly.align_polynomials(*inputs)
  File "/home/quest/juvdnbro/.local/lib/python3.10/site-packages/numpoly/align.py", line 47, in align_polynomials
    polys = align_exponents(*polys)
  File "/home/quest/juvdnbro/.local/lib/python3.10/site-packages/numpoly/align.py", line 211, in align_exponents
    if numpy.all(poly.exponents == global_exponents):
ValueError: operands could not be broadcast together with shapes (2,2) (3,2)

It seems that the scalar 1 gets converted into polynomial([1,1]), but the exponents don't have the correct shape. Is there a better way to do this, or would it be possible to allow for this kind of arithmetic?

Thanks in advance!
Jul

@JulVandenBroeck
Copy link
Author

I have found that the exponents of qs are [array([1, 0]), array([0, 1])], and if I do the following hack (adding 1 and subtracting again):

qs_hack = numpoly.polynomial([q0+1,q1+1]) - 1

the exponents become [array([0, 0]), array([1, 0]), array([0, 1])]. This form allows for the arithmetic that I require. Is there a more elegant way for this? 😆

@JulVandenBroeck
Copy link
Author

JulVandenBroeck commented Jul 4, 2023

Another update: with this hack, division by scalars doesn't work. qs_hack/2 raises the same exception.

Simply commenting out the following lines at the end of align.py solves my problem:

# if numpy.all(poly.exponents == global_exponents):
#     continue

@jonathf
Copy link
Owner

jonathf commented Jul 5, 2023

I'm not reproducing the error on my end. Which version of numpoly are you on?

Also, can you compare version v1.2.9 and v1.2.10 and see if that makes a difference?

@JulVandenBroeck
Copy link
Author

Thanks for the reply. I was using v1.2.9 with Python 3.10.6. I tried both v1.2.9 and v1.2.10 now and for me the error occurs for both versions... Any ideas?

@JulVandenBroeck
Copy link
Author

Also, I'm using Numpy v1.25.0.

@jonathf
Copy link
Owner

jonathf commented Jul 5, 2023

Updating numpy to v1.25 did the trick; I can reproduce the issue.
I'll look into the issue when I have a moment to spare.

@jonathf
Copy link
Owner

jonathf commented Jul 10, 2023

Numpy 1.25 had a few incompatabilities., but the issue should now be resolved in numpoly v1.2.11.
Let me know if there are any issues.

@JulVandenBroeck
Copy link
Author

It works perfectly now. Great work, I really appreciate it!

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

2 participants