Skip to content

Commit 939a910

Browse files
Transurgeonclaude
andcommitted
Remove redundant NULL-after-free, rename const_ files, fix stale comments
Post-refactor cleanup after the Constant→Parameter merge: - Remove dead NULL assignments in free_type_data (quad_form, hstack, index, linear_op, left_matmul, scalar_mult, vector_mult) - Rename const_scalar_mult/const_vector_mult source and test files to drop const_ prefix - Rename test_variable_constant.h → test_variable_parameter.h and test_constant → test_fixed_parameter - Update stale comments in multiply.c and bivariate.h - Rename const_scalar_mult_expr/const_vector_mult_expr structs to scalar_mult_expr/vector_mult_expr - Update left_matmul param_source comment Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9700344 commit 939a910

16 files changed

Lines changed: 31 additions & 55 deletions

File tree

include/bivariate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ expr *new_matmul(expr *x, expr *y);
3333
/* Left matrix multiplication: A @ f(x) where A comes from a parameter node */
3434
expr *new_left_matmul(expr *param_node, expr *child);
3535

36-
/* Right matrix multiplication: f(x) @ A where A is a constant matrix */
36+
/* Right matrix multiplication: f(x) @ A where A is a fixed parameter matrix */
3737
expr *new_right_matmul(expr *u, const CSR_Matrix *A);
3838

3939
/* Scalar multiplication: a * f(x) where a comes from a parameter node */

include/subexpr.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ typedef struct left_matmul_expr
126126
CSC_Matrix *Jchild_CSC;
127127
CSC_Matrix *J_CSC;
128128
int *csc_to_csr_workspace;
129-
expr *param_source; /* if non-NULL, A/AT values come from this parameter */
129+
expr *param_source; /* parameter node; A/AT values are refreshed from this */
130130
int src_m, src_n; /* original matrix dimensions */
131131
} left_matmul_expr;
132132

@@ -142,19 +142,19 @@ typedef struct right_matmul_expr
142142
} right_matmul_expr;
143143

144144
/* Scalar multiplication: y = a * child where a comes from a parameter node */
145-
typedef struct const_scalar_mult_expr
145+
typedef struct scalar_mult_expr
146146
{
147147
expr base;
148148
expr *param_source; /* always set; read a from param_source->value[0] */
149-
} const_scalar_mult_expr;
149+
} scalar_mult_expr;
150150

151151
/* Vector elementwise multiplication: y = a \circ child where a comes from a
152152
* parameter node */
153-
typedef struct const_vector_mult_expr
153+
typedef struct vector_mult_expr
154154
{
155155
expr base;
156156
expr *param_source; /* always set; read a from param_source->value */
157-
} const_vector_mult_expr;
157+
} vector_mult_expr;
158158

159159
/* Index/slicing: y = child[indices] where indices is a list of flat positions */
160160
typedef struct index_expr

src/affine/hstack.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,10 @@ static void free_type_data(expr *node)
165165
for (int i = 0; i < hnode->n_args; i++)
166166
{
167167
free_expr(hnode->args[i]);
168-
hnode->args[i] = NULL;
169168
}
170169

171170
free_csr_matrix(hnode->CSR_work);
172-
hnode->CSR_work = NULL;
173171
free(hnode->args);
174-
hnode->args = NULL;
175172
}
176173

177174
expr *new_hstack(expr **args, int n_args, int n_vars)

src/affine/index.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,7 @@ static bool is_affine(const expr *node)
154154
static void free_type_data(expr *node)
155155
{
156156
index_expr *idx = (index_expr *) node;
157-
if (idx->indices)
158-
{
159-
free(idx->indices);
160-
idx->indices = NULL;
161-
}
157+
free(idx->indices);
162158
}
163159

164160
expr *new_index(expr *child, int d1, int d2, const int *indices, int n_idxs)

src/affine/linear_op.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ static void free_type_data(expr *node)
6262
if (lin_node->b != NULL)
6363
{
6464
free(lin_node->b);
65-
lin_node->b = NULL;
6665
}
67-
68-
lin_node->A_csr = NULL;
69-
lin_node->A_csc = NULL;
7066
}
7167

7268
static void jacobian_init(expr *node)

src/bivariate/left_matmul.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,7 @@ static void free_type_data(expr *node)
9595
free_csc_matrix(lin_node->Jchild_CSC);
9696
free_csc_matrix(lin_node->J_CSC);
9797
free(lin_node->csc_to_csr_workspace);
98-
if (lin_node->param_source) free_expr(lin_node->param_source);
99-
lin_node->A = NULL;
100-
lin_node->AT = NULL;
101-
lin_node->Jchild_CSC = NULL;
102-
lin_node->J_CSC = NULL;
103-
lin_node->csc_to_csr_workspace = NULL;
104-
lin_node->param_source = NULL;
98+
free_expr(lin_node->param_source);
10599
}
106100

107101
static void jacobian_init(expr *node)

