Automatic Differentiation
 
Loading...
Searching...
No Matches
csr_matrix_times_vector.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_CSR_MATRIX_TIMES_VECTOR_HPP
2#define STAN_MATH_PRIM_FUN_CSR_MATRIX_TIMES_VECTOR_HPP
3
8#include <vector>
9
10namespace stan {
11namespace math {
12
72template <typename T1, typename T2,
73 require_all_not_rev_matrix_t<T1, T2>* = nullptr>
74inline Eigen::Matrix<return_type_t<T1, T2>, Eigen::Dynamic, 1>
75csr_matrix_times_vector(int m, int n, const T1& w, const std::vector<int>& v,
76 const std::vector<int>& u, const T2& b) {
77 check_positive("csr_matrix_times_vector", "m", m);
78 check_positive("csr_matrix_times_vector", "n", n);
79 check_size_match("csr_matrix_times_vector", "n", n, "b", b.size());
80 check_size_match("csr_matrix_times_vector", "w", w.size(), "v", v.size());
81 check_size_match("csr_matrix_times_vector", "m", m, "u", u.size() - 1);
82 check_size_match("csr_matrix_times_vector", "u/z",
83 u[m - 1] + csr_u_to_z(u, m - 1) - 1, "v", v.size());
84 for (int i : v) {
85 check_range("csr_matrix_times_vector", "v[]", n, i);
86 }
87 std::vector<int> v_zero_based(v.size());
88 std::transform(v.begin(), v.end(), v_zero_based.begin(),
89 [](auto&& x) { return x - 1; });
90 std::vector<int> u_zero_based(u.size());
91 std::transform(u.begin(), u.end(), u_zero_based.begin(),
92 [](auto&& x) { return x - 1; });
93 // u_zero_based[u.size()] = w.size();
94 auto&& w_ref = to_ref(w);
95 Eigen::Map<const Eigen::SparseMatrix<scalar_type_t<T1>, Eigen::RowMajor>>
96 w_mat(m, n, w_ref.size(), u_zero_based.data(), v_zero_based.data(),
97 w_ref.data());
98 return w_mat * b;
99}
100
101} // namespace math
102} // namespace stan
103
104#endif
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.
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.
Eigen::Matrix< return_type_t< T1, T2 >, Eigen::Dynamic, 1 > csr_matrix_times_vector(int m, int n, const T1 &w, const std::vector< int > &v, const std::vector< int > &u, const T2 &b)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...