MyraMath
UnaryFunctionExpression.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_UNARYFUNCTIONEXPRESSION_H
7 #define MYRAMATH_EXPRESSION_UNARYFUNCTIONEXPRESSION_H
8 
16 
18 
19 namespace myra {
20 namespace detail {
21 
22 // Implementation detail for composing Expression's using unary functions, think f(A)
23 template<class Function,int Arity> class UnaryFunctionExpression : public ExpressionBase<Arity,typename Function::Output>
24  {
25  public:
26 
27  // Reflect types from underlying Function.
28  typedef typename Function::Input Input;
29  typedef typename Function::Output Output;
30 
31  // Constructor, requires Function f and input Expression A.
32  UnaryFunctionExpression(const Function& in_f, const Expression<Arity,Input>& in_A)
33  : f(in_f), A(in_A) { }
34 
35  protected:
36 
37  // Size inspector.
38  virtual Index<Arity> size() const
39  { return A.size(); }
40 
41  // Given an Index i, returns f(A(i))
42  virtual Output evaluate(Index<Arity> i) const
43  { return f.evaluate( A.evaluate(i) ); }
44 
45  // Virtual copy constructor.
46  virtual ExpressionBase<Arity,Output>* clone() const
47  { return new UnaryFunctionExpression<Function,Arity>(f,A); }
48 
49  private:
50 
51  // Underlying Function instance.
52  Function f;
53 
54  // Underlying input Expression.
56 
57  };
58 
59 // Helper factory function, returns f(A).
60 template<class Function, int Arity>
62 make_UnaryFunctionExpression(const Function& f, const Expression<Arity,typename Function::Input>& A)
64 
65 } // namespace detail
66 
67 } // namespace myra
68 
69 #endif
Implementation detail, polymorphic base type contained/erased by Expression.
Returns a std::runtime_error() whose message has been populated using printf()-style formatting...
Number evaluate(Index< Arity > i) const
Given an Index i, returns a Number.
Definition: Expression.cpp:46
Definition: UnaryFunctionExpression.h:23
Definition: Expression.h:25
Definition: syntax.dox:1
Definition: random.cpp:45
virtual Index< Arity > size() const
Size inspector.
Definition: UnaryFunctionExpression.h:38
virtual Output evaluate(Index< Arity > i) const
Given an Index i, returns a Number.
Definition: UnaryFunctionExpression.h:42
Index< Arity > size() const
Size inspector.
Definition: Expression.cpp:42
Returns std::string representation of an Index<N>