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

Untangles all of our includes! #254

Merged
merged 30 commits into from
Mar 16, 2016
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
29c62a4
Updating makefile help
syclik Mar 3, 2016
17ea996
Updating comment in fwd/mat/functor/gradient.hpp
syclik Mar 3, 2016
a2f475d
Removing prim/arr header from prim/scal
syclik Mar 3, 2016
a3fb372
Fixing prim/scal/meta/scalar_type.hpp includes and adding mat test
syclik Mar 3, 2016
2563d58
Updating tests
syclik Mar 4, 2016
a846164
Updating includes
syclik Mar 4, 2016
cb731ec
Adding more to VectorBuilder and VectorBuilderHelper; added tests
syclik Mar 4, 2016
e86e3c8
Untangling more includes
syclik Mar 4, 2016
688acac
Cleaning up includes for neg_binomial_2_cdf functions
syclik Mar 4, 2016
d04dab1
Fixing includes on coupled_ode_system
syclik Mar 5, 2016
bb5b685
Splitting apart prim/mat/err/check_ordered_test.cpp
syclik Mar 5, 2016
ddccb14
Updating integrate_ode
syclik Mar 5, 2016
7dfd17e
Splitting apart OperandsAndPartials_test
syclik Mar 5, 2016
7a3af35
Moving VectorViewMvt to prim/mat/meta
syclik Mar 7, 2016
5ab6556
Reimplementing VectorView
syclik Mar 8, 2016
270c08a
Fixing more test includes
syclik Mar 9, 2016
02d7a88
Splitting apart OperandsAndPartials
syclik Mar 10, 2016
55e70b0
Including header for is_constant_struct
syclik Mar 10, 2016
25240a1
Fixing check_nonzero_size
syclik Mar 11, 2016
5653b99
Fixing doxygen
syclik Mar 11, 2016
7a7e69d
Fixing more includes
syclik Mar 11, 2016
a0b4cea
Cpplint
syclik Mar 11, 2016
1131b6c
Included stdexcept
syclik Mar 11, 2016
52b1b6d
Squashing a compiler warning in a test
syclik Mar 11, 2016
57ab0ed
Fixed one last OperandsAndPartials thing
syclik Mar 11, 2016
55690fc
Fixing use of VectorBuilder
syclik Mar 12, 2016
9ca5021
Fixing neg_binonial_2
syclik Mar 12, 2016
d3f05ae
cpplint
syclik Mar 13, 2016
ddc580d
Moving check_nonzero_size to arr and fixing behavior to act like it d…
syclik Mar 14, 2016
7e49dc2
added to VectorView doc
Mar 16, 2016
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
Splitting apart OperandsAndPartials
  • Loading branch information
syclik committed Mar 10, 2016
commit 02d7a882f30bfa9b12af194391258a0b1282f55b
1 change: 1 addition & 0 deletions stan/math/fwd/scal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/scal/meta/is_fvar.hpp>
#include <stan/math/fwd/scal/meta/partials_type.hpp>
#include <stan/math/fwd/scal/meta/OperandsAndPartials.hpp>

#include <stan/math/prim/scal.hpp>

Expand Down
188 changes: 188 additions & 0 deletions stan/math/fwd/scal/meta/OperandsAndPartials.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#ifndef STAN_MATH_FWD_SCAL_META_OPERANDSANDPARTIALS_HPP
#define STAN_MATH_FWD_SCAL_META_OPERANDSANDPARTIALS_HPP

#include <stan/math/prim/scal/meta/is_constant_struct.hpp>
#include <stan/math/prim/scal/meta/length.hpp>
#include <stan/math/prim/scal/meta/OperandsAndPartials.hpp>
#include <stan/math/fwd/core.hpp>

