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
MonomialIterator.m
Go to the documentation of this file.
1 
2 
3 /* (Autoinserted by mtoc++)
4  * This source code has been filtered by the mtoc++ executable,
5  * which generates code that can be processed by the doxygen documentation tool.
6  *
7  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
8  * Except for the comments, the function bodies of your M-file functions are untouched.
9  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
10  * attached source files that are highly readable by humans.
11  *
12  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
13  * the correct locations in the source code browser.
14  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
15  */
16 
18  :public handle {
44  public:
45 
46  Dim;
47 
48 
49  private:
50 
51  incidx;
52 
53  deg;
54 
55  rnd;
56 
57  expidx;
58 
59 
60  public:
61 
63  if int32(dim) ~= dim || dim <= 0
64  error(" Dimension d must be a positive integer. ");
65  end
66  this.Dim= int32(dim);
67  this.deg= int32(1);
68  this.rnd= RandStream(" mt19937ar "," Seed ",2);
69  this.expidx= int32(1);
70  this.incidx= int32(1);
71  }
80  function colvec<double>alpha = getNullMonomial() {
81  alpha = zeros(this.Dim,1);
82  }
92  function [colvec<double>alpha , int32deg , exps ] = nextMonomial() {
93 
94 
95  /* Collect exponents via histogram count for each dimension
96  *alpha = fliplr(histc(this.expidx, 1:this.Dim))'; */
97  alpha = histc(this.expidx, 1:this.Dim)^t;
98  deg = this.deg;
99  exps = this.expidx;
100 
101  /* Increment exponentials at current incidx */
102  if this.expidx(this.incidx) == this.Dim && this.incidx > 1
103  /* Set increase counter one to the left */
104  this.incidx= this.incidx - 1;
105 
106  /* Einen hoch */
107  this.expidx(this.incidx) = this.expidx(this.incidx) + 1;
108 
109  /* See */
110  if this.expidx(this.incidx) < this.Dim
111  /* Daneben alle auf den Wert setzen */
112  this.expidx(this.incidx+1:this.deg) = this.expidx(this.incidx);
113  this.incidx= this.deg;
114  end
115  else
116  this.expidx(this.incidx) = this.expidx(this.incidx) + 1;
117  end
118 
119  /* Check if current degree is finished */
120  if this.incidx == 0 || this.expidx(this.incidx) > this.Dim /* equiv: all(this.expidx == this.Dim) */
121 
122  this.deg= this.deg + 1;
123  this.expidx= int32(ones(1,this.deg));
124  /* Increment index is last exponent */
125  this.incidx= this.deg;
126  end
127  }
141  function colvec<int32>alpha = getRandMonomial(int32 deg) {
142  alpha = zeros(this.Dim,1);
143  s = 0;
144  while s < deg
145  idx = this.rnd.randi(this.Dim);
146  alpha(idx) = alpha(idx) + this.rnd.randi(deg-s);
147  s = sum(alpha);
148  end
149  }
161  public: /* ( Static ) */
162 
163  static function logicalres = test_MonomialIterator() {
164  for Dim = 1:3 /* #ok<*PROP> */
165 
166  fprintf(" Dim=%d:\n ",Dim)
167  mi = MonomialIterator(Dim);
168  deg = 0;
169  while deg < 5
170  [alpha, deg, exps] = mi.nextMonomial;
171  fprintf(" p=%d, exps=[%s], alpha=[%s]\n ",deg, Utils.implode(exps," , "," %d "),...
172  Utils.implode(alpha," , "," %d "));
173  end
174  end
175  res = true;
176  /* test for d=1 and p=1000 */
177  mi = MonomialIterator(1);
178  for i=1:1000
179  [alpha, deg, exps] = mi.nextMonomial; /* #ok<*PROP> */
180 
181  res = res && alpha == i && deg == i && all(exps==1);
182  end
183 
184  /* test for d=4 and p=3 */
185  mi = MonomialIterator(4);
186  cmpp1 = [1; 2; 3; 4];
187  cmpp2 = [1 1; 1 2; 1 3; 1 4; 2 2; 2 3; 2 4; 3 3; 3 4; 4 4];
188  cmpp3 = [1 1 1; 1 1 2; 1 1 3; 1 1 4; 1 2 2; 1 2 3; 1 2 4; 1 3 3; 1 3 4; 1 4 4; 2 2 2;...
189  2 2 3; 2 2 4; 2 3 3; 2 3 4; 2 4 4; 3 3 3; 3 3 4; 3 4 4; 4 4 4];
190  for i = 1:size(cmpp1,1)
191  [~, deg, exps] = mi.nextMonomial;
192  res = res && deg == 1 && all(exps==cmpp1(i,:));
193  end
194  for i = 1:size(cmpp2,1)
195  [~, deg, exps] = mi.nextMonomial;
196  res = res && deg == 2 && all(exps==cmpp2(i,:));
197  end
198  for i = 1:size(cmpp3,1)
199  [~, deg, exps] = mi.nextMonomial;
200  res = res && deg == 3 && all(exps==cmpp3(i,:));
201  end
202  }
212 };
213 
static function logical res = test_MonomialIterator()
Tests the MonomialIterator for two test cases.
Collection of generally useful functions.
Definition: Utils.m:17
function colvec< int32 > alpha = getRandMonomial(int32 deg)
Returns a random monomial.
MonomialIterator(integer dim)
Creates a new monomial iterator.
An integer value.
Matlab's base handle class (documentation generation substitute)
MonomialIterator: Create list of d-dim monomials.
Speed test * all(1:3)
function colvec< double > alpha = getNullMonomial()
Get the monomial of degree zero.
static function char str = implode(char|rowvec data,char glue,char format)
Implodes the elements of data using glue.
Definition: Utils.m:208
function [ colvec< double > alpha , int32 deg , exps ] = nextMonomial()
Returns the next monomial in the sequence.