Skip to content

Bitpacked GF2 representation #594

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/galois/_domains/_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,6 @@ class FunctionMixin(np.ndarray, metaclass=ArrayMeta):

_UNSUPPORTED_FUNCTIONS = [
# Unary
np.packbits,
np.unpackbits,
np.unwrap,
np.around,
np.round,
Expand Down Expand Up @@ -334,6 +332,9 @@ class FunctionMixin(np.ndarray, metaclass=ArrayMeta):
np.convolve: "_convolve",
np.fft.fft: "_fft",
np.fft.ifft: "_ifft",
np.packbits: "_packbits",
np.unpackbits: "_unpackbits",
np.concatenate: "_concatenate",
}

_convolve: Function
Expand All @@ -351,9 +352,14 @@ def __array_function__(self, func, types, args, kwargs):
Override the standard NumPy function calls with the new finite field functions.
"""
field = type(self)
output = None

if func in field._OVERRIDDEN_FUNCTIONS:
output = getattr(field, field._OVERRIDDEN_FUNCTIONS[func])(*args, **kwargs)
try:
output = getattr(field, field._OVERRIDDEN_FUNCTIONS[func])(*args, **kwargs)
except AttributeError:
# fall through to use the default numpy function
pass

elif func in field._UNSUPPORTED_FUNCTIONS:
raise NotImplementedError(
Expand All @@ -364,16 +370,16 @@ def __array_function__(self, func, types, args, kwargs):
"`array = array.view(np.ndarray)` and then call the function."
)

else:
if func is np.insert:
args = list(args)
args[2] = self._verify_array_like_types_and_values(args[2])
args = tuple(args)
if func is np.insert:
args = list(args)
args[2] = self._verify_array_like_types_and_values(args[2])
args = tuple(args)

if output is None:
output = super().__array_function__(func, types, args, kwargs)

if func in field._FUNCTIONS_REQUIRING_VIEW:
output = field._view(output) if not np.isscalar(output) else field(output, dtype=self.dtype)
if func in field._FUNCTIONS_REQUIRING_VIEW:
output = field._view(output) if not np.isscalar(output) else field(output, dtype=self.dtype)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the need for these various changes to __array_function__()? Is it to ensure that _packbits() is called for GF2BP and not for GF2?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FunctionMixin previously had convolve, fft, and ifft overridden for all classes so that is why no try/except was needed. I needed to add packbits, unpackbits, and concatenate as well, but only GF2BP overrides those. So, for all other classes it needs to fall back to the numpy version.


return output

Expand Down
5 changes: 3 additions & 2 deletions src/galois/_fields/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,7 @@ def _display(self, mode: Literal["repr", "str"], separator=", ") -> str:

prefix = "GF(" if mode == "repr" else ""
order = field._order_str if mode == "repr" else ""
bitpacked = ", bitpacked" if mode == "repr" and hasattr(self, "_axis") else ""
suffix = ")" if mode == "repr" else ""
formatter = field._formatter(self)

Expand All @@ -1804,9 +1805,9 @@ def _display(self, mode: Literal["repr", "str"], separator=", ") -> str:
last_line_width = len(string[idx:] + ", " + order + suffix)

if last_line_width <= np.get_printoptions()["linewidth"]:
output = prefix + string + ", " + order + suffix
output = prefix + string + ", " + order + bitpacked + suffix
else:
output = prefix + string + ",\n" + " " * len(prefix) + order + suffix
output = prefix + string + ",\n" + " " * len(prefix) + order + bitpacked + suffix
else:
output = prefix + string + suffix

Expand Down
Loading
Loading