cs.h 4.9 KB

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