src/bivariate/multiply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// ------------------------------------------------------------------------------
2828
// Implementation of elementwise multiplication when both arguments are vectors.
2929
// If one argument is a scalar variable, the broadcasting should be represented
30-
// as a linear operator child node? How to treat if one variable is a constant?
30+
// as a linear operator child node.
3131
// ------------------------------------------------------------------------------
3232
static void forward(expr *node, const double *u)
3333
{
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void forward(expr *node, const double *u)
3232
child->forward(child, u);
3333

3434
/* local forward pass: multiply each element by scalar a */
35-
const_scalar_mult_expr *sn = (const_scalar_mult_expr *) node;
35+
scalar_mult_expr *sn = (scalar_mult_expr *) node;
3636
double a = sn->param_source->value[0];
3737
for (int i = 0; i < node->size; i++)
3838
{
@@ -56,7 +56,7 @@ static void jacobian_init(expr *node)
5656
static void eval_jacobian(expr *node)
5757
{
5858
expr *child = node->left;
59-
const_scalar_mult_expr *sn = (const_scalar_mult_expr *) node;
59+
scalar_mult_expr *sn = (scalar_mult_expr *) node;
6060
double a = sn->param_source->value[0];
6161

6262
/* evaluate child */
@@ -87,7 +87,7 @@ static void eval_wsum_hess(expr *node, const double *w)
8787
expr *x = node->left;
8888
x->eval_wsum_hess(x, w);
8989

90-
const_scalar_mult_expr *sn = (const_scalar_mult_expr *) node;
90+
scalar_mult_expr *sn = (scalar_mult_expr *) node;
9191
double a = sn->param_source->value[0];
9292
for (int j = 0; j < x->wsum_hess->nnz; j++)
9393
{
@@ -103,17 +103,14 @@ static bool is_affine(const expr *node)
103103

104104
static void free_type_data(expr *node)
105105
{
106-
const_scalar_mult_expr *sn = (const_scalar_mult_expr *) node;
107-
if (sn->param_source)
108-
{
109-
free_expr(sn->param_source);
110-
}
106+
scalar_mult_expr *sn = (scalar_mult_expr *) node;
107+
free_expr(sn->param_source);
111108
}
112109

113110
expr *new_scalar_mult(expr *param_node, expr *child)
114111
{
115-
const_scalar_mult_expr *mult_node =
116-
(const_scalar_mult_expr *) calloc(1, sizeof(const_scalar_mult_expr));
112+
scalar_mult_expr *mult_node =
113+
(scalar_mult_expr *) calloc(1, sizeof(scalar_mult_expr));
117114
expr *node = &mult_node->base;
118115

119116
init_expr(node, child->d1, child->d2, child->n_vars, forward, jacobian_init,
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
static void forward(expr *node, const double *u)
2828
{
2929
expr *child = node->left;
30-
const_vector_mult_expr *vn = (const_vector_mult_expr *) node;
30+
vector_mult_expr *vn = (vector_mult_expr *) node;
3131
const double *a = vn->param_source->value;
3232

3333
/* child's forward pass */
@@ -56,7 +56,7 @@ static void jacobian_init(expr *node)
5656
static void eval_jacobian(expr *node)
5757
{
5858
expr *x = node->left;
59-
const_vector_mult_expr *vn = (const_vector_mult_expr *) node;
59+
vector_mult_expr *vn = (vector_mult_expr *) node;
6060
const double *a = vn->param_source->value;
6161

6262
/* evaluate x */
@@ -90,7 +90,7 @@ static void wsum_hess_init(expr *node)
9090
static void eval_wsum_hess(expr *node, const double *w)
9191
{
9292
expr *x = node->left;
93-
const_vector_mult_expr *vn = (const_vector_mult_expr *) node;
93+
vector_mult_expr *vn = (vector_mult_expr *) node;
9494
const double *a = vn->param_source->value;
9595

9696
/* scale weights w by a */
@@ -113,17 +113,14 @@ static bool is_affine(const expr *node)
113113

114114
static void free_type_data(expr *node)
115115
{
116-
const_vector_mult_expr *vnode = (const_vector_mult_expr *) node;
117-
if (vnode->param_source)
118-
{
119-
free_expr(vnode->param_source);
120-
}
116+
vector_mult_expr *vnode = (vector_mult_expr *) node;
117+
free_expr(vnode->param_source);
121118
}
122119

123120
expr *new_vector_mult(expr *param_node, expr *child)
124121
{
125-
const_vector_mult_expr *vnode =
126-
(const_vector_mult_expr *) calloc(1, sizeof(const_vector_mult_expr));
122+
vector_mult_expr *vnode =
123+
(vector_mult_expr *) calloc(1, sizeof(vector_mult_expr));
127124
expr *node = &vnode->base;
128125

129126
init_expr(node, child->d1, child->d2, child->n_vars, forward, jacobian_init,

src/other/quad_form.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ static void free_type_data(expr *node)
174174
{
175175
quad_form_expr *qnode = (quad_form_expr *) node;
176176
free_csr_matrix(qnode->Q);
177-
qnode->Q = NULL;
178177
}
179178

180179
static bool is_affine(const expr *node)

0 commit comments

Comments
 (0)