Automatic Differentiation
 
Loading...
Searching...
No Matches
to_soa_sparse_matrix.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_TO_SOA_SPARSE_MATRIX_HPP
2#define STAN_MATH_REV_FUN_TO_SOA_SPARSE_MATRIX_HPP
3
8#include <type_traits>
9
10namespace stan {
11namespace math {
12
28template <int Options = Eigen::ColMajor, typename VarMatrix, typename Vec1,
29 typename Vec2, require_var_t<VarMatrix>* = nullptr,
30 require_eigen_dense_base_t<value_type_t<VarMatrix>>* = nullptr,
31 require_all_std_vector_vt<std::is_integral, Vec1, Vec2>* = nullptr>
32inline auto to_soa_sparse_matrix(int m, int n, VarMatrix&& w, Vec1&& u,
33 Vec2&& v) {
34 auto u_arena = to_arena(std::forward<Vec1>(u));
35 auto v_arena = to_arena(std::forward<Vec2>(v));
36 using sparse_mat_t = Eigen::SparseMatrix<double, Options>;
37 using sparse_arena_mat_t = arena_t<sparse_mat_t>;
38 sparse_arena_mat_t arena_val_x(m, n, w.val().size(), u_arena.data(),
39 v_arena.data(), w.vi_->val_.data());
40 sparse_arena_mat_t arena_adj_x(m, n, w.adj().size(), u_arena.data(),
41 v_arena.data(), w.vi_->adj_.data());
42 var_value<sparse_mat_t> var_x(arena_val_x, arena_adj_x);
43 return var_x;
44}
45
62template <int Options = Eigen::ColMajor, typename MatrixVar, typename Vec1,
63 typename Vec2,
66inline auto to_soa_sparse_matrix(int m, int n, MatrixVar&& w, Vec1&& u,
67 Vec2&& v) {
68 auto w_arena = to_arena(std::forward<MatrixVar>(w));
69 auto u_arena = to_arena(std::forward<Vec1>(u));
70 auto v_arena = to_arena(std::forward<Vec2>(v));
72 m, n, w_arena.size(), u_arena.data(), v_arena.data(), w_arena.data());
74 // No need to copy adj, but need to backprop
75 reverse_pass_callback([arena_x, var_x]() mutable {
76 using var_sparse_iterator_t =
77 typename arena_t<Eigen::SparseMatrix<var, Options>>::InnerIterator;
78 using dbl_sparse_iterator_t =
79 typename arena_t<Eigen::SparseMatrix<double, Options>>::InnerIterator;
80 // arena_x.adj() += var_x.adj() once custom adj() for var sparse matrix
81 for (int k = 0; k < arena_x.outerSize(); ++k) {
82 var_sparse_iterator_t it_arena_x(arena_x, k);
83 dbl_sparse_iterator_t it_var_x(var_x.adj(), k);
84 for (; static_cast<bool>(it_arena_x) && static_cast<bool>(it_var_x);
85 ++it_arena_x, ++it_var_x) {
86 it_arena_x.valueRef().adj() += it_var_x.valueRef();
87 }
88 }
89 });
90 return var_x;
91}
92
109template <int Options = Eigen::ColMajor, typename Mat, typename Vec1,
110 typename Vec2,
113inline auto to_soa_sparse_matrix(int m, int n, Mat&& w, Vec1&& u, Vec2&& v) {
114 auto w_arena = to_arena(std::forward<Mat>(w));
115 auto u_arena = to_arena(std::forward<Vec1>(u));
116 auto v_arena = to_arena(std::forward<Vec2>(v));
118 m, n, w_arena.size(), u_arena.data(), v_arena.data(), w_arena.data());
120}
121
122} // namespace math
123} // namespace stan
124
125#endif
require_t< container_type_check_base< is_eigen_dense_base, value_type_t, TypeCheck, Check... > > require_eigen_dense_base_vt
Require type satisfies is_eigen_dense_base.
require_all_t< container_type_check_base< is_std_vector, value_type_t, TypeCheck, Check >... > require_all_std_vector_vt
Require all of the types satisfy is_std_vector.
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
Definition to_arena.hpp:25
auto to_soa_sparse_matrix(int m, int n, VarMatrix &&w, Vec1 &&u, Vec2 &&v)
Create a sparse matrix from the given SoA matrix and indexes.
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...