@@ -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