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

var matrix specializations for a-c unary functions in rev #2256

Merged
merged 30 commits into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0c52267
adds unary functions from a to c for varmat
SteveBronder Dec 10, 2020
17b853d
Merge remote-tracking branch 'origin/develop' into feature/varmat-a-t…
SteveBronder Dec 12, 2020
2f8da9f
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Dec 12, 2020
fe2354c
update docs
SteveBronder Dec 14, 2020
afda49a
Merge commit '7f3792baa9e91438e79a2cf6594a38d105c9b24e' into HEAD
yashikno Dec 14, 2020
91d1472
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Dec 14, 2020
da21330
Merge remote-tracking branch 'origin/develop' into feature/varmat-a-t…
SteveBronder Dec 15, 2020
86d6f8a
add tests for vectors of varmat
SteveBronder Dec 15, 2020
13974c1
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Dec 15, 2020
77f77e8
update abs so it can be vectorized
SteveBronder Dec 15, 2020
1768e1e
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Dec 15, 2020
198bec3
update abs so it can be vectorized
SteveBronder Dec 15, 2020
010ebaf
Merge branch 'feature/varmat-a-to-c-unary' of github.com:stan-dev/mat…
SteveBronder Dec 15, 2020
fac9b65
update with develop, write out sin and tan
SteveBronder Dec 17, 2020
11e3459
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Dec 17, 2020
3a02c30
updates to use cwiseproduct()
SteveBronder Dec 17, 2020
237f6a0
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Dec 17, 2020
0e610cf
update tan test
SteveBronder Dec 17, 2020
37d61ac
update tan requires
SteveBronder Dec 17, 2020
b0738f7
Merge commit '72552dcc6040018b35d2bb1303b461493b815d95' into HEAD
yashikno Dec 17, 2020
ba7dae7
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Dec 17, 2020
8129f72
update tan requires
SteveBronder Dec 17, 2020
88c35d9
update tan requires
SteveBronder Dec 17, 2020
4fa4816
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Dec 17, 2020
0db6223
Fixes tests and docs for var<matrix> trig functions and apply_scalar_…
SteveBronder Dec 18, 2020
a7deb36
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Dec 18, 2020
7e41553
Updated abs/fabs tests to be like the others (Issue #2101)
bbbales2 Dec 18, 2020
b26159f
remove .noalias() from trig functions in rev
SteveBronder Dec 18, 2020
812ae25
Merge commit '3d03131bdcbb2d33878f4117c857ac6404c7e10e' into HEAD
yashikno Dec 18, 2020
c5caa59
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Dec 18, 2020
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
Prev Previous commit
Next Next commit
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
…4.1 (tags/RELEASE_600/final)
  • Loading branch information
stan-buildbot committed Dec 12, 2020
commit 2f8da9f89cd66703daab1e32d1e0f53250616cdd
4 changes: 3 additions & 1 deletion stan/math/rev/fun/abs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ namespace math {
* @return Absolute value of variable.
*/
template <typename T>
inline auto abs(const var_value<T>& a) { return fabs(a); }
inline auto abs(const var_value<T>& a) {
return fabs(a);
}

/**
* Return the absolute value of the complex argument.
Expand Down
8 changes: 5 additions & 3 deletions stan/math/rev/fun/acos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ inline var acos(const var& x) {

template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto acos(const VarMat& x) {
return make_callback_var(x.val().array().acos().matrix(), [x](const auto& vi) mutable {
x.adj().array() -= vi.adj_.array() / (1.0 - (x.val().array() * x.val().array())).sqrt();
});
return make_callback_var(
x.val().array().acos().matrix(), [x](const auto& vi) mutable {
x.adj().array() -= vi.adj_.array()
/ (1.0 - (x.val().array() * x.val().array())).sqrt();
});
}

/**
Expand Down
28 changes: 14 additions & 14 deletions stan/math/rev/fun/acosh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ namespace math {
* @return Inverse hyperbolic cosine of the variable.
*/

inline var acosh(const var& x) {
return make_callback_var(acosh(x.val()), [x](const auto& vi) mutable {
x.adj() += vi.adj_ / std::sqrt(x.val() * x.val() - 1.0);
});
}
inline var acosh(const var& x) {
return make_callback_var(acosh(x.val()), [x](const auto& vi) mutable {
x.adj() += vi.adj_ / std::sqrt(x.val() * x.val() - 1.0);
});
}

template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto acosh(const VarMat& x) {
return make_callback_var(x.val().unaryExpr([](const auto x) {
return acosh(x);
}),
[x](const auto& vi) mutable {
x.adj().array() += vi.adj_.array() / (x.val().array() * x.val().array() - 1.0).sqrt();
});
}
template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto acosh(const VarMat& x) {
return make_callback_var(
x.val().unaryExpr([](const auto x) { return acosh(x); }),
[x](const auto& vi) mutable {
x.adj().array() += vi.adj_.array()
/ (x.val().array() * x.val().array() - 1.0).sqrt();
});
}

/**
* Return the hyperbolic arc cosine of the complex argument.
Expand Down
24 changes: 13 additions & 11 deletions stan/math/rev/fun/asin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@ namespace math {
* @param a Variable in range [-1, 1].
* @return Arc sine of variable, in radians.
*/
inline var asin(const var& x) {
return make_callback_var(std::asin(x.val()), [x](const auto& vi) mutable {
x.adj() += vi.adj_ / std::sqrt(1.0 - (x.val() * x.val()));
});
}
inline var asin(const var& x) {
return make_callback_var(std::asin(x.val()), [x](const auto& vi) mutable {
x.adj() += vi.adj_ / std::sqrt(1.0 - (x.val() * x.val()));
});
}

template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto asin(const VarMat& x) {
return make_callback_var(x.val().array().asin().matrix(), [x](const auto& vi) mutable {
x.adj().array() += vi.adj_.array() / (1.0 - (x.val().array() * x.val().array())).sqrt();
});
}
template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto asin(const VarMat& x) {
return make_callback_var(
x.val().array().asin().matrix(), [x](const auto& vi) mutable {
x.adj().array() += vi.adj_.array()
/ (1.0 - (x.val().array() * x.val().array())).sqrt();
});
}

/**
* Return the arc sine of the complex argument.
Expand Down
29 changes: 14 additions & 15 deletions stan/math/rev/fun/asinh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,21 @@ namespace math {
* @param a The variable.
* @return Inverse hyperbolic sine of the variable.
*/
inline var asinh(const var& x) {
return make_callback_var(asinh(x.val()), [x](const auto& vi) mutable {
x.adj() += vi.adj_ / std::sqrt(x.val() * x.val() + 1.0);
});
}

template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto asinh(const VarMat& x) {
return make_callback_var(x.val().unaryExpr([](const auto x) {
return asinh(x);
}),
[x](const auto& vi) mutable {
x.adj().array() += vi.adj_.array() / (x.val().array() * x.val().array() + 1.0).sqrt();
});
}
inline var asinh(const var& x) {
return make_callback_var(asinh(x.val()), [x](const auto& vi) mutable {
x.adj() += vi.adj_ / std::sqrt(x.val() * x.val() + 1.0);
});
}

template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto asinh(const VarMat& x) {
return make_callback_var(
x.val().unaryExpr([](const auto x) { return asinh(x); }),
[x](const auto& vi) mutable {
x.adj().array() += vi.adj_.array()
/ (x.val().array() * x.val().array() + 1.0).sqrt();
});
}

/**
* Return the hyperbolic arcsine of the complex argument.
Expand Down
24 changes: 13 additions & 11 deletions stan/math/rev/fun/atan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,20 @@ namespace math {
* @param a Variable in range [-1, 1].
* @return Arc tangent of variable, in radians.
*/
inline var atan(const var& x) {
return make_callback_var(std::atan(x.val()), [x](const auto& vi) mutable {
x.adj() += vi.adj_ / (1.0 + (x.val() * x.val()));
});
}
inline var atan(const var& x) {
return make_callback_var(std::atan(x.val()), [x](const auto& vi) mutable {
x.adj() += vi.adj_ / (1.0 + (x.val() * x.val()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have .adj() accessors are the varis now but either works.

});
}

template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto atan(const VarMat& x) {
return make_callback_var(x.val().array().atan().matrix(), [x](const auto& vi) mutable {
x.adj().array() += vi.adj_.array() / (1.0 + (x.val().array() * x.val().array()));
});
}
template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto atan(const VarMat& x) {
return make_callback_var(
x.val().array().atan().matrix(), [x](const auto& vi) mutable {
x.adj().array()
+= vi.adj_.array() / (1.0 + (x.val().array() * x.val().array()));
});
}
/**
* Return the arc tangent of the complex argument.
*
Expand Down
33 changes: 16 additions & 17 deletions stan/math/rev/fun/atan2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ namespace math {
* @return The arc tangent of the fraction, in radians.
*/
inline var atan2(const var& a, const var& b) {
return make_callback_var(std::atan2(a.val(), b.val()), [a, b](const auto& vi) mutable {
double a_sq_plus_b_sq
= (a.val() * a.val()) + (b.val() * b.val());
a.adj() += vi.adj_ * b.val() / a_sq_plus_b_sq;
b.adj() -= vi.adj_ * a.val() / a_sq_plus_b_sq;
});
return make_callback_var(
std::atan2(a.val(), b.val()), [a, b](const auto& vi) mutable {
double a_sq_plus_b_sq = (a.val() * a.val()) + (b.val() * b.val());
a.adj() += vi.adj_ * b.val() / a_sq_plus_b_sq;
b.adj() -= vi.adj_ * a.val() / a_sq_plus_b_sq;
});
}

/**
Expand All @@ -46,11 +46,11 @@ inline var atan2(const var& a, const var& b) {
* @return The arc tangent of the fraction, in radians.
*/
inline var atan2(const var& a, double b) {
return make_callback_var(std::atan2(a.val(), b), [a, b](const auto& vi) mutable {
double a_sq_plus_b_sq
= (a.val() * a.val()) + (b * b);
a.adj() += vi.adj_ * b / a_sq_plus_b_sq;
});
return make_callback_var(
std::atan2(a.val(), b), [a, b](const auto& vi) mutable {
double a_sq_plus_b_sq = (a.val() * a.val()) + (b * b);
a.adj() += vi.adj_ * b / a_sq_plus_b_sq;
});
}

/**
Expand Down Expand Up @@ -90,14 +90,13 @@ inline var atan2(const var& a, double b) {
* @return The arc tangent of the fraction, in radians.
*/
inline var atan2(double a, const var& b) {
return make_callback_var(std::atan2(a, b.val()), [a, b](const auto& vi) mutable {
double a_sq_plus_b_sq
= (a * a) + (b.val() * b.val());
b.adj() -= vi.adj_ * a / a_sq_plus_b_sq;
});
return make_callback_var(
std::atan2(a, b.val()), [a, b](const auto& vi) mutable {
double a_sq_plus_b_sq = (a * a) + (b.val() * b.val());
b.adj() -= vi.adj_ * a / a_sq_plus_b_sq;
});
}


} // namespace math
} // namespace stan
#endif
27 changes: 13 additions & 14 deletions stan/math/rev/fun/atanh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,21 @@ namespace math {
* @return Inverse hyperbolic tangent of the variable.
* @throw std::domain_error if a < -1 or a > 1
*/
inline var atanh(const var& x) {
return make_callback_var(atanh(x.val()), [x](const auto& vi) mutable {
x.adj() += vi.adj_ / (1.0 - x.val() * x.val());
});
}
inline var atanh(const var& x) {
return make_callback_var(atanh(x.val()), [x](const auto& vi) mutable {
x.adj() += vi.adj_ / (1.0 - x.val() * x.val());
});
}

template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto atanh(const VarMat& x) {
return make_callback_var(x.val().unaryExpr([](const auto x) {
return atanh(x);
}),
template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto atanh(const VarMat& x) {
return make_callback_var(
x.val().unaryExpr([](const auto x) { return atanh(x); }),
bbbales2 marked this conversation as resolved.
Show resolved Hide resolved
[x](const auto& vi) mutable {
x.adj().array() += vi.adj_.array() / (1.0 - x.val().array() * x.val().array());
});
}

x.adj().array()
+= vi.adj_.array() / (1.0 - x.val().array() * x.val().array());
});
}

/**
* Return the hyperbolic arc tangent of the complex argument.
Expand Down
10 changes: 6 additions & 4 deletions stan/math/rev/fun/bessel_first_kind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ namespace stan {
namespace math {

inline var bessel_first_kind(int v, const var& a) {
return make_callback_var(bessel_first_kind(v, a.val()), [v, a](const auto& vi) mutable {
a.adj() += vi.adj_ * (v * bessel_first_kind(v, a.val()) / a.val()
- bessel_first_kind(v + 1, a.val()));
});
return make_callback_var(
bessel_first_kind(v, a.val()), [v, a](const auto& vi) mutable {
a.adj() += vi.adj_
* (v * bessel_first_kind(v, a.val()) / a.val()
- bessel_first_kind(v + 1, a.val()));
});
}

} // namespace math
Expand Down
14 changes: 6 additions & 8 deletions stan/math/rev/fun/cbrt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,19 @@ namespace math {
*/
inline var cbrt(const var& a) {
return make_callback_var(cbrt(a.val()), [a](const auto& vi) mutable {
a.adj() += vi.adj_ / (3.0 * vi.val_ * vi.val_);
a.adj() += vi.adj_ / (3.0 * vi.val_ * vi.val_);
});
}

template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto cbrt(const VarMat& a) {
return make_callback_var(a.val().unaryExpr([](const auto x) {
return cbrt(x);
}),
[a](const auto& vi) mutable {
a.adj().array() += vi.adj_.array() / (3.0 * vi.val_.array().square());
});
return make_callback_var(
a.val().unaryExpr([](const auto x) { return cbrt(x); }),
[a](const auto& vi) mutable {
a.adj().array() += vi.adj_.array() / (3.0 * vi.val_.array().square());
bbbales2 marked this conversation as resolved.
Show resolved Hide resolved
});
}


} // namespace math
} // namespace stan
#endif
7 changes: 4 additions & 3 deletions stan/math/rev/fun/cos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ inline var cos(const var& a) {

template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto cos(const VarMat& a) {
return make_callback_var(a.val().array().cos().matrix(), [a](const auto& vi) mutable {
a.adj().array() -= vi.adj_.array() * a.val().array().sin();
});
return make_callback_var(
a.val().array().cos().matrix(), [a](const auto& vi) mutable {
a.adj().array() -= vi.adj_.array() * a.val().array().sin();
});
}
/**
* Return the cosine of the complex argument.
Expand Down
41 changes: 21 additions & 20 deletions stan/math/rev/fun/cosh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,29 @@ namespace math {
* @param a Variable.
* @return Hyperbolic cosine of variable.
*/
inline var cosh(const var& a) {
return make_callback_var(std::cosh(a.val()), [a](const auto& vi) mutable {
a.adj() += vi.adj_ * std::sinh(a.val());
});
}
inline var cosh(const var& a) {
return make_callback_var(std::cosh(a.val()), [a](const auto& vi) mutable {
a.adj() += vi.adj_ * std::sinh(a.val());
});
}

template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto cosh(const VarMat& a) {
return make_callback_var(a.val().array().cosh().matrix(), [a](const auto& vi) mutable {
a.adj().array() += vi.adj_.array() * a.val().array().sinh();
});
}
template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
inline auto cosh(const VarMat& a) {
return make_callback_var(
a.val().array().cosh().matrix(), [a](const auto& vi) mutable {
a.adj().array() += vi.adj_.array() * a.val().array().sinh();
});
}

/**
* Return the hyperbolic cosine of the complex argument.
*
* @param[in] z argument
* @return hyperbolic cosine of the argument
*/
inline std::complex<var> cosh(const std::complex<var>& z) {
return stan::math::internal::complex_cosh(z);
}
/**
* Return the hyperbolic cosine of the complex argument.
*
* @param[in] z argument
* @return hyperbolic cosine of the argument
*/
inline std::complex<var> cosh(const std::complex<var>& z) {
return stan::math::internal::complex_cosh(z);
}

} // namespace math
} // namespace stan
Expand Down
26 changes: 14 additions & 12 deletions stan/math/rev/fun/fabs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,20 @@ inline auto fabs(const VarMat& a) {
} else {
return std::abs(x);
}
}), [a](const auto& vi) mutable {
for (Eigen::Index j = 0; j < vi.cols(); ++j) {
for (Eigen::Index i = 0; i < vi.rows(); ++i) {
const auto x = a.val().coeffRef(i, j);
if (unlikely(is_nan(x))) {
a.adj().coeffRef(i, j) = NOT_A_NUMBER;
} else {
a.adj().coeffRef(i, j) -= vi.adj_.coeff(i, j);
}
}
}
});
}),
[a](const auto& vi) mutable {
for (Eigen::Index j = 0; j < vi.cols(); ++j) {
for (Eigen::Index i = 0; i < vi.rows(); ++i) {
const auto x = a.val().coeffRef(i, j);
if (unlikely(is_nan(x))) {
a.adj().coeffRef(i, j) = NOT_A_NUMBER;
} else {
a.adj().coeffRef(i, j)
-= vi.adj_.coeff(i, j);
}
}
}
});
}

} // namespace math
Expand Down
Loading