QNANO
Real_Function.h
1 #ifndef QNANO_NEW_REAL_FUNCTION_DEFINED_H
2 #define QNANO_NEW_REAL_FUNCTION_DEFINED_H
3 
4 #include <iostream>
5 #include <fstream>
6 #include <cstdlib>
7 
9 template <typename T>
11 public:
13  virtual T fct(double x)=0;
14 
16  inline T operator()(double x){
17  return fct(x);
18  }
19 
21  virtual void plot(const std::string &filename, double xbegin, double xend, size_t Nintervals){
22  std::ofstream ofs(filename.c_str());
23  double h=(xend-xbegin)/(Nintervals);
24  for(size_t i=0; i<Nintervals+1; i++){
25  double x=xbegin+h*i;
26  ofs<<x<<" "<<fct(x)<<std::endl;
27  }
28  }
29 };
30 
31 
33 
34 class: public Real_Function{
35 public: double fct(double x){return 0.;}
36 } Real_Function_Zero;
37 
38 class: public Real_Function{
39 public: double fct(double x){return x;}
40 } Real_Function_Identity;
41 
42 
43 
45 class Multivar{
46 public:
47  size_t sz;
48  double *d;
49 
50  inline size_t size()const{
51  return sz;
52  }
53  void resize(size_t newsz){
54  sz=newsz;
55  delete[] d;
56  d=new double[sz];
57  }
58  void set_zero(){
59  for(size_t i=0; i<sz; i++)d[i]=0;
60  }
61  double access_check(size_t i){
62  if(i<0){std::cerr<<"Error accessing Multivar: i<0"<<std::endl; exit(1);}
63  if(i>=sz){std::cerr<<"Error accessing Multivar: i>="<<sz<<std::endl; exit(1);}
64  return d[i];
65  }
66  double operator()(size_t i)const{
67  return d[i];
68  }
69  double &operator()(size_t i){
70  return d[i];
71  }
72  double &operator[](size_t i){
73  return d[i];
74  }
75  double operator[](size_t i)const{
76  return d[i];
77  }
78 
79 
80  inline void add(const Multivar &other){
81  for(size_t i=0; i<sz; i++)d[i]+=other.d[i];
82  }
83  inline void subtract(const Multivar &other){
84  for(size_t i=0; i<sz; i++)d[i]-=other.d[i];
85  }
86  inline void add_scaled(double scale, const Multivar &other){
87  for(size_t i=0; i<sz; i++)d[i]+=scale*other.d[i];
88  }
89  inline void scale(double scale){
90  for(size_t i=0; i<sz; i++)d[i]*=scale;
91  }
92 
93  void copy(const Multivar &other){
94  resize(other.sz);
95  for(size_t i=0; i<sz; i++)d[i]=other.d[i];
96  }
97  Multivar(const Multivar &other){ d=new double[0]; copy(other);}
98  Multivar & operator=(const Multivar &other){ copy(other);return *this;}
99 
100  Multivar(size_t sz_): sz(sz_){
101  d=new double[sz];
102  set_zero();
103  }
104  Multivar(){
105  d=new double[0];
106  }
107  ~Multivar(){
108  delete[] d;
109  }
110 
111 };
112 
113 
115 template <typename T>
117 public:
118 
120  virtual T fct(const Multivar &v)=0;
121 
123  inline T operator()(const Multivar &v){
124  return fct(v);
125  }
126 
127 };
129 
130 
131 
132 template <typename T>
134 public:
136  Multivar mv_start;
137  Multivar mv_dir;
138 
139 
140  virtual T fct(double x){
141  Multivar mv(mv_start);
142  mv.add_scaled(x, mv_dir);
143  return mvfct->fct(mv);
144  }
145  Function_Template_from_Multivar(Multivar_Function_Template<T>* mvfct_, const Multivar &mv_start_, const Multivar &mv_dir_) : mvfct(mvfct_), mv_start(mv_start_), mv_dir(mv_dir_){
146  }
147 
148 };
150 
151 
152 
153 template <typename T>
155 public:
157  size_t axis;
158  Multivar mv_ref;
159 
160  virtual T fct(double x){
161  mv_ref(axis)=x;
162  return mvfct->fct(mv_ref);
163  }
164  Function_Template_along_Axis(Multivar_Function_Template<T>& mvfct_, size_t axis_, const Multivar &mv_ref_): mvfct(&mvfct_), axis(axis_), mv_ref(mv_ref_){
165  }
166 };
168 
169 #endif
T operator()(double x)
overload parenthesis operator
Definition: Real_Function.h:16
virtual T fct(double x)
actual fuction. To be defined in derived class
Definition: Real_Function.h:140
virtual T fct(double x)=0
actual fuction. To be defined in derived class
Class to store the arguments for multi-variable function.
Definition: Real_Function.h:45
virtual T fct(const Multivar &v)=0
actual fuction. To be defined in derived class
T operator()(const Multivar &v)
overload parenthesis operator
Definition: Real_Function.h:123
Definition: Real_Function.h:133
virtual void plot(const std::string &filename, double xbegin, double xend, size_t Nintervals)
Plots function to file: discretized form &#39;xbegin&#39; to &#39;xend&#39; on &#39;Nintervals&#39;+1 sample points...
Definition: Real_Function.h:21
Definition: Real_Function.h:154
Template for function with multiple double variables.
Definition: Real_Function.h:116
Template for function of a single real variable.
Definition: Real_Function.h:10
virtual T fct(double x)
actual fuction. To be defined in derived class
Definition: Real_Function.h:160