Skip to content

Commit 406c34c

Browse files
Feature/negation (#28)
Adds negation operator
1 parent b86dbfc commit 406c34c

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

nada_algebra/array.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ def __imul__(self, other: Any) -> "NadaArray":
182182
"""
183183
return self.mul(other)
184184

185+
def __neg__(self) -> "NadaArray":
186+
"""
187+
Performs negation operation.
188+
189+
Returns:
190+
NadaArray: Negated NadaArray.
191+
"""
192+
if self.is_rational:
193+
return self.apply(lambda x: x * rational(-1))
194+
return self.apply(lambda x: x * Integer(-1))
195+
185196
def __pow__(self, other: int) -> "NadaArray":
186197
"""
187198
Raises NadaArray to a power.

tests/nada-tests/src/rational_array.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def nada_main():
1919
out_6 = a * c
2020
out_7 = a / c
2121

22+
out_8 = -a
23+
2224
return (
2325
out_0.output(parties[1], "out_0")
2426
+ out_1.output(parties[1], "out_1")
@@ -28,4 +30,5 @@ def nada_main():
2830
+ out_5.output(parties[1], "out_5")
2931
+ out_6.output(parties[1], "out_6")
3032
+ out_7.output(parties[1], "out_7")
33+
+ out_8.output(parties[1], "out_8")
3134
)

tests/nada-tests/tests/rational_array.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ expected_outputs:
6464
SecretInteger: "65536"
6565
out_7_2:
6666
SecretInteger: "131072"
67+
out_8_0:
68+
SecretInteger: "0"
69+
out_8_1:
70+
SecretInteger: "-65536"
71+
out_8_2:
72+
SecretInteger: "-131072"

0 commit comments

Comments
 (0)