KASKADE 7 development version
modelfunction.hh
Go to the documentation of this file.
1#ifndef MODELFUNCTION_HH
2#define MODELFUNCTION_HH
3
5#include "algorithm_base.hh"
6#include "istl/matrix.hh"
8#include <vector>
9#include "dune/common/fmatrix.hh"
10
18namespace Kaskade
19{
20
23{
24public:
25 ScalarModelFunction() : data("SamplingData"), modified(false), datastart(0) {}
26
28virtual void update(double abscissa, double value) { data = std::make_pair(abscissa,value); modified=true; }
29
31 virtual double getValue(double abscissa) = 0;
32
34 virtual double getIntegral(double high, double low) { assert(!"Computation of integrals not implemented"); return 0.0; }
35
37 virtual double getAbscissa(double value) { assert(!"Computation of abscissa not implemented"); return 0.0; }
38
40 virtual void fixUpdate() { if(data.isValid()) data.logValue(); }
41
43
44 void setDataStart(int ds) {datastart=ds;};
45protected:
46 virtual double getDataVal(int i) { return data[i].second;}
47 virtual double getDataAbsc(int i) { return data[i].first;}
48 virtual double getScaling(int i) { return 1.0;}
49
50
54};
55
57{
58public:
59 PolynomialModel(std::vector<double> defaults)
60 : ScalarModelFunction(), degree(defaults.size()-1), coefficients(defaults), maxels(1000000000)
61 {}
62
63 PolynomialModel(int degree_, int maxels_=10000000)
64 : ScalarModelFunction(), degree(degree_), coefficients(degree+1,0.0), maxels(maxels_)
65 {}
66
67 virtual double getValue(double abscissa)
68 {
70 double value=0.0;
71 double absc=1.0;
72 for(int i=0; i<=degree; ++i)
73 {
74 value +=absc*coefficients[i];
75 absc*=abscissa;
76 }
77 return value;
78 }
79
80 double getAbscissa(double value)
81 {
82 assert(degree == 1);
83 return (value-coefficients[0])/coefficients[1];
84 }
85
86 std::vector<double> const& getCoefficients() { return coefficients;}
87
88protected:
89
90 virtual void computeCoefficients()
91 {
92 if(modified)
93 {
94 int enddata=data.size();
95 int rows=std::min(enddata-datastart,maxels);
96 int startdata=enddata-rows;
97 int cols = std::min(degree+1,rows);
98 if(rows==0 || cols==0) return;
99 double absc;
101 std::vector<double> b(rows), x(cols);
102 for(int i=0; i<rows; ++i)
103 {
104 absc=1.0;
105 b[i]=getDataVal(startdata+i)*getScaling(startdata+i);
106 for(int j=0; j<cols; ++j)
107 {
108 A[i][j]=absc*getScaling(startdata+i);
109 absc*=getDataAbsc(startdata+i);
110 }
111 }
113 for(int i=0; i<x.size(); ++i) coefficients[i]=x[i];
114 modified=false;
115 }
116 }
117
119 std::vector<double> coefficients;
121};
122
124{
125public:
126 TransformedPolynomialModel(std::vector<double> defaults)
127 : PolynomialModel(defaults)
128 {}
129
131 : PolynomialModel(degree_)
132 {}
133
134
135 virtual double getValue(double abscissa) { return valBwd(PolynomialModel::getValue(abscFwd(abscissa))); }
136 virtual double getAbscissa(double value) { return abscBwd(PolynomialModel::getAbscissa(valFwd(value))); }
137protected:
138 virtual double abscFwd(double a) {return a;}
139 virtual double abscBwd(double a) {return a;}
140 virtual double valFwd(double v) {return v;}
141 virtual double valBwd(double v) {return v;}
142 virtual double getDataVal(int i) { return valFwd(data[i].second);}
143 virtual double getDataAbsc(int i) { return abscFwd(data[i].first);}
144
145};
146
149{
150public:
151 LogLogModel(std::vector<double> defaults)
153 {}
154 LogLogModel(int degree_)
156 {}
157
158 virtual double getIntegral(double high, double low)
159 {
161 assert(degree == 1);
162 double a=coefficients[1];
163 double b=std::exp(coefficients[0]);
164 assert(a>-1.0 || high*low > 0);
165 return b/(1+a)*(std::pow(high,1+a)-std::pow(low,1+a));
166 }
167
168protected:
169 virtual double abscFwd(double a) { assert(a>0); return std::log(a); }
170 virtual double abscBwd(double a) { return std::exp(a); }
171 virtual double valFwd(double v) { assert(v>0); return std::log(v); }
172 virtual double valBwd(double v) { return std::exp(v); }
173};
174
176{
177public:
178 OmegaModel() : LogLogModel(std::vector<double>(2,-2.0)) {};
179protected:
180 virtual void computeCoefficients()
181 {
182 if(modified)
183 {
185 assert(coefficients[0] > 0);
187 }
188 }
189};
190
191class EtaModel : public LogLogModel
192{
193public:
194 EtaModel() : LogLogModel(std::vector<double>(2,-1.0)) {};
195protected:
196 virtual void computeCoefficients()
197 {
198 if(modified)
199 {
201 assert(coefficients[0] > 0);
203 }
204 }
205};
206
207class JModel : public LogLogModel
208{
209public:
210 JModel() : LogLogModel(std::vector<double>(3,1.0)) { setDataStart(1); };
211 virtual void update(double abscissa, double value) { LogLogModel::update(abscissa,value); }
212
213protected:
214 virtual double getDataVal(int i) { return valFwd(fabs(data[i-1].second-data.value().second));}
215 virtual double getDataAbsc(int i) { return abscFwd(data[i-1].first-data.value().first);}
216 virtual double getScaling(int i)
217 {
218 if(data[i-1].second-data.value().second > 0 && i < data.size()-2)
220 else return 0.0;
221 }
222
223
224 virtual void computeCoefficients()
225 {
226 if(modified)
227 {
229 }
230 }
231};
232
234 {
235 public:
237 };
238
239} // namespace Kaskade
240#endif
Interfaces for function space oriented algorithms.
virtual void computeCoefficients()
virtual void update(double abscissa, double value)
Add new value to the sample points, will be overwritten by the next update, until fixUpdate() is call...
virtual double getDataVal(int i)
virtual double getDataAbsc(int i)
virtual void computeCoefficients()
virtual double getScaling(int i)
Polynomial model in a loglog scale.
virtual double abscBwd(double a)
virtual double abscFwd(double a)
LogLogModel(std::vector< double > defaults)
virtual double valFwd(double v)
virtual double valBwd(double v)
LogLogModel(int degree_)
virtual double getIntegral(double high, double low)
Compute integral of the model function from low to high.
Class that represents a quantity that can be logged during the course of an algortihm.
T const & value() const
int size()
Get size of log buffer.
bool isValid()
Returns true, iff there is a valid current value.
void logValue()
Insert current value into log buffer.
virtual void computeCoefficients()
double getAbscissa(double value)
Compute abscissa corresponding to a value. This is of course only possible for monotone functions.
std::vector< double > coefficients
std::vector< double > const & getCoefficients()
PolynomialModel(std::vector< double > defaults)
virtual void computeCoefficients()
PolynomialModel(int degree_, int maxels_=10000000)
virtual double getValue(double abscissa)
Compute value of the model function at abscissa.
Interface for a scalar model function on a scalar domain that may fit data.
virtual void fixUpdate()
fix the last updated value and thus increase the number of sampling points
virtual void update(double abscissa, double value)
Add new value to the sample points, will be overwritten by the next update, until fixUpdate() is call...
LoggedQuantity< std::pair< double, double > > data
virtual double getAbscissa(double value)
Compute abscissa corresponding to a value. This is of course only possible for monotone functions.
virtual double getDataAbsc(int i)
virtual double getValue(double abscissa)=0
Compute value of the model function at abscissa.
virtual double getScaling(int i)
virtual double getIntegral(double high, double low)
Compute integral of the model function from low to high.
virtual double getDataVal(int i)
TransformedPolynomialModel(std::vector< double > defaults)
virtual double valFwd(double v)
virtual double getAbscissa(double value)
Compute abscissa corresponding to a value. This is of course only possible for monotone functions.
virtual double valBwd(double v)
virtual double getValue(double abscissa)
Compute value of the model function at abscissa.
virtual double abscFwd(double a)
virtual double abscBwd(double a)
Dune::FieldVector< T, n > min(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise minimum.
Definition: fixdune.hh:122
void LeastSquares(SLAPMatrix< double > a, std::vector< double > const &b, std::vector< double > &x)
Solve linear least squares problem, given by a*x=b.
Simple interface to acml (AMD Core Math Library), to be augmented.