Skip to content

[WIP] Separate python interface#12

Open
dance858 wants to merge 16 commits intomainfrom
separate-python-interface
Open

[WIP] Separate python interface#12
dance858 wants to merge 16 commits intomainfrom
separate-python-interface

Conversation

@dance858
Copy link
Copy Markdown
Contributor

Very much work in progress

Comment thread tests/utils.py
"""Set all variables to random values and return the flat vector."""
n = scope.total_var_size
x0 = rng.uniform(low, high, size=n)
scope.set_values(x0)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

why set values here? does this function have to accept scope?

np.testing.assert_allclose(fn.forward(), 3.0 * x0)


def test_left_matmul_parameter_jacobian(scope, rng):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In every test with parameters, set new values of the parameters and verify derivatives again.

import scipy.sparse
import sparsediffpy as sp
from tests.utils import NumericalDerivativeChecker, random_point

Copy link
Copy Markdown
Contributor Author

@dance858 dance858 Apr 18, 2026

Choose a reason for hiding this comment

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

Add parameter tests for test_right_matmul.

checker = NumericalDerivativeChecker(fn, scope)
checker.check_jacobian(random_point(scope, rng))


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Add test where the parameter appears on the right side of the multiplication

Comment thread tests/affine/test_sub.py
import sparsediffpy as sp
from tests.utils import NumericalDerivativeChecker, random_point


Copy link
Copy Markdown
Contributor Author

@dance858 dance858 Apr 18, 2026

Choose a reason for hiding this comment

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

What atoms does this file test? Should also add parameter check.

Comment thread tests/affine/test_sum.py
import sparsediffpy as sp
from tests.utils import NumericalDerivativeChecker, random_point


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Add 3 tests (one axis each) for summing matrix parameters

checker.check_jacobian(random_point(scope, rng))


def test_trace_hessian(scope, rng):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do something nonlinear here

import sparsediffpy as sp
from tests.utils import NumericalDerivativeChecker, random_point


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Add tests with constant and parameter on the right

import numpy as np
import sparsediffpy as sp
from tests.utils import NumericalDerivativeChecker, random_point

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Add forward and jacobian tests for vstack/hstack of 3 expressions: one nonlinear, one parameter, and one constant

np.testing.assert_allclose(fn.forward(), x_val * y_val)


def test_multiply_matrices_jacobian(scope, rng):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also add this test but when you swap order of X and Y


# -----------------------------------------------------------------------
# 4. Broadcast heavy: a * X + r + c
# a: scalar (1,1), X: matrix (3,2), r: row (1,2), c: column (3,1)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also add test for just r + c (without the first term a * X)



# -----------------------------------------------------------------------
# 6. Hstack mixed: hstack([sin(x), A @ x, y])
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Add a test like this with one of them being a parameter

fn_f = sp.compile(f)
fn_g = sp.compile(g)

x0 = random_point(scope, rng)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is confusing that random_point sets the value of the variables in that scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant