MyraMath
BinaryFunctionExpression.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_EXPRESSION_BINARYFUNCTIONEXPRESSION_H
7 #define MYRAMATH_EXPRESSION_BINARYFUNCTIONEXPRESSION_H
8 
16 
18 
19 namespace myra {
20 namespace detail {
21 
22 // Implementation detail for composing Expression's using binary functions, think f(A,B)
23 template<class Function,int Arity> class BinaryFunctionExpression : public ExpressionBase<Arity,typename Function::Output>
24  {
25  public:
26 
27  // Reflect types from underlying Function.
28  typedef typename Function::InputA InputA;
29  typedef typename Function::InputB InputB;
30  typedef typename Function::Output Output;
31 
32  // Constructor, requires Function f and input Expression's (A,B).
33  BinaryFunctionExpression(const Function& in_f, const Expression<Arity,InputA>& in_A, const Expression<Arity,InputB>& in_B)
34  : f(in_f), A(in_A), B(in_B)
35  {
36  // Verify that A and B are the same size.
37  Index<Arity> A_size = A.size();
38  Index<Arity> B_size = B.size();
39  if (A_size != B_size)
40  {
41  std::string A_string = Index2string(A_size);
42  std::string B_string = Index2string(B_size);
43  throw eprintf("BinaryFunctionExpression: size mismatch forming f(A,B) [%s != %s]", A_string.c_str(), B_string.c_str() );
44  }
45  }
46 
47  protected:
48 
49  // Size inspector.
50  virtual Index<Arity> size() const
51  { return A.size(); }
52 
53  // Given an Index i, returns f(A(i),B(i))
54  virtual Output evaluate(Index<Arity> i) const
55  { return f.evaluate( A.evaluate(i), B.evaluate(i) ); }
56 
57  // Virtual copy constructor.
58  virtual ExpressionBase<Arity,Output>* clone() const
59  { return new BinaryFunctionExpression<Function,Arity>(f,A,B); }
60 
61  private:
62 
63  // Underlying Function instance.
64  Function f;
65 
66  // Underlying input Expression's.
69 
70  };
71 
72 // Helper factory function, returns f(A,B).
73 template<class Function, int Arity>
75 make_BinaryFunctionExpression(const Function& f, const Expression<Arity,typename Function::InputA>& A, const Expression<Arity,typename Function::InputB>& B)
77 
78 } // namespace detail
79 
80 } // namespace myra
81 
82 #endif
Definition: BinaryFunctionExpression.h:23
Implementation detail, polymorphic base type contained/erased by Expression.
virtual Output evaluate(Index< Arity > i) const
Given an Index i, returns a Number.
Definition: BinaryFunctionExpression.h:54
Returns a std::runtime_error() whose message has been populated using printf()-style formatting...
virtual Index< Arity > size() const
Size inspector.
Definition: BinaryFunctionExpression.h:50
Number evaluate(Index< Arity > i) const
Given an Index i, returns a Number.
Definition: Expression.cpp:46
Definition: Expression.h:25
Definition: syntax.dox:1
Definition: random.cpp:45
Index< Arity > size() const
Size inspector.
Definition: Expression.cpp:42
Returns std::string representation of an Index<N>