Skip to content

Commit

Permalink
feat: working matrix inverse (not integrated)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcabrero committed Sep 25, 2024
1 parent 4020e52 commit 118eb59
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 74 deletions.
3 changes: 3 additions & 0 deletions examples/linear_regression/nada-project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name = "linear-regression"
version = "0.1.0"
authors = [""]

[test_framework.nada-test]
command = "nada-test ./tests"

[[programs]]
path = "src/linear_regression.py"
prime_size = 64
Expand Down
13 changes: 3 additions & 10 deletions examples/linear_regression/src/matrix_inverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def public_modular_inverse(
power = value ** Integer(
mod - 1
) # value ** modulo = value ** (modulo // 2) * modulo ** (modulo // 2)
power = power * power * value if rem else Integer(1) # value ** mo
power = power * power * (value if rem else Integer(1)) # value ** mo
return power


Expand Down Expand Up @@ -133,14 +133,7 @@ def gauss_jordan_zn(mat: na.NadaArray, modulo: int):

# Forward elimination
for i in range(rows):
# # Find pivot row
# pivot_row = i
# while pivot_row < rows and (mat[pivot_row][i] == Integer(0)) is Boolean(True):
# pivot_row += 1

# # Swap pivot row with current row
# mat[[i, pivot_row]] = mat[[pivot_row, i]]


# Scale pivot row to have leading 1
diagonal_element = mat[i][i]
pivot_inv = public_modular_inverse(diagonal_element, modulo)
Expand Down Expand Up @@ -186,7 +179,7 @@ def matrix_inverse(matrix: np.ndarray, modulo: int):
def nada_main():
parties = na.parties(3)

A = na.array([4, 4], parties[0], "A", nada_type=SecretInteger)
A = na.array([10, 10], parties[0], "A", nada_type=SecretInteger)
A_inv = matrix_inverse(A, PRIME)

result = A @ A_inv
Expand Down
24 changes: 24 additions & 0 deletions examples/linear_regression/tests/matrix_inverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from nada_test import nada_test, NadaTest
import sys
import nada_numpy.client as na
import numpy as np

# Functional style test
@nada_test(program="matrix_inverse")
def my_test():
n = 10
m = np.random.rand(n, n)
mx = np.sum(np.abs(m), axis=1)
np.fill_diagonal(m, mx)
A = na.array(m * (1 << 16), "A", nada_type=int)
print("INPUTS:", A, file=sys.stderr)
outputs = yield A
print(outputs, file=sys.stderr)
for output, value in outputs.items():
output = output.split("_")
if output[-1] == output[-2]:
assert value == 1, f"Expected 1 {output}, got {value}"
else:
assert value == 0, f"Expected 0 {output}, got {value}"

#assert outputs["my_output"] == a + b
62 changes: 0 additions & 62 deletions examples/linear_regression/tests/matrix_inverse.yaml

This file was deleted.

15 changes: 14 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ nillion-python-helpers = "^0.3.0"
black = {version="24.8.0", optional=true}
isort = {version="^5.13.2", optional=true}
scikit-learn = {version="^1.5.1", optional=true}
nada-test = "^0.6.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.2.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
("linear_regression", "determinant", "determinant_2"),
("linear_regression", "determinant", "determinant_3"),
("linear_regression", "gauss_jordan", "gauss_jordan"),
("linear_regression", "matrix_inverse", "matrix_inverse"),
("linear_regression", "matrix_inverse", "nada-test::matrix_inverse.py:my_test"),
("linear_regression", "linear_regression", "linear_regression"),
("linear_regression", "linear_regression", "linear_regression_1"),
("linear_regression", "linear_regression", "linear_regression_2"),
Expand Down

0 comments on commit 118eb59

Please sign in to comment.