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
Orthonormalizer.m
Go to the documentation of this file.
1 namespace general{
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 KerMorObject {
44  public:
45 
46  G = 1;
57  Epsilon = 1e-7;
66  Algorithm = "gs";
84  public:
85 
86  function onvec = orthonormalize(vec) {
87 
88  /* Check for nonempty vec */
89  if isempty(vec)
90  onvec = zeros(size(vec));
91  return;
92  end
93 
94  /* Check on identity of vectors */
95  for i=1:(size(vec,2)-1)
96  for j=(i+1):size(vec,2)
97  if isequal(vec(:,i),vec(:,j))
98  vec(:,j) = 0;
99  end;
100  end;
101  end
102 
103  if strcmp(this.Algorithm," gs ")
104  onvec = this.ortho_gs(vec);
105  elseif strcmp(this.Algorithm," qr ")
106  onvec = this.ortho_qr(vec);
107  elseif strcmp(this.Algorithm," ch ")
108  onvec = this.ortho_ch(vec);
109  end
110  }
122 #if 0 //mtoc++: 'set.G'
123 function G(value) {
124  if ~isa(value, " double ")
125  error(" G should be a double matirx ");
126  end
127  this.G= value;
128  }
129 
130 #endif
131 
132 
133 
134 #if 0 //mtoc++: 'set.Epsilon'
135 function Epsilon(value) {
136  if ~isposrealscalar(value)
137  error(" value must be a positive real scalar. ");
138  end
139  this.Epsilon= value;
140  }
141 
142 #endif
143 
144 
145 
146 #if 0 //mtoc++: 'set.Algorithm'
147 function Algorithm(value) {
148  if ~any(strcmp(value,[" gs " " qr " " ch "]))
149  error(" Invalid method: %s ",value);
150  end
151  this.Algorithm= value;
152  }
153 
154 #endif
155 
156 
157  private:
158 
159  function onvec = ortho_gs(vec) {
160 
161  onvec = vec;
162  for i = 1:size(vec,2)
163  /* orthogonalize next vector i and assume, that it is already
164  * orthogonal to previous ones */
165  n = sqrt(onvec(:,i)^t * this.G * onvec(:,i));
166  if (n < this.Epsilon)
167  onvec(:,i) = 0;
168  else
169  onvec(:,i) = onvec(:,i)/n;
170  end
171 
172  /* orthogonalize remaining vectors wrt this one: */
173  A_mult_onvec_i = this.G*onvec(:,i);
174 
175  /* create row-vector with projections on the created on-vector */
176  coeffs= A_mult_onvec_i^t * onvec(:,i+1:end);
177 
178  /* create matrix of scaled versions of actual orthonormalized vector */
179  coeffmat = onvec(:,i) * coeffs;
180 
181  /* perform orthogonalization */
182  onvec(:,i+1:end) = onvec(:,i+1:end) - coeffmat;
183  end
184 
185  /* eliminate zero-columns */
186  nsqr = sum(onvec.^2);
187  onvec = onvec(:,nsqr > 0.1);
188  }
198  function onvec = ortho_qr(vec) {
199  if 1==1
200  error(" Algorithm 'qr' not working. Need to check. ");
201  end
202  onvec = vec;
203 
204  /* incomplete cholesky of inner-product matrix: */
205  R_M = ichol(sparse(this.G)," inf ");
206 
207  /* qr decomposition of R_M * X with permutation indices E */
208  [Q, R, E]= qr(R_M * onvec, 0);
209 
210  /* sort such that linear independent first and Q * R = R_M * Xon */
211  onvec = onvec(:,E);
212 
213  /* search nonvanishing diagonal entries of R */
214  ind = find(abs(diag(R)) > this.Epsilon);
215  onvec = onvec(:,ind);
216  Rind = R(ind,ind);
217  onvec = onvec(:,ind) / Rind;
218  }
219 
220 
221  function onvec = ortho_ch(vec) {
222  if 1==1
223  error(" Algorithm 'ch' not working. Need to check. ");
224  end
225  onvec = vec;
226 
227  /* get gram matrix */
228  Gr = onvec^t * this.G * onvec;
229  Gr = 0.5* (Gr + Gr^t);
230 
231  options.droptol= this.Epsilon;
232 
233  /* R = full(cholinc(sparse(G),'inf')); % fill small diagonal entries with inf
234  *R = full(cholinc(sparse(G),options)); % fill small diagonal entries with inf */
235 
236  /* cholesky with dropvalue epsilon
237  * If an error gets thrown here, you might have an older matlab
238  * version only knowing "cholinc" as command, which has been
239  * replaced by "ichol" as of R2013A.
240  * R = full(cholinc(sparse(Gr),this.Epsilon)); */
241  R = full(ichol(sparse(Gr),options));
242 
243  ind = find(diag(R) > this.Epsilon);
244  onvec = onvec(:,ind);
245  Ri = R(ind,ind);
246  onvec = onvec / Ri;
247 
248  /* eliminate too small or too large vectors: */
249  norms = onvec^t * this.G * onvec;
250  onvec = onvec(:,abs(diag(norms)-1)<0.5);
251  }
252 
253 
254  public: /* ( Static ) */
255 
256  static function res = test_Orthogonalization() {
257  res = true;
258  for k = 1:5
259  d2 = randi(200);
260  d1 = d2 + k*randi(10);
261 
262  A = rand(d1,d2);
263 
264  o = general.Orthonormalizer;
265  o.Algorithm= " gs ";
266  ov1 = o.orthonormalize(A);
267  /* o.Algorithm = 'ch';
268  * ov2 = o.orthonormalize(A);
269  * o.Algorithm = 'qr';
270  * ov3 = o.orthonormalize(A) */
271 
272  /* res = norm(ov1-ov2) < sqrt(eps); */
273  res = res & isequal(round(ov1^t*ov1),eye(d2));
274  res = res & abs(norm(ov1^t*ov1)-1) < 1000*eps;
275  end
276  }
277 
278 
292 };
293 }
294 
295 
296 
Epsilon
Tolerance for zero columns at orthogonalization.
static function res = test_Orthogonalization()
function onvec = orthonormalize(vec)
Performs orthogonalization using the given column vectors vec and the scalar product induced by the m...
Base class for any KerMor class.
Definition: KerMorObject.m:17
G
The scalar product matrix .
Class that supports orthonormalization of vectors.
function res = isposrealscalar(value)
isposintscalar: Backwards-compatibility function for matlab versions greater than 2012a ...
Algorithm
The orthogonalization algorithm used.