WaveBlocksND
evaluations.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <functional>
5 
6 #include "../types.hpp"
7 
8 
9 namespace waveblocks {
10  namespace utilities {
11  template < int N, int C,
12  template <typename, int, int> class M,
13  class A,
14  class R,
15  template <typename...> class F = std::function >
17  static M<R, N, C> apply( const M<F<R( A )> , N, C>& mf, const A& arg )
18  {
19  M<R, N, C> m;
20 
21  for ( int i = 0; i < N; ++i ) {
22  for ( int j = 0; j < C; ++j ) {
23  m( i, j ) = mf( i, j )( arg );
24  }
25  }
26 
27  return m;
28  }
29  template<template <typename...> class G_in = std::vector,
30  template <typename...> class G_out = G_in>
31  static G_out<M<R, N, C> >
32  in_grid( const M<F<R( A )>, N, C>& mf,
33  G_in<A> g )
34  {
35  G_out<M<R, N, C> > result( g.size() );
36  auto it = result.begin();
37 
38  for ( const auto & arg : g ) {
39  *it = apply( mf, arg );
40  ++it;
41  }
42 
43  return result;
44  }
45 
46  };
47 
48  template < template <typename, int, int> class M,
49  class A,
50  class R,
51  template <typename...> class F>
52  struct FunctionMatrixEvaluator<1,1,M,A,R,F> {
53  static R apply(const F<R(A)>& f, const A& arg) {
54  return f(arg);
55  }
56  static M<R, 1,1> apply(const M<F<R( A )>, 1,1 >& mf, const A& arg )
57  {
58  M<R, 1, 1> m;
59  m( 0,0 ) = mf( 0,0 )( arg );
60 
61  return m;
62  }
63 
64  template< template <typename...> class G_in = std::vector,
65  template <typename...> class G_out = G_in>
66  static G_out<M<R, 1,1> >
67  in_grid(const M<F<R( A )>, 1,1>& mf,
68  const G_in<A>& g )
69  {
70  G_out<M<R, 1,1> > result( g.size() );
71  auto it = result.begin();
72 
73  for ( const auto & arg : g ) {
74  *it = apply( mf, arg );
75  ++it;
76  }
77 
78  return result;
79  }
80 
81  template< template <typename...> class G_in = std::vector,
82  template <typename...> class G_out = G_in>
83  static G_out<R >
84  in_grid( const F<R( A )>& mf,
85  const G_in<A>& g )
86  {
87  G_out<R > result( g.size() );
88  auto it = result.begin();
89 
90  for ( const auto & arg : g ) {
91  *it = apply( mf, arg );
92  ++it;
93  }
94 
95  return result;
96  }
97  };
98 
99 
100 
125  template < class A,
126  class R,
127  template <typename...> class G_in = std::vector,
128  template <typename...> class G_out = G_in,
129  template <typename...> class F = std::function >
130  G_out<R> evaluate_function_in_grid( const F<R( A )>& f, const G_in<A>& g )
131  {
132  G_out<R> result( g.size() );
133  auto it = result.begin();
134 
135  for ( const auto & arg : g ) {
136  *it = f( arg );
137  ++it;
138  }
139 
140  return result;
141  }
142 
195  }
196 }
Definition: coefficients_file_parser.cpp:10
static M< R, 1, 1 > apply(const M< F< R(A)>, 1, 1 > &mf, const A &arg)
Definition: evaluations.hpp:56
static G_out< R > in_grid(const F< R(A)> &mf, const G_in< A > &g)
Definition: evaluations.hpp:84
G_out< R > evaluate_function_in_grid(const F< R(A)> &f, const G_in< A > &g)
Evaluate a function in multiple points at once.
Definition: evaluations.hpp:130
static G_out< M< R, 1, 1 > > in_grid(const M< F< R(A)>, 1, 1 > &mf, const G_in< A > &g)
Definition: evaluations.hpp:67
static M< R, N, C > apply(const M< F< R(A)>, N, C > &mf, const A &arg)
Definition: evaluations.hpp:17
static G_out< M< R, N, C > > in_grid(const M< F< R(A)>, N, C > &mf, G_in< A > g)
Definition: evaluations.hpp:32
static R apply(const F< R(A)> &f, const A &arg)
Definition: evaluations.hpp:53