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
AffParamMatrix.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 general.AProjectable {
74  public:
75 
76  integer N = 0;
89  Matrices = "[]";
102  public:
103 
104  .string funStr = {""};
119  protected:
120 
121  handle cfun = "[]";
150  public:
151 
152 
153  function M = compose(t,mu) {
154  if this.N == 0
155  M = [];
156  return;
157  end
158  M = reshape(this.Matrices * sparse(this.cfun(t,mu)),this.dims(1),[]);
159  }
172  function c = evalCoeffFun(double t,colvec<double> mu) {
173  c = this.cfun(t,mu);
174  }
175 
176 
177  function copy = clone(copy) {
178  if nargin == 1
179  copy = general.AffParamMatrix;
180  end
181  copy = clone@general.AProjectable(this, copy);
182  copy.Matrices= this.Matrices;
183  copy.N= this.N;
184  copy.funStr= this.funStr;
185  copy.dims= this.dims;
186  copy.funStr= this.funStr;
187  copy.cfun= this.cfun;
188  }
200  function addMatrix(coeff_fun,mat) {
201 
202  if ~isa(coeff_fun," char ")
203  error(" Coeff_fun must be a string. ");
204  elseif ~isreal(mat)
205  error(" Parameter 'mat' must be a real matrix. ");
206  end
207  if isempty(this.dims)
208  this.dims= size(mat);
209  end
210  this.N= this.N + 1;
211 
212  this.funStr[end+1] = coeff_fun;
213  /* this.Coefficients{end+1} = coeff_fun; */
214 
215  this.Matrices(:,end+1) = mat(:);
216 
217  /* Create the coefficient function */
218  this.buildCoeffFun;
219  }
235  function pr = mtimes(B) {
236  if isa(A," general.AffParamMatrix ") && isa(B," general.AffParamMatrix ")
237  if ~all(strcmp([class(A),class(B)]," general.AffParamMatrix ")) && ~strcmp(class(A),class(B))
238  warning(" KerMor:Ambiguousclass "," Cannot consistently multiply two real different subclasses of general.AffParamMatrix as the return type is not well defined. ");
239  /* error('Cannot consistently multiply two real different subclasses of general.AffParamMatrix as the return type is not well defined. Please multiply manually.'); */
240  end
241  if A.dims(2) ~= B.dims(1)
242  error(" Matrix dimensions must agree. ");
243  end
244  /* Clone the instance of A as the check above ensures that both */
245  pr = A.clone;
246  pr.N= A.N * B.N;
247  pr.dims= [A.dims(1) B.dims(2)];
248  pr.Matrices= zeros(prod(pr.dims),pr.N);
249  pr.funStr= [];
250  for i = 1:A.N
251  M = A.getMatrix(i);
252  for j = 1:B.N
253  pr.Matrices(:,(i-1)*B.N + j) = ...
254  reshape(M * B.getMatrix(j),1,[]);
255  pr.funStr[end+1] = [" ( " A.funStr[i] " )*( " B.funStr[j] " ) "];
256  end
257  end
258  pr.buildCoeffFun;
259  elseif isa(A," general.AffParamMatrix ")
260  pr = A.clone;
261  if isscalar(B)
262  pr.Matrices= B*pr.Matrices;
263  else
264  if A.dims(2) ~= size(B,1)
265  error(" Matrix dimensions must agree. ");
266  end
267  pr.dims= [A.dims(1) size(B,2)];
268  pr.Matrices= zeros(prod(pr.dims),pr.N);
269  for i=1:A.N
270  pr.Matrices(:,i) = reshape(...
271  A.getMatrix(i) * B,[],1);
272  end
273  end
274  elseif isa(B," general.AffParamMatrix ")
275  pr = B.clone;
276  if isscalar(A)
277  pr.Matrices= A*pr.Matrices;
278  else
279  if size(A,2) ~= B.dims(1)
280  error(" Matrix dimensions must agree. ");
281  end
282  pr.dims= [size(A,1) B.dims(2)];
283  pr.Matrices= zeros(prod(pr.dims),pr.N);
284  for i=1:B.N
285  pr.Matrices(:,i) = reshape(...
286  A * B.getMatrix(i),[],1);
287  end
288  end
289  end
290  }
303  function diff = minus(B) {
304  if isa(A," general.AffParamMatrix ") && isa(B," general.AffParamMatrix ")
305  if ~all(strcmp([class(A),class(B)]," general.AffParamMatrix ")) && ~strcmp(class(A),class(B))
306  error(" Cannot consistently subtract two real different subclasses of general.AffParamMatrix as the return type is not well defined. Please perform subtraction manually. ");
307  end
308  if any(A.dims ~= B.dims)
309  error(" Matrix dimensions must agree. ");
310  end
311  if A.N == B.N && all(strcmp(A.funStr,B.funStr))
312  diff = A.clone;
313  diff.Matrices= A.Matrices - B.Matrices;
314  else
315  error(" If two AffParamMatrices are subtracted, the number of elements must be the same and the coefficient functions must match. ");
316  end
317  elseif isa(A," general.AffParamMatrix ")
318  diff = A.clone;
319  if isscalar(B)
320  B = ones(size(A.Matrices,1),1)*B;
321  end
322  diff.addMatrix(" -1 ",B)
323  elseif isa(B," general.AffParamMatrix ")
324  diff = B.clone;
325  diff.Matrices= -diff.Matrices;
326  if isscalar(A)
327  A = ones(size(B.Matrices,1),1)*A;
328  end
329  diff.addMatrix(" 1 ",A);
330  end
331  }
348  function transp = ctranspose() {
349  transp = this.clone;
350  transp.dims= fliplr(transp.dims);
351  /* Autodetects size & type of projected matrix! */
352  if this.N > 0
353  transp.Matrices= reshape(this.getMatrix(1)^t,[],1);
354  end
355  for i=2:this.N
356  transp.Matrices(:,i) = reshape(this.getMatrix(i)^t,[],1);
357  end
358  }
368  function dotprod = times(B) {
369  if isa(A," general.AffParamMatrix ") && isa(B," general.AffParamMatrix ")
370  error(" Not yet implemented ");
371  elseif isa(A," general.AffParamMatrix ")
372  dotprod = A.clone;
373  dotprod.Matrices= A.Matrices .* repmat(B(:),1,A.N);
374  elseif isa(B," general.AffParamMatrix ")
375  dotprod = B.times(A);
376  end
377  }
378 
379 
380  function M = getMatrix(idx) {
381  if idx > this.N
382  error(" Invalid index %d. Max index is %d. ",idx,this.N);
383  end
384  M = reshape(this.Matrices(:,idx),this.dims);
385  }
396  function general.AffParamMatrixtarget = project(matrix<double> V,matrix<double> W,general.AffParamMatrix target) {
397 
398  if isempty(V) || isempty(W)
399  error(" Both V and W must be given and nonempty (Set either to 1 for neutral as required). ");
400  end
401  if nargin < 4
402  target = this.clone;
403  end
404  if ~isequal(V,1)
405  target.dims(2) = size(V,2);
406  end
407  if ~isequal(W,1)
408  target.dims(1) = size(W,2);
409  end
410  /* Autodetects size & type of projected matrix! */
411  if this.N > 0
412  target.Matrices= reshape(W^t*(this.getMatrix(1)*V),[],1);
413  end
414  for idx=2:this.N
415  target.Matrices(:,idx) = reshape(W^t*(this.getMatrix(idx)*V),[],1);
416  end
417  }
437  function [n , m ] = size(dim) {
438  n = this.dims;
439  if nargin == 2
440  if dim > 0 && dim < 3
441  n = n(dim);
442  else
443  n = 0;
444  end
445  elseif nargout == 2
446  n = this.dims(1);
447  m = this.dims(2);
448  end
449  }
458  function clear() {
459  this.N= 0;
460  this.Matrices= [];
461  this.cfun= [];
462  this.dims= [];
463  this.funStr= [];
464  }
465 
466 
467  private:
468 
469  function buildCoeffFun() {
470  this.cfun= eval([" @(t,mu)[ " Utils.implode(this.funStr," ; ") " ] "]);
471  }
481 };
482 }
483 
484 
485 
Collection of generally useful functions.
Definition: Utils.m:17
Interface for all components that can be projected.
Definition: AProjectable.m:18
function diff = minus(B)
Implements the default substraction method.
Matrices
The matrices for the affine function.
string funStr
The function strings defining for the affine-linear combination of the matrices. ...
function addMatrix(coeff_fun, mat)
Adds a matrix with corresponding coefficient function to the affine parametric matrix.
reshape
hanges the dimensions of the handle object array to the specified dimensions. See the MATLAB reshape ...
handle cfun
coefficient function handle as specified by funStr
function transp = ctranspose()
Implements the transposition for affine parametric matrices.
An integer value.
Matlab's base handle class (documentation generation substitute)
function general.AffParamMatrix target = project(matrix< double > V,matrix< double > W,general.AffParamMatrix target)
Projects the affine parametric matrix using and .
V
The matrix of the biorthogonal pair .
Definition: AProjectable.m:61
function M = getMatrix(idx)
Returns the -th matrix of the AffParamMatrix.
function M = compose(t, mu)
Composes the affine-linear combination of matrices for given time and parameter .
Speed test * all(1:3)
rowvec< integer > dims
dimension of the matrices
function [ n , m ] = size(dim)
Implementation of ABlockedData.size.
function c = evalCoeffFun(double t,colvec< double > mu)
static function char str = implode(char|rowvec data,char glue,char format)
Implodes the elements of data using glue.
Definition: Utils.m:208
function copy = clone(copy)
Creates a copy of this affine parametric matrix.
General time/parameter-affine matrix.
W
The matrix of the biorthogonal pair .
Definition: AProjectable.m:72
integer N
The number of affine matrices / size of the linear combination.
function dotprod = times(B)
function pr = mtimes(B)
Implements the default multiplication method.