1#ifndef STAN_MATH_REV_CORE_ARENA_MATRIX_HPP
2#define STAN_MATH_REV_CORE_ARENA_MATRIX_HPP
13template <
typename MatrixType>
15 :
public Eigen::Map<MatrixType> {
18 using Base = Eigen::Map<MatrixType>;
20 static constexpr int RowsAtCompileTime = MatrixType::RowsAtCompileTime;
21 static constexpr int ColsAtCompileTime = MatrixType::ColsAtCompileTime;
28 RowsAtCompileTime ==
Eigen::Dynamic ? 0 : RowsAtCompileTime,
29 ColsAtCompileTime ==
Eigen::Dynamic ? 0 : ColsAtCompileTime) {
55 return (RowsAtCompileTime == 1 && T::ColsAtCompileTime == 1)
56 || (ColsAtCompileTime == 1 && T::RowsAtCompileTime == 1)
62 return (RowsAtCompileTime == 1 && T::ColsAtCompileTime == 1)
63 || (ColsAtCompileTime == 1 && T::RowsAtCompileTime == 1)
73 template <
typename T, require_eigen_t<T>* =
nullptr>
77 get_rows(other), get_cols(other)) {
86 template <
typename T, require_eigen_t<T>* =
nullptr>
90 get_rows(other), get_cols(other));
91 Base::operator=(other);
104 template <
typename T, require_eigen_t<T>* =
nullptr,
105 require_not_arena_matrix_t<T>* =
nullptr,
106 require_t<std::is_rvalue_reference<T&&>>* =
nullptr,
107 require_plain_type_t<T>* =
nullptr,
108 require_same_t<T, MatrixType>* =
nullptr>
110 :
Base::Map([](auto&& x) {
115 return base_map_t(&(other_ptr->coeffRef(0)), other_ptr->rows(),
117 }(std::move(other))) {}
126 template <
typename T, require_eigen_t<T>* =
nullptr,
127 require_not_arena_matrix_t<T>* =
nullptr,
128 require_t<std::is_rvalue_reference<T&&>>* =
nullptr,
129 require_plain_type_t<T>* =
nullptr,
130 require_same_t<T, MatrixType>* =
nullptr>
134 Base(&(other_ptr->coeffRef(0)), other_ptr->rows(), other_ptr->cols());
144 :
Base::Map(other) {}
156 using Base::operator=;
166 Base(
const_cast<Scalar*
>(other.data()), other.rows(), other.cols());
175 template <
typename T>
181template <
typename MatrixType>
183 :
public Eigen::Map<MatrixType> {
186 using Base = Eigen::Map<MatrixType>;
217 :
Base::Map(
rows,
cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr,
221 template <
typename T,
typename Integral>
226 std::copy_n(ptr,
size, ret);
235 template <
typename T, require_same_t<T, PlainObject>* =
nullptr>
238 other.
rows(), other.
cols(), other.nonZeros(),
239 copy_vector(other.outerIndexPtr(), other.outerSize() + 1),
240 copy_vector(other.innerIndexPtr(), other.nonZeros()),
241 copy_vector(other.valuePtr(), other.nonZeros()),
243 other.innerNonZeroPtr(),
244 other.innerNonZeroPtr() == nullptr ? 0 : other.innerSize())) {}
250 template <
typename S, require_convertible_t<S&, PlainObject>* =
nullptr,
251 require_not_same_t<S, PlainObject>* =
nullptr>
270 :
Base::Map(other.
rows(), other.
cols(), other.nonZeros(),
273 const_cast<
Scalar*>(other.valuePtr()),
282 :
Base::Map(other.
rows(), other.
cols(), other.nonZeros(),
285 const_cast<
Scalar*>(other.valuePtr()),
294 :
Base::Map(other.
rows(), other.
cols(), other.nonZeros(),
297 const_cast<
Scalar*>(other.valuePtr()),
302 using Base::operator=;
310 template <
typename ArenaMatrix,
315 new (
this)
Base(other.rows(), other.cols(), other.nonZeros(),
318 const_cast<Scalar*
>(other.valuePtr()),
330 template <
typename Expr,
348 template <
typename F,
typename Expr,
353 auto* val_ptr = (*this).valuePtr();
354 auto* x_val_ptr = x.valuePtr();
355 const auto non_zeros = (*this).nonZeros();
356 for (Eigen::Index i = 0; i < non_zeros; ++i) {
357 f(val_ptr[i], x_val_ptr[i]);
371 template <
typename F,
typename Expr,
376 for (
int k = 0; k < (*this).outerSize(); ++k) {
377 typename Base::InnerIterator it(*
this, k);
378 typename std::decay_t<Expr>::InnerIterator iz(x, k);
379 for (;
static_cast<bool>(it) &&
static_cast<bool>(iz); ++it, ++iz) {
380 f(it.valueRef(), iz.value());
395 template <
typename F,
typename Expr,
400 for (
int k = 0; k < (*this).outerSize(); ++k) {
401 typename Base::InnerIterator it(*
this, k);
402 for (;
static_cast<bool>(it); ++it) {
403 f(it.valueRef(), x(it.row(), it.col()));
418 template <
typename F,
typename T,
421 auto* val_ptr = (*this).valuePtr();
422 const auto non_zeros = (*this).nonZeros();
423 for (Eigen::Index i = 0; i < non_zeros; ++i) {
424 f(val_ptr[i], other);
441 template <
typename T>
444 [](
auto&& x,
auto&& y) {
448 std::forward<T>(other));
464 template <
typename T>
467 [](
auto&& x,
auto&& y) {
471 std::forward<T>(other));
483struct traits<
stan::math::arena_matrix<T>> {
484 using base = traits<Eigen::Map<T>>;
485 using XprKind =
typename Eigen::internal::traits<std::decay_t<T>>::XprKind;
487 PlainObjectTypeInnerSize = base::PlainObjectTypeInnerSize,
488 InnerStrideAtCompileTime = base::InnerStrideAtCompileTime,
489 OuterStrideAtCompileTime = base::OuterStrideAtCompileTime,
490 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 &other)
Overwrite the current arena_matrix with new memory and assign a matrix to it.
arena_matrix()
Default constructor.
arena_matrix(Eigen::Index rows, Eigen::Index cols)
Constructs arena_matrix with given number of rows and columns.
constexpr auto get_cols(const T &x)
value_type_t< MatrixType > Scalar
constexpr auto get_rows(const T &x)
arena_matrix(const T &other)
Constructs arena_matrix from an expression.
arena_matrix(T &&other)
Constructs arena_matrix from an rvalue expression that is a plain_type, then movies it to the object ...
arena_matrix & operator=(T &&other)
Assignment operator for assigning 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.
int64_t cols(const T_x &x)
Returns the number of columns in the specified kernel generator expression.
int64_t rows(const T_x &x)
Returns the number of rows 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.
int64_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.
auto make_chainable_ptr(T &&obj)
Store the given object in a chainable_object so it is destructed only when the chainable stack memory...
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.