|
| 1 | +#include "affine.h" |
| 2 | +#include <assert.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +// Forward pass for transpose atom |
| 7 | +static void forward(expr *node, const double *u) |
| 8 | +{ |
| 9 | + /* forward pass for child */ |
| 10 | + node->left->forward(node->left, u); |
| 11 | + |
| 12 | + /* local forward pass */ |
| 13 | + int d1 = node->d1; |
| 14 | + int d2 = node->d2; |
| 15 | + double *X = node->left->value; |
| 16 | + double *XT = node->value; |
| 17 | + for (int i = 0; i < d1; ++i) |
| 18 | + { |
| 19 | + for (int j = 0; j < d2; ++j) |
| 20 | + { |
| 21 | + XT[j * d1 + i] = X[i * d2 + j]; |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +static void jacobian_init(expr *node) |
| 27 | +{ |
| 28 | + expr *child = node->left; |
| 29 | + child->jacobian_init(child); |
| 30 | + CSR_Matrix *Jc = child->jacobian; |
| 31 | + node->jacobian = new_csr_matrix(node->size, node->n_vars, Jc->nnz); |
| 32 | + |
| 33 | + /* fill sparsity */ |
| 34 | + CSR_Matrix *J = node->jacobian; |
| 35 | + int d1 = node->d1; |
| 36 | + int d2 = node->d2; |
| 37 | + int nnz = 0; |
| 38 | + J->p[0] = 0; |
| 39 | + |
| 40 | + /* 'k' is the old row that gets swapped to 'row'*/ |
| 41 | + int k, len; |
| 42 | + for (int row = 0; row < J->m; ++row) |
| 43 | + { |
| 44 | + k = (row / d1) + (row % d1) * d2; |
| 45 | + len = Jc->p[k + 1] - Jc->p[k]; |
| 46 | + memcpy(J->i + nnz, Jc->i + Jc->p[k], len * sizeof(int)); |
| 47 | + nnz += len; |
| 48 | + J->p[row + 1] = nnz; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +static void eval_jacobian(expr *node) |
| 53 | +{ |
| 54 | + expr *child = node->left; |
| 55 | + child->eval_jacobian(child); |
| 56 | + CSR_Matrix *Jc = child->jacobian; |
| 57 | + CSR_Matrix *J = node->jacobian; |
| 58 | + |
| 59 | + int d1 = node->d1; |
| 60 | + int d2 = node->d2; |
| 61 | + int nnz = 0; |
| 62 | + for (int row = 0; row < J->m; ++row) |
| 63 | + { |
| 64 | + int k = (row / d1) + (row % d1) * d2; |
| 65 | + int len = Jc->p[k + 1] - Jc->p[k]; |
| 66 | + memcpy(J->x + nnz, Jc->x + Jc->p[k], len * sizeof(double)); |
| 67 | + nnz += len; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +static void wsum_hess_init(expr *node) |
| 72 | +{ |
| 73 | + /* initialize child */ |
| 74 | + expr *x = node->left; |
| 75 | + x->wsum_hess_init(x); |
| 76 | + |
| 77 | + /* same sparsity pattern as child */ |
| 78 | + CSR_Matrix *H = node->wsum_hess; |
| 79 | + H = new_csr_matrix(x->wsum_hess->m, node->n_vars, x->wsum_hess->nnz); |
| 80 | + memcpy(H->p, x->wsum_hess->p, (H->m + 1) * sizeof(int)); |
| 81 | + memcpy(H->i, x->wsum_hess->i, H->nnz * sizeof(int)); |
| 82 | + node->wsum_hess = H; |
| 83 | + |
| 84 | + /* for computing Kw where K is the commutation matrix */ |
| 85 | + node->dwork = (double *) malloc(node->size * sizeof(double)); |
| 86 | +} |
| 87 | +static void eval_wsum_hess(expr *node, const double *w) |
| 88 | +{ |
| 89 | + int d2 = node->d2; |
| 90 | + int d1 = node->d1; |
| 91 | + // TODO: meaybe more efficient to do this with memcpy first |
| 92 | + |
| 93 | + /* evaluate hessian of child at Kw */ |
| 94 | + for (int i = 0; i < d2; ++i) |
| 95 | + { |
| 96 | + for (int j = 0; j < d1; ++j) |
| 97 | + { |
| 98 | + node->dwork[j * d2 + i] = w[i * d1 + j]; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + node->left->eval_wsum_hess(node->left, node->dwork); |
| 103 | + |
| 104 | + /* copy to this node's hessian */ |
| 105 | + memcpy(node->wsum_hess->x, node->left->wsum_hess->x, |
| 106 | + node->wsum_hess->nnz * sizeof(double)); |
| 107 | +} |
| 108 | + |
| 109 | +static bool is_affine(const expr *node) |
| 110 | +{ |
| 111 | + return node->left->is_affine(node->left); |
| 112 | +} |
| 113 | + |
| 114 | +expr *new_transpose(expr *child) |
| 115 | +{ |
| 116 | + expr *node = (expr *) calloc(1, sizeof(expr)); |
| 117 | + init_expr(node, child->d2, child->d1, child->n_vars, forward, jacobian_init, |
| 118 | + eval_jacobian, is_affine, wsum_hess_init, eval_wsum_hess, NULL); |
| 119 | + node->left = child; |
| 120 | + expr_retain(child); |
| 121 | + |
| 122 | + return node; |
| 123 | +} |
0 commit comments