Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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 // #include <numeric>
7 
8 namespace stan {
9 
10  namespace math {
11 
16  /* Extract the non-zero values from a sparse matrix.
17  *
18  * @tparam T Type of matrix entries.
19  * @param[in] A sparse matrix.
20  * @return Vector of non-zero entries of A.
21  */
22  template <typename T>
23  const Eigen::Matrix<T, Eigen::Dynamic, 1>
24  csr_extract_w(const Eigen::SparseMatrix<T, Eigen::RowMajor>& A) {
25  Eigen::Matrix<T, Eigen::Dynamic, 1> w(A.nonZeros());
26  w.setZero();
27  for (int nze = 0; nze < A.nonZeros(); ++nze)
28  w[nze] = *(A.valuePtr() + nze);
29  return w;
30  }
31 
32  /* Extract the non-zero values from a dense matrix by converting
33  * to sparse and calling the sparse matrix extractor.
34  *
35  * @tparam T Type of matrix entries.
36  * @param[in] A dense matrix.
37  * @return Vector of non-zero entries of A.
38  */
39  template <typename T, int R, int C>
40  const Eigen::Matrix<T, Eigen::Dynamic, 1>
41  csr_extract_w(const Eigen::Matrix<T, R, C>& A) {
42  Eigen::SparseMatrix<T, Eigen::RowMajor> B = A.sparseView();
43  return csr_extract_w(B);
44  }
45  // end of csr_format group
47 
48  }
49 }
50 #endif
const Eigen::Matrix< T, Eigen::Dynamic, 1 > csr_extract_w(const Eigen::SparseMatrix< T, Eigen::RowMajor > &A)

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