Automatic Differentiation
 
Loading...
Searching...
No Matches
var.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CORE_VAR_HPP
2#define STAN_MATH_REV_CORE_VAR_HPP
3
4#ifdef STAN_OPENCL
7#endif
15#include <ostream>
16#include <vector>
17
18namespace stan {
19namespace math {
20
21// forward declare
22template <typename Vari>
23static void grad(Vari* vi);
24
39template <typename T>
41 public:
42 using value_type = std::decay_t<T>; // type in vari_value.
44
53
63 inline bool is_uninitialized() { return (vi_ == nullptr); }
64
72 var_value() : vi_(nullptr) {}
73
81 template <typename S, require_convertible_t<S&, value_type>* = nullptr>
82 var_value(S x) : vi_(new vari_type(x, false)) {} // NOLINT
83
88 var_value(vari_type* vi) : vi_(vi) {} // NOLINT
89
95 inline const auto& val() const noexcept { return vi_->val(); }
96
105 inline auto& adj() const noexcept { return vi_->adj(); }
106
115 inline auto& adj() noexcept { return vi_->adj_; }
116
132 inline void grad(std::vector<var_value<T>>& x, std::vector<value_type>& g) {
133 stan::math::grad(vi_);
134 g.resize(x.size());
135 for (size_t i = 0; i < x.size(); ++i) {
136 g[i] = x[i].vi_->adj_;
137 }
138 }
139
149 void grad() { stan::math::grad(vi_); }
150
151 // POINTER OVERRIDES
152
165 inline vari_type& operator*() { return *vi_; }
166
177 inline vari_type* operator->() { return vi_; }
178
179 // COMPOUND ASSIGNMENT OPERATORS
180
191 inline var_value<T>& operator+=(const var_value<T>& b);
192
203 inline var_value<T>& operator+=(T b);
204
216 inline var_value<T>& operator-=(const var_value<T>& b);
217
229 inline var_value<T>& operator-=(T b);
230
242 inline var_value<T>& operator*=(const var_value<T>& b);
243
255 inline var_value<T>& operator*=(T b);
256
267 inline var_value<T>& operator/=(const var_value<T>& b);
268
280 inline var_value<T>& operator/=(T b);
281
290 friend std::ostream& operator<<(std::ostream& os, const var_value<T>& v) {
291 if (v.vi_ == nullptr) {
292 return os << "uninitialized";
293 }
294 return os << v.val();
295 }
296};
297
298namespace internal {
299template <typename T>
302 && std::is_floating_point<value_type_t<T>>::value>>;
303}
304
319template <typename T>
320class var_value<T, internal::require_matrix_var_value<T>> {
321 public:
322 using value_type = T; // type in vari_value.
323 using vari_type = std::conditional_t<is_plain_type<value_type>::value,
325
326 static constexpr int RowsAtCompileTime{vari_type::RowsAtCompileTime};
327 static constexpr int ColsAtCompileTime{vari_type::ColsAtCompileTime};
328
337
347 inline bool is_uninitialized() noexcept { return (vi_ == nullptr); }
348
356 var_value() : vi_(nullptr) {}
357
365 template <typename S, require_assignable_t<value_type, S>* = nullptr>
366 var_value(S&& x) : vi_(new vari_type(std::forward<S>(x), false)) {} // NOLINT
367
375 template <typename S,
376 require_assignable_t<vari_type,
377 typename var_value<S>::vari_type>* = nullptr,
379 var_value(const var_value<S>& other) : vi_(other.vi_) {}
380
392 template <typename S,
394 vari_type, typename var_value<S>::vari_type>* = nullptr,
397 var_value(const var_value<S>& other) : vi_(new vari_type(other.vi_->val_)) {
398 reverse_pass_callback([this_vi = this->vi_, other_vi = other.vi_]() {
399 other_vi->adj_ += this_vi->adj_;
400 });
401 }
402
410 template <typename S, typename T_ = T,
413 require_plain_type_t<T_>* = nullptr>
414 var_value(const var_value<S>& other) : vi_(new vari_type(other.vi_->val_)) {
416 [this_vi = this->vi_, other_vi = other.vi_]() mutable {
417 other_vi->adj_ += this_vi->adj_;
418 });
419 }
420
428 template <typename S, typename T_ = T,
430 require_arena_matrix_t<S>* = nullptr>
431 var_value(const S& val, const S& adj) : vi_(new vari_type(val, adj)) {}
432
437 var_value(vari_type* vi) : vi_(vi) {} // NOLINT
438
444 inline const auto& val() const noexcept { return vi_->val(); }
445 inline auto& val_op() noexcept { return vi_->val_op(); }
446
455 inline auto& adj() noexcept { return vi_->adj(); }
456 inline auto& adj() const noexcept { return vi_->adj(); }
457 inline auto& adj_op() noexcept { return vi_->adj(); }
458
459 inline Eigen::Index rows() const noexcept { return vi_->rows(); }
460 inline Eigen::Index cols() const noexcept { return vi_->cols(); }
461 inline Eigen::Index size() const noexcept { return vi_->size(); }
462
463 // POINTER OVERRIDES
464
477 inline vari_type& operator*() { return *vi_; }
478
489 inline vari_type* operator->() { return vi_; }
490
491 // COMPOUND ASSIGNMENT OPERATORS
492
504
516
528 template <typename S, require_st_var<S>* = nullptr>
529 inline var_value<T>& operator-=(const S& b);
530
542 template <typename S, require_st_arithmetic<S>* = nullptr>
543 inline var_value<T>& operator-=(const S& b);
544
557
570
581 inline var_value<T>& operator/=(const var_value<T>& b);
582
594 inline var_value<T>& operator/=(T b);
595
603 inline auto block(Eigen::Index start_row, Eigen::Index start_col,
604 Eigen::Index num_rows, Eigen::Index num_cols) const {
605 using vari_sub
606 = decltype(vi_->block(start_row, start_col, num_rows, num_cols));
607 using var_sub = var_value<value_type_t<vari_sub>>;
608 return var_sub(
609 new vari_sub(vi_->block(start_row, start_col, num_rows, num_cols)));
610 }
611 inline auto block(Eigen::Index start_row, Eigen::Index start_col,
612 Eigen::Index num_rows, Eigen::Index num_cols) {
613 using vari_sub
614 = decltype(vi_->block(start_row, start_col, num_rows, num_cols));
615 using var_sub = var_value<value_type_t<vari_sub>>;
616 return var_sub(
617 new vari_sub(vi_->block(start_row, start_col, num_rows, num_cols)));
618 }
619
623 inline auto transpose() const {
624 using vari_sub = decltype(vi_->transpose());
625 using var_sub = var_value<value_type_t<vari_sub>>;
626 return var_sub(new vari_sub(vi_->transpose()));
627 }
628 inline auto transpose() {
629 using vari_sub = decltype(vi_->transpose());
630 using var_sub = var_value<value_type_t<vari_sub>>;
631 return var_sub(new vari_sub(vi_->transpose()));
632 }
633
638 inline auto head(Eigen::Index n) const {
639 using vari_sub = decltype(vi_->head(n));
640 using var_sub = var_value<value_type_t<vari_sub>>;
641 return var_sub(new vari_sub(vi_->head(n)));
642 }
643 inline auto head(Eigen::Index n) {
644 using vari_sub = decltype(vi_->head(n));
645 using var_sub = var_value<value_type_t<vari_sub>>;
646 return var_sub(new vari_sub(vi_->head(n)));
647 }
648
653 inline auto tail(Eigen::Index n) const {
654 using vari_sub = decltype(vi_->tail(n));
655 using var_sub = var_value<value_type_t<vari_sub>>;
656 return var_sub(new vari_sub(vi_->tail(n)));
657 }
658 inline auto tail(Eigen::Index n) {
659 using vari_sub = decltype(vi_->tail(n));
660 using var_sub = var_value<value_type_t<vari_sub>>;
661 return var_sub(new vari_sub(vi_->tail(n)));
662 }
663
669 inline auto segment(Eigen::Index i, Eigen::Index n) const {
670 using vari_sub = decltype(vi_->segment(i, n));
671 using var_sub = var_value<value_type_t<vari_sub>>;
672 return var_sub(new vari_sub(vi_->segment(i, n)));
673 }
674 inline auto segment(Eigen::Index i, Eigen::Index n) {
675 using vari_sub = decltype(vi_->segment(i, n));
676 using var_sub = var_value<value_type_t<vari_sub>>;
677 return var_sub(new vari_sub(vi_->segment(i, n)));
678 }
679
684 inline auto row(Eigen::Index i) const {
685 using vari_sub = decltype(vi_->row(i));
686 using var_sub = var_value<value_type_t<vari_sub>>;
687 return var_sub(new vari_sub(vi_->row(i)));
688 }
689 inline auto row(Eigen::Index i) {
690 using vari_sub = decltype(vi_->row(i));
691 using var_sub = var_value<value_type_t<vari_sub>>;
692 return var_sub(new vari_sub(vi_->row(i)));
693 }
694
699 inline auto col(Eigen::Index i) const {
700 using vari_sub = decltype(vi_->col(i));
701 using var_sub = var_value<value_type_t<vari_sub>>;
702 return var_sub(new vari_sub(vi_->col(i)));
703 }
704 inline auto col(Eigen::Index i) {
705 using vari_sub = decltype(vi_->col(i));
706 using var_sub = var_value<value_type_t<vari_sub>>;
707 return var_sub(new vari_sub(vi_->col(i)));
708 }
709
714 inline auto diagonal() const {
715 using vari_sub = decltype(vi_->diagonal());
716 using var_sub = var_value<value_type_t<vari_sub>>;
717 return var_sub(new vari_sub(vi_->diagonal()));
718 }
719 inline auto diagonal() {
720 using vari_sub = decltype(vi_->diagonal());
721 using var_sub = var_value<value_type_t<vari_sub>>;
722 return var_sub(new vari_sub(vi_->diagonal()));
723 }
724
728 inline auto as_column_vector_or_scalar() const {
729 using vari_sub = decltype(vi_->as_column_vector_or_scalar());
730 using var_sub = var_value<value_type_t<vari_sub>>;
731 return var_sub(new vari_sub(vi_->as_column_vector_or_scalar()));
732 }
734 using vari_sub = decltype(vi_->as_column_vector_or_scalar());
735 using var_sub = var_value<value_type_t<vari_sub>>;
736 return var_sub(new vari_sub(vi_->as_column_vector_or_scalar()));
737 }
738
746 inline auto coeff(Eigen::Index i) const {
747 using vari_sub = decltype(vi_->coeff(i));
748 vari_sub* vari_coeff = new vari_sub(vi_->coeff(i));
749 reverse_pass_callback([this_vi = this->vi_, vari_coeff, i]() {
750 this_vi->adj_.coeffRef(i) += vari_coeff->adj_;
751 });
752 return var_value<value_type_t<vari_sub>>(vari_coeff);
753 }
754 inline auto coeff(Eigen::Index i) {
755 using vari_sub = decltype(vi_->coeff(i));
756 vari_sub* vari_coeff = new vari_sub(vi_->coeff(i));
757 reverse_pass_callback([this_vi = this->vi_, vari_coeff, i]() {
758 this_vi->adj_.coeffRef(i) += vari_coeff->adj_;
759 });
760 return var_value<value_type_t<vari_sub>>(vari_coeff);
761 }
762
771 inline auto coeff(Eigen::Index i, Eigen::Index j) const {
772 using vari_sub = decltype(vi_->coeff(i, j));
773 vari_sub* vari_coeff = new vari_sub(vi_->coeff(i, j));
774 reverse_pass_callback([this_vi = this->vi_, vari_coeff, i, j]() {
775 this_vi->adj_.coeffRef(i, j) += vari_coeff->adj_;
776 });
777 return var_value<value_type_t<vari_sub>>(vari_coeff);
778 }
779 inline auto coeff(Eigen::Index i, Eigen::Index j) {
780 using vari_sub = decltype(vi_->coeff(i, j));
781 vari_sub* vari_coeff = new vari_sub(vi_->coeff(i, j));
782 reverse_pass_callback([this_vi = this->vi_, vari_coeff, i, j]() {
783 this_vi->adj_.coeffRef(i, j) += vari_coeff->adj_;
784 });
785 return var_value<value_type_t<vari_sub>>(vari_coeff);
786 }
787
795 inline auto operator()(Eigen::Index i) const { return this->coeff(i); }
796 inline auto operator()(Eigen::Index i) { return this->coeff(i); }
797
806 inline auto operator()(Eigen::Index i, Eigen::Index j) const {
807 return this->coeff(i, j);
808 }
809 inline auto operator()(Eigen::Index i, Eigen::Index j) {
810 return this->coeff(i, j);
811 }
812
820 inline auto coeffRef(Eigen::Index i) const { return this->coeff(i); }
821 inline auto coeffRef(Eigen::Index i) { return this->coeff(i); }
822
831 inline auto coeffRef(Eigen::Index i, Eigen::Index j) const {
832 return this->coeff(i, j);
833 }
834 inline auto coeffRef(Eigen::Index i, Eigen::Index j) {
835 return this->coeff(i, j);
836 }
837
841 inline auto rowwise_reverse() const {
842 using vari_sub = decltype(vi_->rowwise_reverse());
843 using var_sub = var_value<value_type_t<vari_sub>>;
844 return var_sub(new vari_sub(vi_->rowwise_reverse()));
845 }
846 inline auto rowwise_reverse() {
847 using vari_sub = decltype(vi_->rowwise_reverse());
848 using var_sub = var_value<value_type_t<vari_sub>>;
849 return var_sub(new vari_sub(vi_->rowwise_reverse()));
850 }
851
855 inline auto colwise_reverse() const {
856 using vari_sub = decltype(vi_->colwise_reverse());
857 using var_sub = var_value<value_type_t<vari_sub>>;
858 return var_sub(new vari_sub(vi_->colwise_reverse()));
859 }
860 inline auto colwise_reverse() {
861 using vari_sub = decltype(vi_->colwise_reverse());
862 using var_sub = var_value<value_type_t<vari_sub>>;
863 return var_sub(new vari_sub(vi_->colwise_reverse()));
864 }
865
870 inline auto reverse() const {
871 using vari_sub = decltype(vi_->reverse());
872 using var_sub = var_value<value_type_t<vari_sub>>;
873 return var_sub(new vari_sub(vi_->reverse()));
874 }
875 inline auto reverse() {
876 using vari_sub = decltype(vi_->reverse());
877 using var_sub = var_value<value_type_t<vari_sub>>;
878 return var_sub(new vari_sub(vi_->reverse()));
879 }
880
885 inline auto topRows(Eigen::Index n) const {
886 using vari_sub = decltype(vi_->topRows(n));
887 using var_sub = var_value<value_type_t<vari_sub>>;
888 return var_sub(new vari_sub(vi_->topRows(n)));
889 }
890 inline auto topRows(Eigen::Index n) {
891 using vari_sub = decltype(vi_->topRows(n));
892 using var_sub = var_value<value_type_t<vari_sub>>;
893 return var_sub(new vari_sub(vi_->topRows(n)));
894 }
895
900 inline auto bottomRows(Eigen::Index n) const {
901 using vari_sub = decltype(vi_->bottomRows(n));
902 using var_sub = var_value<value_type_t<vari_sub>>;
903 return var_sub(new vari_sub(vi_->bottomRows(n)));
904 }
905 inline auto bottomRows(Eigen::Index n) {
906 using vari_sub = decltype(vi_->bottomRows(n));
907 using var_sub = var_value<value_type_t<vari_sub>>;
908 return var_sub(new vari_sub(vi_->bottomRows(n)));
909 }
910
916 inline auto middleRows(Eigen::Index start_row, Eigen::Index n) const {
917 using vari_sub = decltype(vi_->middleRows(start_row, n));
918 using var_sub = var_value<value_type_t<vari_sub>>;
919 return var_sub(new vari_sub(vi_->middleRows(start_row, n)));
920 }
921 inline auto middleRows(Eigen::Index start_row, Eigen::Index n) {
922 using vari_sub = decltype(vi_->middleRows(start_row, n));
923 using var_sub = var_value<value_type_t<vari_sub>>;
924 return var_sub(new vari_sub(vi_->middleRows(start_row, n)));
925 }
926
931 inline auto leftCols(Eigen::Index n) const {
932 using vari_sub = decltype(vi_->leftCols(n));
933 using var_sub = var_value<value_type_t<vari_sub>>;
934 return var_sub(new vari_sub(vi_->leftCols(n)));
935 }
936 inline auto leftCols(Eigen::Index n) {
937 using vari_sub = decltype(vi_->leftCols(n));
938 using var_sub = var_value<value_type_t<vari_sub>>;
939 return var_sub(new vari_sub(vi_->leftCols(n)));
940 }
941
946 inline auto rightCols(Eigen::Index n) const {
947 using vari_sub = decltype(vi_->rightCols(n));
948 using var_sub = var_value<value_type_t<vari_sub>>;
949 return var_sub(new vari_sub(vi_->rightCols(n)));
950 }
951 inline auto rightCols(Eigen::Index n) {
952 using vari_sub = decltype(vi_->rightCols(n));
953 using var_sub = var_value<value_type_t<vari_sub>>;
954 return var_sub(new vari_sub(vi_->rightCols(n)));
955 }
956
962 inline auto middleCols(Eigen::Index start_col, Eigen::Index n) const {
963 using vari_sub = decltype(vi_->middleCols(start_col, n));
964 using var_sub = var_value<value_type_t<vari_sub>>;
965 return var_sub(new vari_sub(vi_->middleCols(start_col, n)));
966 }
967 inline auto middleCols(Eigen::Index start_col, Eigen::Index n) {
968 using vari_sub = decltype(vi_->middleCols(start_col, n));
969 using var_sub = var_value<value_type_t<vari_sub>>;
970 return var_sub(new vari_sub(vi_->middleCols(start_col, n)));
971 }
972
976 inline auto array() const {
977 using vari_sub = decltype(vi_->array());
978 using var_sub = var_value<value_type_t<vari_sub>>;
979 return var_sub(new vari_sub(vi_->array()));
980 }
981 inline auto array() {
982 using vari_sub = decltype(vi_->array());
983 using var_sub = var_value<value_type_t<vari_sub>>;
984 return var_sub(new vari_sub(vi_->array()));
985 }
986
990 inline auto matrix() const {
991 using vari_sub = decltype(vi_->matrix());
992 using var_sub = var_value<value_type_t<vari_sub>>;
993 return var_sub(new vari_sub(vi_->matrix()));
994 }
995 inline auto matrix() {
996 using vari_sub = decltype(vi_->matrix());
997 using var_sub = var_value<value_type_t<vari_sub>>;
998 return var_sub(new vari_sub(vi_->matrix()));
999 }
1000
1009 friend std::ostream& operator<<(std::ostream& os, const var_value<T>& v) {
1010 if (v.vi_ == nullptr) {
1011 return os << "uninitialized";
1012 }
1013 return os << v.val();
1014 }
1015
1020 template <typename U = T,
1022 inline auto rows() const noexcept {
1023 return vi_->rows();
1024 }
1025
1030 template <typename U = T,
1032 inline auto cols() const noexcept {
1033 return vi_->cols();
1034 }
1035
1043 template <typename S, require_assignable_t<value_type, S>* = nullptr,
1044 require_all_plain_type_t<T, S>* = nullptr,
1045 require_same_t<plain_type_t<T>, plain_type_t<S>>* = nullptr>
1046 inline var_value<T>& operator=(const var_value<S>& other) {
1047 vi_ = other.vi_;
1048 return *this;
1049 }
1050
1060 template <typename S, typename T_ = T,
1064 inline var_value<T>& operator=(const var_value<S>& other) {
1065 static_assert(
1066 EIGEN_PREDICATE_SAME_MATRIX_SIZE(T, S),
1067 "You mixed matrices of different sizes that are not assignable.");
1068 vi_ = new vari_type(other.vi_);
1069 return *this;
1070 }
1071
1079 template <typename S, typename T_ = T,
1081 require_not_plain_type_t<S>* = nullptr,
1082 require_plain_type_t<T_>* = nullptr>
1083 inline var_value<T>& operator=(const var_value<S>& other) {
1084 // If vi_ is nullptr then the var needs initialized via copy constructor
1085 if (!(this->vi_)) {
1086 *this = var_value<T>(other);
1087 return *this;
1088 }
1089 arena_t<plain_type_t<T>> prev_val(vi_->val_.rows(), vi_->val_.cols());
1090 prev_val.deep_copy(vi_->val_);
1091 vi_->val_.deep_copy(other.val());
1092 // no need to change any adjoints - these are just zeros before the reverse
1093 // pass
1094
1096 [this_vi = this->vi_, other_vi = other.vi_, prev_val]() mutable {
1097 this_vi->val_.deep_copy(prev_val);
1098
1099 // we have no way of detecting aliasing between this->vi_->adj_ and
1100 // other.vi_->adj_, so we must copy adjoint before reseting to zero
1101
1102 // we can reuse prev_val instead of allocating a new matrix
1103 prev_val.deep_copy(this_vi->adj_);
1104 this_vi->adj_.setZero();
1105 other_vi->adj_ += prev_val;
1106 });
1107 return *this;
1108 }
1119 template <typename S, typename T_ = T,
1122 inline var_value<T>& operator=(const var_value<S>& other) {
1123 // If vi_ is nullptr then the var needs initialized via copy constructor
1124 if (!(this->vi_)) {
1125 []() STAN_COLD_PATH {
1126 throw std::domain_error(
1127 "var_value<matrix>::operator=(var_value<expression>):"
1128 " Internal Bug! Please report this with an example"
1129 " of your model to the Stan math github repository.");
1130 }();
1131 }
1132 arena_t<plain_type_t<T>> prev_val = vi_->val_;
1133 vi_->val_ = other.val();
1134 // no need to change any adjoints - these are just zeros before the reverse
1135 // pass
1136
1138 [this_vi = this->vi_, other_vi = other.vi_, prev_val]() mutable {
1139 this_vi->val_ = prev_val;
1140
1141 // we have no way of detecting aliasing between this->vi_->adj_ and
1142 // other.vi_->adj_, so we must copy adjoint before reseting to zero
1143
1144 // we can reuse prev_val instead of allocating a new matrix
1145 prev_val = this_vi->adj_;
1146 this_vi->adj_.setZero();
1147 other_vi->adj_ += prev_val;
1148 });
1149 return *this;
1150 }
1151
1155 template <typename T_ = T, require_plain_type_t<T_>* = nullptr>
1156 inline auto& eval() noexcept {
1157 return *this;
1158 }
1159 template <typename T_ = T, require_plain_type_t<T_>* = nullptr>
1160 inline const auto& eval() const noexcept {
1161 return *this;
1162 }
1163
1167 template <typename T_ = T, require_not_plain_type_t<T_>* = nullptr>
1168 inline auto eval() {
1169 return var_value<plain_type_t<T>>(*this);
1170 }
1171 template <typename T_ = T, require_not_plain_type_t<T_>* = nullptr>
1172 inline auto eval() const {
1173 return var_value<plain_type_t<T>>(*this);
1174 }
1175
1181 inline var_value<T>& operator=(const var_value<T>& other) {
1182 return operator=<T>(other);
1183 }
1184};
1185
1186// For backwards compatability the default value is double
1188
1189} // namespace math
1190
1198template <typename T>
1199struct scalar_type<T, std::enable_if_t<is_var<T>::value>> {
1200 using type
1202};
1203
1204} // namespace stan
1205#endif
auto reverse() const
Return an expression an expression to reverse the order of the coefficients inside of a vari matrix.
Definition var.hpp:870
auto segment(Eigen::Index i, Eigen::Index n) const
View block of N elements starting at position i
Definition var.hpp:669
auto coeff(Eigen::Index i, Eigen::Index j) const
View element of eigen matrices.
Definition var.hpp:771
var_value< T > & operator*=(const var_value< T > &b)
The compound multiply/assignment operator for variables (C++).
auto coeff(Eigen::Index i) const
View element of eigen matrices.
Definition var.hpp:746
var_value< T > & operator*=(T b)
The compound multiply/assignment operator for scalars (C++).
auto as_column_vector_or_scalar() const
View a matrix_cl as a column vector.
Definition var.hpp:728
var_value(const var_value< S > &other)
Construct from a var_value with different inner vari_type
Definition var.hpp:397
auto & adj() noexcept
Return a reference to the derivative of the root expression with respect to this expression.
Definition var.hpp:455
std::conditional_t< is_plain_type< value_type >::value, vari_value< value_type >, vari_view< T > > vari_type
Definition var.hpp:324
vari_type * operator->()
Return a pointer to the underlying implementation of this variable.
Definition var.hpp:489
auto operator()(Eigen::Index i) const
View element of eigen matrices.
Definition var.hpp:795
auto rows() const noexcept
Returns number of rows.
Definition var.hpp:1022
vari_type * vi_
Pointer to the implementation of this variable.
Definition var.hpp:336
auto diagonal() const
View diagonal of eigen matrices.
Definition var.hpp:714
const auto & val() const noexcept
Return a constant reference to the value of this variable.
Definition var.hpp:444
auto middleCols(Eigen::Index start_col, Eigen::Index n) const
Return a block consisting of columns in the middle.
Definition var.hpp:962
var_value(const var_value< S > &other)
Construct a var_value with a plain type from another var_value containing an expression.
Definition var.hpp:414
auto leftCols(Eigen::Index n) const
Return a block consisting of the left-most columns.
Definition var.hpp:931
var_value< T > & operator+=(const var_value< T > &b)
The compound add/assignment operator for variables (C++).
auto cols() const noexcept
Returns number of columns.
Definition var.hpp:1032
auto row(Eigen::Index i) const
View row of eigen matrices.
Definition var.hpp:684
bool is_uninitialized() noexcept
Return true if this variable has been declared, but not been defined.
Definition var.hpp:347
auto eval()
For non-plain types evaluate to the plain type.
Definition var.hpp:1168
auto colwise_reverse() const
Return an expression that operates on the columns of the matrix vari
Definition var.hpp:855
auto block(Eigen::Index start_row, Eigen::Index start_col, Eigen::Index num_rows, Eigen::Index num_cols)
Definition var.hpp:611
auto col(Eigen::Index i) const
View column of eigen matrices.
Definition var.hpp:699
var_value< T > & operator=(const var_value< S > &other)
Assignment of one plain type to another when one sides compile time columns differ from the other.
Definition var.hpp:1064
auto bottomRows(Eigen::Index n) const
Return a block consisting of the bottom rows.
Definition var.hpp:900
auto head(Eigen::Index n) const
View of the head of Eigen vector types.
Definition var.hpp:638
auto tail(Eigen::Index n) const
View of the tail of the Eigen vector types.
Definition var.hpp:653
auto middleRows(Eigen::Index start_row, Eigen::Index n) const
Return a block consisting of rows in the middle.
Definition var.hpp:916
vari_type & operator*()
Return a reference to underlying implementation of this variable.
Definition var.hpp:477
auto rightCols(Eigen::Index n) const
Return a block consisting of the right-most columns.
Definition var.hpp:946
var_value(const S &val, const S &adj)
Construct a var_value with premade arena_matrix types.
Definition var.hpp:431
var_value(S &&x)
Construct a variable from the specified floating point argument by constructing a new vari_value<valu...
Definition var.hpp:366
friend std::ostream & operator<<(std::ostream &os, const var_value< T > &v)
Write the value of this autodiff variable and its adjoint to the specified output stream.
Definition var.hpp:1009
auto middleRows(Eigen::Index start_row, Eigen::Index n)
Definition var.hpp:921
auto operator()(Eigen::Index i, Eigen::Index j) const
View element of eigen matrices.
Definition var.hpp:806
var_value< T > & operator-=(const S &b)
The compound subtract/assignment operator for variables (C++).
var_value< T > & operator=(const var_value< S > &other)
Assignment of another plain var value, when this also contains a plain type.
Definition var.hpp:1046
var_value< T > & operator=(const var_value< T > &other)
Copy assignment operator delegates to general assignment operator.
Definition var.hpp:1181
auto block(Eigen::Index start_row, Eigen::Index start_col, Eigen::Index num_rows, Eigen::Index num_cols) const
A block view of the underlying Eigen matrices.
Definition var.hpp:603
auto & eval() noexcept
No-op to match with Eigen methods which call eval.
Definition var.hpp:1156
var_value(vari_type *vi)
Construct a variable from a pointer to a variable implementation.
Definition var.hpp:437
var_value()
Construct a variable for later assignment.
Definition var.hpp:356
var_value(const var_value< S > &other)
Copy constructor for var_val when the vari_type from other is directly assignable.
Definition var.hpp:379
auto coeffRef(Eigen::Index i) const
View element of eigen matrices.
Definition var.hpp:820
auto coeffRef(Eigen::Index i, Eigen::Index j) const
View element of eigen matrices.
Definition var.hpp:831
auto rowwise_reverse() const
Return an expression that operates on the rows of the matrix vari
Definition var.hpp:841
var_value< T > & operator+=(T b)
The compound add/assignment operator for scalars (C++).
auto middleCols(Eigen::Index start_col, Eigen::Index n)
Definition var.hpp:967
auto topRows(Eigen::Index n) const
Return a block consisting of the top rows.
Definition var.hpp:885
auto transpose() const
View transpose of eigen matrix.
Definition var.hpp:623
void grad(std::vector< var_value< T > > &x, std::vector< value_type > &g)
Compute the gradient of this (dependent) variable with respect to the specified vector of (independen...
Definition var.hpp:132
auto & adj() const noexcept
Return a reference of the derivative of the root expression with respect to this expression.
Definition var.hpp:105
var_value(vari_type *vi)
Construct a variable from a pointer to a variable implementation.
Definition var.hpp:88
vari_type * operator->()
Return a pointer to the underlying implementation of this variable.
Definition var.hpp:177
vari_type * vi_
Pointer to the implementation of this variable.
Definition var.hpp:52
bool is_uninitialized()
Return true if this variable has been declared, but not been defined.
Definition var.hpp:63
var_value()
Construct a variable for later assignment.
Definition var.hpp:72
friend std::ostream & operator<<(std::ostream &os, const var_value< T > &v)
Write the value of this autodiff variable and its adjoint to the specified output stream.
Definition var.hpp:290
vari_type & operator*()
Return a reference to underlying implementation of this variable.
Definition var.hpp:165
void grad()
Compute the gradient of this (dependent) variable with respect to all (independent) variables.
Definition var.hpp:149
var_value(S x)
Construct a variable from the specified floating point argument by constructing a new vari_value<valu...
Definition var.hpp:82
const auto & val() const noexcept
Return a constant reference to the value of this variable.
Definition var.hpp:95
auto & adj() noexcept
Return a reference to the derivative of the root expression with respect to this expression.
Definition var.hpp:115
A vari_view is used to read from a slice of a vari_value with an inner eigen type.
Definition vari.hpp:206
#define STAN_COLD_PATH
require_t< is_arena_matrix< std::decay_t< T > > > require_arena_matrix_t
Require type satisfies is_arena_matrix.
require_t< std::is_assignable< std::decay_t< T >, std::decay_t< S > > > require_assignable_t
Require types T and S satisfies std::is_assignable.
require_not_t< std::is_assignable< std::decay_t< T >, std::decay_t< S > > > require_not_assignable_t
Require types T and S does not satisfy std::is_assignable.
require_t< std::is_constructible< std::decay_t< T >, std::decay_t< S > > > require_constructible_t
Require types T and S satisfies std::is_constructible.
require_t< std::is_floating_point< std::decay_t< T > > > require_floating_point_t
Require type satisfies std::is_floating_point.
require_not_t< is_plain_type< std::decay_t< T > > > require_not_plain_type_t
Require type does not satisfy is_plain_type.
require_all_t< is_plain_type< std::decay_t< Types > >... > require_all_plain_type_t
Require all of the types satisfy is_plain_type.
require_t< is_plain_type< std::decay_t< T > > > require_plain_type_t
Require type satisfies is_plain_type.
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.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
std::integral_constant< bool, B > bool_constant
Alias for structs used for wraps a static constant of bool.
require_t< bool_constant<(is_eigen< T >::value||is_kernel_expression_and_not_scalar< T >::value) &&std::is_floating_point< value_type_t< T > >::value > > require_matrix_var_value
Definition var.hpp:302
T1 operator+=(T1 &&a, T2 &&b)
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
T1 operator-=(T1 &&a, T2 &&b)
T1 operator*=(T1 &&a, T2 &&b)
static void grad()
Compute the gradient for all variables starting from the end of the AD tape.
Definition grad.hpp:26
std::enable_if_t< math::disjunction< Checks... >::value > require_any_t
If any condition is true, template is enabled.
typename plain_type< T >::type plain_type_t
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.
std::enable_if_t< Check::value > require_t
If condition is true, template is enabled.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
STL namespace.
Check if type derives from EigenBase
Definition is_eigen.hpp:21
Determines whether a type is non-scalar type that is a valid kernel generator expression.
Checks if the decayed type of T is a matrix_cl.
Metaprogram structure to determine the base scalar type of a template argument.