Stan Math Library  2.20.0
reverse mode automatic differentiation
csr_to_dense_matrix.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CSR_TO_DENSE_MATRIX_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CSR_TO_DENSE_MATRIX_HPP
3 
10 #include <vector>
11 
12 namespace stan {
13 namespace math {
14 
35 template <typename T>
36 inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> csr_to_dense_matrix(
37  int m, int n, const Eigen::Matrix<T, Eigen::Dynamic, 1>& w,
38  const std::vector<int>& v, const std::vector<int>& u) {
39  using Eigen::Dynamic;
40  using Eigen::Matrix;
41 
42  check_positive("csr_to_dense_matrix", "m", m);
43  check_positive("csr_to_dense_matrix", "n", n);
44  check_size_match("csr_to_dense_matrix", "m", m, "u", u.size() - 1);
45  check_size_match("csr_to_dense_matrix", "w", w.size(), "v", v.size());
46  check_size_match("csr_to_dense_matrix", "u/z",
47  u[m - 1] + csr_u_to_z(u, m - 1) - 1, "v", v.size());
48  for (int i : v)
49  check_range("csr_to_dense_matrix", "v[]", n, i);
50 
51  Matrix<T, Dynamic, Dynamic> result(m, n);
52  result.setZero();
53  for (int row = 0; row < m; ++row) {
54  int row_end_in_w = (u[row] - stan::error_index::value) + csr_u_to_z(u, row);
55  check_range("csr_to_dense_matrix", "w", w.size(), row_end_in_w);
56  for (int nze = u[row] - stan::error_index::value; nze < row_end_in_w;
57  ++nze) {
58  // row is row index, v[nze] is column index. w[nze] is entry value.
59  check_range("csr_to_dense_matrix", "j", n, v[nze]);
60  result(row, v[nze] - stan::error_index::value) = w(nze);
61  }
62  }
63  return result;
64 } // end of csr_format group
66 
67 } // namespace math
68 } // namespace stan
69 #endif
void check_range(const char *function, const char *name, int max, int index, int nested_level, const char *error_msg)
Check if specified index is within range.
Definition: check_range.hpp:24
int csr_u_to_z(const std::vector< int > &u, int i)
Return the z vector computed from the specified u vector at the index for the z vector.
Definition: csr_u_to_z.hpp:26
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
Eigen::Matrix< T, 1, Eigen::Dynamic > row(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m, size_t i)
Return the specified row of the specified matrix, using start-at-1 indexing.
Definition: row.hpp:24
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > csr_to_dense_matrix(int m, int n, const Eigen::Matrix< T, Eigen::Dynamic, 1 > &w, const std::vector< int > &v, const std::vector< int > &u)
Construct a dense Eigen matrix from the CSR format components.

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