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

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