Skip to content

Commit ff777fa

Browse files
committed
one more test
1 parent ecab1d5 commit ff777fa

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

tests/all_tests.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ int main(void)
355355
mu_run_test(test_param_left_matmul_problem, tests_run);
356356
mu_run_test(test_param_right_matmul_problem, tests_run);
357357
mu_run_test(test_param_left_matmul_rectangular, tests_run);
358+
mu_run_test(test_param_right_matmul_rectangular, tests_run);
358359
mu_run_test(test_param_fixed_skip_in_update, tests_run);
359360
#endif /* PROFILE_ONLY */
360361

tests/problem/test_param_prob.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,64 @@ const char *test_param_left_matmul_rectangular(void)
340340
return 0;
341341
}
342342

343+
const char *test_param_right_matmul_rectangular(void)
344+
{
345+
int n = 2;
346+
347+
/* minimize sum(x) subject to xA = ?, with A parameter (2x3) */
348+
expr *x = new_variable(1, 2, 0, n);
349+
expr *objective = new_sum(x, -1);
350+
expr *A_param = new_parameter(2, 3, 0, n, NULL);
351+
352+
/* dense 2x3 matrix */
353+
double Ax[6] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
354+
expr *constraint = new_right_matmul_dense(A_param, x, 2, 3, Ax);
355+
expr *constraints[1] = {constraint};
356+
problem *prob = new_problem(objective, constraints, 1, false);
357+
358+
/* register parameters and fill sparsity patterns */
359+
expr *param_nodes[1] = {A_param};
360+
problem_register_params(prob, param_nodes, 1);
361+
problem_init_derivatives(prob);
362+
363+
/* point for evaluating and utilities for test */
364+
double x_vals[2] = {1.0, 2.0};
365+
int Ap[4] = {0, 2, 4, 6};
366+
int Ai[6] = {0, 1, 0, 1, 0, 1};
367+
368+
/* test 1: initial jacobian */
369+
problem_constraint_forward(prob, x_vals);
370+
double constrs[3] = {9.0, 12.0, 15.0};
371+
problem_jacobian(prob);
372+
mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 3));
373+
mu_assert("rows fail", cmp_int_array(prob->jacobian->p, Ap, 4));
374+
mu_assert("cols fail", cmp_int_array(prob->jacobian->i, Ai, 6));
375+
double jac_x[6] = {1.0, 4.0, 2.0, 5.0, 3.0, 6.0};
376+
mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 6));
377+
378+
/* test 2: A = [[7,8,9],[10,11,12]] (column-major [7,10,8,11,9,12]) */
379+
double theta[6] = {7.0, 10.0, 8.0, 11.0, 9.0, 12.0};
380+
problem_update_params(prob, theta);
381+
problem_constraint_forward(prob, x_vals);
382+
problem_jacobian(prob);
383+
constrs[0] = 27.0;
384+
constrs[1] = 30.0;
385+
constrs[2] = 33.0;
386+
jac_x[0] = 7.0;
387+
jac_x[1] = 10.0;
388+
jac_x[2] = 8.0;
389+
jac_x[3] = 11.0;
390+
jac_x[4] = 9.0;
391+
jac_x[5] = 12.0;
392+
mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 3));
393+
mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 6));
394+
mu_assert("rows fail", cmp_int_array(prob->jacobian->p, Ap, 4));
395+
mu_assert("cols fail", cmp_int_array(prob->jacobian->i, Ai, 6));
396+
mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 3));
397+
398+
free_problem(prob);
399+
400+
return 0;
401+
}
402+
343403
#endif /* TEST_PARAM_PROB_H */

0 commit comments

Comments
 (0)