Skip to content

Commit

Permalink
Encapsulate std::complex functions (#3122)
Browse files Browse the repository at this point in the history
* testing encapsulation of std complex

* fix all headers in rev

* add docs for all new functions

* fix conj

* fix complex conj

* [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1

* fix double header includes

* fix docs

* update fwd includes

* update includes for fwd to always prefer fwd version before prim version.

* update includes for fwd to always prefer fwd version before prim version.

* Docstring duplication and edits

* update header includes

* [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1

* update header includes

* fix headers

* update headers

* [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1

* cpplint fix

---------

Co-authored-by: Stan Jenkins <[email protected]>
Co-authored-by: Brian Ward <[email protected]>
  • Loading branch information
3 people authored Nov 18, 2024
1 parent 4a812be commit baae551
Show file tree
Hide file tree
Showing 281 changed files with 1,240 additions and 782 deletions.
4 changes: 2 additions & 2 deletions stan/math/fwd/fun/Phi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/pow.hpp>
#include <stan/math/fwd/fun/exp.hpp>
#include <stan/math/prim/fun/constants.hpp>
#include <stan/math/prim/fun/Phi.hpp>
#include <cmath>
Expand All @@ -12,8 +14,6 @@ namespace math {

template <typename T>
inline fvar<T> Phi(const fvar<T>& x) {
using std::exp;
using std::sqrt;
T xv = x.val_;
return fvar<T>(Phi(xv), x.d_ * exp(xv * xv / -2.0) * INV_SQRT_TWO_PI);
}
Expand Down
5 changes: 3 additions & 2 deletions stan/math/fwd/fun/Phi_approx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/fun/inv_logit.hpp>
#include <stan/math/fwd/fun/inv_logit.hpp>
#include <stan/math/fwd/fun/pow.hpp>
#include <stan/math/prim/fun/Phi_approx.hpp>
#include <cmath>

namespace stan {
Expand All @@ -21,7 +23,6 @@ namespace math {
*/
template <typename T>
inline fvar<T> Phi_approx(const fvar<T>& x) {
using std::pow;
return inv_logit(0.07056 * pow(x, 3.0) + 1.5976 * x);
}

Expand Down
4 changes: 2 additions & 2 deletions stan/math/fwd/fun/abs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/fun/abs.hpp>
#include <stan/math/fwd/fun/value_of.hpp>
#include <stan/math/prim/fun/constants.hpp>
#include <stan/math/prim/fun/value_of.hpp>
#include <stan/math/prim/fun/abs.hpp>
#include <complex>

namespace stan {
Expand Down
5 changes: 2 additions & 3 deletions stan/math/fwd/fun/acos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/meta.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <stan/math/fwd/fun/sqrt.hpp>
#include <stan/math/fwd/fun/square.hpp>
#include <stan/math/prim/fun/acos.hpp>
#include <cmath>
#include <complex>
Expand All @@ -13,8 +14,6 @@ namespace math {

template <typename T>
inline fvar<T> acos(const fvar<T>& x) {
using std::acos;
using std::sqrt;
return fvar<T>(acos(x.val_), x.d_ / -sqrt(1 - square(x.val_)));
}

Expand Down
5 changes: 2 additions & 3 deletions stan/math/fwd/fun/acosh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/sqrt.hpp>
#include <stan/math/fwd/fun/square.hpp>
#include <stan/math/prim/fun/acosh.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <stan/math/prim/fun/sqrt.hpp>
#include <cmath>
#include <complex>

Expand All @@ -14,7 +14,6 @@ namespace math {

template <typename T>
inline fvar<T> acosh(const fvar<T>& x) {
using std::sqrt;
return fvar<T>(acosh(x.val_), x.d_ / sqrt(square(x.val_) - 1));
}

Expand Down
5 changes: 2 additions & 3 deletions stan/math/fwd/fun/asin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/sqrt.hpp>
#include <stan/math/fwd/fun/square.hpp>
#include <stan/math/prim/fun/asin.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <cmath>
#include <complex>

Expand All @@ -13,8 +14,6 @@ namespace math {

template <typename T>
inline fvar<T> asin(const fvar<T>& x) {
using std::asin;
using std::sqrt;
return fvar<T>(asin(x.val_), x.d_ / sqrt(1 - square(x.val_)));
}

Expand Down
4 changes: 2 additions & 2 deletions stan/math/fwd/fun/asinh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/fun/sqrt.hpp>
#include <stan/math/fwd/fun/square.hpp>
#include <stan/math/prim/fun/asinh.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <cmath>
#include <complex>

Expand All @@ -13,7 +14,6 @@ namespace math {

template <typename T>
inline fvar<T> asinh(const fvar<T>& x) {
using std::sqrt;
return fvar<T>(asinh(x.val_), x.d_ / sqrt(square(x.val_) + 1));
}

Expand Down
4 changes: 2 additions & 2 deletions stan/math/fwd/fun/atan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/fun/atanh.hpp>
#include <stan/math/fwd/fun/square.hpp>
#include <stan/math/prim/fun/atan.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <cmath>
#include <complex>

Expand All @@ -13,7 +14,6 @@ namespace math {

template <typename T>
inline fvar<T> atan(const fvar<T>& x) {
using std::atan;
return fvar<T>(atan(x.val_), x.d_ / (1 + square(x.val_)));
}

Expand Down
6 changes: 2 additions & 4 deletions stan/math/fwd/fun/atan2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <stan/math/fwd/fun/square.hpp>
#include <stan/math/prim/fun/atan2.hpp>
#include <cmath>

namespace stan {
namespace math {

template <typename T>
inline fvar<T> atan2(const fvar<T>& x1, const fvar<T>& x2) {
using std::atan2;
return fvar<T>(atan2(x1.val_, x2.val_),
(x1.d_ * x2.val_ - x1.val_ * x2.d_)
/ (square(x2.val_) + square(x1.val_)));
}

template <typename T>
inline fvar<T> atan2(double x1, const fvar<T>& x2) {
using std::atan2;
return fvar<T>(atan2(x1, x2.val_),
(-x1 * x2.d_) / (square(x1) + square(x2.val_)));
}

template <typename T>
inline fvar<T> atan2(const fvar<T>& x1, double x2) {
using std::atan2;
return fvar<T>(atan2(x1.val_, x2),
(x1.d_ * x2) / (square(x2) + square(x1.val_)));
}
Expand Down
4 changes: 3 additions & 1 deletion stan/math/fwd/fun/atanh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/fun/log.hpp>
#include <stan/math/fwd/fun/square.hpp>
#include <stan/math/fwd/fun/value_of_rec.hpp>
#include <stan/math/prim/fun/atanh.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <cmath>
#include <complex>

Expand Down
2 changes: 1 addition & 1 deletion stan/math/fwd/fun/beta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/digamma.hpp>
#include <stan/math/prim/fun/beta.hpp>
#include <stan/math/prim/fun/digamma.hpp>

namespace stan {
namespace math {
Expand Down
2 changes: 1 addition & 1 deletion stan/math/fwd/fun/cbrt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/square.hpp>
#include <stan/math/prim/fun/cbrt.hpp>
#include <stan/math/prim/fun/square.hpp>

namespace stan {
namespace math {
Expand Down
2 changes: 1 addition & 1 deletion stan/math/fwd/fun/ceil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/fun/ceil.hpp>
#include <cmath>

namespace stan {
namespace math {

template <typename T>
inline fvar<T> ceil(const fvar<T>& x) {
using std::ceil;
return fvar<T>(ceil(x.val_), 0);
}

Expand Down
18 changes: 0 additions & 18 deletions stan/math/fwd/fun/conj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,4 @@
#include <stan/math/prim/fun/conj.hpp>
#include <complex>

namespace stan {
namespace math {

/**
* Return the phase angle of the complex argument.
*
* @tparam T value type of autodiff variable
* @param[in] z argument
* @return phase angle of the argument
*/
template <typename T>
inline std::complex<fvar<T>> conj(const std::complex<fvar<T>>& z) {
return internal::complex_conj(z);
}

} // namespace math
} // namespace stan

#endif
4 changes: 2 additions & 2 deletions stan/math/fwd/fun/cos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/fun/sin.hpp>
#include <stan/math/fwd/fun/cosh.hpp>
#include <stan/math/prim/fun/cos.hpp>
#include <cmath>
#include <complex>
Expand All @@ -12,8 +14,6 @@ namespace math {

template <typename T>
inline fvar<T> cos(const fvar<T>& x) {
using std::cos;
using std::sin;
return fvar<T>(cos(x.val_), x.d_ * -sin(x.val_));
}

Expand Down
3 changes: 1 addition & 2 deletions stan/math/fwd/fun/cosh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/exp.hpp>
#include <stan/math/fwd/fun/sinh.hpp>
#include <stan/math/prim/fun/cosh.hpp>
#include <cmath>
#include <complex>
Expand All @@ -13,8 +14,6 @@ namespace math {

template <typename T>
inline fvar<T> cosh(const fvar<T>& x) {
using std::cosh;
using std::sinh;
return fvar<T>(cosh(x.val_), x.d_ * sinh(x.val_));
}

Expand Down
4 changes: 2 additions & 2 deletions stan/math/fwd/fun/determinant.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef STAN_MATH_FWD_FUN_DETERMINANT_HPP
#define STAN_MATH_FWD_FUN_DETERMINANT_HPP

#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/err.hpp>
#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/fun/to_ref.hpp>
#include <stan/math/fwd/core.hpp>

namespace stan {
namespace math {
Expand Down
2 changes: 1 addition & 1 deletion stan/math/fwd/fun/digamma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/trigamma.hpp>

#include <stan/math/prim/fun/digamma.hpp>
#include <stan/math/prim/fun/trigamma.hpp>

namespace stan {
namespace math {
Expand Down
4 changes: 2 additions & 2 deletions stan/math/fwd/fun/erf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/exp.hpp>
#include <stan/math/fwd/fun/square.hpp>
#include <stan/math/prim/fun/constants.hpp>
#include <stan/math/prim/fun/erf.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <cmath>

namespace stan {
namespace math {

template <typename T>
inline fvar<T> erf(const fvar<T>& x) {
using std::exp;
return fvar<T>(erf(x.val_), x.d_ * exp(-square(x.val_)) * TWO_OVER_SQRT_PI);
}

Expand Down
4 changes: 2 additions & 2 deletions stan/math/fwd/fun/erfc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/exp.hpp>
#include <stan/math/fwd/fun/square.hpp>
#include <stan/math/prim/fun/constants.hpp>
#include <stan/math/prim/fun/erfc.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <cmath>

namespace stan {
namespace math {

template <typename T>
inline fvar<T> erfc(const fvar<T>& x) {
using std::exp;
return fvar<T>(erfc(x.val_), -x.d_ * exp(-square(x.val_)) * TWO_OVER_SQRT_PI);
}

Expand Down
3 changes: 2 additions & 1 deletion stan/math/fwd/fun/exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/cos.hpp>
#include <stan/math/fwd/fun/sin.hpp>
#include <stan/math/prim/fun/exp.hpp>
#include <cmath>
#include <complex>
Expand All @@ -11,7 +13,6 @@ namespace stan {
namespace math {
template <typename T>
inline fvar<T> exp(const fvar<T>& x) {
using std::exp;
return fvar<T>(exp(x.val_), x.d_ * exp(x.val_));
}

Expand Down
2 changes: 1 addition & 1 deletion stan/math/fwd/fun/exp2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/fun/exp2.hpp>
#include <stan/math/prim/fun/constants.hpp>
#include <stan/math/prim/fun/exp2.hpp>
#include <cmath>

namespace stan {
Expand Down
4 changes: 2 additions & 2 deletions stan/math/fwd/fun/expm1.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#ifndef STAN_MATH_FWD_FUN_EXPM1_HPP
#define STAN_MATH_FWD_FUN_EXPM1_HPP

#include <stan/math/prim/fun/expm1.hpp>
#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/exp.hpp>
#include <stan/math/prim/fun/expm1.hpp>
#include <cmath>

namespace stan {
namespace math {

template <typename T>
inline fvar<T> expm1(const fvar<T>& x) {
using std::exp;
return fvar<T>(expm1(x.val_), x.d_ * exp(x.val_));
}

Expand Down
Loading

0 comments on commit baae551

Please sign in to comment.