Stan Math Library  2.20.0
reverse mode automatic differentiation
csr_matrix_times_vector.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CSR_MATRIX_TIMES_VECTOR_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CSR_MATRIX_TIMES_VECTOR_HPP
3 
10 #include <boost/math/tools/promotion.hpp>
11 #include <vector>
12 
13 namespace stan {
14 namespace math {
75 template <typename T1, typename T2>
76 inline Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type,
77  Eigen::Dynamic, 1>
79  const Eigen::Matrix<T1, Eigen::Dynamic, 1>& w,
80  const std::vector<int>& v, const std::vector<int>& u,
81  const Eigen::Matrix<T2, Eigen::Dynamic, 1>& b) {
82  typedef typename boost::math::tools::promote_args<T1, T2>::type result_t;
83 
84  check_positive("csr_matrix_times_vector", "m", m);
85  check_positive("csr_matrix_times_vector", "n", n);
86  check_size_match("csr_matrix_times_vector", "n", n, "b", b.size());
87  check_size_match("csr_matrix_times_vector", "m", m, "u", u.size() - 1);
88  check_size_match("csr_matrix_times_vector", "w", w.size(), "v", v.size());
89  check_size_match("csr_matrix_times_vector", "u/z",
90  u[m - 1] + csr_u_to_z(u, m - 1) - 1, "v", v.size());
91  for (int i : v)
92  check_range("csr_matrix_times_vector", "v[]", n, i);
93 
94  Eigen::Matrix<result_t, Eigen::Dynamic, 1> result(m);
95  result.setZero();
96  for (int row = 0; row < m; ++row) {
97  int idx = csr_u_to_z(u, row);
98  int row_end_in_w = (u[row] - stan::error_index::value) + idx;
99  int i = 0;
100  Eigen::Matrix<result_t, Eigen::Dynamic, 1> b_sub(idx);
101  b_sub.setZero();
102  for (int nze = u[row] - stan::error_index::value; nze < row_end_in_w;
103  ++nze, ++i) {
104  check_range("csr_matrix_times_vector", "j", n, v[nze]);
105  b_sub.coeffRef(i) = b.coeffRef(v[nze] - stan::error_index::value);
106  }
107  Eigen::Matrix<T1, Eigen::Dynamic, 1> w_sub(
108  w.segment(u[row] - stan::error_index::value, idx));
109  result.coeffRef(row) = dot_product(w_sub, b_sub);
110  }
111  return result;
112 } // end of csr_format group
114 
115 } // namespace math
116 } // namespace stan
117 #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
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, Eigen::Dynamic, 1 > csr_matrix_times_vector(int m, int n, const Eigen::Matrix< T1, Eigen::Dynamic, 1 > &w, const std::vector< int > &v, const std::vector< int > &u, const Eigen::Matrix< T2, Eigen::Dynamic, 1 > &b)
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
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:14
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.

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