libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moMathDMatrix.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moMathDMatrix.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28  Andrés Colubri
29 
30  Portions taken from
31  Wild Magic Source Code
32  David Eberly
33  http://www.geometrictools.com
34  Copyright (c) 1998-2007
35 
36 *******************************************************************************/
37 
38 #include "moMathDMatrix.h"
39 
40 // moDMatrix class ------------------------------------------------
41 
42 template <class Real>
43 moDMatrix<Real>::moDMatrix (int iRows, int iCols)
44 {
45  m_afData = 0;
46  m_aafEntry = 0;
47  SetSize(iRows,iCols);
48 }
49 
50 template <class Real>
51 moDMatrix<Real>::moDMatrix (int iRows, int iCols, const Real* afEntry)
52 {
53  m_afData = 0;
54  m_aafEntry = 0;
55  SetMatrix(iRows,iCols,afEntry);
56 }
57 
58 template <class Real>
59 moDMatrix<Real>::moDMatrix (int iRows, int iCols, const Real** aafMatrix)
60 {
61  m_afData = 0;
62  m_aafEntry = 0;
63  SetMatrix(iRows,iCols,aafMatrix);
64 }
65 
66 template <class Real>
68 {
69  m_iRows = 0;
70  m_iCols = 0;
71  m_iQuantity = 0;
72  m_afData = 0;
73  m_aafEntry = 0;
74  *this = rkM;
75 }
76 
77 template <class Real>
79 {
80  Deallocate();
81 }
82 
83 template <class Real>
84 void moDMatrix<Real>::Allocate (bool bSetToZero)
85 {
86  // //assert: m_iRows, m_iCols, and m_iQuantity already initialized
87 
88  m_afData = new Real[m_iQuantity];
89  if (bSetToZero)
90  {
91  memset(m_afData,0,m_iQuantity*sizeof(Real));
92  }
93 
94  m_aafEntry = new Real*[m_iRows];
95  for (int iRow = 0; iRow < m_iRows; iRow++)
96  {
97  m_aafEntry[iRow] = &m_afData[iRow*m_iCols];
98  }
99 }
100 
101 template <class Real>
103 {
104  delete[] m_afData;
105  delete[] m_aafEntry;
106 }
107 
108 template <class Real>
109 void moDMatrix<Real>::SetSize (int iRows, int iCols)
110 {
111  Deallocate();
112  if (iRows > 0 && iCols > 0)
113  {
114  m_iRows = iRows;
115  m_iCols = iCols;
116  m_iQuantity = m_iRows*m_iCols;
117  Allocate(true);
118  }
119  else
120  {
121  m_iRows = 0;
122  m_iCols = 0;
123  m_iQuantity = 0;
124  m_afData = 0;
125  m_aafEntry = 0;
126  }
127 }
128 
129 template <class Real>
130 void moDMatrix<Real>::GetSize (int& riRows, int& riCols) const
131 {
132  riRows = m_iRows;
133  riCols = m_iCols;
134 }
135 
136 template <class Real>
138 {
139  return m_iRows;
140 }
141 
142 template <class Real>
144 {
145  return m_iCols;
146 }
147 
148 template <class Real>
150 {
151  return m_iQuantity;
152 }
153 
154 template <class Real>
155 moDMatrix<Real>::operator const Real* () const
156 {
157  return m_afData;
158 }
159 
160 template <class Real>
162 {
163  return m_afData;
164 }
165 
166 template <class Real>
167 const Real* moDMatrix<Real>::operator[] (int iRow) const
168 {
169  //assert(0 <= iRow && iRow < m_iRows);
170  return m_aafEntry[iRow];
171 }
172 
173 template <class Real>
175 {
176  //assert(0 <= iRow && iRow < m_iRows);
177  return m_aafEntry[iRow];
178 }
179 
180 template <class Real>
181 Real moDMatrix<Real>::operator() (int iRow, int iCol) const
182 {
183  return m_aafEntry[iRow][iCol];
184 }
185 
186 template <class Real>
187 Real& moDMatrix<Real>::operator() (int iRow, int iCol)
188 {
189  //assert(0 <= iRow && iRow < m_iRows && 0 <= iCol && iCol <= m_iCols);
190  return m_aafEntry[iRow][iCol];
191 }
192 
193 template <class Real>
194 void moDMatrix<Real>::SwapRows (int iRow0, int iRow1)
195 {
196  //assert(0 <= iRow0 && iRow0 < m_iRows && 0 <= iRow1 && iRow1 < m_iRows);
197  Real* afSave = m_aafEntry[iRow0];
198  m_aafEntry[iRow0] = m_aafEntry[iRow1];
199  m_aafEntry[iRow1] = afSave;
200 }
201 
202 template <class Real>
203 void moDMatrix<Real>::SetRow (int iRow, const moDVector<Real>& rkV)
204 {
205  //assert((0 <= iRow && iRow < m_iRows) && (rkV.GetSize() == m_iCols));
206  for (int iCol = 0; iCol < m_iCols; iCol++)
207  {
208  m_aafEntry[iRow][iCol] = rkV[iCol];
209  }
210 }
211 
212 template <class Real>
214 {
215  //assert(0 <= iRow && iRow < m_iRows);
216  moDVector<Real> kV(m_iCols);
217  for (int iCol = 0; iCol < m_iCols; iCol++)
218  {
219  kV[iCol] = m_aafEntry[iRow][iCol];
220  }
221  return kV;
222 }
223 
224 template <class Real>
225 void moDMatrix<Real>::SetColumn (int iCol, const moDVector<Real>& rkV)
226 {
227  //assert((0 <= iCol && iCol < m_iCols) && (rkV.GetSize() == m_iRows));
228  for (int iRow = 0; iRow < m_iRows; iRow++)
229  {
230  m_aafEntry[iRow][iCol] = rkV[iRow];
231  }
232 }
233 
234 template <class Real>
236 {
237  //assert(0 <= iCol && iCol < m_iCols);
238  moDVector<Real> kV(m_iRows);
239  for (int iRow = 0; iRow < m_iRows; iRow++)
240  {
241  kV[iRow] = m_aafEntry[iRow][iCol];
242  }
243  return kV;
244 }
245 
246 template <class Real>
247 void moDMatrix<Real>::SetMatrix (int iRows, int iCols, const Real* afData)
248 {
249  Deallocate();
250  if (iRows > 0 && iCols > 0)
251  {
252  m_iRows = iRows;
253  m_iCols = iCols;
254  m_iQuantity = m_iRows*m_iCols;
255  Allocate(false);
256  size_t uiSize = m_iQuantity*sizeof(Real);
257  memcpy(m_afData,afData,uiSize);
258  }
259  else
260  {
261  m_iRows = 0;
262  m_iCols = 0;
263  m_iQuantity = 0;
264  m_afData = 0;
265  m_aafEntry = 0;
266  }
267 }
268 
269 template <class Real>
270 void moDMatrix<Real>::SetMatrix (int iRows, int iCols, const Real** aafEntry)
271 {
272  Deallocate();
273  if (iRows > 0 && iCols > 0)
274  {
275  m_iRows = iRows;
276  m_iCols = iCols;
277  m_iQuantity = m_iRows*m_iCols;
278  Allocate(false);
279  for (int iRow = 0; iRow < m_iRows; iRow++)
280  {
281  for (int iCol = 0; iCol < m_iCols; iCol++)
282  {
283  m_aafEntry[iRow][iCol] = aafEntry[iRow][iCol];
284  }
285  }
286  }
287  else
288  {
289  m_iRows = 0;
290  m_iCols = 0;
291  m_iQuantity = 0;
292  m_afData = 0;
293  m_aafEntry = 0;
294  }
295 }
296 
297 template <class Real>
298 void moDMatrix<Real>::GetColumnMajor (Real* afCMajor) const
299 {
300  for (int iRow = 0, i = 0; iRow < m_iRows; iRow++)
301  {
302  for (int iCol = 0; iCol < m_iCols; iCol++)
303  {
304  afCMajor[i++] = m_aafEntry[iCol][iRow];
305  }
306  }
307 }
308 
309 template <class Real>
311 {
312  if (rkM.m_iQuantity > 0)
313  {
314  if (m_iRows != rkM.m_iRows || m_iCols != rkM.m_iCols)
315  {
316  Deallocate();
317  m_iRows = rkM.m_iRows;
318  m_iCols = rkM.m_iCols;
319  m_iQuantity = rkM.m_iQuantity;
320  Allocate(false);
321  }
322  for (int iRow = 0; iRow < m_iRows; iRow++)
323  {
324  for (int iCol = 0; iCol < m_iCols; iCol++)
325  {
326  m_aafEntry[iRow][iCol] = rkM.m_aafEntry[iRow][iCol];
327  }
328  }
329  }
330  else
331  {
332  Deallocate();
333  m_iRows = 0;
334  m_iCols = 0;
335  m_iQuantity = 0;
336  m_afData = 0;
337  m_aafEntry = 0;
338  }
339  return *this;
340 }
341 
342 template <class Real>
344 {
345  return memcmp(m_afData,rkM.m_afData,m_iQuantity*sizeof(Real));
346 }
347 
348 template <class Real>
350 {
351  return CompareArrays(rkM) == 0;
352 }
353 
354 template <class Real>
356 {
357  return CompareArrays(rkM) != 0;
358 }
359 
360 template <class Real>
362 {
363  return CompareArrays(rkM) < 0;
364 }
365 
366 template <class Real>
368 {
369  return CompareArrays(rkM) <= 0;
370 }
371 
372 template <class Real>
374 {
375  return CompareArrays(rkM) > 0;
376 }
377 
378 template <class Real>
380 {
381  return CompareArrays(rkM) >= 0;
382 }
383 
384 template <class Real>
386 {
387  moDMatrix<Real> kSum(rkM.m_iRows,rkM.m_iCols);
388  for (int i = 0; i < m_iQuantity; i++)
389  {
390  kSum.m_afData[i] = m_afData[i] + rkM.m_afData[i];
391  }
392  return kSum;
393 }
394 
395 template <class Real>
397 {
398  moDMatrix<Real> kDiff(rkM.m_iRows,rkM.m_iCols);
399  for (int i = 0; i < m_iQuantity; i++)
400  {
401  kDiff.m_afData[i] = m_afData[i] - rkM.m_afData[i];
402  }
403  return kDiff;
404 }
405 
406 template <class Real>
408 {
409  // 'this' is RxN, 'M' is NxC, 'product = this*M' is RxC
410  //assert(m_iCols == rkM.m_iRows);
411  moDMatrix<Real> kProd(m_iRows,rkM.m_iCols);
412  for (int iRow = 0; iRow < kProd.m_iRows; iRow++)
413  {
414  for (int iCol = 0; iCol < kProd.m_iCols; iCol++)
415  {
416  for (int iMid = 0; iMid < m_iCols; iMid++)
417  {
418  kProd.m_aafEntry[iRow][iCol] += m_aafEntry[iRow][iMid] *
419  rkM.m_aafEntry[iMid][iCol];
420  }
421  }
422  }
423  return kProd;
424 }
425 
426 template <class Real>
428 {
429  moDMatrix<Real> kProd(m_iRows,m_iCols);
430  for (int i = 0; i < m_iQuantity; i++)
431  {
432  kProd.m_afData[i] = fScalar*m_afData[i];
433  }
434  return kProd;
435 }
436 
437 template <class Real>
439 {
440  moDMatrix<Real> kQuot(m_iRows,m_iCols);
441  int i;
442 
443  if (fScalar != (Real)0.0)
444  {
445  Real fInvScalar = ((Real)1.0)/fScalar;
446  for (i = 0; i < m_iQuantity; i++)
447  {
448  kQuot.m_afData[i] = fInvScalar*m_afData[i];
449  }
450  }
451  else
452  {
453  for (i = 0; i < m_iQuantity; i++)
454  {
455  kQuot.m_afData[i] = moMath<Real>::MAX_REAL;
456  }
457  }
458 
459  return kQuot;
460 }
461 
462 template <class Real>
464 {
465  moDMatrix<Real> kNeg(m_iRows,m_iCols);
466  for (int i = 0; i < m_iQuantity; i++)
467  {
468  kNeg.m_afData[i] = -m_afData[i];
469  }
470  return kNeg;
471 }
472 
473 template <class Real>
474 moDMatrix<Real> operator* (Real fScalar, const moDMatrix<Real>& rkM)
475 {
476  moDMatrix<Real> kProd(rkM.GetRows(),rkM.GetColumns());
477  const Real* afMEntry = rkM;
478  Real* afPEntry = kProd;
479  for (int i = 0; i < rkM.GetQuantity(); i++)
480  {
481  afPEntry[i] = fScalar*afMEntry[i];
482  }
483  return kProd;
484 }
485 
486 template <class Real>
488 {
489  for (int i = 0; i < m_iQuantity; i++)
490  {
491  m_afData[i] += rkM.m_afData[i];
492  }
493  return *this;
494 }
495 
496 template <class Real>
498 {
499  for (int i = 0; i < m_iQuantity; i++)
500  {
501  m_afData[i] -= rkM.m_afData[i];
502  }
503  return *this;
504 }
505 
506 template <class Real>
508 {
509  for (int i = 0; i < m_iQuantity; i++)
510  {
511  m_afData[i] *= fScalar;
512  }
513  return *this;
514 }
515 
516 template <class Real>
518 {
519  int i;
520 
521  if (fScalar != (Real)0.0)
522  {
523  Real fInvScalar = ((Real)1.0)/fScalar;
524  for (i = 0; i < m_iQuantity; i++)
525  {
526  m_afData[i] *= fInvScalar;
527  }
528  }
529  else
530  {
531  for (i = 0; i < m_iQuantity; i++)
532  {
533  m_afData[i] = moMath<Real>::MAX_REAL;
534  }
535  }
536 
537  return *this;
538 }
539 
540 template <class Real>
542 {
543  moDMatrix<Real> kTranspose(m_iCols,m_iRows);
544  for (int iRow = 0; iRow < m_iRows; iRow++)
545  {
546  for (int iCol = 0; iCol < m_iCols; iCol++)
547  {
548  kTranspose.m_aafEntry[iCol][iRow] = m_aafEntry[iRow][iCol];
549  }
550  }
551  return kTranspose;
552 }
553 
554 template <class Real>
556 {
557  // P = A^T*B, P[r][c] = sum_m A[m][r]*B[m][c]
558  //assert(m_iRows == rkM.m_iRows);
559  moDMatrix<Real> kProd(m_iCols,rkM.m_iCols);
560  for (int iRow = 0; iRow < kProd.m_iRows; iRow++)
561  {
562  for (int iCol = 0; iCol < kProd.m_iCols; iCol++)
563  {
564  for (int iMid = 0; iMid < m_iRows; iMid++)
565  {
566  kProd.m_aafEntry[iRow][iCol] += m_aafEntry[iMid][iRow] *
567  rkM.m_aafEntry[iMid][iCol];
568  }
569  }
570  }
571  return kProd;
572 }
573 
574 template <class Real>
576 {
577  // P = A*B^T, P[r][c] = sum_m A[r][m]*B[c][m]
578  //assert(m_iCols == rkM.m_iCols);
579  moDMatrix<Real> kProd(m_iRows,rkM.m_iRows);
580  for (int iRow = 0; iRow < kProd.m_iRows; iRow++)
581  {
582  for (int iCol = 0; iCol < kProd.m_iCols; iCol++)
583  {
584  for (int iMid = 0; iMid < m_iCols; iMid++)
585  {
586  kProd.m_aafEntry[iRow][iCol] += m_aafEntry[iRow][iMid] *
587  rkM.m_aafEntry[iCol][iMid];
588  }
589  }
590  }
591  return kProd;
592 }
593 
594 template <class Real>
596 {
597  //assert(rkV.GetSize() == m_iCols);
598  moDVector<Real> kProd(m_iRows);
599  for (int iRow = 0; iRow < m_iRows; iRow++)
600  {
601  for (int iCol = 0; iCol < m_iCols; iCol++)
602  {
603  kProd[iRow] += m_aafEntry[iRow][iCol]*rkV[iCol];
604  }
605 
606  }
607  return kProd;
608 }
609 
610 template <class Real>
612 {
613  //assert(rkV.GetSize() == rkM.GetRows());
614  moDVector<Real> kProd(rkM.GetColumns());
615  Real* afPEntry = kProd;
616  for (int iCol = 0; iCol < rkM.GetColumns(); iCol++)
617  {
618  for (int iRow = 0; iRow < rkM.GetRows(); iRow++)
619  {
620  afPEntry[iCol] += rkV[iRow]*rkM[iRow][iCol];
621  }
622  }
623  return kProd;
624 }
625 
626 template <class Real>
628  const
629 {
630  //assert(rkU.GetSize() == m_iRows && rkV.GetSize() == m_iCols);
631  return rkU.Dot((*this)*rkV);
632 }
633 
634 template <class Real>
636 {
637  // computations are performed in-place
638  if (GetRows() > 0 && GetRows() != GetColumns())
639  {
640  return false;
641  }
642 
643  int iSize = GetRows();
644  rkInverse = *this;
645 
646  int* aiColIndex = new int[iSize];
647  int* aiRowIndex = new int[iSize];
648  bool* abPivoted = new bool[iSize];
649  memset(abPivoted,0,iSize*sizeof(bool));
650 
651  int i1, i2, iRow = 0, iCol = 0;
652  Real fSave;
653 
654  // elimination by full pivoting
655  for (int i0 = 0; i0 < iSize; i0++)
656  {
657  // search matrix (excluding pivoted rows) for maximum absolute entry
658  Real fMax = (Real)0.0;
659  for (i1 = 0; i1 < iSize; i1++)
660  {
661  if (!abPivoted[i1])
662  {
663  for (i2 = 0; i2 < iSize; i2++)
664  {
665  if (!abPivoted[i2])
666  {
667  Real fAbs = moMath<Real>::FAbs(rkInverse[i1][i2]);
668  if (fAbs > fMax)
669  {
670  fMax = fAbs;
671  iRow = i1;
672  iCol = i2;
673  }
674  }
675  }
676  }
677  }
678 
679  if (fMax == (Real)0.0)
680  {
681  // matrix is not invertible
682  delete[] aiColIndex;
683  delete[] aiRowIndex;
684  delete[] abPivoted;
685  return false;
686  }
687 
688  abPivoted[iCol] = true;
689 
690  // swap rows so that A[iCol][iCol] contains the pivot entry
691  if (iRow != iCol)
692  {
693  rkInverse.SwapRows(iRow,iCol);
694  }
695 
696  // keep track of the permutations of the rows
697  aiRowIndex[i0] = iRow;
698  aiColIndex[i0] = iCol;
699 
700  // scale the row so that the pivot entry is 1
701  Real fInv = ((Real)1.0)/rkInverse[iCol][iCol];
702  rkInverse[iCol][iCol] = (Real)1.0;
703  for (i2 = 0; i2 < iSize; i2++)
704  {
705  rkInverse[iCol][i2] *= fInv;
706  }
707 
708  // zero out the pivot column locations in the other rows
709  for (i1 = 0; i1 < iSize; i1++)
710  {
711  if (i1 != iCol)
712  {
713  fSave = rkInverse[i1][iCol];
714  rkInverse[i1][iCol] = (Real)0.0;
715  for (i2 = 0; i2 < iSize; i2++)
716  {
717  rkInverse[i1][i2] -= rkInverse[iCol][i2]*fSave;
718  }
719  }
720  }
721  }
722 
723  // reorder rows so that A[][] stores the inverse of the original matrix
724  for (i1 = iSize-1; i1 >= 0; i1--)
725  {
726  if (aiRowIndex[i1] != aiColIndex[i1])
727  {
728  for (i2 = 0; i2 < iSize; i2++)
729  {
730  fSave = rkInverse[i2][aiRowIndex[i1]];
731  rkInverse[i2][aiRowIndex[i1]] =
732  rkInverse[i2][aiColIndex[i1]];
733  rkInverse[i2][aiColIndex[i1]] = fSave;
734  }
735  }
736  }
737 
738  delete[] aiColIndex;
739  delete[] aiRowIndex;
740  delete[] abPivoted;
741  return true;
742 }
743 
744 // moDBandedMatrix class ------------------------------------------------
745 
746 template <class Real>
747 moDBandedMatrix<Real>::moDBandedMatrix (int iSize, int iLBands, int iUBands)
748 {
749  //assert(iSize > 0 && iLBands >= 0 && iUBands >= 0);
750  //assert(iLBands < iSize && iUBands < iSize);
751 
752  m_iSize = iSize;
753  m_iLBands = iLBands;
754  m_iUBands = iUBands;
755  Allocate();
756 }
757 
758 template <class Real>
760 {
761  m_afDBand = 0;
762  m_aafLBand = 0;
763  m_aafUBand = 0;
764  *this = rkM;
765 }
766 
767 template <class Real>
769 {
770  Deallocate();
771 }
772 
773 template <class Real>
775 {
776  Deallocate();
777  m_iSize = rkM.m_iSize;
778  m_iLBands = rkM.m_iLBands;
779  m_iUBands = rkM.m_iUBands;
780  Allocate();
781 
782  size_t uiSize = m_iSize*sizeof(Real);
783  memcpy(m_afDBand,rkM.m_afDBand,uiSize);
784 
785  int i;
786  for (i = 0; i < m_iLBands; i++)
787  {
788  uiSize = (m_iSize-1-i)*sizeof(Real);
789  memcpy(m_aafLBand[i],rkM.m_aafLBand[i],uiSize);
790  }
791 
792  for (i = 0; i < m_iUBands; i++)
793  {
794  uiSize = (m_iSize-1-i)*sizeof(Real);
795  memcpy(m_aafUBand[i],rkM.m_aafUBand[i],uiSize);
796  }
797 
798  return *this;
799 }
800 
801 template <class Real>
803 {
804  return m_iSize;
805 }
806 
807 template <class Real>
809 {
810  return m_iLBands;
811 }
812 
813 template <class Real>
815 {
816  return m_iUBands;
817 }
818 
819 template <class Real>
821 {
822  return m_afDBand;
823 }
824 
825 template <class Real>
827 {
828  return m_afDBand;
829 }
830 
831 template <class Real>
833 {
834  //assert(0 <= i && i < m_iLBands);
835  return m_iSize-1-i;
836 }
837 
838 template <class Real>
840 {
841  if ( m_aafLBand )
842  {
843  //assert(0 <= i && i < m_iLBands);
844  return m_aafLBand[i];
845  }
846  return 0;
847 }
848 
849 template <class Real>
850 const Real* moDBandedMatrix<Real>::GetLBand (int i) const
851 {
852  if (m_aafLBand)
853  {
854  //assert(0 <= i && i < m_iLBands);
855  return m_aafLBand[i];
856  }
857  return 0;
858 }
859 
860 template <class Real>
862 {
863  //assert(0 <= i && i < m_iUBands);
864  return m_iSize-1-i;
865 }
866 
867 template <class Real>
869 {
870  if (m_aafUBand)
871  {
872  //assert(0 <= i && i < m_iUBands);
873  return m_aafUBand[i];
874  }
875  return 0;
876 }
877 
878 template <class Real>
879 const Real* moDBandedMatrix<Real>::GetUBand (int i) const
880 {
881  if (m_aafUBand)
882  {
883  //assert(0 <= i && i < m_iUBands);
884  return m_aafUBand[i];
885  }
886  return 0;
887 }
888 
889 template <class Real>
890 Real& moDBandedMatrix<Real>::operator() (int iRow, int iCol)
891 {
892  //assert(0 <= iRow && iRow < m_iSize && 0 <= iCol && iCol < m_iSize);
893 
894  int iBand = iCol - iRow;
895  if (iBand > 0)
896  {
897  if (--iBand < m_iUBands && iRow < m_iSize-1-iBand)
898  {
899  return m_aafUBand[iBand][iRow];
900  }
901  }
902  else if ( iBand < 0 )
903  {
904  iBand = -iBand;
905  if (--iBand < m_iLBands && iCol < m_iSize-1-iBand)
906  {
907  return m_aafLBand[iBand][iCol];
908  }
909  }
910  else
911  {
912  return m_afDBand[iRow];
913  }
914 
915  static Real s_fDummy = (Real)0.0;
916  return s_fDummy;
917 }
918 
919 template <class Real>
920 Real moDBandedMatrix<Real>::operator() (int iRow, int iCol) const
921 {
922  //assert(0 <= iRow && iRow < m_iSize && 0 <= iCol && iCol < m_iSize);
923 
924  int iBand = iCol - iRow;
925  if (iBand > 0)
926  {
927  if (--iBand < m_iUBands && iRow < m_iSize-1-iBand)
928  {
929  return m_aafUBand[iBand][iRow];
930  }
931  }
932  else if ( iBand < 0 )
933  {
934  iBand = -iBand;
935  if (--iBand < m_iLBands && iCol < m_iSize-1-iBand)
936  {
937  return m_aafLBand[iBand][iCol];
938  }
939  }
940  else
941  {
942  return m_afDBand[iRow];
943  }
944 
945  return (Real)0.0;
946 }
947 
948 template <class Real>
950 {
951  //assert(m_iSize > 0);
952 
953  memset(m_afDBand,0,m_iSize*sizeof(Real));
954 
955  int i;
956  for (i = 0; i < m_iLBands; i++)
957  {
958  memset(m_aafLBand[i],0,(m_iSize-1-i)*sizeof(Real));
959  }
960 
961  for (i = 0; i < m_iUBands; i++)
962  {
963  memset(m_aafUBand[i],0,(m_iSize-1-i)*sizeof(Real));
964  }
965 }
966 
967 template <class Real>
969 {
970  //assert(m_iSize > 0);
971 
972  int i;
973  for (i = 0; i < m_iSize; i++)
974  {
975  m_afDBand[i] = (Real)1.0;
976  }
977 
978  for (i = 0; i < m_iLBands; i++)
979  {
980  memset(m_aafLBand[i],0,(m_iSize-1-i)*sizeof(Real));
981  }
982 
983  for (i = 0; i < m_iUBands; i++)
984  {
985  memset(m_aafUBand[i],0,(m_iSize-1-i)*sizeof(Real));
986  }
987 }
988 
989 template <class Real>
991 {
992  // //assert: m_iSize, m_iLBands, m_iRBandQuantity already set
993  // //assert: m_afDBand, m_aafLBand, m_aafUBand all null
994 
995  m_afDBand = new Real[m_iSize];
996  memset(m_afDBand,0,m_iSize*sizeof(Real));
997 
998  if (m_iLBands > 0)
999  {
1000  m_aafLBand = new Real*[m_iLBands];
1001  }
1002  else
1003  {
1004  m_aafLBand = 0;
1005  }
1006 
1007  if (m_iUBands > 0)
1008  {
1009  m_aafUBand = new Real*[m_iUBands];
1010  }
1011  else
1012  {
1013  m_aafUBand = 0;
1014  }
1015 
1016  int i;
1017  for (i = 0; i < m_iLBands; i++)
1018  {
1019  m_aafLBand[i] = new Real[m_iSize-1-i];
1020  memset(m_aafLBand[i],0,(m_iSize-1-i)*sizeof(Real));
1021  }
1022 
1023  for (i = 0; i < m_iUBands; i++)
1024  {
1025  m_aafUBand[i] = new Real[m_iSize-1-i];
1026  memset(m_aafUBand[i],0,(m_iSize-1-i)*sizeof(Real));
1027  }
1028 }
1029 
1030 template <class Real>
1032 {
1033  delete[] m_afDBand;
1034 
1035  int i;
1036 
1037  if (m_aafLBand)
1038  {
1039  for (i = 0; i < m_iLBands; i++)
1040  {
1041  delete[] m_aafLBand[i];
1042  }
1043 
1044  delete[] m_aafLBand;
1045  m_aafLBand = 0;
1046  }
1047 
1048  if (m_aafUBand)
1049  {
1050  for (i = 0; i < m_iUBands; i++)
1051  {
1052  delete[] m_aafUBand[i];
1053  }
1054 
1055  delete[] m_aafUBand;
1056  m_aafUBand = 0;
1057  }
1058 }
1059 
1060 
void SetRow(int iRow, const moDVector< Real > &rkV)
moDBandedMatrix(int iSize, int iLBands, int iUBands)
Real operator()(int iRow, int iCol) const
moDMatrix operator-() const
void SetMatrix(int iRows, int iCols, const Real *afEntry)
moDMatrix & operator+=(const moDMatrix &rkM)
int CompareArrays(const moDMatrix &rkM) const
moDMatrix< Real > operator*(Real fScalar, const moDMatrix< Real > &rkM)
void SetSize(int iRows, int iCols)
Real ** m_aafEntry
Clase base abstracta de donde deben derivar los objetos [virtual pura].
Definition: moAbstract.h:191
moDBandedMatrix & operator=(const moDBandedMatrix &rkM)
Real Dot(const moDVector &rkV) const
void GetColumnMajor(Real *afCMajor) const
int GetUBands() const
moDMatrix & operator*=(Real fScalar)
bool operator==(const moDMatrix &rkM) const
moDMatrix Transpose() const
moDMatrix operator/(Real fScalar) const
bool GetInverse(moDMatrix< Real > &rkInverse) const
bool operator!=(const moDMatrix &rkM) const
moDVector< Real > GetRow(int iRow) const
moDMatrix(int iRows=0, int iCols=0)
void Allocate(bool bSetToZero)
Real * GetLBand(int i)
moDMatrix & operator/=(Real fScalar)
moDMatrix TransposeTimes(const moDMatrix &rkM) const
Real QForm(const moDVector< Real > &rkU, const moDVector< Real > &rkV) const
int GetColumns() const
int GetRows() const
moDMatrix & operator=(const moDMatrix &rkM)
Real * GetUBand(int i)
int GetSize() const
bool operator<(const moDMatrix &rkM) const
bool operator>(const moDMatrix &rkM) const
void SwapRows(int iRow0, int iRow1)
Real * m_afData
void GetSize(int &riRows, int &riCols) const
Definition: moMath.h:64
int GetLBandMax(int i) const
int GetQuantity() const
int GetLBands() const
void Deallocate()
const Real * operator[](int iRow) const
moDVector< Real > GetColumn(int iCol) const
bool operator>=(const moDMatrix &rkM) const
int GetUBandMax(int i) const
static Real FAbs(Real fValue)
Definition: moMath.h:180
moDMatrix & operator-=(const moDMatrix &rkM)
moDMatrix TimesTranspose(const moDMatrix &rkM) const
Real & operator()(int iRow, int iCol)
moDMatrix operator+(const moDMatrix &rkM) const
moDMatrix operator*(const moDMatrix &rkM) const
bool operator<=(const moDMatrix &rkM) const
void SetColumn(int iCol, const moDVector< Real > &rkV)