KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TPWLApprox.m
Go to the documentation of this file.
1 namespace approx{
2 
3 
4 /* (Autoinserted by mtoc++)
5  * This source code has been filtered by the mtoc++ executable,
6  * which generates code that can be processed by the doxygen documentation tool.
7  *
8  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
9  * Except for the comments, the function bodies of your M-file functions are untouched.
10  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
11  * attached source files that are highly readable by humans.
12  *
13  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
14  * the correct locations in the source code browser.
15  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
16  */
17 
19  :public approx.BaseApprox {
53  public: /* ( setObservable ) */
54 
55  MinWeightValue = 1e-10;
72  public: /* ( Dependent, setObservable ) */
73 
86  public:
87 
88  xi;
98  Ai;
108  bi;
109 
111 
112  fBeta = 25;
113 
114 
115  public:
116 
117 
118 #if 0 //mtoc++: 'set.MinWeightValue'
119 function MinWeightValue(value) {
120  if ~isposrealscalar(value)
121  error(" MinWeightValue must be a positive real scalar ");
122  end
123  this.MinWeightValue= value;
124  }
125 
126 #endif
127 
128 
129 
130 #if 0 //mtoc++: 'set.Beta'
131 function Beta(value) {
132  if ~isposrealscalar(value)
133  error(" Beta must be a positive real scalar. ");
134  end
135  this.fBeta= value;
136  this.GaussWeight.Gamma= 1/sqrt(value);
137  }
138 
139 #endif
140 
141 
142 
143 #if 0 //mtoc++: 'get.Beta'
144 function value = Beta() {
145  value = this.fBeta;
146  }
147 
148 #endif
149 
150 
151  TPWLApprox(sys) {
152  this = this@approx.BaseApprox(sys);
153 
154  /* Beta from Paper is e^{-25 d/m}, for gauss: e^{d/(m*g^2)}, so g=.2
155  * Initialize the gaussian weight with what ever Beta is set to */
156  this.GaussWeight= kernels.GaussKernel(1/sqrt(this.Beta));
157  this.registerProps(" Beta "," MinWeightValue ");
158 
159  /* Set training data selection to epsselector (default strategy for original TPWL) */
160  this.TrainDataSelector= data.selection.EpsSelector;
161  this.CustomProjection= true;
162  }
163 
164 
165  function copy = clone() {
166  copy = approx.TPWLApprox(this.System);
167  copy = clone@approx.BaseApprox(this, copy);
168  copy.Ai= this.Ai;
169  copy.bi= this.bi;
170  copy.xi= this.xi;
171  copy.Beta= this.Beta;
172  copy.MinWeightValue= this.MinWeightValue;
173  }
174 
175 
176  function projected = project(V,W) {
177  projected = project@approx.BaseApprox(this, V, W, this.clone);
178  for i=1:length(this.Ai)
179  projected.Ai[i] = W^t*this.Ai[i]*V;
180  end
181  projected.bi= W^t*this.bi;
182  projected.xi= W^t*this.xi;
183  }
194  function matrix<double>y = evaluate(colvec<double> x,double t,colvec<double> mu) {
195  as = length(this.Ai);
196  y = zeros(size(x));
197  /* Single argument evaluation */
198  if (size(x,2) == 1)
199  /* Compute all distances */
200  di = sqrt(sum((this.xi - repmat(x,1,as)).^2));
201  di(di == 0) = eps;
202  w = this.GaussWeight.evaluateScalar(sqrt(di/min(di)));
203  /* Normalize local weights */
204  w = w / sum(w);
205  idx = 1:as;
206  idx = idx(w > this.MinWeightValue);
207  for i=idx
208  y = y + w(i)*(this.Ai[i]*x + this.bi(:,i));
209  end
210  /* Multi argument evaluation */
211  else
212  n = size(x,2);
213  xisel = reshape(meshgrid(1:n,1:as),[],1)^t;
214  /* Compute all distances */
215  di = sqrt(sum((repmat(this.xi,1,n)-x(:,xisel)).^2));
216  di = reshape(di" ,as,[]) ";
217  di(di == 0) = eps;
218  dimin = min(di,[],2);
219  w = this.GaussWeight.evaluateScalar(sqrt(di./repmat(dimin,1,as)));
220  w = w ./ repmat(sum(w,2),1,as);
221  y = zeros(size(x));
222  for i=1:as
223  /* Find relevant weights */
224  rel = w(:,i) > this.MinWeightValue;
225  /* Normalize local weights
226  *wl = w(rel,i) ./ s(rel);
227  * Only update entries for relevant weights */
228  hlp = this.Ai[i]*x(:,rel) + this.bi(:,i*ones(1,sum(rel)));
229  y(:,rel) = y(:,rel) + hlp*diag(w(rel,i));
230  end
231  end
232  }
248  atd = model.Data.ApproxTrainData;
249  this.xi= atd.xi;
250  ti = atd.ti;
251  mui = atd.mui;
252  fxi = atd.fxi;
253 
254  as = size(this.xi,2);
255  this.Ai= cell(0,as);
256  for i = 1:as
257  if isempty(mui)
258  mu = [];
259  else
260  mu = mui(:,i);
261  end
262  model.System.setConfig(mu,model.DefaultInput);
263  this.Ai[i] = model.System.f.getStateJacobian(this.xi(:,i),ti(i));
264  this.bi(:,i) = fxi(:,i) - this.Ai[i]*this.xi(:,i);
265  end
266  }
267 
268 
269  function matrix<double>y = evaluateCoreFun() {
270  }
278  public: /* ( Static ) */
279 
280  static function res = test_TWPLApprox() {
281  m = models.synth.KernelTest(100,false);
282  m.ErrorEstimator= [];
283  m.Approx= approx.TPWLApprox(m.System);
284  m.Approx.Beta= 25;
285  m.dt= 0.01;
286  m.Sampler= sampling.ManualSampler(m.getRandomParam);
287 
288  m.offlineGenerations;
289  mu = m.getRandomParam;
290  [~,y] = m.simulate(mu);
291  r = m.buildReducedModel;
292  [~,yr] = r.simulate(mu);
293  fprintf(" Max absolute error: %g\n ",max(Norm.L2(y-yr)));
294  res = true;
295  }
296 
297 
307 };
308 }
309 
310 
311 
The base class for any KerMor detailed model.
Definition: BaseFullModel.m:18
CustomProjection
Set this property if the projection process is customized by overriding the default project method...
Definition: ACoreFun.m:108
function J = getStateJacobian(x, t)
Default implementation of jacobian matrix evaluation via finite differences.
Definition: ACoreFun.m:395
models.BaseFirstOrderSystem System
The actual dynamical system used in the model.
Definition: BaseModel.m:102
function matrix< double > y = evaluateCoreFun()
Noting to do here, evaluate is implemented directly. This method will never be called.
Definition: TPWLApprox.m:269
Trajectory-piecewise function approximation.
Definition: TPWLApprox.m:18
function setConfig(mu, inputidx)
Sets the dynamical system's configuration.
A MatLab cell array or matrix.
function registerProps(varargin)
Call this method at any class that defines DPCM observed properties.
Definition: DPCMObject.m:125
reshape
hanges the dimensions of the handle object array to the specified dimensions. See the MATLAB reshape ...
static function res = test_TWPLApprox()
Definition: TPWLApprox.m:280
function approximateSystemFunction(models.BaseFullModel model)
Definition: TPWLApprox.m:247
xi
The centers.
Definition: TPWLApprox.m:88
Ai
The gradient matrix.
Definition: TPWLApprox.m:98
models.BaseFirstOrderSystem System
The system associated with the current ACoreFun.
Definition: ACoreFun.m:193
function copy = clone()
Definition: TPWLApprox.m:165
integer DefaultInput
The default input to use if none is given.
Definition: BaseModel.m:189
V
The matrix of the biorthogonal pair .
Definition: AProjectable.m:61
colvec< double > mu
The current model parameter mu for evaluations. Will not be persisted as only valid for runtime durin...
Definition: ACoreFun.m:208
data.ModelData Data
The full model's data container. Defaults to an empty container.
MinWeightValue
The minimum value a weight needs to have after normalization in order to affect the evaluation...
Definition: TPWLApprox.m:55
Abstract base class for all core function approximations inside dynamical systems.
Definition: BaseApprox.m:18
function matrix< double > y = evaluate(colvec< double > x,double t,colvec< double > mu)
Directly overrides the evaluate method of the ACoreFun as custom projection and multi-argument evalua...
Definition: TPWLApprox.m:194
dscomponents.ACoreFun f
The core f function from the dynamical system.
static function rowvec< double > n = L2(matrix< double > x)
Returns the discrete norm for each column vector in x.
Definition: Norm.m:39
Norm: Static class for commonly used norms on sets of vectors.
Definition: Norm.m:17
W
The matrix of the biorthogonal pair .
Definition: AProjectable.m:72
data.ApproxTrainData ApproxTrainData
Training data for the core function approximation.
Definition: ModelData.m:110
data.selection.ASelector TrainDataSelector
The algorithm that selects the approximation training data.
Definition: BaseApprox.m:62
function res = isposrealscalar(value)
isposintscalar: Backwards-compatibility function for matlab versions greater than 2012a ...
function projected = project(V, W)
Implements AProjectable.project()
Definition: TPWLApprox.m:176