namespace stan {
namespace math {

// These are helpers to the OperandsAndPartials specialization for
// stan::math::fvar
namespace {
template <typename T_derivative,
typename T,
typename T_partials,
bool is_vec = is_vector<T>::value,
bool is_const = is_constant_struct<T>::value>
struct increment_derivative {
inline T_derivative operator()(const T& x,
const T_partials& d_dx) {
return 0;
}
};

template <typename T_derivative,
typename T,
typename T_partials>
struct increment_derivative<T_derivative, T, T_partials, false, false> {
inline T_derivative operator()(const T& x,
const T_partials& d_dx) {
return d_dx[0] * x.d_;
}
};

template <typename T_derivative,
typename T,
typename T_partials>
struct increment_derivative<T_derivative, T, T_partials, true, false> {
inline T_derivative operator()(const T& x,
const T_partials& d_dx) {
T_derivative derivative(0);
for (size_t n = 0; n < length(x); n++)
derivative += d_dx[n] * x[n].d_;
return derivative;
}
};

template <typename T,
typename T1, typename D1, typename T2, typename D2,
typename T3, typename D3, typename T4, typename D4,
typename T5, typename D5, typename T6, typename D6>
fvar<T> partials_to_fvar(T& logp,
const T1& x1, D1& d_x1,
const T2& x2, D2& d_x2,
const T3& x3, D3& d_x3,
const T4& x4, D4& d_x4,
const T5& x5, D5& d_x5,
const T6& x6, D6& d_x6) {
T deriv = 0;
if (!is_constant_struct<T1>::value)
deriv += increment_derivative<T, T1, D1>()(x1, d_x1);
if (!is_constant_struct<T2>::value)
deriv += increment_derivative<T, T2, D2>()(x2, d_x2);
if (!is_constant_struct<T3>::value)
deriv += increment_derivative<T, T3, D3>()(x3, d_x3);
if (!is_constant_struct<T4>::value)
deriv += increment_derivative<T, T4, D4>()(x4, d_x4);
if (!is_constant_struct<T5>::value)
deriv += increment_derivative<T, T5, D5>()(x5, d_x5);
if (!is_constant_struct<T6>::value)
deriv += increment_derivative<T, T6, D6>()(x6, d_x6);
return stan::math::fvar<T>(logp, deriv);
}
}


/**
* This class builds partial derivatives with respect to a set of
* operands. There are two reason for the generality of this
* class. The first is to handle vector and scalar arguments
* without needing to write additional code. The second is to use
* this class for writing probability distributions that handle
* primitives, reverse mode, and forward mode variables
* seamlessly.
*
* This is the partial template specialization for when the return
* type is stan::math::fvar<T>.
*
* @tparam T1 First set of operands.
* @tparam T2 Second set of operands.
* @tparam T3 Third set of operands.
* @tparam T4 Fourth set of operands.
* @tparam T5 Fifth set of operands.
* @tparam T6 Sixth set of operands.
* @tparam T_return_type Return type of the expression. This defaults
* to a template metaprogram that calculates the scalar promotion of
* T1 -- T6.
*/
template<typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6,
typename T_partials_return>
struct OperandsAndPartials<T1, T2, T3, T4, T5, T6,
typename stan::math::fvar<T_partials_return> > {
typedef typename stan::math::fvar<T_partials_return> T_return_type;

const T1& x1_;
const T2& x2_;
const T3& x3_;
const T4& x4_;
const T5& x5_;
const T6& x6_;

size_t n_partials;
T_partials_return* all_partials;


VectorView<T_partials_return,
is_vector<T1>::value,
is_constant_struct<T1>::value> d_x1;
VectorView<T_partials_return,
is_vector<T2>::value,
is_constant_struct<T2>::value> d_x2;
VectorView<T_partials_return,
is_vector<T3>::value,
is_constant_struct<T3>::value> d_x3;
VectorView<T_partials_return,
is_vector<T4>::value,
is_constant_struct<T4>::value> d_x4;
VectorView<T_partials_return,
is_vector<T5>::value,
is_constant_struct<T5>::value> d_x5;
VectorView<T_partials_return,
is_vector<T6>::value,
is_constant_struct<T6>::value> d_x6;

OperandsAndPartials(const T1& x1 = 0, const T2& x2 = 0, const T3& x3 = 0,
const T4& x4 = 0, const T5& x5 = 0, const T6& x6 = 0)
: x1_(x1), x2_(x2), x3_(x3), x4_(x4), x5_(x5), x6_(x6),
n_partials(!is_constant_struct<T1>::value * length(x1) +
!is_constant_struct<T2>::value * length(x2) +
!is_constant_struct<T3>::value * length(x3) +
!is_constant_struct<T4>::value * length(x4) +
!is_constant_struct<T5>::value * length(x5) +
!is_constant_struct<T6>::value * length(x6)),
all_partials(new T_partials_return[n_partials]),
Copy link
Member Author

Choose a reason for hiding this comment

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

@bob-carpenter, this used to look like this:

all_partials(static_cast<T_partials_return*>
                       (vari::operator new
                        (sizeof(T_partials_return) * n_partials)))

Should I have left it alone? I was trying to trace back to a way to allocate on the right managed stack without having to touch anything in reverse mode (stan::vari::operator new is in reverse mode).

d_x1(all_partials),
d_x2(all_partials
+ (!is_constant_struct<T1>::value) * length(x1)),
d_x3(all_partials
+ (!is_constant_struct<T1>::value) * length(x1)
+ (!is_constant_struct<T2>::value) * length(x2)),
d_x4(all_partials
+ (!is_constant_struct<T1>::value) * length(x1)
+ (!is_constant_struct<T2>::value) * length(x2)
+ (!is_constant_struct<T3>::value) * length(x3)),
d_x5(all_partials
+ (!is_constant_struct<T1>::value) * length(x1)
+ (!is_constant_struct<T2>::value) * length(x2)
+ (!is_constant_struct<T3>::value) * length(x3)
+ (!is_constant_struct<T4>::value) * length(x4)),
d_x6(all_partials
+ (!is_constant_struct<T1>::value) * length(x1)
+ (!is_constant_struct<T2>::value) * length(x2)
+ (!is_constant_struct<T3>::value) * length(x3)
+ (!is_constant_struct<T4>::value) * length(x4)
+ (!is_constant_struct<T5>::value) * length(x5)) {
std::fill(all_partials, all_partials + n_partials, 0);
}

T_return_type
value(T_partials_return value) {
return partials_to_fvar(value,
x1_, d_x1, x2_, d_x2,
x3_, d_x3, x4_, d_x4,
x5_, d_x4, x6_, d_x5);
}

~OperandsAndPartials() {
delete all_partials;
}
};


}
}
#endif
2 changes: 2 additions & 0 deletions stan/math/mix/scal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/scal/meta/is_fvar.hpp>
#include <stan/math/fwd/scal/meta/partials_type.hpp>
#include <stan/math/fwd/scal/meta/OperandsAndPartials.hpp>

