Skip to content

Commit b5703bc

Browse files
committed
new interface for calling composite functions with named arguments...
1 parent 426a209 commit b5703bc

File tree

12 files changed

+434
-174
lines changed

12 files changed

+434
-174
lines changed

examples/misc/quick_test.inl

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@
4343
#include <hydra/Lambda.h>
4444
#include <hydra/Parameter.h>
4545
#include <hydra/detail/Compose.h>
46+
#include <hydra/detail/Divide.h>
47+
#include <hydra/detail/Minus.h>
48+
#include <hydra/detail/Multiply.h>
49+
#include <hydra/detail/Sum.h>
4650
#include <hydra/functions/Gaussian.h>
47-
#include <hydra/functions/LogNormal.h>
51+
4852

4953

5054
#include <hydra/detail/external/hydra_thrust/random.h>
@@ -88,26 +92,40 @@ int main(int argv, char** argc)
8892
auto data = hydra::device::vector< double>(10, .0);
8993

9094
//Parameters
91-
auto mean = hydra::Parameter::Create("mean" ).Value(0.0);
92-
auto sigma = hydra::Parameter::Create("sigma" ).Value(1.0);
93-
auto factor = hydra::Parameter::Create("factor").Value(1.0);
95+
auto mean1 = hydra::Parameter::Create("mean1" ).Value(-1.0);
96+
auto mean2 = hydra::Parameter::Create("mean2" ).Value( 1.0);
97+
auto sigma = hydra::Parameter::Create("sigma" ).Value(1.0);
98+
auto factor = hydra::Parameter::Create("factor").Value(1.0);
9499

95100
//Gaussian distribution
96-
auto gauss = hydra::Gaussian<xvar>(mean, sigma);
101+
auto gauss1 = hydra::Gaussian<xvar>(mean1, sigma);
97102
//LogNormal distribution
98-
auto lognormal = hydra::LogNormal<yvar>(mean, sigma);
103+
auto gauss2 = hydra::Gaussian<yvar>(mean2, sigma);
99104
//
100105

101106
auto combiner = hydra::wrap_lambda( [] __hydra_dual__ (unsigned int npar, const hydra::Parameter* params, double x, double y) {
102107

103-
printf("gauss %f , log-gauss %f\n", x, y );
108+
printf("Gauss1 %f , Gauss2 %f\n", x, y );
104109
return x + params[0]*y;
105110
}, factor);
106111

107-
auto fcomposed = hydra::compose( combiner, gauss, lognormal);
112+
auto fcomposed = hydra::compose( combiner, gauss1, gauss2);
113+
auto fdivided = hydra::divide( gauss1, gauss2 );
114+
auto fminus = hydra::minus( gauss1, gauss2 );
115+
auto fmultiply = hydra::multiply( gauss1, gauss2 , gauss2 );
116+
auto fsum = hydra::sum( gauss1, gauss2 , gauss2 );
108117

109118
for(size_t i=0; i< data.size(); ++i)
110-
fcomposed(xvar(1.0), yvar(1.0));
119+
{
120+
std::cout << "fcomposed: " << fcomposed(xvar(-1.0), yvar(1.0)) << std::endl;
121+
std::cout << "fdivided: " << fdivided(xvar(-1.0), yvar(1.0)) << std::endl;
122+
std::cout << "fminus: " << fminus(xvar(-1.0), yvar(1.0)) << std::endl;
123+
std::cout << "fmultiply: " << fmultiply(xvar(-1.0), yvar(1.0)) << std::endl;
124+
std::cout << "fsum: " << fsum(xvar(-1.0), yvar(1.0)) << std::endl;
125+
126+
}
127+
128+
111129

112130
return 0;
113131
}

examples/numerical_integration/plain_mc.inl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@
6060
#include <hydra/device/System.h>
6161

6262

63+
declarg(AxisX, double)
64+
declarg(AxisY, double)
6365

66+
using namespace hydra::arguments;
6467

6568
int main(int argv, char** argc)
6669
{
@@ -104,7 +107,7 @@ int main(int argv, char** argc)
104107
}
105108

