|
12 | 12 | #include "subexpr.h" |
13 | 13 | #include "test_helpers.h" |
14 | 14 |
|
15 | | -const char *test_param_broadcast_vector_mult(void) |
| 15 | +const char *test_constant_broadcast_vector_mult(void) |
16 | 16 | { |
17 | 17 | int n = 6; |
18 | 18 |
|
19 | | - /* minimize sum(x) subject to broadcast(p) ∘ x, with p parameter */ |
| 19 | + /* minimize sum(x) subject to broadcast(c) ∘ x, with c constant */ |
20 | 20 | expr *x = new_variable(2, 3, 0, n); |
21 | 21 | expr *objective = new_sum(x, -1); |
22 | | - expr *p_param = new_parameter(1, 3, 0, n, NULL); |
23 | | - expr *p_bcast = new_broadcast(p_param, 2, 3); |
24 | | - expr *constraint = new_vector_mult(p_bcast, x); |
| 22 | + double c_vals[3] = {1.0, 2.0, 3.0}; |
| 23 | + expr *c = new_parameter(1, 3, PARAM_FIXED, n, c_vals); |
| 24 | + expr *c_bcast = new_broadcast(c, 2, 3); |
| 25 | + expr *constraint = new_vector_mult(c_bcast, x); |
25 | 26 | expr *constraints[1] = {constraint}; |
26 | 27 | problem *prob = new_problem(objective, constraints, 1, false); |
27 | | - |
28 | | - /* register parameters and fill sparsity patterns */ |
29 | | - expr *param_nodes[1] = {p_param}; |
30 | | - problem_register_params(prob, param_nodes, 1); |
31 | 28 | problem_init_derivatives(prob); |
32 | 29 |
|
33 | 30 | /* point for evaluating */ |
34 | 31 | double x_vals[6] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; |
35 | 32 |
|
36 | | - /* test 1: p=[1,2,3] */ |
37 | | - double theta[3] = {1.0, 2.0, 3.0}; |
38 | | - problem_update_params(prob, theta); |
39 | 33 | problem_constraint_forward(prob, x_vals); |
40 | 34 | double constrs[6] = {1.0, 2.0, 6.0, 8.0, 15.0, 18.0}; |
41 | 35 | problem_jacobian(prob); |
42 | 36 | double jac_x[6] = {1.0, 1.0, 2.0, 2.0, 3.0, 3.0}; |
43 | 37 | mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 6)); |
44 | 38 | mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 6)); |
45 | 39 |
|
46 | | - /* test 2: p=[10,20,30] */ |
47 | | - theta[0] = 10.0; |
48 | | - theta[1] = 20.0; |
49 | | - theta[2] = 30.0; |
50 | | - problem_update_params(prob, theta); |
51 | | - problem_constraint_forward(prob, x_vals); |
52 | | - problem_jacobian(prob); |
53 | | - constrs[0] = 10.0; |
54 | | - constrs[1] = 20.0; |
55 | | - constrs[2] = 60.0; |
56 | | - constrs[3] = 80.0; |
57 | | - constrs[4] = 150.0; |
58 | | - constrs[5] = 180.0; |
59 | | - jac_x[0] = 10.0; |
60 | | - jac_x[1] = 10.0; |
61 | | - jac_x[2] = 20.0; |
62 | | - jac_x[3] = 20.0; |
63 | | - jac_x[4] = 30.0; |
64 | | - jac_x[5] = 30.0; |
65 | | - mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 6)); |
66 | | - mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 6)); |
67 | | - |
68 | 40 | free_problem(prob); |
69 | 41 | return 0; |
70 | 42 | } |
71 | 43 |
|
72 | | -const char *test_param_sum_scalar_mult(void) |
| 44 | +const char *test_constant_promote_vector_mult(void) |
73 | 45 | { |
74 | | - int n = 3; |
| 46 | + int n = 6; |
75 | 47 |
|
76 | | - /* minimize sum(x) subject to sum(p) * x, with p parameter */ |
77 | | - expr *x = new_variable(3, 1, 0, n); |
| 48 | + /* minimize sum(x) subject to promote(c) ∘ x, with c constant */ |
| 49 | + expr *x = new_variable(2, 3, 0, n); |
78 | 50 | expr *objective = new_sum(x, -1); |
79 | | - expr *p_param = new_parameter(2, 1, 0, n, NULL); |
80 | | - expr *p_sum = new_sum(p_param, -1); |
81 | | - expr *constraint = new_scalar_mult(p_sum, x); |
| 51 | + double c_vals = 3.0; |
| 52 | + expr *c = new_parameter(1, 1, PARAM_FIXED, n, &c_vals); |
| 53 | + expr *c_bcast = new_promote(c, 2, 3); |
| 54 | + expr *constraint = new_vector_mult(c_bcast, x); |
82 | 55 | expr *constraints[1] = {constraint}; |
83 | 56 | problem *prob = new_problem(objective, constraints, 1, false); |
84 | 57 |
|
85 | | - /* register parameters and fill sparsity patterns */ |
86 | | - expr *param_nodes[1] = {p_param}; |
87 | | - problem_register_params(prob, param_nodes, 1); |
88 | 58 | problem_init_derivatives(prob); |
89 | 59 |
|
90 | 60 | /* point for evaluating */ |
91 | | - double x_vals[3] = {1.0, 2.0, 3.0}; |
92 | | - |
93 | | - /* test 1: p=[1,2], sum(p)=3 */ |
94 | | - double theta[2] = {1.0, 2.0}; |
95 | | - problem_update_params(prob, theta); |
96 | | - problem_constraint_forward(prob, x_vals); |
97 | | - double constrs[3] = {3.0, 6.0, 9.0}; |
98 | | - problem_jacobian(prob); |
99 | | - double jac_x[3] = {3.0, 3.0, 3.0}; |
100 | | - mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 3)); |
101 | | - mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 3)); |
102 | | - |
103 | | - /* test 2: p=[5,10], sum(p)=15 */ |
104 | | - theta[0] = 5.0; |
105 | | - theta[1] = 10.0; |
106 | | - problem_update_params(prob, theta); |
107 | | - problem_constraint_forward(prob, x_vals); |
108 | | - problem_jacobian(prob); |
109 | | - constrs[0] = 15.0; |
110 | | - constrs[1] = 30.0; |
111 | | - constrs[2] = 45.0; |
112 | | - jac_x[0] = 15.0; |
113 | | - jac_x[1] = 15.0; |
114 | | - jac_x[2] = 15.0; |
115 | | - mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 3)); |
116 | | - mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 3)); |
117 | | - |
118 | | - free_problem(prob); |
119 | | - return 0; |
120 | | -} |
121 | | - |
122 | | -const char *test_param_broadcast_left_matmul(void) |
123 | | -{ |
124 | | - int n = 2; |
125 | | - |
126 | | - /* minimize sum(x) subject to broadcast(p)@x, with p parameter */ |
127 | | - expr *x = new_variable(2, 1, 0, n); |
128 | | - expr *objective = new_sum(x, -1); |
129 | | - expr *p_param = new_parameter(1, 2, 0, n, NULL); |
130 | | - expr *p_bcast = new_broadcast(p_param, 3, 2); |
131 | | - double Ax[6] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; |
132 | | - expr *constraint = new_left_matmul_dense(p_bcast, x, 3, 2, Ax); |
133 | | - expr *constraints[1] = {constraint}; |
134 | | - problem *prob = new_problem(objective, constraints, 1, false); |
135 | | - |
136 | | - /* register parameters and fill sparsity patterns */ |
137 | | - expr *param_nodes[1] = {p_param}; |
138 | | - problem_register_params(prob, param_nodes, 1); |
139 | | - problem_init_derivatives(prob); |
140 | | - |
141 | | - /* point for evaluating and utilities for test */ |
142 | | - double x_vals[2] = {3.0, 4.0}; |
143 | | - int Ap[4] = {0, 2, 4, 6}; |
144 | | - int Ai[6] = {0, 1, 0, 1, 0, 1}; |
145 | | - |
146 | | - /* test 1: p=[1,2] */ |
147 | | - double theta[2] = {1.0, 2.0}; |
148 | | - problem_update_params(prob, theta); |
149 | | - problem_constraint_forward(prob, x_vals); |
150 | | - double constrs[3] = {11.0, 11.0, 11.0}; |
151 | | - problem_jacobian(prob); |
152 | | - double jac_x[6] = {1.0, 2.0, 1.0, 2.0, 1.0, 2.0}; |
153 | | - mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 3)); |
154 | | - mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 6)); |
155 | | - mu_assert("rows fail", cmp_int_array(prob->jacobian->p, Ap, 4)); |
156 | | - mu_assert("cols fail", cmp_int_array(prob->jacobian->i, Ai, 6)); |
| 61 | + double x_vals[6] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; |
157 | 62 |
|
158 | | - /* test 2: p=[5,10] */ |
159 | | - theta[0] = 5.0; |
160 | | - theta[1] = 10.0; |
161 | | - problem_update_params(prob, theta); |
162 | 63 | problem_constraint_forward(prob, x_vals); |
| 64 | + double constrs[6] = {3.0, 6.0, 9.0, 12.0, 15.0, 18.0}; |
163 | 65 | problem_jacobian(prob); |
164 | | - constrs[0] = 55.0; |
165 | | - constrs[1] = 55.0; |
166 | | - constrs[2] = 55.0; |
167 | | - jac_x[0] = 5.0; |
168 | | - jac_x[1] = 10.0; |
169 | | - jac_x[2] = 5.0; |
170 | | - jac_x[3] = 10.0; |
171 | | - jac_x[4] = 5.0; |
172 | | - jac_x[5] = 10.0; |
173 | | - mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 3)); |
| 66 | + double jac_x[6] = {3.0, 3.0, 3.0, 3.0, 3.0, 3.0}; |
| 67 | + mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 6)); |
174 | 68 | mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 6)); |
175 | | - mu_assert("rows fail", cmp_int_array(prob->jacobian->p, Ap, 4)); |
176 | | - mu_assert("cols fail", cmp_int_array(prob->jacobian->i, Ai, 6)); |
177 | 69 |
|
178 | 70 | free_problem(prob); |
179 | 71 | return 0; |
180 | 72 | } |
181 | | - |
182 | 73 | #endif /* TEST_PARAM_BROADCAST_H */ |
0 commit comments