|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +#include "affine.h" |
| 4 | +#include <assert.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +/* diag_vec: converts a vector of size n into an n×n diagonal matrix. |
| 9 | + * In Fortran (column-major) order, element i of the input maps to |
| 10 | + * position i*(n+1) in the flattened output (the diagonal positions). */ |
| 11 | + |
| 12 | +static void forward(expr *node, const double *u) |
| 13 | +{ |
| 14 | + expr *x = node->left; |
| 15 | + int n = x->size; |
| 16 | + |
| 17 | + /* child's forward pass */ |
| 18 | + x->forward(x, u); |
| 19 | + |
| 20 | + /* zero-initialize output */ |
| 21 | + memset(node->value, 0, node->size * sizeof(double)); |
| 22 | + |
| 23 | + /* place input elements on the diagonal */ |
| 24 | + for (int i = 0; i < n; i++) |
| 25 | + { |
| 26 | + node->value[i * (n + 1)] = x->value[i]; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +static void jacobian_init(expr *node) |
| 31 | +{ |
| 32 | + expr *x = node->left; |
| 33 | + int n = x->size; |
| 34 | + x->jacobian_init(x); |
| 35 | + |
| 36 | + CSR_Matrix *Jx = x->jacobian; |
| 37 | + CSR_Matrix *J = new_csr_matrix(node->size, node->n_vars, Jx->nnz); |
| 38 | + |
| 39 | + /* Output has n² rows but only n diagonal positions are non-empty. |
| 40 | + * Diagonal position i is at row i*(n+1) in Fortran order. */ |
| 41 | + int nnz = 0; |
| 42 | + int next_diag = 0; |
| 43 | + for (int row = 0; row < node->size; row++) |
| 44 | + { |
| 45 | + J->p[row] = nnz; |
| 46 | + if (row == next_diag) |
| 47 | + { |
| 48 | + int child_row = row / (n + 1); |
| 49 | + int len = Jx->p[child_row + 1] - Jx->p[child_row]; |
| 50 | + memcpy(J->i + nnz, Jx->i + Jx->p[child_row], len * sizeof(int)); |
| 51 | + nnz += len; |
| 52 | + next_diag += n + 1; |
| 53 | + } |
| 54 | + } |
| 55 | + J->p[node->size] = nnz; |
| 56 | + |
| 57 | + node->jacobian = J; |
| 58 | +} |
| 59 | + |
| 60 | +static void eval_jacobian(expr *node) |
| 61 | +{ |
| 62 | + expr *x = node->left; |
| 63 | + int n = x->size; |
| 64 | + x->eval_jacobian(x); |
| 65 | + |
| 66 | + CSR_Matrix *J = node->jacobian; |
| 67 | + CSR_Matrix *Jx = x->jacobian; |
| 68 | + |
| 69 | + /* Copy values from child row i to output diagonal row i*(n+1) */ |
| 70 | + for (int i = 0; i < n; i++) |
| 71 | + { |
| 72 | + int out_row = i * (n + 1); |
| 73 | + int len = J->p[out_row + 1] - J->p[out_row]; |
| 74 | + memcpy(J->x + J->p[out_row], Jx->x + Jx->p[i], len * sizeof(double)); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +static void wsum_hess_init(expr *node) |
| 79 | +{ |
| 80 | + expr *x = node->left; |
| 81 | + |
| 82 | + /* initialize child's wsum_hess */ |
| 83 | + x->wsum_hess_init(x); |
| 84 | + |
| 85 | + /* workspace for extracting diagonal weights */ |
| 86 | + node->dwork = (double *) calloc(x->size, sizeof(double)); |
| 87 | + |
| 88 | + /* Copy child's Hessian structure (diag_vec is linear, so its own Hessian is zero) */ |
| 89 | + CSR_Matrix *Hx = x->wsum_hess; |
| 90 | + node->wsum_hess = new_csr_matrix(Hx->m, Hx->n, Hx->nnz); |
| 91 | + memcpy(node->wsum_hess->p, Hx->p, (Hx->m + 1) * sizeof(int)); |
| 92 | + memcpy(node->wsum_hess->i, Hx->i, Hx->nnz * sizeof(int)); |
| 93 | +} |
| 94 | + |
| 95 | +static void eval_wsum_hess(expr *node, const double *w) |
| 96 | +{ |
| 97 | + expr *x = node->left; |
| 98 | + int n = x->size; |
| 99 | + |
| 100 | + /* Extract weights from diagonal positions of w (which has n² elements) */ |
| 101 | + for (int i = 0; i < n; i++) |
| 102 | + { |
| 103 | + node->dwork[i] = w[i * (n + 1)]; |
| 104 | + } |
| 105 | + |
| 106 | + /* Evaluate child's Hessian with extracted weights */ |
| 107 | + x->eval_wsum_hess(x, node->dwork); |
| 108 | + memcpy(node->wsum_hess->x, x->wsum_hess->x, x->wsum_hess->nnz * sizeof(double)); |
| 109 | +} |
| 110 | + |
| 111 | +static bool is_affine(const expr *node) |
| 112 | +{ |
| 113 | + return node->left->is_affine(node->left); |
| 114 | +} |
| 115 | + |
| 116 | +expr *new_diag_vec(expr *child) |
| 117 | +{ |
| 118 | + /* child must be a vector: either column (n, 1) or row (1, n) */ |
| 119 | + assert(child->d1 == 1 || child->d2 == 1); |
| 120 | + |
| 121 | + /* n is the number of elements (works for both row and column vectors) */ |
| 122 | + int n = child->size; |
| 123 | + expr *node = (expr *) calloc(1, sizeof(expr)); |
| 124 | + init_expr(node, n, n, child->n_vars, forward, jacobian_init, eval_jacobian, |
| 125 | + is_affine, wsum_hess_init, eval_wsum_hess, NULL); |
| 126 | + node->left = child; |
| 127 | + expr_retain(child); |
| 128 | + |
| 129 | + return node; |
| 130 | +} |
0 commit comments