Skip to content

Commit bbdad92

Browse files
committed
make a broadcast_array for rev and cleanup includes
1 parent f38996f commit bbdad92

6 files changed

Lines changed: 48 additions & 7 deletions

File tree

stan/math/fwd/functor/broadcast_array.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#ifndef STAN_MATH_FWD_META_BROADCAST_ARRAY_HPP
2-
#define STAN_MATH_FWD_META_BROADCAST_ARRAY_HPP
1+
#ifndef STAN_MATH_FWD_FUNCTOR_BROADCAST_ARRAY_HPP
2+
#define STAN_MATH_FWD_FUNCTOR_BROADCAST_ARRAY_HPP
33

44
#include <stan/math/prim/fun/Eigen.hpp>
55
#include <stan/math/fwd/fun/sum.hpp>
6-
#include <stan/math/prim/functor/broadcast_array_fwd.hpp>
6+
#include <stan/math/prim/functor/broadcast_array.hpp>
77
#include <stan/math/prim/meta.hpp>
88

99
namespace stan {

stan/math/prim/functor/broadcast_array.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef STAN_MATH_PRIM_META_BROADCAST_ARRAY_HPP
2-
#define STAN_MATH_PRIM_META_BROADCAST_ARRAY_HPP
1+
#ifndef STAN_MATH_PRIM_FUNCTOR_BROADCAST_ARRAY_HPP
2+
#define STAN_MATH_PRIM_FUNCTOR_BROADCAST_ARRAY_HPP
33

44
#include <stan/math/prim/functor/broadcast_array_fwd.hpp>
55
#include <stan/math/prim/meta/is_eigen.hpp>
@@ -16,7 +16,7 @@ namespace math {
1616
namespace internal {
1717

1818
template <typename T>
19-
class broadcast_array<T, require_t<is_var_or_arithmetic<T>>> {
19+
class broadcast_array<T, require_st_arithmetic<T>> {
2020
private:
2121
std::reference_wrapper<T> prim_;
2222

stan/math/rev/functor.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <stan/math/rev/functor/apply_scalar_unary.hpp>
99
#include <stan/math/rev/functor/apply_scalar_binary.hpp>
1010
#include <stan/math/rev/functor/apply_vector_unary.hpp>
11+
#include <stan/math/rev/functor/broadcast_array.hpp>
1112
#include <stan/math/rev/functor/coupled_ode_system.hpp>
1213
#include <stan/math/rev/functor/cvodes_integrator.hpp>
1314
#include <stan/math/rev/functor/cvodes_utils.hpp>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef STAN_MATH_REV_FUNCTOR_BROADCAST_ARRAY_HPP
2+
#define STAN_MATH_REV_FUNCTOR_BROADCAST_ARRAY_HPP
3+
4+
#include <stan/math/prim/fun/Eigen.hpp>
5+
#include <stan/math/rev/fun/sum.hpp>
6+
#include <stan/math/prim/functor/broadcast_array.hpp>
7+
#include <stan/math/prim/meta.hpp>
8+
9+
namespace stan {
10+
namespace math {
11+
namespace internal {
12+
13+
template <typename T>
14+
class broadcast_array<T, require_st_var<T>> {
15+
private:
16+
std::reference_wrapper<T> prim_;
17+
18+
public:
19+
template <typename TT>
20+
explicit broadcast_array(TT&& prim) : prim_(std::forward<TT>(prim)) {}
21+
22+
T& operator[](int /*i*/) { return prim_.get(); }
23+
24+
/** \ingroup type_trait
25+
* Broadcast array can be assigned a scalar or a vector. If assigned a scalar,
26+
* it will be used directly. If assigned a vector, the argument will be summed
27+
* first.
28+
*/
29+
template <typename Y>
30+
void operator=(const Y& m) {
31+
prim_.get() = sum(m);
32+
}
33+
};
34+
35+
} // namespace internal
36+
} // namespace math
37+
} // namespace stan
38+
#endif

stan/math/rev/functor/operands_and_partials.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
#include <stan/math/rev/core/var.hpp>
88
#include <stan/math/rev/core/vari.hpp>
99
#include <stan/math/rev/core/typedefs.hpp>
10+
#include <stan/math/rev/functor/broadcast_array.hpp>
1011
#include <stan/math/prim/meta/is_eigen.hpp>
1112
#include <stan/math/prim/meta/is_vector_like.hpp>
1213
#include <stan/math/prim/meta/compiler_attributes.hpp>
1314
#include <stan/math/prim/meta/promote_scalar_type.hpp>
1415
#include <stan/math/prim/fun/size.hpp>
15-
#include <stan/math/prim/functor/broadcast_array.hpp>
1616
#include <stan/math/prim/functor/operands_and_partials.hpp>
17+
#include <stan/math/prim/functor/broadcast_array.hpp>
1718
#include <stan/math/prim/functor/apply.hpp>
1819
#include <stan/math/prim/functor/for_each.hpp>
1920
#include <vector>

stan/math/rev/functor/partials_propagator.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stan/math/rev/core/var.hpp>
66
#include <stan/math/rev/core/typedefs.hpp>
77
#include <stan/math/rev/functor/operands_and_partials.hpp>
8+
#include <stan/math/rev/functor/broadcast_array.hpp>
89
#include <stan/math/prim/functor/for_each.hpp>
910
#include <vector>
1011
#include <tuple>

0 commit comments

Comments
 (0)