Automatic Differentiation
 
Loading...
Searching...
No Matches
csr_extract_w.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_CSR_EXTRACT_W_HPP
2#define STAN_MATH_PRIM_FUN_CSR_EXTRACT_W_HPP
3
6
7namespace stan {
8namespace math {
9
14/* Extract the non-zero values from a sparse matrix.
15 *
16 * @tparam T type of elements in the matrix
17 * @param[in] A sparse matrix.
18 * @return Vector of non-zero entries of A.
19 */
20template <typename T>
21const Eigen::Matrix<T, Eigen::Dynamic, 1> csr_extract_w(
22 const Eigen::SparseMatrix<T, Eigen::RowMajor>& A) {
23 auto a_nonzeros = A.nonZeros();
24 Eigen::Matrix<T, Eigen::Dynamic, 1> w
25 = Eigen::Matrix<T, Eigen::Dynamic, 1>::Zero(a_nonzeros);
26 for (int nze = 0; nze < a_nonzeros; ++nze) {
27 w[nze] = *(A.valuePtr() + nze);
28 }
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 elements in the matrix
36 * @tparam R number of rows, can be Eigen::Dynamic
37 * @tparam C number of columns, can be Eigen::Dynamic
38 *
39 * @param[in] A dense matrix.
40 * @return Vector of non-zero entries of A.
41 */
42template <typename T, require_eigen_dense_base_t<T>* = nullptr>
43const Eigen::Matrix<scalar_type_t<T>, Eigen::Dynamic, 1> csr_extract_w(
44 const T& A) {
45 // conversion to sparse seems to touch data twice, so we need to call to_ref
46 Eigen::SparseMatrix<scalar_type_t<T>, Eigen::RowMajor> B
47 = to_ref(A).sparseView();
48 return csr_extract_w(B);
49}
50 // end of csr_format group
52
53} // namespace math
54} // namespace stan
55
56#endif
const Eigen::Matrix< T, Eigen::Dynamic, 1 > csr_extract_w(const Eigen::SparseMatrix< T, Eigen::RowMajor > &A)
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...