106109
// create functor using C++11 lambda
107-
auto GAUSSIAN = [=] __hydra_dual__ ( double x, double y ){
110+
auto GAUSSIAN = [=] __hydra_dual__ ( AxisX x, AxisY y ){
108111

109112
double g = 1.0;
110113
double f = 0.0;

hydra/Random.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include <hydra/detail/FunctorTraits.h>
4141
#include <hydra/detail/CompositeTraits.h>
4242
#include <hydra/detail/utility/Utility_Tuple.h>
43-
43+
#include <hydra/detail/ArgumentTraits.h>
4444
#include <hydra/detail/PRNGTypedefs.h>
4545

4646
#include <hydra/Range.h>
@@ -364,6 +364,25 @@ Range<Iterator> >::type
364364
sample(Iterator begin, Iterator end , std::array<double,N>const& min, std::array<double,N>const& max,
365365
Functor const& functor, size_t seed=0xb56c4feeef1b);
366366

367+
/**
368+
* @brief Fill a range with numbers distributed according a user defined distribution.
369+
* @param begin beginning of the range storing the generated values
370+
* @param end ending of the range storing the generated values
371+
* @param min tuple of lower limits of sampling region
372+
* @param max tuple of upper limits of sampling region.
373+
* @param functor distribution to be sampled
374+
* @return range with the generated values
375+
*/
376+
template<typename RNG=default_random_engine, typename Functor, typename Iterator>
377+
typename std::enable_if<
378+
detail::random::is_callable<Functor>::value &&
379+
detail::random::is_iterator<Iterator>::value &&
380+
detail::is_tuple_type< decltype(*std::declval<Iterator>())>::value,
381+
Range<Iterator> >::type
382+
sample(Iterator begin, Iterator end ,
383+
typename Functor::argument_type const& min, typename Functor::argument_type const& max,
384+
Functor const& functor, size_t seed=0xb56c4feeef1b);
385+
367386
/**
368387
* @brief Fill a range with numbers distributed according a user defined distribution.
369388
* @param policy backend to perform the calculation.
@@ -415,6 +434,24 @@ sample( Iterable&& output ,
415434
std::array<double,N>const& min, std::array<double,N>const& max,
416435
Functor const& functor, size_t seed=0xb56c4feeef1b);
417436

437+
/**
438+
* @brief Fill a range with numbers distributed according a user defined distribution.
439+
* @param output range storing the generated values
440+
* @param min tuple of lower limits of sampling region
441+
* @param max tuple of upper limits of sampling region.
442+
* @param functor distribution to be sampled
443+
* @return output range with the generated values
444+
*/
445+
template<typename RNG=default_random_engine, typename Functor, typename Iterable>
446+
typename std::enable_if<
447+
detail::random::is_callable<Functor>::value &&
448+
detail::random::is_iterable<Iterable>::value &&
449+
detail::is_tuple_type< decltype(*std::declval<Iterable>().begin())>::value ,
450+
Range< decltype(std::declval<Iterable>().begin())>>::type
451+
sample( Iterable&& output ,
452+
typename Functor::argument_type const& min,typename Functor::argument_type const& max,
453+
Functor const& functor, size_t seed=0xb56c4feeef1b);
454+
418455
/**
419456
* \ingroup random
420457
*

hydra/detail/Compose.h

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,39 +42,40 @@
4242
#include <hydra/Tuple.h>
4343
#include <hydra/detail/BaseCompositeFunctor.h>
4444
#include <hydra/detail/TupleUtility.h>
45-
45+
#include <hydra/detail/TupleTraits.h>
4646

4747
namespace hydra {
4848

4949

50-
namespace detail {
51-
52-
namespace compose_signature {
53-
54-
55-
56-
} // namespace compose_signature
57-
58-
} // namespace detail
59-
60-
6150
template<typename F0, typename F1, typename... Fs >
6251
class Compose: public BaseCompositeFunctor<
6352
Compose<F0, F1,Fs...>,
6453
hydra_thrust::tuple<F0, F1, Fs...>,
65-
typename detail::merged_tuple<
66-
hydra_thrust::tuple<typename F0::return_type>,
67-
typename F1::argument_type, typename Fs::argument_type ... >::type >
54+
typename detail::merged_tuple<
55+
hydra_thrust::tuple<typename F0::return_type>,
56+
typename detail::stripped_tuple<
57+
typename detail::merged_tuple<
58+
typename F1::argument_type,
59+
typename Fs::argument_type ...
60+
>::type
61+
>::type
62+
>::type
63+
>
6864
{
6965

70-
71-
typedef BaseCompositeFunctor<
72-
Compose<F0, F1,Fs...>,
73-
hydra_thrust::tuple<F0, F1, Fs...>,
74-
typename detail::merged_tuple< hydra_thrust::tuple<typename F0::return_type>,
75-
typename F1::argument_type, typename Fs::argument_type ... >::type > super_type;
76-
77-
66+
typedef BaseCompositeFunctor<
67+
Compose<F0, F1,Fs...>,
68+
hydra_thrust::tuple<F0, F1, Fs...>,
69+
typename detail::merged_tuple<
70+
hydra_thrust::tuple<typename F0::return_type>,
71+
typename detail::stripped_tuple<
72+
typename detail::merged_tuple<
73+
typename F1::argument_type,
74+
typename Fs::argument_type ...
75+
>::type
76+
>::type
77+
>::type
78+
> super_type;
7879

7980
public:
8081

@@ -120,10 +121,10 @@ class Compose: public BaseCompositeFunctor<
120121
// Conveniency function
121122
template < typename T0, typename T1, typename ...Ts >
122123
inline typename std::enable_if<
123-
(detail::is_hydra_functor<T0>::value || detail::is_hydra_lambda<T0>::value || detail::is_hydra_composite_functor<T0>::value) &&
124-
(detail::is_hydra_functor<T1>::value || detail::is_hydra_lambda<T1>::value || detail::is_hydra_composite_functor<T1>::value) &&
124+
(detail::is_hydra_functor<T0>::value || detail::is_hydra_lambda<T0>::value ) &&
125+
(detail::is_hydra_functor<T1>::value || detail::is_hydra_lambda<T1>::value ) &&
125126
detail::all_true<
126-
(detail::is_hydra_functor<Ts>::value || detail::is_hydra_lambda<Ts>::value || detail::is_hydra_composite_functor<Ts>::value)...>::value,
127+
(detail::is_hydra_functor<Ts>::value || detail::is_hydra_lambda<Ts>::value )...>::value,
127128
Compose<T0,T1,Ts...>>::type
128129
compose(T0 const& F0, T1 const& F1, Ts const&...Fs){
129130

hydra/detail/Divide.h

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,48 +41,74 @@
4141
#include <hydra/detail/utility/Utility_Tuple.h>
4242
#include <hydra/detail/base_functor.h>
4343
#include <hydra/detail/Constant.h>
44-
#include <hydra/detail/CompositeBase.h>
44+
#include <hydra/Complex.h>
4545
#include <hydra/detail/FunctorTraits.h>
46-
#include <hydra/detail/CompositeTraits.h>
4746
#include <hydra/Parameter.h>
4847
#include <hydra/Tuple.h>
48+
#include <hydra/detail/BaseCompositeFunctor.h>
49+
#include <hydra/detail/TupleUtility.h>
50+
#include <hydra/detail/TupleTraits.h>
4951

5052
namespace hydra {
5153

5254

5355
template<typename F1, typename F2 >
54-
class Divide: public detail::CompositeBase<F1, F2>
56+
class Divide : public BaseCompositeFunctor<
57+
Divide<F1, F2>,
58+
hydra_thrust::tuple<F1, F2>,
59+
typename detail::merged_tuple<
60+
hydra_thrust::tuple< decltype( std::declval<typename F1::return_type>()/std::declval<typename F2::return_type>() )>,
61+
typename detail::stripped_tuple<
62+
typename detail::merged_tuple<
63+
typename F1::argument_type,
64+
typename F2::argument_type
65+
>::type
66+
>::type
67+
>::type
68+
>
5569
{
70+
71+
typedef BaseCompositeFunctor<
72+
Divide<F1, F2>,
73+
hydra_thrust::tuple<F1, F2>,
74+
typename detail::merged_tuple<
75+
hydra_thrust::tuple< decltype( std::declval<typename F1::return_type>()/std::declval<typename F2::return_type>() )>,
76+
typename detail::stripped_tuple<
77+
typename detail::merged_tuple<
78+
typename F1::argument_type,
79+
typename F2::argument_type
80+
>::type
81+
>::type
82+
>::type
83+
> super_type;
84+
5685
public:
5786

58-
//tag
59-
typedef typename detail::divide_result<typename F1::return_type,
60-
typename F2::return_type >::type return_type;
6187

6288
Divide() = delete;
6389

6490
Divide(F1 const& f1, F2 const& f2):
65-
detail::CompositeBase<F1, F2>( f1, f2)
91+
super_type( f1, f2)
6692
{ }
6793

6894
__hydra_host__ __hydra_device__
6995
Divide(Divide<F1,F2> const& other):
70-
detail::CompositeBase<F1, F2>( other )
96+
super_type( other )
7197
{ }
7298

7399
__hydra_host__ __hydra_device__
74100
Divide<F1,F2>& operator=(Divide<F1,F2> const& other)
75101
{
76102
if(this==&other) return *this;
77-
detail::CompositeBase< F1, F2>::operator=( other);
103+
super_type::operator=( other);
78104
return *this;
79105
}
80106

81-
template<typename T1>
107+
template<typename ...T>
82108
__hydra_host__ __hydra_device__ inline
83-
return_type operator()(T1&& t ) const
109+
typename super_type::return_type Evaluate(T... x ) const
84110
{
85-
return hydra_thrust::get<0>(this->GetFunctors())(t)/hydra_thrust::get<1>(this->GetFunctors())(t);
111+
return hydra_thrust::get<0>(this->GetFunctors())( hydra::tie(x...) )/hydra_thrust::get<1>(this->GetFunctors())( hydra::tie(x...) );
86112
}
87113

88114

@@ -91,8 +117,8 @@ class Divide: public detail::CompositeBase<F1, F2>
91117
// divide: / operator two functors
92118
template<typename T1, typename T2>
93119
inline typename std::enable_if<
94-
(detail::is_hydra_functor<T1>::value || detail::is_hydra_lambda<T1>::value || detail::is_hydra_composite_functor<T1>::value) &&
95-
(detail::is_hydra_functor<T2>::value || detail::is_hydra_lambda<T2>::value || detail::is_hydra_composite_functor<T2>::value),
120+
(detail::is_hydra_functor<T1>::value || detail::is_hydra_lambda<T1>::value) &&
121+
(detail::is_hydra_functor<T2>::value || detail::is_hydra_lambda<T2>::value),
96122
Divide<T1, T2> >::type
97123
operator/(T1 const& F1, T2 const& F2)
98124
{
@@ -101,7 +127,7 @@ operator/(T1 const& F1, T2 const& F2)
101127

102128
template <typename T, typename U>
103129
inline typename std::enable_if<
104-
(detail::is_hydra_functor<T>::value || detail::is_hydra_lambda<T>::value || detail::is_hydra_composite_functor<T>::value) &&
130+
(detail::is_hydra_functor<T>::value || detail::is_hydra_lambda<T>::value ) &&
105131
(std::is_arithmetic<U>::value),
106132
Divide< Constant<U>, T> >::type
107133
operator/(U const cte, T const& F)
@@ -111,7 +137,7 @@ operator/(U const cte, T const& F)
111137

112138
template <typename T, typename U>
113139
inline typename std::enable_if<
114-
(detail::is_hydra_functor<T>::value || detail::is_hydra_lambda<T>::value || detail::is_hydra_composite_functor<T>::value) &&
140+
(detail::is_hydra_functor<T>::value || detail::is_hydra_lambda<T>::value ) &&
115141
(std::is_arithmetic<U>::value),
116142
Divide< Constant<U>, T> >::type
117143
operator/( T const& F, U cte)
@@ -121,7 +147,7 @@ operator/( T const& F, U cte)
121147

122148
template <typename T, typename U>
123149
inline typename std::enable_if<
124-
(detail::is_hydra_functor<T>::value || detail::is_hydra_lambda<T>::value || detail::is_hydra_composite_functor<T>::value) &&
150+
(detail::is_hydra_functor<T>::value || detail::is_hydra_lambda<T>::value ) &&
125151
(std::is_arithmetic<U>::value),
126152
Divide< Constant<hydra::complex<U>>, T> >::type
127153
operator/(hydra::complex<U> const& cte, T const& F)
@@ -131,7 +157,7 @@ operator/(hydra::complex<U> const& cte, T const& F)
131157

132158
template <typename T, typename U>
133159
inline typename std::enable_if<
134-
(detail::is_hydra_functor<T>::value || detail::is_hydra_lambda<T>::value || detail::is_hydra_composite_functor<T>::value) &&
160+
(detail::is_hydra_functor<T>::value || detail::is_hydra_lambda<T>::value ) &&
135161
(std::is_arithmetic<U>::value),
136162
Divide< Constant<hydra::complex<U>>, T> >::type
137163
operator/( T const& F, hydra::complex<U> const& cte)
@@ -143,8 +169,8 @@ operator/( T const& F, hydra::complex<U> const& cte)
143169
// Convenience function
144170
template <typename F1, typename F2, typename ...Fs>
145171
inline typename std::enable_if<
146-
(detail::is_hydra_functor<F1>::value || detail::is_hydra_lambda<F1>::value || detail::is_hydra_composite_functor<F1>::value) &&
147-
(detail::is_hydra_functor<F2>::value || detail::is_hydra_lambda<F2>::value || detail::is_hydra_composite_functor<F2>::value),
172+
(detail::is_hydra_functor<F1>::value || detail::is_hydra_lambda<F1>::value ) &&
173+
(detail::is_hydra_functor<F2>::value || detail::is_hydra_lambda<F2>::value ),
148174
Divide<F1, F2>>::type
149175
divide(F1 const& f1, F2 const& f2)
150176
{

0 commit comments

Comments
 (0)