From 0139a83b89cc1410e03a69af2e0f15a51ddebb0d Mon Sep 17 00:00:00 2001 From: Erik Welch Date: Wed, 29 Mar 2023 10:05:18 -0500 Subject: [PATCH] Improve import times (#419) --- graphblas/core/operator.py | 13 ------------- graphblas/monoid/numpy.py | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/graphblas/core/operator.py b/graphblas/core/operator.py index bfd03d9df..b38add7f1 100644 --- a/graphblas/core/operator.py +++ b/graphblas/core/operator.py @@ -2741,19 +2741,6 @@ def _initialize(cls): # Builtin monoids that are idempotent; i.e., `op(x, x) == x` for any x for name in ["any", "band", "bor", "land", "lor", "max", "min"]: getattr(monoid, name)._is_idempotent = True - for name in [ - "bitwise_and", - "bitwise_or", - "fmax", - "fmin", - "gcd", - "logical_and", - "logical_or", - "maximum", - "minimum", - ]: - getattr(monoid.numpy, name)._is_idempotent = True - # Allow some functions to work on UDTs any_ = monoid.any any_._identity = 0 diff --git a/graphblas/monoid/numpy.py b/graphblas/monoid/numpy.py index 475266d5c..2d8d70c20 100644 --- a/graphblas/monoid/numpy.py +++ b/graphblas/monoid/numpy.py @@ -140,6 +140,19 @@ # _graphblas_to_numpy = {val: key for key, val in _numpy_to_graphblas.items()} # Soon... # Not included: maximum, minimum, gcd, hypot, logaddexp, logaddexp2 +# True if ``monoid(x, x) == x`` for any x. +_idempotent = { + "bitwise_and", + "bitwise_or", + "fmax", + "fmin", + "gcd", + "logical_and", + "logical_or", + "maximum", + "minimum", +} + def __dir__(): return globals().keys() | _delayed.keys() | _monoid_identities.keys() @@ -163,5 +176,7 @@ def __getattr__(name): from ..core import operator func = getattr(_binary.numpy, name) - operator.Monoid.register_new(f"numpy.{name}", func, _monoid_identities[name]) + operator.Monoid.register_new( + f"numpy.{name}", func, _monoid_identities[name], is_idempotent=name in _idempotent + ) return globals()[name]