mhostetter / galois

A performant NumPy extension for Galois fields and their applications
https://mhostetter.github.io/galois/
MIT License
338 stars 31 forks source link

Support NumPy 2.0 #550

Closed mhostetter closed 4 months ago

mhostetter commented 5 months ago

Probably should merge into release/0.4.x.

iyanmv commented 4 months ago

The trick of converting -1 to the max of the unsigned dtype does not work with NumPy 2.0 anymore. I think this should be enough to fix it:

diff --git a/src/galois/_polys/_dense.py b/src/galois/_polys/_dense.py
index b1b3e28..4f45d78 100644
--- a/src/galois/_polys/_dense.py
+++ b/src/galois/_polys/_dense.py
@@ -487,7 +487,7 @@ class roots_jit(Function):
         # Test if 0 is a root
         if nonzero_degrees[-1] != 0:
             roots.append(0)
-            powers.append(-1)
+            powers.append(np.iinfo(nonzero_coeffs.dtype).max)

         # Test if 1 is a root
         _sum = 0

With this patch in addition to your 3 commits all tests pass again with NumPy 2.0 🙂

iyanmv commented 4 months ago

Sorry, I forgot to include in the diff one more needed change to pass all tests. Here it is:

diff --git a/src/galois/_fields/_array.py b/src/galois/_fields/_array.py
index 5cf847d..2ff32fa 100644
--- a/src/galois/_fields/_array.py
+++ b/src/galois/_fields/_array.py
@@ -988,7 +988,7 @@ class FieldArray(Array, metaclass=FieldArrayMeta):
         if x.ndim == 0:
             order = 1 if x == 0 else field.characteristic
         else:
-            order = field.characteristic * np.ones(x.shape, dtype=np.int64)
+            order = field.characteristic * np.ones(x.shape, dtype=x.dtype)
             order[np.where(x == 0)] = 1

         return order
iyanmv commented 4 months ago

If it's easier for you, I have made both changes available in this branch. I guess you can cherry-pick those commits directly here: https://github.com/mhostetter/galois/compare/numpy-2.0...iyanmv:galois:numpy-2.0

mhostetter commented 4 months ago

Thanks for the fixes @iyanmv. I modified them slightly (found a bug in my code in one, made your solution more general in the other). Can you test release/0.4.x and see if all is good there? If so, I plan to release 0.4.0 with NumPy 2.0 support.

iyanmv commented 4 months ago

That branch works well on Arch Linux :ok_hand: