Skip to content

Commit

Permalink
fix: changed __comparison_operator to use .apply
Browse files Browse the repository at this point in the history
  • Loading branch information
jcabrero committed Aug 27, 2024
1 parent c716483 commit 58be49f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
39 changes: 26 additions & 13 deletions nada_numpy/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
SecretUnsignedInteger, UnsignedInteger)

from nada_numpy.context import UnsafeArithmeticSession
from nada_numpy.nada_typing import (NadaBoolean, NadaCleartextType,
NadaInteger, NadaRational,
NadaUnsignedInteger)
from nada_numpy.nada_typing import (AnyNadaType, NadaBoolean,
NadaCleartextType, NadaInteger,
NadaRational, NadaUnsignedInteger)
from nada_numpy.types import (Rational, SecretRational, fxp_abs, get_log_scale,
public_rational, rational, secret_rational, sign)
from nada_numpy.utils import copy_metadata
Expand Down Expand Up @@ -390,32 +390,45 @@ def __imatmul__(self, other: Any) -> "NadaArray":
"""
return self.matmul(other)

def __comparison_operator(self, value: Any, operator: Callable) -> "NadaArray":
def __comparison_operator(
self, value: Union["NadaArray", "AnyNadaType", np.ndarray], operator: Callable
) -> "NadaArray":
"""
Perform element-wise comparison with broadcasting.
NOTE: Specially for __eq__ and __ne__ operators, the result expected is bool.
If we don't define this method, the result will be a NadaArray with bool outputs.
Args:
other (Any): The object to compare.
value (Any): The object to compare.
operator (str): The comparison operator.
Returns:
NadaArray: A new NadaArray representing the element-wise comparison result.
"""
if isinstance(value, NadaArray):
value = value.inner
result = []
if isinstance(value, (SecretInteger, PublicInteger, Integer)):
for x in self.inner:
result.append(operator(x, value))
elif isinstance(value, np.ndarray):
if isinstance(
value,
(
SecretInteger,
Integer,
SecretUnsignedInteger,
UnsignedInteger,
SecretRational,
Rational,
),
):
return self.apply(lambda x: operator(x, value))

if isinstance(value, np.ndarray):
if len(self.inner) != len(value):
raise ValueError("Arrays must have the same length")
for x, y in zip(self.inner, value):
result.append(operator(x, y))
return NadaArray(np.array(result))
return NadaArray(
np.array([operator(x, y) for x, y in zip(self.inner, value)])
)

raise ValueError(f"Unsupported type: {type(value)}")

def __eq__(self, value: Any) -> "NadaArray": # type: ignore
"""
Expand Down
1 change: 0 additions & 1 deletion nada_numpy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2929,7 +2929,6 @@ def _chebyshev_polynomials(x: _NadaRational, terms: int) -> np.ndarray:

# return polynomials


polynomials = [x]
y = rational(4) * x * x - rational(2)
z = y - rational(1)
Expand Down

0 comments on commit 58be49f

Please sign in to comment.