Mobile API Reference  MicroStrategy 2019
Matrix.H
Go to the documentation of this file.
2 
3 typedef Int32 Index;
4 typedef double Real;
5 
6 
8 {
9 private:
10  Index *buf; // stores specification of the permutation
11 
12 public:
13  const Index dim;
14 
16  Permutation(Permutation &); // copy constructor allocates a new buffer
17  ~Permutation();
18 
19  Index & operator [] (Index n); // get P(n)
20  Index & operator () (Index n); // get P(n)
21 
22  Permutation & AddCycle(Index, Index); // add a cycle
23 };
24 
25 
26 
27 class Matrix
28 {
29 private:
30  Real *buf; // stores the data contained in the matrix
31  bool destroiable; // set to false if buf was provided to the constructor
32 
33 public:
34  const Index height, width;
35 
36  Matrix(Index, Index); // This creator allocates memory for the Matrix
37  Matrix(Index, Index, Real *); // This creator uses the given buffer. The destroier does not
38  // deallocate the buffer
39  Matrix(Matrix &); // copy constructor allocates a new buffer
40  ~Matrix();
41 
42  Real & operator () (Index i, Index j); // returns element i,j
44 
45  Matrix & Permute(Matrix &A, Permutation &rowP, Permutation &colP);
46  Matrix & PermuteBack(Matrix &A, Permutation &rowP, Permutation &colP);
47 };
48 
49 
50 
51 
52 
53 class Vector
54 {
55 private:
56  Real *buf; // stores the data contained in the vector
57  bool destroiable; // set to false if buf was provided to the constructor
58 
59 public:
60  const Index dim;
61 
62  Vector(Index); // This creator allocates memory for the vector
63  Vector(Index, Real *); // This creator uses the given buffer. The destroier does not
64  // deallocate the buffer
65  Vector(Vector &); // copy constructor allocates a new buffer
66  ~Vector();
67 
68 
71  Vector & operator += (Vector &v) { return Sum(*this, v); }
72  Vector & operator -= (Vector &v) { return Diff(*this, v); }
75  Vector & operator *= (Real x) {return RealMult(*this, x); }
76  Vector & operator /= (Real x) {return RealDiv(*this, x);}
77 
78  Vector & Permute(Vector &v, Permutation &P); // set this vector to P(v)(if dimensions agree)
79  Vector & PermuteBack(Vector &v, Permutation &p);// set this vector to P'(v) (if dimensions agree)
80  Vector & MatrixMult(Matrix &A, Vector &v); // set this vector to Av (if dimensions agree)
81  Vector & Sum(Vector &, Vector &);
82  Vector & Diff(Vector &, Vector &);
83  Vector & RealMult(Vector &, Real);
84  Vector & RealDiv(Vector &, Real);
85 };
86 
87 
88 
89 
90 
92 {
93  buf = new Index[d];
94 
95  for (Index i=0; i<d; i++)
96  buf[i] = i;
97 }
98 
99 inline Permutation::Permutation(Permutation &X): dim(X.dim)
100 {
101  buf = new Index[dim];
102 
103  for (Index i=0; i<dim; i++)
104  buf[i] = X(i);
105 }
106 
108 {
109  delete buf;
110 }
111 
113 {
114  return buf[n];
115 }
116 
118 {
119  return buf[n];
120 }
121 
123 {
124  Index temp;
125 
126  temp=buf[i];
127  buf[i]=buf[j];
128  buf[j]=temp;
129 
130  return *this;
131 }
132 
133 inline Matrix::Matrix(Index h, Index w): height(h), width(w), destroiable(true)
134 {
135  buf = new Real[h*w];
136 }
137 
138 inline Matrix::Matrix(Index h, Index w, Real *M): height(h), width(w), destroiable(false), buf(M)
139 {}
140 
141 inline Matrix::Matrix(Matrix &X): height(X.height), width(X.width), destroiable(true)
142  {
143  buf = new Real[height*width];
144  for (Index i=0; i < height*width; i++)
145  buf[i] = X.buf[i];
146  }
147 
149 {
150  if (destroiable) delete buf;
151 }
152 
154 {
155  return buf[i*width+j];
156 }
157 
159 {
160  if ( A.height == height && A.width == width )
161  {
162  for (Index i=0; i<height*width; i++)
163  {
164  buf[i] = A.buf[i];
165  }
166  }
167 
168  return A;
169 }
170 
172 {
173  if ( A.height == height && A.width == width )
174  {
175  Index i,j;
176 
177  for (i=0; i<height; i++)
178  for (j=0; j<width; j++)
179  (*this)(i, j) = A(rowP(i), colP(j));
180  }
181 
182  return *this;
183 }
184 
186 {
187  if ( A.height == height && A.width == width )
188  {
189  Index i,j;
190 
191  for (i=0; i<height; i++)
192  for (j=0; j<width; j++)
193  (*this)(rowP(i), colP(j)) = A(i, j);
194  }
195 
196  return *this;
197 }
198 
199 
200 
201 inline Vector::Vector(Index d): dim(d), destroiable(true)
202 {
203  buf = new Real[d];
204 }
205 
206 inline Vector::Vector(Index d, Real *v): dim(d), destroiable(false), buf(v)
207 {}
208 
209 inline Vector::Vector(Vector &X): dim(X.dim), destroiable(true)
210  {
211  buf = new Real[dim];
212  for (Index i=0; i<dim; i++)
213  buf[i] = X.buf[i];
214  }
215 
217 {
218  if (destroiable)
219  delete buf;
220 }
221 
223 {
224  return buf[i];
225 }
226 
228 {
229  return buf[i];
230 }
231 
233 {
234  Real result=0;
235 
236  if ( dim == v.dim )
237  {
238  for (Index i=0; i<dim; i++)
239  result += buf[i] * v.buf[i];
240  }
241 
242  return result;
243 }
244 
246 {
247  if ( dim == v.dim )
248  {
249  for (Index i=0; i<dim; i++)
250  buf[i] = v.buf[i];
251  }
252 
253  return v;
254 }
255 
257 {
258  if ( dim == v.dim && dim == P.dim )
259  {
260  for (Index i=0; i<dim; i++)
261  buf[i] = v.buf[P[i]];
262  }
263 
264  return *this;
265 }
266 
268 {
269  if ( dim == v.dim && dim == P.dim )
270  {
271  for (Index i=0; i<dim; i++)
272  buf[P[i]] = v.buf[i];
273  }
274 
275  return *this;
276 }
277 
279 {
280  if ( dim == A.height && v.dim == A.width )
281  {
282  Index i,j;
283  for (i=0; i<A.height; i++)
284  {
285  buf[i] = 0;
286 
287  for (j=0; j<A.width; j++)
288  buf[i] += A(i,j) * v.buf[j];
289  }
290  }
291 
292  return *this;
293 }
294 
295 inline Vector & Vector::Sum(Vector &v, Vector &w)
296 {
297  if ( dim == v.dim && dim == w.dim )
298  {
299  for (Index i=0; i<dim; i++)
300  buf[i] = v.buf[i] + w.buf[i];
301  }
302 
303  return *this;
304 }
305 
307 {
308  if ( dim == v.dim && dim == w.dim )
309  {
310  for (Index i=0; i<dim; i++)
311  buf[i] = v.buf[i] - w.buf[i];
312  }
313 
314  return *this;
315 }
316 
318 {
319  if ( dim == v.dim )
320  {
321  for (Index i=0; i<dim; i++)
322  buf[i] = v.buf[i] * x;
323  }
324 
325  return *this;
326 }
327 
329 {
330  if ( dim == v.dim )
331  {
332  for (Index i=0; i<dim; i++)
333  buf[i] = v.buf[i] / x;
334  }
335 
336  return *this;
337 }
Real operator*(Vector &v)
Definition: Matrix.H:232
Vector & Sum(Vector &, Vector &)
Definition: Matrix.H:295
Vector & MatrixMult(Matrix &A, Vector &v)
Definition: Matrix.H:278
Vector & operator-=(Vector &v)
Definition: Matrix.H:72
Vector(Index)
Definition: Matrix.H:201
Matrix(Index, Index)
Definition: Matrix.H:133
const Index height
Definition: Matrix.H:34
Definition: Matrix.H:7
Int32 Index
Definition: Matrix.H:3
Permutation(Index)
Definition: Matrix.H:91
Permutation & AddCycle(Index, Index)
Definition: Matrix.H:122
#define Int32
Definition: BasicTypes.h:20
Real & operator[](Index)
Definition: Matrix.H:222
~Matrix()
Definition: Matrix.H:148
Matrix & operator=(Matrix &)
Definition: Matrix.H:158
Vector & RealDiv(Vector &, Real)
Definition: Matrix.H:328
const Index width
Definition: Matrix.H:34
Index & operator()(Index n)
Definition: Matrix.H:117
Vector & Diff(Vector &, Vector &)
Definition: Matrix.H:306
Matrix & PermuteBack(Matrix &A, Permutation &rowP, Permutation &colP)
Definition: Matrix.H:185
Definition: Matrix.H:27
Vector & Permute(Vector &v, Permutation &P)
Definition: Matrix.H:256
Vector & operator/=(Real x)
Definition: Matrix.H:76
double Real
Definition: Matrix.H:4
Vector & RealMult(Vector &, Real)
Definition: Matrix.H:317
Matrix & Permute(Matrix &A, Permutation &rowP, Permutation &colP)
Definition: Matrix.H:171
~Vector()
Definition: Matrix.H:216
Real & operator()(Index)
Definition: Matrix.H:227
Vector & operator+=(Vector &v)
Definition: Matrix.H:71
Real & operator()(Index i, Index j)
Definition: Matrix.H:153
Definition: Matrix.H:53
Vector & PermuteBack(Vector &v, Permutation &p)
Definition: Matrix.H:267
const Index dim
Definition: Matrix.H:60
Vector & operator=(Vector &v)
Definition: Matrix.H:245
~Permutation()
Definition: Matrix.H:107
Vector & operator*=(Real x)
Definition: Matrix.H:75
Index & operator[](Index n)
Definition: Matrix.H:112
const Index dim
Definition: Matrix.H:13