Skip to content

Commit 618efc2

Browse files
committed
refactored to remove some redundant init functions
1 parent 5527b17 commit 618efc2

9 files changed

Lines changed: 40 additions & 108 deletions

File tree

include/affine.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#include "subexpr.h"
66
#include "utils/CSR_Matrix.h"
77

8-
/* Helper function to initialize a linear operator expr (can be used with derived
9-
* types) */
10-
void init_linear_op(expr *node, expr *child, int d1, int d2);
11-
128
expr *new_linear(expr *u, const CSR_Matrix *A);
139

1410
expr *new_add(expr *left, expr *right);

include/expr.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ typedef struct expr
5454

5555
} expr;
5656

57-
void init_expr(expr *node, int d1, int d2, int n_vars);
57+
void init_expr(expr *node, int d1, int d2, int n_vars, forward_fn forward,
58+
jacobian_init_fn jacobian_init, eval_jacobian_fn eval_jacobian,
59+
is_affine_fn is_affine, free_type_data_fn free_type_data);
5860

5961
expr *new_expr(int d1, int d2, int n_vars);
6062
void free_expr(expr *node);

include/other.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
#include "subexpr.h"
66
#include "utils/CSR_Matrix.h"
77

8-
/* Helper function to initialize a quad_form expr (can be used with derived types) */
9-
void init_quad_form(expr *node, expr *child);
10-
118
expr *new_quad_form(expr *child, CSR_Matrix *Q);
129

1310
#endif /* OTHER_H */

src/affine/hstack.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,8 @@ expr *new_hstack(expr **args, int n_args, int n_vars)
115115
/* Allocate the type-specific struct */
116116
hstack_expr *hnode = (hstack_expr *) calloc(1, sizeof(hstack_expr));
117117
expr *node = &hnode->base;
118-
119-
/* Initialize basic fields */
120-
init_expr(node, args[0]->d1, d2, n_vars);
121-
122-
/* Set function pointers */
123-
node->forward = forward;
124-
node->jacobian_init = jacobian_init;
125-
node->eval_jacobian = eval_jacobian;
126-
node->is_affine = is_affine;
127-
node->free_type_data = free_type_data;
118+
init_expr(node, args[0]->d1, d2, n_vars, forward, jacobian_init, eval_jacobian,
119+
is_affine, free_type_data);
128120

129121
/* Set type-specific fields */
130122
hnode->args = args;

src/affine/linear_op.c

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,21 @@ static void free_type_data(expr *node)
2323
free_csc_matrix(lin_node->A_csc);
2424
}
2525

