GeneralBrokenLines V03-01-01
using EIGEN
VMatrix.cpp
Go to the documentation of this file.
1/*
2 * VMatrix.cpp
3 *
4 * Created on: Feb 15, 2012
5 * Author: kleinwrt
6 */
7
30#include "VMatrix.h"
31
33namespace gbl {
34
35/*********** simple Matrix based on std::vector<double> **********/
36
37VMatrix::VMatrix(const unsigned int nRows, const unsigned int nCols) :
38 numRows(nRows), numCols(nCols), theVec(nRows * nCols) {
39}
40
41VMatrix::VMatrix(const VMatrix &aMatrix) :
42 numRows(aMatrix.numRows), numCols(aMatrix.numCols), theVec(
43 aMatrix.theVec) {
44
45}
46
48}
49
51
55void VMatrix::resize(const unsigned int nRows, const unsigned int nCols) {
56 numRows = nRows;
57 numCols = nCols;
58 theVec.resize(nRows * nCols);
59}
60
63 std::fill(theVec.begin(), theVec.end(), 0.);
64}
65
67
71 VMatrix aResult(numCols, numRows);
72 for (unsigned int i = 0; i < numRows; ++i) {
73 for (unsigned int j = 0; j < numCols; ++j) {
74 aResult(j, i) = theVec[numCols * i + j];
75 }
76 }
77 return aResult;
78}
79
81
84unsigned int VMatrix::getNumRows() const {
85 return numRows;
86}
87
89
92unsigned int VMatrix::getNumCols() const {
93 return numCols;
94}
95
97void VMatrix::print() const {
98 std::cout << " VMatrix: " << numRows << "*" << numCols << std::endl;
99 for (unsigned int i = 0; i < numRows; ++i) {
100 for (unsigned int j = 0; j < numCols; ++j) {
101 if (j % 5 == 0) {
102 std::cout << std::endl << std::setw(4) << i << ","
103 << std::setw(4) << j << "-" << std::setw(4)
104 << std::min(j + 4, numCols) << ":";
105 }
106 std::cout << std::setw(13) << theVec[numCols * i + j];
107 }
108 }
109 std::cout << std::endl << std::endl;
110}
111
113VVector VMatrix::operator*(const VVector &aVector) const {
114 VVector aResult(numRows);
115 for (unsigned int i = 0; i < this->numRows; ++i) {
116 double sum = 0.0;
117 for (unsigned int j = 0; j < this->numCols; ++j) {
118 sum += theVec[numCols * i + j] * aVector(j);
119 }
120 aResult(i) = sum;
121 }
122 return aResult;
123}
124
126VMatrix VMatrix::operator*(const VMatrix &aMatrix) const {
127
128 VMatrix aResult(numRows, aMatrix.numCols);
129 for (unsigned int i = 0; i < numRows; ++i) {
130 for (unsigned int j = 0; j < aMatrix.numCols; ++j) {
131 double sum = 0.0;
132 for (unsigned int k = 0; k < numCols; ++k) {
133 sum += theVec[numCols * i + k] * aMatrix(k, j);
134 }
135 aResult(i, j) = sum;
136 }
137 }
138 return aResult;
139}
140
142VMatrix VMatrix::operator+(const VMatrix &aMatrix) const {
143 VMatrix aResult(numRows, numCols);
144 for (unsigned int i = 0; i < numRows; ++i) {
145 for (unsigned int j = 0; j < numCols; ++j) {
146 aResult(i, j) = theVec[numCols * i + j] + aMatrix(i, j);
147 }
148 }
149 return aResult;
150}
151
154 if (this != &aMatrix) { // Gracefully handle self assignment
155 numRows = aMatrix.getNumRows();
156 numCols = aMatrix.getNumCols();
157 theVec.resize(numRows * numCols);
158 for (unsigned int i = 0; i < numRows; ++i) {
159 for (unsigned int j = 0; j < numCols; ++j) {
160 theVec[numCols * i + j] = aMatrix(i, j);
161 }
162 }
163 }
164 return *this;
165}
166
167/*********** simple symmetric Matrix based on std::vector<double> **********/
168
169VSymMatrix::VSymMatrix(const unsigned int nRows) :
170 numRows(nRows), theVec((nRows * nRows + nRows) / 2) {
171}
172
174}
175
177
180void VSymMatrix::resize(const unsigned int nRows) {
181 numRows = nRows;
182 theVec.resize((nRows * nRows + nRows) / 2);
183}
184
187 std::fill(theVec.begin(), theVec.end(), 0.);
188}
189
191
194unsigned int VSymMatrix::getNumRows() const {
195 return numRows;
196}
197
199void VSymMatrix::print() const {
200 std::cout << " VSymMatrix: " << numRows << "*" << numRows << std::endl;
201 for (unsigned int i = 0; i < numRows; ++i) {
202 for (unsigned int j = 0; j <= i; ++j) {
203 if (j % 5 == 0) {
204 std::cout << std::endl << std::setw(4) << i << ","
205 << std::setw(4) << j << "-" << std::setw(4)
206 << std::min(j + 4, i) << ":";
207 }
208 std::cout << std::setw(13) << theVec[(i * i + i) / 2 + j];
209 }
210 }
211 std::cout << std::endl << std::endl;
212}
213
216 VSymMatrix aResult(numRows);
217 for (unsigned int i = 0; i < numRows; ++i) {
218 for (unsigned int j = 0; j <= i; ++j) {
219 aResult(i, j) = theVec[(i * i + i) / 2 + j] - aMatrix(i, j);
220 }
221 }
222 return aResult;
223}
224
226VVector VSymMatrix::operator*(const VVector &aVector) const {
227 VVector aResult(numRows);
228 for (unsigned int i = 0; i < numRows; ++i) {
229 aResult(i) = theVec[(i * i + i) / 2 + i] * aVector(i);
230 for (unsigned int j = 0; j < i; ++j) {
231 aResult(j) += theVec[(i * i + i) / 2 + j] * aVector(i);
232 aResult(i) += theVec[(i * i + i) / 2 + j] * aVector(j);
233 }
234 }
235 return aResult;
236}
237
239VMatrix VSymMatrix::operator*(const VMatrix &aMatrix) const {
240 unsigned int nCol = aMatrix.getNumCols();
241 VMatrix aResult(numRows, nCol);
242 for (unsigned int l = 0; l < nCol; ++l) {
243 for (unsigned int i = 0; i < numRows; ++i) {
244 aResult(i, l) = theVec[(i * i + i) / 2 + i] * aMatrix(i, l);
245 for (unsigned int j = 0; j < i; ++j) {
246 aResult(j, l) += theVec[(i * i + i) / 2 + j] * aMatrix(i, l);
247 aResult(i, l) += theVec[(i * i + i) / 2 + j] * aMatrix(j, l);
248 }
249 }
250 }
251 return aResult;
252}
253
254/*********** simple Vector based on std::vector<double> **********/
255
256VVector::VVector(const unsigned int nRows) :
257 numRows(nRows), theVec(nRows) {
258}
259
260VVector::VVector(const VVector &aVector) :
261 numRows(aVector.numRows), theVec(aVector.theVec) {
262
263}
264
266}
267
269
272void VVector::resize(const unsigned int nRows) {
273 numRows = nRows;
274 theVec.resize(nRows);
275}
276
279 std::fill(theVec.begin(), theVec.end(), 0.);
280}
281
283
288VVector VVector::getVec(unsigned int len, unsigned int start) const {
289 VVector aResult(len);
290 std::memcpy(&aResult.theVec[0], &theVec[start], sizeof(double) * len);
291 return aResult;
292}
293
295
299void VVector::putVec(const VVector &aVector, unsigned int start) {
300 std::memcpy(&theVec[start], &aVector.theVec[0],
301 sizeof(double) * aVector.numRows);
302}
303
305
308unsigned int VVector::getNumRows() const {
309 return numRows;
310}
311
313void VVector::print() const {
314 std::cout << " VVector: " << numRows << std::endl;
315 for (unsigned int i = 0; i < numRows; ++i) {
316
317 if (i % 5 == 0) {
318 std::cout << std::endl << std::setw(4) << i << "-" << std::setw(4)
319 << std::min(i + 4, numRows) << ":";
320 }
321 std::cout << std::setw(13) << theVec[i];
322 }
323 std::cout << std::endl << std::endl;
324}
325
327VVector VVector::operator-(const VVector &aVector) const {
328 VVector aResult(numRows);
329 for (unsigned int i = 0; i < numRows; ++i) {
330 aResult(i) = theVec[i] - aVector(i);
331 }
332 return aResult;
333}
334
337 if (this != &aVector) { // Gracefully handle self assignment
338 numRows = aVector.getNumRows();
339 theVec.resize(numRows);
340 for (unsigned int i = 0; i < numRows; ++i) {
341 theVec[i] = aVector(i);
342 }
343 }
344 return *this;
345}
346
347/*============================================================================
348 from mpnum.F (MillePede-II by V. Blobel, Univ. Hamburg)
349 ============================================================================*/
351
363unsigned int VSymMatrix::invert() {
364
365 const double eps = 1.0E-10;
366 unsigned int aSize = numRows;
367 std::vector<int> next(aSize);
368 std::vector<double> diag(aSize);
369 int nSize = aSize;
370
371 int first = 1;
372 for (int i = 1; i <= nSize; ++i) {
373 next[i - 1] = i + 1; // set "next" pointer
374 diag[i - 1] = fabs(theVec[(i * i + i) / 2 - 1]); // save abs of diagonal elements
375 }
376 next[aSize - 1] = -1; // end flag
377
378 unsigned int nrank = 0;
379 for (int i = 1; i <= nSize; ++i) { // start of loop
380 int kp = 0;
381 double vkk = 0.0;
382
383 int jp = first;
384 int previous = 0;
385 int last = previous;
386 // look for pivot
387 while (jp > 0) {
388 int jj = (jp * jp + jp) / 2 - 1;
389 if (fabs(theVec[jj]) > std::max(fabs(vkk), eps * diag[jp - 1])) {
390 vkk = theVec[jj];
391 kp = jp;
392 last = previous;
393 }
394 previous = jp;
395 jp = next[jp - 1];
396 }
397 // pivot found
398 if (kp > 0) {
399 int kk = (kp * kp + kp) / 2 - 1;
400 if (last <= 0) {
401 first = next[kp - 1];
402 } else {
403 next[last - 1] = next[kp - 1];
404 }
405 next[kp - 1] = 0; // index is used, reset
406 nrank++; // increase rank and ...
407
408 vkk = 1.0 / vkk;
409 theVec[kk] = -vkk;
410 int jk = kk - kp;
411 int jl = -1;
412 for (int j = 1; j <= nSize; ++j) { // elimination
413 if (j == kp) {
414 jk = kk;
415 jl += j;
416 } else {
417 if (j < kp) {
418 ++jk;
419 } else {
420 jk += j - 1;
421 }
422
423 double vjk = theVec[jk];
424 theVec[jk] = vkk * vjk;
425 int lk = kk - kp;
426 if (j >= kp) {
427 for (int l = 1; l <= kp - 1; ++l) {
428 ++jl;
429 ++lk;
430 theVec[jl] -= theVec[lk] * vjk;
431 }
432 ++jl;
433 lk = kk;
434 for (int l = kp + 1; l <= j; ++l) {
435 ++jl;
436 lk += l - 1;
437 theVec[jl] -= theVec[lk] * vjk;
438 }
439 } else {
440 for (int l = 1; l <= j; ++l) {
441 ++jl;
442 ++lk;
443 theVec[jl] -= theVec[lk] * vjk;
444 }
445 }
446 }
447 }
448 } else {
449 for (int k = 1; k <= nSize; ++k) {
450 if (next[k - 1] >= 0) {
451 int kk = (k * k - k) / 2 - 1;
452 for (int j = 1; j <= nSize; ++j) {
453 if (next[j - 1] >= 0) {
454 theVec[kk + j] = 0.0; // clear matrix row/col
455 }
456 }
457 }
458 }
459 throw 1; // singular
460 }
461 }
462 for (int ij = 0; ij < (nSize * nSize + nSize) / 2; ++ij) {
463 theVec[ij] = -theVec[ij]; // finally reverse sign of all matrix elements
464 }
465 return nrank;
466}
467}
VMatrix definition.
Simple Matrix based on std::vector<double>
Definition: VMatrix.h:64
unsigned int getNumCols() const
Get number of columns.
Definition: VMatrix.cpp:92
VMatrix transpose() const
Get transposed matrix.
Definition: VMatrix.cpp:70
VVector operator*(const VVector &aVector) const
Multiplication Matrix*Vector.
Definition: VMatrix.cpp:113
unsigned int numRows
Number of rows.
Definition: VMatrix.h:82
unsigned int getNumRows() const
Get number of rows.
Definition: VMatrix.cpp:84
void resize(const unsigned int nRows, const unsigned int nCols)
Resize Matrix.
Definition: VMatrix.cpp:55
VMatrix operator+(const VMatrix &aMatrix) const
Addition Matrix+Matrix.
Definition: VMatrix.cpp:142
unsigned int numCols
Number of columns.
Definition: VMatrix.h:83
VMatrix & operator=(const VMatrix &aMatrix)
Assignment Matrix=Matrix.
Definition: VMatrix.cpp:153
VMatrix(const unsigned int nRows=0, const unsigned int nCols=0)
Definition: VMatrix.cpp:37
std::vector< double > theVec
Data.
Definition: VMatrix.h:84
void print() const
Print matrix.
Definition: VMatrix.cpp:97
virtual ~VMatrix()
Definition: VMatrix.cpp:47
void setZero()
Set content to 0.
Definition: VMatrix.cpp:62
Simple symmetric Matrix based on std::vector<double>
Definition: VMatrix.h:88
VSymMatrix(const unsigned int nRows=0)
Definition: VMatrix.cpp:169
VVector operator*(const VVector &aVector) const
Multiplication SymMatrix*Vector.
Definition: VMatrix.cpp:226
void print() const
Print matrix.
Definition: VMatrix.cpp:199
unsigned int invert()
Matrix inversion.
Definition: VMatrix.cpp:363
unsigned int getNumRows() const
Get number of rows (= number of colums).
Definition: VMatrix.cpp:194
std::vector< double > theVec
Data (symmetric storage)
Definition: VMatrix.h:104
unsigned int numRows
Number of rows.
Definition: VMatrix.h:103
void resize(const unsigned int nRows)
Resize symmetric matrix.
Definition: VMatrix.cpp:180
virtual ~VSymMatrix()
Definition: VMatrix.cpp:173
VSymMatrix operator-(const VMatrix &aMatrix) const
Subtraction SymMatrix-(sym)Matrix.
Definition: VMatrix.cpp:215
void setZero()
Set content to 0.
Definition: VMatrix.cpp:186
Simple Vector based on std::vector<double>
Definition: VMatrix.h:43
virtual ~VVector()
Definition: VMatrix.cpp:265
void putVec(const VVector &aVector, unsigned int start=0)
Put part of vector.
Definition: VMatrix.cpp:299
std::vector< double > theVec
Data.
Definition: VMatrix.h:60
VVector getVec(unsigned int len, unsigned int start=0) const
Get part of vector.
Definition: VMatrix.cpp:288
VVector operator-(const VVector &aVector) const
Subtraction Vector-Vector.
Definition: VMatrix.cpp:327
void setZero()
Set content to 0.
Definition: VMatrix.cpp:278
void resize(const unsigned int nRows)
Resize vector.
Definition: VMatrix.cpp:272
VVector(const unsigned int nRows=0)
Definition: VMatrix.cpp:256
unsigned int numRows
Number of rows.
Definition: VMatrix.h:59
void print() const
Print vector.
Definition: VMatrix.cpp:313
VVector & operator=(const VVector &aVector)
Assignment Vector=Vector.
Definition: VMatrix.cpp:336
unsigned int getNumRows() const
Get number of rows.
Definition: VMatrix.cpp:308
Namespace for the general broken lines package.