Stan Math Library: stan/math/prim/mat/fun/csr_extract_w.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
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
stan::math::csr_extract_w
const Eigen::Matrix< T, Eigen::Dynamic, 1 > csr_extract_w(const Eigen::SparseMatrix< T, Eigen::RowMajor > &A)
Definition: csr_extract_w.hpp:24
stan
Definition: log_sum_exp.hpp:8
Eigen.hpp

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