26-
/* Helper function to initialize a linear operator expr */
27-
void init_linear_op(expr *node, expr *child, int d1, int d2)
28-
{
29-
node->d1 = d1;
30-
node->d2 = d2;
31-
node->size = d1 * d2;
32-
node->n_vars = child->n_vars;
33-
node->var_id = -1;
34-
node->refcount = 1;
35-
node->left = child;
36-
node->right = NULL;
37-
node->dwork = NULL;
38-
node->iwork = NULL;
39-
node->value = (double *) calloc(node->size, sizeof(double));
40-
node->jacobian = NULL;
41-
node->wsum_hess = NULL;
42-
node->jacobian_init = NULL;
43-
node->wsum_hess_init = NULL;
44-
node->eval_jacobian = NULL;
45-
node->eval_wsum_hess = NULL;
46-
node->local_jacobian = NULL;
47-
node->local_wsum_hess = NULL;
48-
node->forward = forward;
49-
node->is_affine = is_affine;
50-
node->free_type_data = free_type_data;
51-
52-
expr_retain(child);
53-
}
54-
5526
expr *new_linear(expr *u, const CSR_Matrix *A)
5627
{
5728
/* Allocate the type-specific struct */
58-
linear_op_expr *lin_node = (linear_op_expr *) malloc(sizeof(linear_op_expr));
29+
linear_op_expr *lin_node = (linear_op_expr *) calloc(1, sizeof(linear_op_expr));
5930
if (!lin_node) return NULL;
6031

6132
expr *node = &lin_node->base;
6233

63-
/* Initialize base linear operator fields */
64-
init_linear_op(node, u, A->m, 1);
34+
/* Initialize base fields */
35+
init_expr(node, A->m, 1, u->n_vars, forward, NULL, NULL, is_affine,
36+
free_type_data);
37+
38+
/* Set left child */
39+
node->left = u;
40+
expr_retain(u);
6541

6642
/* Check if allocation succeeded */
6743
if (!node->value)

src/elementwise_univariate/common.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,39 +103,23 @@ bool is_affine_elementwise(const expr *node)
103103
*/
104104
void init_elementwise(expr *node, expr *child)
105105
{
106-
/* Initialize dimensions from child */
107-
node->d1 = child->d1;
108-
node->d2 = child->d2;
109-
node->size = child->d1 * child->d2;
110-
node->n_vars = child->n_vars;
111-
node->var_id = -1;
112-
node->refcount = 1;
106+
/* Initialize base fields */
107+
init_expr(node, child->d1, child->d2, child->n_vars, NULL,
108+
jacobian_init_elementwise, eval_jacobian_elementwise,
109+
is_affine_elementwise, NULL);
113110

114-
/* Allocate the value array */
115-
node->value = (double *) calloc(node->size, sizeof(double));
116-
117-
node->left = child;
118-
node->right = NULL;
119-
node->dwork = NULL;
120-
node->iwork = NULL;
121-
node->jacobian = NULL;
122-
node->wsum_hess = NULL;
123-
node->jacobian_init = jacobian_init_elementwise;
111+
/* Set wsum_hess functions */
124112
node->wsum_hess_init = wsum_hess_init_elementwise;
125-
node->eval_jacobian = eval_jacobian_elementwise;
126113
node->eval_wsum_hess = eval_wsum_hess_elementwise;
127-
node->local_jacobian = NULL;
128-
node->local_wsum_hess = NULL;
129-
node->is_affine = is_affine_elementwise;
130-
node->forward = NULL;
131-
node->free_type_data = NULL;
132114

115+
/* Set left child */
116+
node->left = child;
133117
expr_retain(child);
134118
}
135119

136120
expr *new_elementwise(expr *child)
137121
{
138-
expr *node = (expr *) malloc(sizeof(expr));
122+
expr *node = (expr *) calloc(1, sizeof(expr));
139123
if (!node) return NULL;
140124

141125
init_elementwise(node, child);

src/elementwise_univariate/power.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static void local_wsum_hess(expr *node, double *out, const double *w)
4343
expr *new_power(expr *child, int p)
4444
{
4545
/* Allocate the type-specific struct */
46-
power_expr *pnode = (power_expr *) malloc(sizeof(power_expr));
46+
power_expr *pnode = (power_expr *) calloc(1, sizeof(power_expr));
4747
expr *node = &pnode->base;
4848

4949
/* Initialize base elementwise fields */

src/expr.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include <stdlib.h>
44
#include <string.h>
55

6-
void init_expr(expr *node, int d1, int d2, int n_vars)
6+
void init_expr(expr *node, int d1, int d2, int n_vars, forward_fn forward,
7+
jacobian_init_fn jacobian_init, eval_jacobian_fn eval_jacobian,
8+
is_affine_fn is_affine, free_type_data_fn free_type_data)
79
{
810
node->d1 = d1;
911
node->d2 = d2;
@@ -12,12 +14,17 @@ void init_expr(expr *node, int d1, int d2, int n_vars)
1214
node->refcount = 1;
1315
node->value = (double *) calloc(d1 * d2, sizeof(double));
1416
node->var_id = -1;
17+
node->forward = forward;
18+
node->jacobian_init = jacobian_init;
19+
node->eval_jacobian = eval_jacobian;
20+
node->is_affine = is_affine;
21+
node->free_type_data = free_type_data;
1522
}
1623

1724
expr *new_expr(int d1, int d2, int n_vars)
1825
{
1926
expr *node = (expr *) calloc(1, sizeof(expr));
20-
init_expr(node, d1, d2, n_vars);
27+
init_expr(node, d1, d2, n_vars, NULL, NULL, NULL, NULL, NULL);
2128
return node;
2229
}
2330

src/other/quad_form.c

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -103,40 +103,18 @@ static void eval_jacobian(expr *node)
103103
}
104104
}
105105

106-
/* Helper function to initialize a quad_form expr */
107-
void init_quad_form(expr *node, expr *child)
108-
{
109-
node->d1 = child->d1;
110-
node->d2 = 1;
111-
node->size = child->d1 * 1;
112-
node->n_vars = child->n_vars;
113-
node->var_id = -1;
114-
node->refcount = 1;
115-
node->left = child;
116-
node->right = NULL;
117-
node->dwork = NULL;
118-
node->iwork = NULL;
119-
node->value = (double *) calloc(node->size, sizeof(double));
120-
node->jacobian = NULL;
121-
node->wsum_hess = NULL;
122-
node->jacobian_init = jacobian_init;
123-
node->wsum_hess_init = NULL;
124-
node->eval_jacobian = eval_jacobian;
125-
node->eval_wsum_hess = NULL;
126-
node->local_jacobian = NULL;
127-
node->local_wsum_hess = NULL;
128-
node->is_affine = NULL;
129-
node->forward = forward;
130-
node->free_type_data = NULL;
131-
132-
expr_retain(child);
133-
}
134-
135106
expr *new_quad_form(expr *left, CSR_Matrix *Q)
136107
{
137-
quad_form_expr *qnode = (quad_form_expr *) malloc(sizeof(quad_form_expr));
108+
quad_form_expr *qnode = (quad_form_expr *) calloc(1, sizeof(quad_form_expr));
138109
expr *node = &qnode->base;
139-
init_quad_form(node, left);
110+
111+
/* Initialize base fields */
112+
init_expr(node, left->d1, 1, left->n_vars, forward, jacobian_init, eval_jacobian,
113+
NULL, NULL);
114+
115+
/* Set left child */
116+
node->left = left;
117+
expr_retain(left);
140118

141119
/* Set type-specific field */
142120
qnode->Q = Q;

0 commit comments

Comments
 (0)