Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame] | 1 | #ifndef CS_H |
| 2 | # define CS_H |
| 3 | |
| 4 | # ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | # endif // ifdef __cplusplus |
| 7 | |
| 8 | # include "types.h" // CSC matrix type |
| 9 | # include "lin_alg.h" // Vector copy operations |
| 10 | |
| 11 | /***************************************************************************** |
| 12 | * Create and free CSC Matrices * |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | /** |
| 16 | * Create Compressed-Column-Sparse matrix from existing arrays |
| 17 | (no MALLOC to create inner arrays x, i, p) |
| 18 | * @param m First dimension |
| 19 | * @param n Second dimension |
| 20 | * @param nzmax Maximum number of nonzero elements |
| 21 | * @param x Vector of data |
| 22 | * @param i Vector of row indices |
| 23 | * @param p Vector of column pointers |
| 24 | * @return New matrix pointer |
| 25 | */ |
| 26 | csc* csc_matrix(c_int m, |
| 27 | c_int n, |
| 28 | c_int nzmax, |
| 29 | c_float *x, |
| 30 | c_int *i, |
| 31 | c_int *p); |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Create uninitialized CSC matrix atricture |
| 36 | (uses MALLOC to create inner arrays x, i, p) |
| 37 | * @param m First dimension |
| 38 | * @param n Second dimension |
| 39 | * @param nzmax Maximum number of nonzero elements |
| 40 | * @param values Allocate values (0/1) |
| 41 | * @param triplet Allocate CSC or triplet format matrix (1/0) |
| 42 | * @return Matrix pointer |
| 43 | */ |
| 44 | csc* csc_spalloc(c_int m, |
| 45 | c_int n, |
| 46 | c_int nzmax, |
| 47 | c_int values, |
| 48 | c_int triplet); |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Free sparse matrix |
| 53 | (uses FREE to free inner arrays x, i, p) |
| 54 | * @param A Matrix in CSC format |
| 55 | */ |
| 56 | void csc_spfree(csc *A); |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * free workspace and return a sparse matrix result |
| 61 | * @param C CSC matrix |
| 62 | * @param w Workspace vector |
| 63 | * @param x Workspace vector |
| 64 | * @param ok flag |
| 65 | * @return Return result C if OK, otherwise free it |
| 66 | */ |
| 67 | csc* csc_done(csc *C, |
| 68 | void *w, |
| 69 | void *x, |
| 70 | c_int ok); |
| 71 | |
| 72 | /***************************************************************************** |
| 73 | * Copy Matrices * |
| 74 | *****************************************************************************/ |
| 75 | |
| 76 | /** |
| 77 | * Copy sparse CSC matrix A to output. |
| 78 | * output is allocated by this function (uses MALLOC) |
| 79 | */ |
| 80 | csc* copy_csc_mat(const csc *A); |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * Copy sparse CSC matrix A to B (B is preallocated, NO MALOC) |
| 85 | */ |
| 86 | void prea_copy_csc_mat(const csc *A, |
| 87 | csc *B); |
| 88 | |
| 89 | |
| 90 | /***************************************************************************** |
| 91 | * Matrices Conversion * |
| 92 | *****************************************************************************/ |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * C = compressed-column CSC from matrix T in triplet form |
| 97 | * |
| 98 | * TtoC stores the vector of indices from T to C |
| 99 | * -> C[TtoC[i]] = T[i] |
| 100 | * |
| 101 | * @param T matrix in triplet format |
| 102 | * @param TtoC vector of indices from triplet to CSC format |
| 103 | * @return matrix in CSC format |
| 104 | */ |
| 105 | csc* triplet_to_csc(const csc *T, |
| 106 | c_int *TtoC); |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * C = compressed-row CSR from matrix T in triplet form |
| 111 | * |
| 112 | * TtoC stores the vector of indices from T to C |
| 113 | * -> C[TtoC[i]] = T[i] |
| 114 | * |
| 115 | * @param T matrix in triplet format |
| 116 | * @param TtoC vector of indices from triplet to CSR format |
| 117 | * @return matrix in CSR format |
| 118 | */ |
| 119 | csc* triplet_to_csr(const csc *T, |
| 120 | c_int *TtoC); |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * Convert sparse to dense |
| 125 | */ |
| 126 | c_float* csc_to_dns(csc *M); |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * Convert square CSC matrix into upper triangular one |
| 131 | * |
| 132 | * @param M Matrix to be converted |
| 133 | * @return Upper triangular matrix in CSC format |
| 134 | */ |
| 135 | csc* csc_to_triu(csc *M); |
| 136 | |
| 137 | |
| 138 | /***************************************************************************** |
| 139 | * Extra operations * |
| 140 | *****************************************************************************/ |
| 141 | |
| 142 | /** |
| 143 | * p [0..n] = cumulative sum of c [0..n-1], and then copy p [0..n-1] into c |
| 144 | * |
| 145 | * @param p Create cumulative sum into p |
| 146 | * @param c Vector of which we compute cumulative sum |
| 147 | * @param n Number of elements |
| 148 | * @return Exitflag |
| 149 | */ |
| 150 | c_int csc_cumsum(c_int *p, |
| 151 | c_int *c, |
| 152 | c_int n); |
| 153 | |
| 154 | /** |
| 155 | * Compute inverse of permutation matrix stored in the vector p. |
| 156 | * The computed inverse is also stored in a vector. |
| 157 | */ |
| 158 | c_int* csc_pinv(c_int const *p, |
| 159 | c_int n); |
| 160 | |
| 161 | /** |
| 162 | * C = A(p,p)= PAP' where A and C are symmetric the upper part stored; |
| 163 | * NB: pinv not p! |
| 164 | * @param A Original matrix (upper-triangular) |
| 165 | * @param pinv Inverse of permutation vector |
| 166 | * @param AtoC Mapping from indices of A-x to C->x |
| 167 | * @param values Are values of A allocated? |
| 168 | * @return New matrix (allocated) |
| 169 | */ |
| 170 | csc* csc_symperm(const csc *A, |
| 171 | const c_int *pinv, |
| 172 | c_int *AtoC, |
| 173 | c_int values); |
| 174 | |
| 175 | |
| 176 | # ifdef __cplusplus |
| 177 | } |
| 178 | # endif // ifdef __cplusplus |
| 179 | |
| 180 | #endif // ifndef CS_H |