22 #ifndef __dgGeneralMatrix__ 23 #define __dgGeneralMatrix__ 27 #include "dgGeneralVector.h" 32 template <
class T> dgInt32 dgGeneralMatrixCalcBufferSizeInBytes(dgInt32 row, dgInt32 column) {
34 columnPad = ((column *
sizeof(T) + 0x0f) & -0x0f) /
sizeof(T);
51 dgInt32 GetRowCount()
const;
52 dgInt32 GetColCount()
const;
57 void SwapRows(dgInt32 i, dgInt32 j);
58 void SwapColumns(dgInt32 i, dgInt32 j);
62 void GaussianPivotStep(dgInt32 srcRow, dgInt32 pivotRow, dgInt32 pivotCol, T tol = T(1.0e-6f));
80 bool CholeskyDecomposition();
82 bool TestSymetry()
const;
108 NEWTON_ASSERT(row > 0);
109 NEWTON_ASSERT(column > 0);
116 columnPad = ((column *
sizeof(T) + 0x0f) & -0x0f) /
sizeof(T);
117 m_buffer =
new T [row * columnPad];
120 for (i = 0; i < row; i ++) {
132 m_rowCount = src.m_rowCount;
133 m_colCount = src.m_colCount;
135 columnPad = ((m_colCount *
sizeof(T) + 0x0f) & -0x0f) /
sizeof(T);
136 m_buffer =
new T [m_rowCount * columnPad];
139 for (i = 0; i < m_rowCount; i ++) {
157 NEWTON_ASSERT((((dgUnsigned32) elemBuffer) & 0x0f) == 0);
159 m_buffer = elemBuffer;
160 columnPad = ((m_colCount *
sizeof(T) + 0x0f) & -0x0f) /
sizeof(T);
162 for (i = 0; i < row; i ++) {
176 m_rowCount = src.m_rowCount;
177 m_colCount = src.m_colCount;
179 NEWTON_ASSERT((((dgUnsigned32) elemBuffer) & 0x0f) == 0);
180 m_buffer = elemBuffer;
182 columnPad = ((m_colCount *
sizeof(T) + 0x0f) & -0x0f) /
sizeof(T);
184 for (i = 0; i < m_rowCount; i ++) {
215 for (i = 0; i < m_rowCount; i ++) {
224 NEWTON_ASSERT(i < m_rowCount);
225 NEWTON_ASSERT(i >= 0);
231 NEWTON_ASSERT(i < m_rowCount);
232 NEWTON_ASSERT(i >= 0);
239 for (i = 0; i < m_rowCount; i ++) {
240 m_rows[i].Clear(val);
248 for (i = 0; i < m_rowCount; i ++) {
249 m_rows[i].Clear(T(0.0f));
250 m_rows[i][i] = T(1.0f);
285 T num(me[pivotRow][pivotCol]);
286 if (T(dgAbsf(num)) > tol) {
287 T den(me[srcRow][pivotCol]);
288 NEWTON_ASSERT(T(dgAbsf(den)) > T(0.0f));
290 #ifdef DG_COUNT_FLOAT_OPS 295 me[pivotRow].LinearCombine(den, me[srcRow], me[pivotRow]);
315 NEWTON_ASSERT(&v != &out);
316 NEWTON_ASSERT(m_rowCount == v.m_colCount);
317 NEWTON_ASSERT(m_colCount == out.m_colCount);
322 for (i = 0; i < m_colCount; i ++) {
324 for (j = 0; j < m_rowCount; j ++) {
325 acc = acc + inMem[j] * me[j][i];
327 #ifdef DG_COUNT_FLOAT_OPS 332 #ifdef DG_COUNT_FLOAT_OPS 343 NEWTON_ASSERT(&v != &out);
344 NEWTON_ASSERT(m_rowCount == out.m_colCount);
345 NEWTON_ASSERT(m_colCount == v.m_colCount);
347 for (i = 0; i < m_rowCount; i ++) {
348 out[i] = v.DotProduct(m_rows[i]);
361 NEWTON_ASSERT(m_rowCount == A.m_rowCount);
362 NEWTON_ASSERT(m_colCount == B.m_colCount);
363 NEWTON_ASSERT(A.m_colCount == B.m_rowCount);
365 NEWTON_ASSERT(
this != &A);
367 count = A.m_colCount;
368 for (i = 0; i < m_rowCount; i ++) {
370 rowA = &A.m_rows[i][0];
371 for (j = 0; j < m_colCount; j ++) {
373 for (k = 0; k < count; k ++) {
374 acc = acc + rowA[k] * B.m_rows[k][j];
376 #ifdef DG_COUNT_FLOAT_OPS 382 #ifdef DG_COUNT_FLOAT_OPS 400 NEWTON_ASSERT(m_rowCount == A.m_rowCount);
401 NEWTON_ASSERT(m_colCount == Bt.m_rowCount);
402 NEWTON_ASSERT(A.m_colCount == Bt.m_colCount);
404 NEWTON_ASSERT(
this != &A);
405 NEWTON_ASSERT(
this != &Bt);
407 count = A.m_colCount;
408 for (i = 0; i < m_rowCount; i ++) {
410 rowA = &A.m_rows[i][0];
411 for (j = 0; j < m_colCount; j ++) {
413 rowB = &Bt.m_rows[j][0];
414 for (k = 0; k < count; k ++) {
415 acc = acc + rowA[k] * rowB[k];
417 #ifdef DG_COUNT_FLOAT_OPS 423 #ifdef DG_COUNT_FLOAT_OPS 442 NEWTON_ASSERT(m_rowCount == m_colCount);
443 NEWTON_ASSERT(b.m_colCount == m_colCount);
447 for (i = 0; i < m_rowCount - 1; i ++) {
448 rowI = &m_rows[i][0];
451 if (T(dgAbsf(den)) < T(0.0f)) {
455 for (k = i + 1; k < m_rowCount; k ++) {
456 rowK = &m_rows[k][0];
459 if (T(dgAbsf(num)) > tol) {
461 for (j = i + 1; j < m_rowCount; j ++) {
462 rowK[j] = rowK[j] - rowI[j] * num;
464 #ifdef DG_COUNT_FLOAT_OPS 469 B[k] = B[k] - B[i] * num;
471 #ifdef DG_COUNT_FLOAT_OPS 480 B[m_rowCount - 1] = B[m_rowCount - 1] / m_rows[m_rowCount - 1][m_rowCount - 1];
481 for (i = m_rowCount - 2; i >= 0; i --) {
483 rowI = &m_rows[i][0];
484 for (j = i + 1 ; j < m_rowCount; j ++) {
485 acc = acc + rowI[j] * B[j];
487 #ifdef DG_COUNT_FLOAT_OPS 491 B[i] = (B[i] - acc) / rowI[i];
493 #ifdef DG_COUNT_FLOAT_OPS 499 #ifdef DG_COUNT_FLOAT_OPS 509 NEWTON_ASSERT(i >= 0);
510 NEWTON_ASSERT(j >= 0);
511 NEWTON_ASSERT(i < m_rowCount);
512 NEWTON_ASSERT(j < m_rowCount);
513 NEWTON_ASSERT(j != i);
514 Swap(m_rows[i].m_columns, m_rows[j].m_columns);
520 NEWTON_ASSERT(i >= 0);
521 NEWTON_ASSERT(j >= 0);
522 NEWTON_ASSERT(i < m_colCount);
523 NEWTON_ASSERT(j < m_colCount);
524 for (k = 0; k < m_colCount; k ++) {
525 Swap(m_rows[k][i], m_rows[k][j]);
536 if (m_colCount != m_rowCount) {
541 for (i = 0; i < m_rowCount; i ++) {
542 for (j = i + 1; j < m_rowCount; j ++) {
543 if (dgAbsf(mat[i][j] - mat[j][i]) > 1.0e-12f) {
553 if (!TestSymetry()) {
558 return tmp.CholeskyDecomposition();
572 #ifdef DG_COUNT_FLOAT_OPS 580 for (j = 0; j < m_rowCount; j++) {
583 rowJ = &m_rows[j].m_columns[0];
585 for (k = 0; k < j; k ++) {
586 rowK = &m_rows[k].m_columns[0];
589 if (dgAbsf(factor) > 1.0e-6f) {
590 for (i = j; i < m_rowCount; i ++) {
591 rowJ[i] -= rowK[i] * factor;
592 #ifdef DG_COUNT_FLOAT_OPS 601 if (factor <= T(0.0f)) {
602 if (factor <= T(-5.0e-4f)) {
605 factor = T(1.0e-12f);
608 factor = T(dgSqrt(dgFloat32(factor)));
610 factor = T(1.0f / dgFloat32(factor));
612 #ifdef DG_COUNT_FLOAT_OPS 617 for (k = j + 1; k < m_rowCount; k ++) {
619 #ifdef DG_COUNT_FLOAT_OPS 626 #ifdef DG_COUNT_FLOAT_OPS Definition: dgGeneralVector.h:34
Definition: dgGeneralMatrix.h:40