#include <stan/math/rev/core.hpp>
#include <stan/math/rev/scal/meta/is_var.hpp>
#include <stan/math/rev/scal/meta/partials_type.hpp>
#include <stan/math/rev/scal/meta/OperandsAndPartials.hpp>

#include <stan/math/prim/scal.hpp>
#include <stan/math/fwd/scal.hpp>
Expand Down
3 changes: 1 addition & 2 deletions stan/math/prim/arr/meta/VectorBuilderHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ namespace stan {
explicit VectorBuilderHelper(size_t n) : x_(n) { }

typedef std::vector<T1> type;

T1& operator[](size_t i) {
return x_[i];
}

inline type& data() {
return x_;
}

};
}
#endif
8 changes: 4 additions & 4 deletions stan/math/prim/arr/meta/VectorView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace stan {
class VectorView<std::vector<T>, true, false> {
public:
typedef typename scalar_type<T>::type scalar_t;

template <typename X>
explicit VectorView(X& x) : x_(&x[0]) { }

scalar_t& operator[](int i) {
return x_[i];
}
Expand All @@ -27,10 +27,10 @@ namespace stan {
public:
typedef typename boost::add_const<typename scalar_type<T>::type>::type
scalar_t;

template <typename X>
explicit VectorView(X& x) : x_(&x[0]) { }

scalar_t& operator[](int i) const {
return x_[i];
}
Expand Down
8 changes: 4 additions & 4 deletions stan/math/prim/mat/meta/VectorView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace stan {
class VectorView<Eigen::Matrix<T, R, C>, true, false> {
public:
typedef typename scalar_type<T>::type scalar_t;

template <typename X>
explicit VectorView(X& x) : x_(x.data()) { }

scalar_t& operator[](int i) {
return x_[i];
}
Expand All @@ -28,10 +28,10 @@ namespace stan {
public:
typedef typename boost::add_const<typename scalar_type<T>::type>::type
scalar_t;

template <typename X>
explicit VectorView(X& x) : x_(x.data()) { }

scalar_t& operator[](int i) const {
return x_[i];
}
Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/mat/prob/multi_student_t_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#include <stan/math/prim/scal/fun/log1p.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/meta/include_summand.hpp>
#include <cmath>
#include <cstdlib>
#include <boost/math/special_functions/gamma.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/random/variate_generator.hpp>
#include <cmath>
#include <cstdlib>

namespace stan {
namespace math {
Expand Down
Loading