diff --git a/sparsediff_linalg b/sparsediff_linalg index b89ee0e5..04c108e8 160000 --- a/sparsediff_linalg +++ b/sparsediff_linalg @@ -1 +1 @@ -Subproject commit b89ee0e5ff97096932a1de4af23b4a8279305610 +Subproject commit 04c108e8335c789398a24f30864acdf09a5137d0 diff --git a/src/atoms/affine/add.c b/src/atoms/affine/add.c index af863c56..58fb7f58 100644 --- a/src/atoms/affine/add.c +++ b/src/atoms/affine/add.c @@ -42,12 +42,7 @@ static void jacobian_init_impl(expr *node) jacobian_init(node->left); jacobian_init(node->right); - /* we never have to store more than the sum of children's nnz */ - int nnz_max = node->left->jacobian->nnz + node->right->jacobian->nnz; - node->jacobian = new_sparse_matrix_alloc(node->size, node->n_vars, nnz_max); - - /* fill sparsity pattern */ - sum_matrices_alloc(node->left->jacobian, node->right->jacobian, node->jacobian); + node->jacobian = sum_matrices_alloc(node->left->jacobian, node->right->jacobian); } static void eval_jacobian_impl(expr *node) diff --git a/src/atoms/bivariate_full_dom/matmul.c b/src/atoms/bivariate_full_dom/matmul.c index 3d0d2a45..6cfacb30 100644 --- a/src/atoms/bivariate_full_dom/matmul.c +++ b/src/atoms/bivariate_full_dom/matmul.c @@ -234,10 +234,7 @@ static void jacobian_init_chain_rule(expr *node) mnode->X = new_permuted_dense_full(m, k, NULL); mnode->term1 = BA_dense_kron_I_matrices_alloc(mnode->YT, m, f->jacobian); mnode->term2 = BA_kron_matrices_alloc(mnode->X, n, g->jacobian); - int max_nnz = mnode->term1->nnz + mnode->term2->nnz; - max_nnz = MIN(max_nnz, sat_mul_int(node->size, node->n_vars)); - node->jacobian = new_sparse_matrix_alloc(node->size, node->n_vars, max_nnz); - sum_matrices_alloc(mnode->term1, mnode->term2, node->jacobian); + node->jacobian = sum_matrices_alloc(mnode->term1, mnode->term2); } static void eval_jacobian_chain_rule(expr *node) diff --git a/src/atoms/bivariate_full_dom/multiply.c b/src/atoms/bivariate_full_dom/multiply.c index ac1f1807..fddad667 100644 --- a/src/atoms/bivariate_full_dom/multiply.c +++ b/src/atoms/bivariate_full_dom/multiply.c @@ -50,11 +50,7 @@ static void jacobian_init_impl(expr *node) { jacobian_init(node->left); jacobian_init(node->right); - int nnz_max = node->left->jacobian->nnz + node->right->jacobian->nnz; - node->jacobian = new_sparse_matrix_alloc(node->size, node->n_vars, nnz_max); - - /* fill sparsity pattern */ - sum_matrices_alloc(node->left->jacobian, node->right->jacobian, node->jacobian); + node->jacobian = sum_matrices_alloc(node->left->jacobian, node->right->jacobian); } static void eval_jacobian_impl(expr *node) diff --git a/src/atoms/bivariate_restricted_dom/quad_over_lin.c b/src/atoms/bivariate_restricted_dom/quad_over_lin.c index ceabde8b..327b4fff 100644 --- a/src/atoms/bivariate_restricted_dom/quad_over_lin.c +++ b/src/atoms/bivariate_restricted_dom/quad_over_lin.c @@ -100,9 +100,7 @@ static void jacobian_init_impl(expr *node) ey->i[0] = y->var_id; qnode->ypart = new_sparse_matrix(ey); - node->jacobian = - new_sparse_matrix_alloc(1, node->n_vars, qnode->xpart->nnz + 1); - sum_matrices_alloc(qnode->xpart, qnode->ypart, node->jacobian); + node->jacobian = sum_matrices_alloc(qnode->xpart, qnode->ypart); } } diff --git a/src/atoms/elementwise_full_dom/common.c b/src/atoms/elementwise_full_dom/common.c index 64b9684a..3fd60cac 100644 --- a/src/atoms/elementwise_full_dom/common.c +++ b/src/atoms/elementwise_full_dom/common.c @@ -116,11 +116,8 @@ void wsum_hess_init_elementwise(expr *node) node->work->hess_term2 = copy_sparsity_matrices_alloc(child->wsum_hess); /* wsum_hess = term1 + term2 */ - int max_nnz = node->work->hess_term1->nnz + node->work->hess_term2->nnz; node->wsum_hess = - new_sparse_matrix_alloc(node->n_vars, node->n_vars, max_nnz); - sum_matrices_alloc(node->work->hess_term1, node->work->hess_term2, - node->wsum_hess); + sum_matrices_alloc(node->work->hess_term1, node->work->hess_term2); } } } diff --git a/src/atoms/other/quad_form.c b/src/atoms/other/quad_form.c index 432b412e..d963b276 100644 --- a/src/atoms/other/quad_form.c +++ b/src/atoms/other/quad_form.c @@ -173,10 +173,8 @@ static void wsum_hess_init_impl(expr *node) node->work->hess_term2 = copy_sparsity_matrices_alloc(x->wsum_hess); /* hess = term1 + term2 */ - int max_nnz = node->work->hess_term1->nnz + node->work->hess_term2->nnz; - node->wsum_hess = new_sparse_matrix_alloc(node->n_vars, node->n_vars, max_nnz); - sum_matrices_alloc(node->work->hess_term1, node->work->hess_term2, - node->wsum_hess); + node->wsum_hess = + sum_matrices_alloc(node->work->hess_term1, node->work->hess_term2); } static void eval_wsum_hess_impl(expr *node, const double *w) diff --git a/tests/all_tests.c b/tests/all_tests.c index 32b15cd7..123da9bb 100644 --- a/tests/all_tests.c +++ b/tests/all_tests.c @@ -85,6 +85,7 @@ #include "wsum_hess/bivariate_restricted_dom/test_rel_entr.h" #include "wsum_hess/bivariate_restricted_dom/test_rel_entr_scalar_vector.h" #include "wsum_hess/bivariate_restricted_dom/test_rel_entr_vector_scalar.h" +#include "wsum_hess/composite/test_binary_sums.h" #include "wsum_hess/composite/test_chain_rule_wsum_hess.h" #include "wsum_hess/composite/test_shared_dag.h" #include "wsum_hess/elementwise_full_dom/test_exp.h" @@ -394,6 +395,10 @@ int main(void) mu_run_test(test_wsum_hess_add_shared_subexpr, tests_run); mu_run_test(test_wsum_hess_multiply_shared_subexpr, tests_run); mu_run_test(test_wsum_hess_matmul_shared_subexpr, tests_run); + mu_run_test(test_binary_sum_add_dense, tests_run); + mu_run_test(test_binary_sum_multiply_dense, tests_run); + mu_run_test(test_binary_sum_multiply_sparse, tests_run); + mu_run_test(test_binary_sum_matmul_dense, tests_run); mu_run_test(test_shared_dag_chain, tests_run); mu_run_test(test_shared_dag_scaling_atoms, tests_run); mu_run_test(test_shared_dag_quad_form, tests_run); diff --git a/tests/jacobian_tests/bivariate_restricted_dom/test_quad_over_lin.h b/tests/jacobian_tests/bivariate_restricted_dom/test_quad_over_lin.h index 998d78e5..1b78056a 100644 --- a/tests/jacobian_tests/bivariate_restricted_dom/test_quad_over_lin.h +++ b/tests/jacobian_tests/bivariate_restricted_dom/test_quad_over_lin.h @@ -122,6 +122,8 @@ const char *test_quad_over_lin4(void) eval_jacobian(node); double expected_vals[4] = {-76.25, 71.0, 94.0, 117.0}; + mu_assert("composed quad_over_lin keeps full row dense", + node->jacobian->kind == MATRIX_PERMUTED_DENSE); int expected_Ap[2] = {0, 4}; int expected_Ai[4] = {2, 5, 6, 7}; diff --git a/tests/wsum_hess/composite/test_binary_sums.h b/tests/wsum_hess/composite/test_binary_sums.h new file mode 100644 index 00000000..225c02d7 --- /dev/null +++ b/tests/wsum_hess/composite/test_binary_sums.h @@ -0,0 +1,136 @@ +#ifndef TEST_BINARY_SUMS_H +#define TEST_BINARY_SUMS_H + +#include "atoms/affine.h" +#include "atoms/bivariate_full_dom.h" +#include "atoms/elementwise_full_dom.h" +#include "minunit.h" +#include "numerical_diff.h" + +/* A binary sum of two matrices that together fill a dense block must stay + dense instead of falling back to a sparse matrix. */ + +/* sum(sqrt(1 + (A x + A y)^2)): the Jacobian of the add and the Hessian of + the outer power are both binary sums with a dense result. */ +const char *test_binary_sum_add_dense(void) +{ + double A[12] = {1, 2, -1, 3, 2, -1, 3, 1, -2, 1, 1, 2}; + double u[6] = {0.2, 0.3, -0.1, 0.4, 0.1, -0.2}; + double w = 1.0; + double one = 1.0; + + expr *x = new_variable(3, 1, 0, 6); + expr *y = new_variable(3, 1, 3, 6); + expr *Ax = new_left_matmul_dense(NULL, x, 4, 3, A); + expr *Ay = new_left_matmul_dense(NULL, y, 4, 3, A); + expr *z = new_add(Ax, Ay); + expr *shifted = new_add(new_power(z, 2.0), + new_promote(new_parameter(1, 1, -1, 6, &one), 4, 1)); + expr *root = new_sum(new_power(shifted, 0.5), -1); + + mu_assert("add dense jacobian check failed", + check_jacobian_num(root, u, NUMERICAL_DIFF_DEFAULT_H)); + mu_assert("add dense wsum_hess check failed", + check_wsum_hess(root, u, &w, NUMERICAL_DIFF_DEFAULT_H)); + mu_assert("add jacobian should be dense", + z->jacobian->kind == MATRIX_PERMUTED_DENSE && z->jacobian->nnz == 24); + mu_assert("elementwise wsum_hess should be dense", + root->wsum_hess->kind == MATRIX_PERMUTED_DENSE && + root->wsum_hess->nnz == 36); + + free_expr(root); + return 0; +} + +/* (A x) * (B x) elementwise: the Jacobian is a binary sum of two dense + matrices. */ +const char *test_binary_sum_multiply_dense(void) +{ + double A[12] = {1, 2, -1, 3, 2, -1, 3, 1, -2, 1, 1, 2}; + double B[12] = {2, -1, 3, 1, 1, 2, -1, 2, 1, 3, -2, 1}; + double u[3] = {0.2, 0.3, -0.1}; + double w[4] = {1.0, -0.5, 2.0, 0.3}; + + expr *x = new_variable(3, 1, 0, 3); + expr *Ax = new_left_matmul_dense(NULL, x, 4, 3, A); + expr *Bx = new_left_matmul_dense(NULL, x, 4, 3, B); + expr *root = new_elementwise_mult(Ax, Bx); + + mu_assert("multiply dense jacobian check failed", + check_jacobian_num(root, u, NUMERICAL_DIFF_DEFAULT_H)); + mu_assert("multiply dense wsum_hess check failed", + check_wsum_hess(root, u, w, NUMERICAL_DIFF_DEFAULT_H)); + mu_assert("multiply jacobian should be dense", + root->jacobian->kind == MATRIX_PERMUTED_DENSE && + root->jacobian->nnz == 12); + + free_expr(root); + return 0; +} + +/* (S x) * (T x) elementwise with one entry per row in S and T: the Jacobian + is a binary sum that does not fill a dense block and must stay sparse. */ +const char *test_binary_sum_multiply_sparse(void) +{ + double u[3] = {0.2, 0.3, -0.1}; + double w[4] = {1.0, -0.5, 2.0, 0.3}; + + CSR_matrix *S = new_CSR_matrix(4, 3, 4); + CSR_matrix *T = new_CSR_matrix(4, 3, 4); + for (int i = 0; i < 4; i++) + { + S->p[i] = i; + S->i[i] = i % 3; + S->x[i] = 0.5 + i; + T->p[i] = i; + T->i[i] = (i + 1) % 3; + T->x[i] = 2.0 - i; + } + S->p[4] = 4; + T->p[4] = 4; + + expr *x = new_variable(3, 1, 0, 3); + expr *Sx = new_left_matmul(NULL, x, S); + expr *Tx = new_left_matmul(NULL, x, T); + expr *root = new_elementwise_mult(Sx, Tx); + free_CSR_matrix(S); + free_CSR_matrix(T); + + mu_assert("multiply sparse jacobian check failed", + check_jacobian_num(root, u, NUMERICAL_DIFF_DEFAULT_H)); + mu_assert("multiply sparse wsum_hess check failed", + check_wsum_hess(root, u, w, NUMERICAL_DIFF_DEFAULT_H)); + mu_assert("multiply jacobian should stay sparse", + root->jacobian->kind == MATRIX_SPARSE && root->jacobian->nnz == 8); + + free_expr(root); + return 0; +} + +/* (A x)^T (B x): the chain-rule Jacobian of matmul is a binary sum with a + dense result. */ +const char *test_binary_sum_matmul_dense(void) +{ + double A[4] = {1, 2, -1, 3}; + double B[4] = {2, -1, 3, 1}; + double u[2] = {0.2, 0.3}; + double w = 1.0; + + expr *x = new_variable(2, 1, 0, 2); + expr *Ax = new_left_matmul_dense(NULL, x, 2, 2, A); + expr *Bx = new_left_matmul_dense(NULL, x, 2, 2, B); + expr *root = new_matmul(new_transpose(Ax), Bx); + + mu_assert("matmul dense jacobian check failed", + check_jacobian_num(root, u, NUMERICAL_DIFF_DEFAULT_H)); + mu_assert("matmul dense wsum_hess check failed", + check_wsum_hess(root, u, &w, NUMERICAL_DIFF_DEFAULT_H)); + mu_assert("matmul jacobian should be dense", + root->jacobian->kind == MATRIX_PERMUTED_DENSE && + root->jacobian->nnz == 2); + + free_expr(root); + return 0; +} + +#endif diff --git a/tests/wsum_hess/other/test_quad_form_dense.h b/tests/wsum_hess/other/test_quad_form_dense.h index bc44953a..6a2515f7 100644 --- a/tests/wsum_hess/other/test_quad_form_dense.h +++ b/tests/wsum_hess/other/test_quad_form_dense.h @@ -96,6 +96,8 @@ const char *test_wsum_hess_quad_form_dense_exp(void) mu_assert("dense quad_form nonlinear composition wsum_hess failed", check_wsum_hess(node, u_vals, &w, NUMERICAL_DIFF_DEFAULT_H)); + mu_assert("quad_form binary Hessian sum remains dense", + node->wsum_hess->kind == MATRIX_PERMUTED_DENSE); free_expr(node); return 0;