MyraMath
TernaryFunctionExpression.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_TERNARYFUNCTIONEXPRESSION_H
7 #define MYRAMATH_EXPRESSION_TERNARYFUNCTIONEXPRESSION_H
8 
16 
18 
19 namespace myra {
20 namespace detail {
21 
22 // Implementation detail for composing Expression's using ternary functions, think f(A,B,C)
23 template<class Function,int Arity> class TernaryFunctionExpression : 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::InputC InputC;
31  typedef typename Function::Output Output;
32 
33  // Constructor, requires Function f and input Expression's (A,B,C).
34  TernaryFunctionExpression(const Function& in_f, const Expression<Arity,InputA>& in_A, const Expression<Arity,InputB>& in_B, const Expression<Arity,InputC>& in_C)
35  : f(in_f), A(in_A), B(in_B), C(in_C)
36  {
37  // Verify that A and B and C are the same size.
38  Index<Arity> A_size = A.size();
39  Index<Arity> B_size = B.size();
40  Index<Arity> C_size = C.size();
41  if (A_size != B_size || B_size != C_size)
42  {
43  std::string A_string = Index2string(A_size);
44  std::string B_string = Index2string(B_size);
45  std::string C_string = Index2string(C_size);
46  throw eprintf("TernaryFunctionExpression: size mismatch forming f(A,B,C) [%s,%s,%s]", A_string.c_str(), B_string.c_str(), C_string.c_str() );
47  }
48  }
49 
50  protected:
51 
52  // Size inspector.
53  virtual Index<Arity> size() const
54  { return A.size(); }
55 
56  // Given an Index i, returns f(A(i),B(i),C(i))
57  virtual Output evaluate(Index<Arity> i) const
58  { return f.evaluate( A.evaluate(i), B.evaluate(i), C.evaluate(i) ); }
59 
60  // Virtual copy constructor.
61  virtual ExpressionBase<Arity,Output>* clone() const
62  { return new TernaryFunctionExpression<Function,Arity>(f,A,B,C); }
63 
64  private:
65 
66  // Underlying Function instance.
67  Function f;
68 
69  // Underlying input Expression's.
73  };
74 
75 // Helper factory function, returns f(A,B,C).
76 template<class Function, int Arity>
78 make_TernaryFunctionExpression(const Function& f, const Expression<Arity,typename Function::InputA>& A, const Expression<Arity,typename Function::InputB>& B, const Expression<Arity,typename Function::InputC>& C)
80 
81 } // namespace detail
82 
83 } // namespace myra
84 
85 #endif
Implementation detail, polymorphic base type contained/erased by Expression.
Returns a std::runtime_error() whose message has been populated using printf()-style formatting...
Definition: TernaryFunctionExpression.h:23
Number evaluate(Index< Arity > i) const
Given an Index i, returns a Number.
Definition: Expression.cpp:46
virtual Index< Arity > size() const
Size inspector.
Definition: TernaryFunctionExpression.h:53
virtual Output evaluate(Index< Arity > i) const
Given an Index i, returns a Number.
Definition: TernaryFunctionExpression.h:57
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>