Skip to content

Commit 2031ed5

Browse files
Transurgeonclaude
andcommitted
Upgrade upper_tri jacobian and hessian tests to 4x4
The 3x3 tests produced identical indices for both row-major and column-major orderings, so they didn't actually verify the fix. 4x4 indices [4,8,12,9,13,14] differ from column-major [4,8,9,12,13,14]. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 93eb23c commit 2031ed5

File tree

2 files changed

+65
-46
lines changed

2 files changed

+65
-46
lines changed

tests/jacobian_tests/affine/test_upper_tri.h

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,64 @@
88

99
const char *test_upper_tri_jacobian_variable(void)
1010
{
11-
/* upper_tri of a 3x3 variable (9 vars total)
12-
* Upper tri flat indices: [3, 6, 7]
13-
* Jacobian is 3x9 CSR: row k has col indices[k] */
14-
double u[9] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
15-
expr *var = new_variable(3, 3, 0, 9);
11+
/* upper_tri of a 4x4 variable (16 vars total)
12+
* Row-major upper tri indices: [4, 8, 12, 9, 13, 14]
13+
* Jacobian is 6x16 CSR: row k has a single 1.0 at col indices[k] */
14+
double u[16];
15+
for (int k = 0; k < 16; k++)
16+
{
17+
u[k] = (double) (k + 1);
18+
}
19+
expr *var = new_variable(4, 4, 0, 16);
1620
expr *ut = new_upper_tri(var);
1721

1822
ut->forward(ut, u);
1923
jacobian_init(ut);
2024
ut->eval_jacobian(ut);
2125

22-
double expected_x[3] = {1.0, 1.0, 1.0};
23-
int expected_p[4] = {0, 1, 2, 3};
24-
int expected_i[3] = {3, 6, 7};
26+
double expected_x[6] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
27+
int expected_p[7] = {0, 1, 2, 3, 4, 5, 6};
28+
int expected_i[6] = {4, 8, 12, 9, 13, 14};
2529

2630
mu_assert("upper_tri jac vals",
27-
cmp_double_array(ut->jacobian->x, expected_x, 3));
28-
mu_assert("upper_tri jac p", cmp_int_array(ut->jacobian->p, expected_p, 4));
29-
mu_assert("upper_tri jac i", cmp_int_array(ut->jacobian->i, expected_i, 3));
31+
cmp_double_array(ut->jacobian->x, expected_x, 6));
32+
mu_assert("upper_tri jac p",
33+
cmp_int_array(ut->jacobian->p, expected_p, 7));
34+
mu_assert("upper_tri jac i",
35+
cmp_int_array(ut->jacobian->i, expected_i, 6));
3036

3137
free_expr(ut);
3238
return 0;
3339
}
3440

3541
const char *test_upper_tri_jacobian_of_log(void)
3642
{
37-
/* upper_tri(log(X)) where X is 3x3 variable
38-
* Upper tri flat indices: [3, 6, 7]
39-
* X values at those positions: x[3]=4, x[6]=7, x[7]=8
40-
* d/dx log at those positions:
41-
* Row 0: 1/4 = 0.25 at col 3
42-
* Row 1: 1/7 at col 6
43-
* Row 2: 1/8 = 0.125 at col 7 */
44-
double u[9] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
45-
expr *var = new_variable(3, 3, 0, 9);
43+
/* upper_tri(log(X)) where X is 4x4 variable
44+
* Row-major upper tri indices: [4, 8, 12, 9, 13, 14]
45+
* Values at those positions: u[4]=5, u[8]=9, u[12]=13,
46+
* u[9]=10, u[13]=14, u[14]=15
47+
* d/dx log at those positions: 1/5, 1/9, 1/13, 1/10, 1/14, 1/15 */
48+
double u[16];
49+
for (int k = 0; k < 16; k++)
50+
{
51+
u[k] = (double) (k + 1);
52+
}
53+
expr *var = new_variable(4, 4, 0, 16);
4654
expr *log_node = new_log(var);
4755
expr *ut = new_upper_tri(log_node);
4856

4957
ut->forward(ut, u);
5058
jacobian_init(ut);
5159
ut->eval_jacobian(ut);
5260

53-
double expected_x[3] = {0.25, 1.0 / 7.0, 0.125};
54-
int expected_i[3] = {3, 6, 7};
61+
double expected_x[6] = {0.2, 1.0 / 9.0, 1.0 / 13.0,
62+
0.1, 1.0 / 14.0, 1.0 / 15.0};
63+
int expected_i[6] = {4, 8, 12, 9, 13, 14};
5564

5665
mu_assert("upper_tri log jac vals",
57-
cmp_double_array(ut->jacobian->x, expected_x, 3));
66+
cmp_double_array(ut->jacobian->x, expected_x, 6));
5867
mu_assert("upper_tri log jac cols",
59-
cmp_int_array(ut->jacobian->i, expected_i, 3));
68+
cmp_int_array(ut->jacobian->i, expected_i, 6));
6069

