Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operator- for var<matrix> #2196

Merged
merged 12 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stan/math/prim/fun/size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ inline size_t size(const T& m) {
return m.size();
}

template <typename T, require_var_matrix_t<T>* = nullptr>
inline size_t size(const T& m) {
return m.size();
}

} // namespace math
} // namespace stan
#endif
12 changes: 8 additions & 4 deletions stan/math/prim/fun/subtract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ namespace math {
* @return difference between first scalar and second scalar
*/
template <typename ScalarA, typename ScalarB,
typename = require_all_stan_scalar_t<ScalarA, ScalarB>>
require_all_stan_scalar_t<ScalarA, ScalarB>* = nullptr,
require_all_not_var_t<ScalarA, ScalarB>* = nullptr>
inline return_type_t<ScalarA, ScalarB> subtract(const ScalarA& a,
const ScalarB& b) {
return a - b;
Expand All @@ -37,7 +38,8 @@ inline return_type_t<ScalarA, ScalarB> subtract(const ScalarA& a,
* @return Difference between first matrix and second matrix.
*/
template <typename Mat1, typename Mat2,
typename = require_all_eigen_t<Mat1, Mat2>>
require_all_eigen_t<Mat1, Mat2>* = nullptr,
require_all_not_st_var<Mat1, Mat2>* = nullptr>
inline auto subtract(const Mat1& m1, const Mat2& m2) {
check_matching_dims("subtract", "m1", m1, "m2", m2);
return (m1 - m2).eval();
Expand All @@ -54,7 +56,8 @@ inline auto subtract(const Mat1& m1, const Mat2& m2) {
* @return The scalar minus the matrix.
*/
template <typename Scal, typename Mat, typename = require_stan_scalar_t<Scal>,
typename = require_eigen_t<Mat>>
require_eigen_t<Mat>* = nullptr,
require_all_not_st_var<Mat, Scal>* = nullptr>
inline auto subtract(const Scal c, const Mat& m) {
return (c - m.array()).matrix().eval();
}
Expand All @@ -70,7 +73,8 @@ inline auto subtract(const Scal c, const Mat& m) {
* @return The matrix minus the scalar.
*/
template <typename Mat, typename Scal, typename = require_eigen_t<Mat>,
typename = require_stan_scalar_t<Scal>>
require_stan_scalar_t<Scal>* = nullptr,
require_all_not_st_var<Scal, Mat>* = nullptr>
inline auto subtract(const Mat& m, const Scal c) {
return (m.array() - c).matrix().eval();
}
Expand Down
8 changes: 4 additions & 4 deletions stan/math/rev/core/operator_minus_equal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
#include <stan/math/rev/core/var.hpp>
#include <stan/math/rev/core/operator_subtraction.hpp>
#include <stan/math/prim/meta.hpp>

#include <stan/math/prim/fun/size.hpp>
namespace stan {
namespace math {

template <typename T>
inline var_value<T>& var_value<T, require_floating_point_t<T>>::operator-=(
const var_value<T>& b) {
vi_ = new internal::subtract_vv_vari(vi_, b.vi_);
vi_ = (*this - b).vi_;
return *this;
}

template <typename T>
inline var_value<T>& var_value<T, require_floating_point_t<T>>::operator-=(
T b) {
if (b == 0.0) {
if (unlikely(b == 0.0)) {
return *this;
}
vi_ = new internal::subtract_vd_vari(vi_, b);
vi_ = (*this - b).vi_;
return *this;
}

Expand Down
Loading