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
GaussKernel.m
Go to the documentation of this file.
1 namespace kernels{
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 kernels.BellFunction {
48  public:
49 
50  GaussKernel(double Gamma) {
51  this = this@kernels.BellFunction;
52  this.registerProps(" Gamma ");/* ,'Sigma' */
53 
54 
55  if nargin == 1
56  this.Gamma= Gamma;
57  end
58  this.updateGammaDependants;
59  this.addlistener(" Gamma "," PostSet ",@this.updateGammaDependants);
60  }
70  K = exp(-this.getSqDiffNorm(x, y)/this.Gamma^2);
71  }
88  function Nablax = getNabla(colvec<double> x,matrix<double> y) {
89  if size(x,2) > 1 && size(y,2) > 1
90  error(" One argument must be a vector. ");
91  end
92  if ~isempty(this.fP)
93  error(" Not yet implemented correctly. ");
94 /* xl = x(this.P,:);
95  * yl = y(this.P,:);
96  * else
97  * xl = x; yl = y; */
98  end
99  hlp = bsxfun(@minus,x,y);
100  hlp = -2*hlp/this.Gamma^2;
101  Nablax = bsxfun(@times,hlp,this.evaluate(x, y));
102  }
115  function dx = evaluateD1(r) {
116  dx = -2*r/this.Gamma^2 .* exp(-r.^2/this.Gamma^2);
117  }
126  function ddx = evaluateD2(r) {
127  ddx = (2/this.Gamma^2) * (2*r.^2/this.Gamma^2-1) .* exp(-r.^2/this.Gamma^2);
128  }
137  function phi = evaluateScalar(r) {
138  phi = exp(-r.^2/this.Gamma^2);
139  }
149  function g = setGammaForDistance(dist,ep) {
150  if nargin == 2
151  ep = eps;
152  end
153  g = dist/sqrt(-log(ep));
154  this.Gamma= g;
155 
156  if KerMor.App.Verbose > 3
157  fprintf(" Setting Gamma = %12.20f\n ",g);
158  end
159  }
177  function copy = clone() {
178  copy = kernels.GaussKernel;
179  copy.Gamma= this.Gamma;
180  copy = clone@kernels.BellFunction(this, copy);
181  }
182 
183 
184  private:
185 
186  function updateGammaDependants(unused1,unused2) {
187  this.r0= this.Gamma/sqrt(2);
188  }
198  protected: /* ( Static ) */
199 
200  static function obj = loadobj(obj) {
201  if ~isa(obj, " kernels.GaussKernel ")
202  newinst = kernels.GaussKernel;
203  newinst.Gamma= obj.Gamma;
204  newinst.updateGammaDependants;
205  obj = loadobj@kernels.BellFunction(newinst, obj);
206  else
207  obj = loadobj@kernels.BellFunction(obj);
208  end
209  obj.addlistener(" Gamma "," PostSet ",@this.updateGammaDependants);
210  }
211 
212 
213  public: /* ( Static ) */
214 
215  static function res = test_InterpolGamma() {
216  ki = general.interpolation.KernelInterpol;
217  ki.UseNewtonBasis= false;
218  kexp = kernels.KernelExpansion;
219  k = kernels.GaussKernel;
220  kexp.Kernel= k;
221  dx = .2;
222  x = -3:dx:3;
223  fx = sin(x*pi);
224  plot(x,fx);
225  epsteps = 0.05:.05:.95;
226  dlog = zeros(3,length(epsteps));
227  for epidx=1:length(epsteps)
228  ep = epsteps(epidx);
229  k.setGammaForDistance(dx,ep);
230  for idx = 1:length(x)
231  x2 = x;
232  x2(idx) = [];
233  fx2 = fx;
234  fx2(idx) = [];
235 
236  kexp.Centers.xi= x2;
237  ki.init(kexp);
238  kexp.Ma= ki.interpolate(fx2);
239 
240  fxi = kexp.evaluate(x);
241  diff(idx) = abs(fx(idx) - fxi(idx));/* #ok */
242 
243  end
244  dlog(1,epidx) = ep;
245  dlog(2,epidx) = min(diff);
246  dlog(3,epidx) = max(diff);
247  end
248  disp(dlog);
249  [~, idx] = min(dlog(2,:));
250  fprintf(" Min distance: %f at ep=%f\n ",dlog(2,idx),dlog(1,idx));
251  res = true;
252  }
253 
254 
255  static function [res , pm ] = test_GaussKernel(pm) {
256  if nargin < 1
257  pm = PlotManager(false,2,3);
258  pm.LeaveOpen= true;
259  end
260  c = 0;
261  x = (-1.2:.01:1.2)+c;
262  [X,Y] = meshgrid(x);
263  x2 = [X(:)" ; Y(:) "];
264  k = kernels.GaussKernel;
265  kexp = kernels.KernelExpansion;
266  kexp.Kernel= k;
267  kexp.Centers.xi= c;
268  kexp.Ma= 1;
269  conf = [1 2 3];
270  for n = 1:length(conf)
271  d = conf(n);
272  g = k.setGammaForDistance(d,eps);
273  tag = strrep(sprintf(" g_1d_d%d_g%g ",d,g)," . "," _ ");
274  h = pm.nextPlot(tag,sprintf(" Gauss kernel with dist=%d,\\gamma=%g on 1D data ",d,g));
275  plot(h,x,kexp.evaluate(x));
276  end
277  kexp.Centers.xi= [c; c];
278  for n = 1:length(conf)
279  d = conf(n);
280  g = k.setGammaForDistance(d,eps);
281  tag = strrep(sprintf(" g_2d_d%d_g%g ",d,g)," . "," _ ");
282  h = pm.nextPlot(tag,sprintf(" Gauss kernel with dist=%d,\\gamma=%g on 2D data ",d,g));
283  surf(h,X,Y,reshape(kexp.evaluate(x2),length(x),[])," EdgeColor "," none ");
284  end
285  if nargin < 1
286  pm.done;
287  end
288  res = true;
289  }
290 
291 
292 
293 };
294 }
295 
296 
297 
Radial Basis Function Kernel.
Definition: GaussKernel.m:18
function g = setGammaForDistance(dist, ep)
Computes the value for which the Gaussian is smaller than in a distance of dist, i.e. Returns the computed value AND sets the kernel's Gamma property to this value.
Definition: GaussKernel.m:149
function K = evaluate(matrix< double > x,matrix< double > y)
Evaluates the gaussian.
Definition: GaussKernel.m:69
function copy = clone()
Definition: GaussKernel.m:177
BELLFUNCTION Summary of this class goes here Detailed explanation goes here.
Definition: BellFunction.m:18
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 ...
function dx = evaluateD1(r)
Method for first derivative evaluation.
Definition: GaussKernel.m:115
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
Definition: PlotManager.m:17
function phi = evaluateScalar(r)
Implements the required method from the IRotationInvariant interface.
Definition: GaussKernel.m:137
double Gamma
Univariate scaling.
Definition: ARBFKernel.m:55
function ddx = evaluateD2(r)
Method for second derivative evaluation.
Definition: GaussKernel.m:126
r0
Point of maximum first derivative on scalar evaluation.
Definition: BellFunction.m:61
static function obj = loadobj(obj)
Definition: GaussKernel.m:200
disp
Handle object disp method which is called by the display method. See the MATLAB disp function...
#define X(i, j)
static function res = test_InterpolGamma()
Definition: GaussKernel.m:215
#define Y(i, j)
function r = getSqDiffNorm(matrix< double > x,matrix< double > y)
Returns the weighted squared norm of the difference .
Definition: ARBFKernel.m:116
Global configuration class for all KerMor run-time settings.
Definition: KerMor.m:17
static function [ res , pm ] = test_GaussKernel(pm)
Definition: GaussKernel.m:255
static function KerMor theinstance = App()
The singleton KerMor instance.
Definition: KerMor.m:910
addlistener
Creates a listener for the specified event and assigns a callback function to execute when the event ...
GaussKernel(double Gamma)
Creates a new GaussKernel.
Definition: GaussKernel.m:50
function Nablax = getNabla(colvec< double > x,matrix< double > y)
Method for first derivative evaluation.
Definition: GaussKernel.m:88