1#ifndef STAN_MATH_REV_CORE_ARENA_MATRIX_HPP
2#define STAN_MATH_REV_CORE_ARENA_MATRIX_HPP
12template <
typename MatrixType>
14 :
public Eigen::Map<MatrixType> {
17 using Base = Eigen::Map<MatrixType>;
19 static constexpr int RowsAtCompileTime = MatrixType::RowsAtCompileTime;
20 static constexpr int ColsAtCompileTime = MatrixType::ColsAtCompileTime;
27 RowsAtCompileTime ==
Eigen::Dynamic ? 0 : RowsAtCompileTime,
28 ColsAtCompileTime ==
Eigen::Dynamic ? 0 : ColsAtCompileTime) {
55 template <
typename T, require_eigen_t<T>* =
nullptr>
60 (RowsAtCompileTime == 1 && T::ColsAtCompileTime == 1)
61 || (ColsAtCompileTime == 1 && T::RowsAtCompileTime == 1)
64 (RowsAtCompileTime == 1 && T::ColsAtCompileTime == 1)
65 || (ColsAtCompileTime == 1 && T::RowsAtCompileTime == 1)
89 using Base::operator=;
99 Base(
const_cast<Scalar*
>(other.data()), other.rows(), other.cols());
108 template <
typename T>
111 if ((RowsAtCompileTime == 1 && T::ColsAtCompileTime == 1)
112 || (ColsAtCompileTime == 1 && T::RowsAtCompileTime == 1)) {
131 template <
typename T>
137template <
typename MatrixType>
139 :
public Eigen::Map<MatrixType> {
142 using Base = Eigen::Map<MatrixType>;
173 :
Base::Map(
rows,
cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr,
177 template <
typename T,
typename Integral>
182 std::copy_n(ptr,
size, ret);
191 template <
typename T, require_same_t<T, PlainObject>* =
nullptr>
194 other.
rows(), other.
cols(), other.nonZeros(),
195 copy_vector(other.outerIndexPtr(), other.outerSize() + 1),
196 copy_vector(other.innerIndexPtr(), other.nonZeros()),
197 copy_vector(other.valuePtr(), other.nonZeros()),
199 other.innerNonZeroPtr(),
200 other.innerNonZeroPtr() == nullptr ? 0 : other.innerSize())) {}
206 template <
typename S, require_convertible_t<S&, PlainObject>* =
nullptr,
207 require_not_same_t<S, PlainObject>* =
nullptr>
226 :
Base::Map(other.
rows(), other.
cols(), other.nonZeros(),
229 const_cast<
Scalar*>(other.valuePtr()),
238 :
Base::Map(other.
rows(), other.
cols(), other.nonZeros(),
241 const_cast<
Scalar*>(other.valuePtr()),
250 :
Base::Map(other.
rows(), other.
cols(), other.nonZeros(),
253 const_cast<
Scalar*>(other.valuePtr()),
258 using Base::operator=;
266 template <
typename ArenaMatrix,
271 new (
this)
Base(other.rows(), other.cols(), other.nonZeros(),
274 const_cast<Scalar*
>(other.valuePtr()),
286 template <
typename Expr,
304 template <
typename F,
typename Expr,
309 auto* val_ptr = (*this).valuePtr();
310 auto* x_val_ptr = x.valuePtr();
311 const auto non_zeros = (*this).nonZeros();
312 for (Eigen::Index i = 0; i < non_zeros; ++i) {
313 f(val_ptr[i], x_val_ptr[i]);
327 template <
typename F,
typename Expr,
332 for (
int k = 0; k < (*this).outerSize(); ++k) {
333 typename Base::InnerIterator it(*
this, k);
334 typename std::decay_t<Expr>::InnerIterator iz(x, k);
335 for (;
static_cast<bool>(it) &&
static_cast<bool>(iz); ++it, ++iz) {
336 f(it.valueRef(), iz.value());
351 template <
typename F,
typename Expr,
356 for (
int k = 0; k < (*this).outerSize(); ++k) {
357 typename Base::InnerIterator it(*
this, k);
358 for (;
static_cast<bool>(it); ++it) {
359 f(it.valueRef(), x(it.row(), it.col()));
374 template <
typename F,
typename T,
377 auto* val_ptr = (*this).valuePtr();
378 const auto non_zeros = (*this).nonZeros();
379 for (Eigen::Index i = 0; i < non_zeros; ++i) {
380 f(val_ptr[i], other);
397 template <
typename T>
400 [](
auto&& x,
auto&& y) {
404 std::forward<T>(other));
420 template <
typename T>
423 [](
auto&& x,
auto&& y) {
427 std::forward<T>(other));
439struct traits<
stan::math::arena_matrix<T>> {
440 using base = traits<Eigen::Map<T>>;
441 using XprKind =
typename Eigen::internal::traits<std::decay_t<T>>::XprKind;
443 PlainObjectTypeInnerSize = base::PlainObjectTypeInnerSize,
444 InnerStrideAtCompileTime = base::InnerStrideAtCompileTime,
445 OuterStrideAtCompileTime = base::OuterStrideAtCompileTime,
446 Alignment = base::Alignment,
arena_matrix(const arena_matrix< MatrixType > &other)
Copy constructor.
arena_matrix(Eigen::Index size)
Constructs arena_matrix with given size.
arena_matrix(const Base &other)
Constructs arena_matrix from an expression.
Eigen::Map< MatrixType > Base
arena_matrix & operator=(const T &a)
Assignment operator for assigning an expression.
arena_matrix()
Default constructor.
arena_matrix(Eigen::Index rows, Eigen::Index cols)
Constructs arena_matrix with given number of rows and columns.
value_type_t< MatrixType > Scalar
arena_matrix(const T &other)
Constructs arena_matrix from an expression.
void deep_copy(const T &x)
Forces hard copying matrices into an arena matrix.
arena_matrix & operator=(const arena_matrix< MatrixType > &other)
Copy assignment operator.
std::decay_t< MatrixType > PlainObject
void inplace_ops_impl(F &&f, Expr &&other)
inplace operations functor for Sparse (.
arena_matrix(arena_matrix< MatrixType > &&other)
Move constructor.
arena_matrix(Eigen::Index rows, Eigen::Index cols, Eigen::Index nnz, StorageIndex *outerIndexPtr, StorageIndex *innerIndexPtr, Scalar *valuePtr, StorageIndex *innerNonZerosPtr=nullptr)
Constructor for CDC formatted data.
void inplace_ops_impl(F &&f, T &&other)
inplace operations functor for Sparse (.
T * copy_vector(const T *ptr, Integral size)
arena_matrix(arena_matrix< MatrixType > &other)
Copy constructor.
arena_matrix()
Default constructor.
arena_matrix & operator=(ArenaMatrix &&other)
Copy assignment operator.
arena_matrix & operator-=(T &&other)
Inplace operator +=
arena_matrix & operator=(Expr &&expr)
Assignment operator for assigning an expression.
Eigen::Map< MatrixType > Base
arena_matrix(const arena_matrix< MatrixType > &other)
Const copy constructor.
value_type_t< MatrixType > Scalar
typename PlainObject::StorageIndex StorageIndex
arena_matrix(S &&other)
Constructs arena_matrix from an Eigen expression.
arena_matrix & operator+=(T &&other)
Inplace operator +=
arena_matrix(const Base &other)
Constructs arena_matrix from an expression.
std::decay_t< MatrixType > PlainObject
arena_matrix(T &&other)
Constructs arena_matrix from an Eigen::SparseMatrix.
Equivalent to Eigen::Matrix, except that the data is stored on AD stack.
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
require_not_t< std::is_convertible< std::decay_t< T >, std::decay_t< S > > > require_not_convertible_t
Require types T and S does not satisfy std::is_convertible.
require_t< std::is_convertible< std::decay_t< T >, std::decay_t< S > > > require_convertible_t
Require types T and S satisfies std::is_convertible.
require_t< is_eigen_dense_base< std::decay_t< T > > > require_eigen_dense_base_t
Require type satisfies is_eigen_dense_base.
require_t< is_eigen_sparse_base< std::decay_t< T > > > require_eigen_sparse_base_t
Require type satisfies is_eigen_sparse_base.
int rows(const T_x &x)
Returns the number of rows in the specified kernel generator expression.
int cols(const T_x &x)
Returns the number of columns in the specified kernel generator expression.
require_not_t< std::is_same< std::decay_t< T >, std::decay_t< S > > > require_not_same_t
Require types T and S does not satisfy std::is_same.
require_t< std::is_same< std::decay_t< T >, std::decay_t< S > > > require_same_t
Require types T and S satisfies std::is_same.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
size_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
(Expert) Numerical traits for algorithmic differentiation variables.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
traits< Eigen::Map< T > > base
typename Eigen::internal::traits< std::decay_t< T > >::XprKind XprKind
static thread_local AutodiffStackStorage * instance_
This struct always provides access to the autodiff stack using the singleton pattern.