3 #include <funcy/concepts.h> 4 #include <funcy/util/chainer.h> 5 #include <funcy/util/compute_sum.h> 6 #include <funcy/util/derivative_wrappers.h> 7 #include <funcy/util/evaluate_if_present.h> 8 #include <funcy/util/mathop_traits.h> 9 #include <funcy/util/type_traits.h> 19 template < Function F, Function G >
20 struct Sum : Chainer< Sum< F, G > >
27 constexpr
Sum(
const F& f_,
const G& g_ )
28 : f( f_ ), g( g_ ), value( add_via_traits( f(), g() ) )
37 constexpr
Sum( F&& f_,
const G& g_ )
38 : f( std::move( f_ ) ), g( g_ ), value( add_via_traits( f(), g() ) )
47 constexpr
Sum(
const F& f_, G&& g_ )
48 : f( f_ ), g( std::move( g_ ) ), value( add_via_traits( f(), g() ) )
57 constexpr
Sum( F&& f_, G&& g_ )
58 : f( std::move( f_ ) ), g( std::move( g_ ) ), value( add_via_traits( f(), g() ) )
67 template <
class InitF,
class InitG >
68 constexpr
Sum( InitF&& f_, InitG&& g_ ) requires(
69 std::is_constructible_v< F, std::decay_t< InitF > >&&
70 std::is_constructible_v< G, std::decay_t< InitG > > &&
71 !Function< InitF > && !Function< InitG > )
72 : f( std::forward< InitF >( f_ ) ), g( std::forward< InitG >( g_ ) ),
73 value( add_via_traits( f(), g() ) )
78 template <
class Arg >
81 update_if_present( f, x );
82 update_if_present( g, std::forward< Arg >( x ) );
83 value = add_via_traits( f(), g() );
87 template <
int index,
class Arg >
90 update_if_present< index >( f, x );
91 update_if_present< index >( g, std::forward< Arg >( x ) );
92 value = add_via_traits( f(), g() );
95 template <
class... IndexedArgs >
96 void bulk_update( IndexedArgs&&... args )
98 bulk_update_if_present( f, args... );
99 bulk_update_if_present( g, std::forward< IndexedArgs >( args )... );
100 value = add_via_traits( f(), g() );
104 constexpr decltype(
auto ) d0()
const noexcept
110 template <
int id,
class Arg,
class IndexedArg = IndexedType< std::decay_t< Arg >,
id > >
111 auto d1( Arg&& dx )
const 112 requires( ComputeSum< D1< F, IndexedArg >, D1< G, IndexedArg > >::present )
114 return ComputeSum< D1< F, IndexedArg >, D1< G, IndexedArg > >(
115 f, g, std::forward< Arg >( dx ) )();
119 template <
int idx,
int idy,
class ArgX,
class ArgY,
122 auto d2( ArgX&& dx, ArgY&& dy )
const 123 requires( ComputeSum< D2< F, IndexedArgX, IndexedArgY >,
124 D2< G, IndexedArgX, IndexedArgY > >::present )
126 return ComputeSum< D2< F, IndexedArgX, IndexedArgY >,
127 D2< G, IndexedArgX, IndexedArgY > >(
128 f, g, std::forward< ArgX >( dx ), std::forward< ArgY >( dy ) )();
132 template <
int idx,
int idy,
int idz,
class ArgX,
class ArgY,
class ArgZ,
136 auto d3( ArgX&& dx, ArgY&& dy, ArgZ&& dz )
const 137 requires( ComputeSum< D3< F, IndexedArgX, IndexedArgY, IndexedArgZ >,
138 D3< G, IndexedArgX, IndexedArgY, IndexedArgZ > >::present )
140 return ComputeSum< D3< F, IndexedArgX, IndexedArgY, IndexedArgZ >,
141 D3< G, IndexedArgX, IndexedArgY, IndexedArgZ > >(
142 f, g, std::forward< ArgX >( dx ), std::forward< ArgY >( dy ),
143 std::forward< ArgZ >( dz ) )();
constexpr Sum(F &&f_, const G &g_)
Constructor.
Definition: sum.h:37
constexpr Sum(InitF &&f_, InitG &&g_) requires(std
Constructor.
Definition: sum.h:68
auto d3(ArgX &&dx, ArgY &&dy, ArgZ &&dz) const requires(ComputeSum< D3< F
Third directional derivative.
Sum of functions of type F and G.
Definition: sum.h:20
void update(Arg &&x)
Update point of evaluation.
Definition: sum.h:79
Mathematical operations and corresponding differentation rules.
auto d2(ArgX &&dx, ArgY &&dy) const requires(ComputeSum< D2< F
Second directional derivative.
constexpr Sum(const F &f_, G &&g_)
Constructor.
Definition: sum.h:47
typename Decay< std::decay_t< F > >::type decay_t
Access underlying type (if it is hidden by expression templates).
Definition: type_traits.h:47
void update(Arg &&x)
Update variable corresponding to index.
Definition: sum.h:88
constexpr Sum(const F &f_, const G &g_)
Constructor.
Definition: sum.h:27
Definition: type_traits.h:54
constexpr Sum(F &&f_, G &&g_)
Constructor.
Definition: sum.h:57
auto d1(Arg &&dx) const requires(ComputeSum< D1< F
First directional derivative.