| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/mat/fun/csr_u_to_z.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/prim/mat/fun/dot_product.hpp>
7 #include <stan/math/prim/scal/err/check_size_match.hpp>
8 #include <stan/math/prim/scal/err/check_positive.hpp>
9 #include <stan/math/prim/mat/err/check_range.hpp>
10 #include <boost/math/tools/promotion.hpp>
11 #include <vector>
12
13 namespace stan {
14
15 namespace math {
75 template <typename T1, typename T2>
76 inline
77 Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type,
78 Eigen::Dynamic, 1>
79 csr_matrix_times_vector(const int& m,
80 const int& n,
81 const Eigen::Matrix<T1, Eigen::Dynamic, 1>& w,
82 const std::vector<int>& v,
83 const std::vector<int>& u,
84 const Eigen::Matrix<T2, Eigen::Dynamic, 1>& b) {
85 typedef typename boost::math::tools::promote_args<T1, T2>::type
86 result_t;
87
88 check_positive("csr_matrix_times_vector", "m", m);
89 check_positive("csr_matrix_times_vector", "n", n);
90 check_size_match("csr_matrix_times_vector", "n", n, "b", b.size());
91 check_size_match("csr_matrix_times_vector", "m", m, "u", u.size() - 1);
92 check_size_match("csr_matrix_times_vector", "w", w.size(), "v", v.size());
93 check_size_match("csr_matrix_times_vector", "u/z",
94 u[m - 1] + csr_u_to_z(u, m - 1) - 1, "v", v.size());
95 for (unsigned int i = 0; i < v.size(); ++i)
96 check_range("csr_matrix_times_vector", "v[]", n, v[i]);
97
98 Eigen::Matrix<result_t, Eigen::Dynamic, 1> result(m);
99 result.setZero();
100 for (int row = 0; row < m; ++row) {
101 int idx = csr_u_to_z(u, row);
102 int row_end_in_w = (u[row] - stan::error_index::value) + idx;
103 int i = 0; // index into dot-product segment entries.
104 Eigen::Matrix<result_t, Eigen::Dynamic, 1> b_sub(idx);
105 b_sub.setZero();
106 for (int nze = u[row] - stan::error_index::value;
107 nze < row_end_in_w; ++nze, ++i) {
108 check_range("csr_matrix_times_vector", "j", n, v[nze]);
109 b_sub.coeffRef(i) = b.coeffRef(v[nze] - stan::error_index::value);
110 } // loop skipped when z is zero.
111 Eigen::Matrix<T1, Eigen::Dynamic, 1>
112 w_sub(w.segment(u[row] - stan::error_index::value, idx));
113 result.coeffRef(row) = dot_product(w_sub, b_sub);
114 }
115 return result;
116 }
117 // end of csr_format group
119
120 }
121
122 }
123
124 #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::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
stan::math::dot_product
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:20
dot_product.hpp
Eigen.hpp
csr_u_to_z.hpp
stan::math::csr_matrix_times_vector
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, Eigen::Dynamic, 1 > csr_matrix_times_vector(const int &m, const 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)
Definition: csr_matrix_times_vector.hpp:79
check_range.hpp

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