MyraMath
UserExpression


Source: tests/expression/UserExpression.cpp

1 
2 // ========================================================================= //
3 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
4 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
5 // ========================================================================= //
6 
15 
17 
18 // Reporting.
19 #include <tests/myratest.h>
20 
21 using namespace myra;
22 
24 class PascalExpression
25  {
26  public:
27 
28  // Required typedefs/constants.
29  const static int Arity=2;
30  typedef double Number;
31 
32  // Constructor, requires size info.
33  PascalExpression(int in_N)
34  : N(in_N) { }
35 
36  // Size inspector.
37  Index<Arity> size() const
38  { return make_Index(N,N); }
39 
40  // Evaluate method.
41  double evaluate(Index<Arity> ij) const
42  {
43  int i = ij[0];
44  int j = ij[1];
45  return choose(i+j,i);
46  }
47 
48  private:
49 
50  // Implementation detail of evaluate()
51  static double choose(int n, int k)
52  {
53  double answer = 1.0;
54  for (int i = 1; i <= k; ++i)
55  {
56  answer *= static_cast<double>(n-k+i);
57  answer /= static_cast<double>(i);
58  }
59  return answer;
60  }
61 
62  // Size, a Pascal matrix is always square.
63  int N;
64 
65  };
66 
67 ADD_TEST("UserExpression","[expression]")
68  {
69  auto E = make_UserExpression( PascalExpression(10) );
70  myra::out() << E << std::endl;
71  // TODO compute/interrogate the Cholesky triangle of E, it's very structured.
72  }
Adapts user code (encapsulated in a class) into an Expression.
Expression< UserClass::Arity, typename UserClass::Number > make_UserExpression(const UserClass &u)
Helper function to adapt some user code (encapsulated in a class) into an Expression.
Definition: UserExpression.h:58
Implementation detail of Expression templates.
An interface used to fill containers from Expression&#39;s (see Matrix::evaluate(), for example)...
Definition: syntax.dox:1


Results: [PASS]

size 10 by 10 arity-2 Expression of double:
[ 1 1 1 1 1 1 1 1 1 1 ]
[ 1 2 3 4 5 6 7 8 9 10 ]
[ 1 3 6 10 15 21 28 36 45 55 ]
[ 1 4 10 20 35 56 84 120 165 220 ]
[ 1 5 15 35 70 126 210 330 495 715 ]
[ 1 6 21 56 126 252 462 792 1287 2002 ]
[ 1 7 28 84 210 462 924 1716 3003 5005 ]
[ 1 8 36 120 330 792 1716 3432 6435 11440 ]
[ 1 9 45 165 495 1287 3003 6435 12870 24310 ]
[ 1 10 55 220 715 2002 5005 11440 24310 48620 ]


Go back to Summary of /test programs.