6170
free_expr(ut);
6271
return 0;

tests/wsum_hess/affine/test_upper_tri.h

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,29 @@
1111

1212
const char *test_wsum_hess_upper_tri_log(void)
1313
{
14-
/* upper_tri(log(X)) where X is 3x3, w = [1, 1, 1]
15-
* X (column-major): [1, 2, 3, 4, 5, 6, 7, 8, 9]
16-
* Upper tri flat indices (row-major): [3, 6, 7]
17-
* Hessian of log is diag(-1/x^2)
18-
* Weights scatter: parent_w[3]=1, parent_w[6]=1, parent_w[7]=1
19-
* All other parent_w entries = 0
14+
/* upper_tri(log(X)) where X is 4x4, w = [1, 1, 1, 1, 1, 1]
15+
* X (column-major): [1..16]
16+
* Row-major upper tri indices: [4, 8, 12, 9, 13, 14]
17+
* Weights scatter to parent_w: 1.0 at positions
18+
* {4, 8, 9, 12, 13, 14}, zero elsewhere.
2019
*
20+
* Hessian of log is diag(-1/x^2).
2121
* H[k,k] = parent_w[k] * (-1/x[k]^2):
22-
* H[0,0] = 0, H[1,1] = 0, H[2,2] = 0
23-
* H[3,3] = -1/16 = -0.0625
24-
* H[4,4] = 0, H[5,5] = 0
25-
* H[6,6] = -1/49
26-
* H[7,7] = -1/64 = -0.015625
27-
* H[8,8] = 0 */
28-
double u[9] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
29-
double w[3] = {1.0, 1.0, 1.0};
22+
* H[4,4] = -1/25 = -0.04
23+
* H[8,8] = -1/81
24+
* H[9,9] = -1/100 = -0.01
25+
* H[12,12] = -1/169
26+
* H[13,13] = -1/196
27+
* H[14,14] = -1/225
28+
* All others = 0 */
29+
double u[16];
30+
for (int k = 0; k < 16; k++)
31+
{
32+
u[k] = (double) (k + 1);
33+
}
34+
double w[6] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
3035

31-
expr *var = new_variable(3, 3, 0, 9);
36+
expr *var = new_variable(4, 4, 0, 16);
3237
expr *log_node = new_log(var);
3338
expr *ut = new_upper_tri(log_node);
3439

@@ -37,17 +42,22 @@ const char *test_wsum_hess_upper_tri_log(void)
3742
wsum_hess_init(ut);
3843
ut->eval_wsum_hess(ut, w);
3944

40-
double expected_x[9] = {0.0, 0.0, 0.0, -0.0625, 0.0,
41-
0.0, -1.0 / 49.0, -0.015625, 0.0};
42-
int expected_p[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
43-
int expected_i[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
45+
double expected_x[16] = {
46+
0.0, 0.0, 0.0, 0.0,
47+
-1.0 / 25.0, 0.0, 0.0, 0.0,
48+
-1.0 / 81.0, -1.0 / 100.0, 0.0, 0.0,
49+
-1.0 / 169.0, -1.0 / 196.0, -1.0 / 225.0, 0.0};
50+
int expected_p[17] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
51+
11, 12, 13, 14, 15, 16};
52+
int expected_i[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
53+
11, 12, 13, 14, 15};
4454

4555
mu_assert("upper_tri log hess vals",
46-
cmp_double_array(ut->wsum_hess->x, expected_x, 9));
56+
cmp_double_array(ut->wsum_hess->x, expected_x, 16));
4757
mu_assert("upper_tri log hess p",
48-
cmp_int_array(ut->wsum_hess->p, expected_p, 10));
58+
cmp_int_array(ut->wsum_hess->p, expected_p, 17));
4959
mu_assert("upper_tri log hess i",
50-
cmp_int_array(ut->wsum_hess->i, expected_i, 9));
60+
cmp_int_array(ut->wsum_hess->i, expected_i, 16));
5161

5262
free_expr(ut);
5363
return 0;

0 commit comments

Comments
 (0)