Automatic Differentiation
 
Loading...
Searching...
No Matches
csr_to_dense_matrix.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_CSR_TO_DENSE_MATRIX_HPP
2#define STAN_MATH_PRIM_FUN_CSR_TO_DENSE_MATRIX_HPP
3
8#include <vector>
9
10namespace stan {
11namespace math {
12
33template <typename T>
34inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>
35csr_to_dense_matrix(int m, int n, const T& w, const std::vector<int>& v,
36 const std::vector<int>& u) {
37 using Eigen::Dynamic;
38 using Eigen::Matrix;
39
40 check_positive("csr_to_dense_matrix", "m", m);
41 check_positive("csr_to_dense_matrix", "n", n);
42 check_size_match("csr_to_dense_matrix", "m", m, "u", u.size() - 1);
43 check_size_match("csr_to_dense_matrix", "w", w.size(), "v", v.size());
44 check_size_match("csr_to_dense_matrix", "u/z",
45 u[m - 1] + csr_u_to_z(u, m - 1) - 1, "v", v.size());
46 for (int i : v) {
47 check_range("csr_to_dense_matrix", "v[]", n, i);
48 }
49 const auto& w_ref = to_ref(w);
50 Matrix<value_type_t<T>, Dynamic, Dynamic> result(m, n);
51 result.setZero();
52 for (int row = 0; row < m; ++row) {
53 int row_nnz = csr_u_to_z(u, row);
54
55 if (row_nnz > 0) {
56 int row_end_in_w = (u[row] - stan::error_index::value) + row_nnz;
57 check_range("csr_to_dense_matrix", "w", w.size(), row_end_in_w);
58 for (int nze = u[row] - stan::error_index::value; nze < row_end_in_w;
59 ++nze) {
60 // row is row index, v[nze] is column index. w[nze] is entry value.
61 check_range("csr_to_dense_matrix", "j", n, v[nze]);
62 result(row, v[nze] - stan::error_index::value) = w_ref.coeff(nze);
63 }
64 }
65 }
66 return result;
67} // end of csr_format group
69
70} // namespace math
71} // namespace stan
72
73#endif
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, Eigen::Dynamic > csr_to_dense_matrix(int m, int n, const T &w, const std::vector< int > &v, const std::vector< int > &u)
Construct a dense Eigen matrix from the CSR format components.
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.
auto row(T_x &&x, size_t j)
Return the specified row of the specified kernel generator expression using start-at-1 indexing.
Definition row.hpp:23
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
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.
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...