Stan Math Library  2.15.0
reverse mode automatic differentiation
csr_extract_w.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CSR_EXTRACT_W_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CSR_EXTRACT_W_HPP
3 
5 #include <Eigen/Sparse>
6 
7 namespace stan {
8  namespace math {
9 
14  /* Extract the non-zero values from a sparse matrix.
15  *
16  * @tparam T Type of matrix entries.
17  * @param[in] A sparse matrix.
18  * @return Vector of non-zero entries of A.
19  */
20  template <typename T>
21  const Eigen::Matrix<T, Eigen::Dynamic, 1>
22  csr_extract_w(const Eigen::SparseMatrix<T, Eigen::RowMajor>& A) {
23  Eigen::Matrix<T, Eigen::Dynamic, 1> w(A.nonZeros());
24  w.setZero();
25  for (int nze = 0; nze < A.nonZeros(); ++nze)
26  w[nze] = *(A.valuePtr() + nze);
27  return w;
28  }
29 
30  /* Extract the non-zero values from a dense matrix by converting
31  * to sparse and calling the sparse matrix extractor.
32  *
33  * @tparam T Type of matrix entries.
34  * @param[in] A dense matrix.
35  * @return Vector of non-zero entries of A.
36  */
37  template <typename T, int R, int C>
38  const Eigen::Matrix<T, Eigen::Dynamic, 1>
39  csr_extract_w(const Eigen::Matrix<T, R, C>& A) {
40  Eigen::SparseMatrix<T, Eigen::RowMajor> B = A.sparseView();
41  return csr_extract_w(B);
42  }
43  // end of csr_format group
45 
46  }
47 }
48 #endif
const Eigen::Matrix< T, Eigen::Dynamic, 1 > csr_extract_w(const Eigen::SparseMatrix< T, Eigen::RowMajor > &A)

     [ Stan Home Page ] © 2011–2016, Stan Development Team.