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

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/mat/err/check_range.hpp>
5 #include <stan/math/prim/mat/fun/csr_u_to_z.hpp>
6 #include <stan/math/prim/mat/fun/Eigen.hpp>
7 #include <stan/math/prim/mat/fun/dot_product.hpp>
8 #include <stan/math/prim/scal/err/check_size_match.hpp>
9 #include <stan/math/prim/scal/err/check_positive.hpp>
10 #include <vector>
11
12 namespace stan {
13
14 namespace math {
15
33 template <typename T>
34 inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
35 csr_to_dense_matrix(const int& m,
36 const int& n,
37 const Eigen::Matrix<T, Eigen::Dynamic, 1>& w,
38 const std::vector<int>& v,
39 const std::vector<int>& u) {
40 using Eigen::Dynamic;
41 using Eigen::Matrix;
42
43 check_positive("csr_to_dense_matrix", "m", m);
44 check_positive("csr_to_dense_matrix", "n", n);
45 check_size_match("csr_to_dense_matrix", "m", m, "u", u.size()-1);
46 check_size_match("csr_to_dense_matrix", "w", w.size(), "v", v.size());
47 check_size_match("csr_to_dense_matrix", "u/z",
48 u[m-1] + csr_u_to_z(u, m - 1) - 1,
49 "v", v.size());
50 for (size_t i = 0; i < v.size(); ++i)
51 check_range("csr_to_dense_matrix", "v[]", n, v[i]);
52
53 Matrix<T, Dynamic, Dynamic> result(m, n);
54 result.setZero();
55 for (int row = 0; row < m; ++row) {
56 int row_end_in_w = (u[row] - stan::error_index::value)
57 + csr_u_to_z(u, row);
58 check_range("csr_to_dense_matrix", "w", w.size(), row_end_in_w);
59 for (int nze = u[row] - stan::error_index::value;
60 nze < row_end_in_w; ++nze) {
61 // row is row index, v[nze] is column index. w[nze] is entry value.
62 check_range("csr_to_dense_matrix", "j", n, v[nze]);
63 result(row, v[nze] - stan::error_index::value) = w(nze);
64 }
65 }
66 return result;
67 }
68 // end of csr_format group
70
71 }
72 }
73 #endif
check_size_match.hpp
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::csr_u_to_z
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:24
stan::math::csr_to_dense_matrix
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > csr_to_dense_matrix(const int &m, const 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.
Definition: csr_to_dense_matrix.hpp:35
stan::math::check_range
bool check_range(const char *function, const char *name, const int max, const int index, const int nested_level, const char *error_msg)
Return true if specified index is within range.
Definition: check_range.hpp:29
stan::math::row
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:25
stan::error_index::value
Definition: error_index.hpp:7
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:68
stan::math::check_size_match
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
Definition: check_size_match.hpp:30
dot_product.hpp
Eigen.hpp
csr_u_to_z.hpp
check_range.hpp

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