10 #ifndef vtk_m_Matrix_h
11 #define vtk_m_Matrix_h
32 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
81 return (*
this)[rowIndex][colIndex];
94 return (*
this)[rowIndex][colIndex];
104 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
109 return matrix[rowIndex];
115 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
122 columnValues[rowIndex] = matrix(rowIndex, columnIndex);
129 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
134 matrix[rowIndex] = rowValues;
139 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
146 matrix(rowIndex, columnIndex) = columnValues[rowIndex];
152 template <
typename T,
165 T sum = T(leftFactor(rowIndex, 0) * rightFactor(0, colIndex));
166 for (
vtkm::IdComponent internalIndex = 1; internalIndex < NumInternal; internalIndex++)
168 sum = T(sum + (leftFactor(rowIndex, internalIndex) * rightFactor(internalIndex, colIndex)));
170 result(rowIndex, colIndex) = sum;
178 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
193 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
208 template <
typename T, vtkm::IdComponent Size>
214 result(index, index) = T(1.0);
221 template <
typename T, vtkm::IdComponent Size>
224 matrix = vtkm::MatrixIdentity<T, Size>();
229 template <
typename T, vtkm::IdComponent NumRows, vtkm::IdComponent NumCols>
254 template <
typename T, vtkm::IdComponent Size>
262 T maxValue = vtkm::Abs(A(maxRowIndex, topCornerIndex));
263 for (
vtkm::IdComponent rowIndex = topCornerIndex + 1; rowIndex < Size; rowIndex++)
265 T compareValue = vtkm::Abs(A(rowIndex, topCornerIndex));
266 if (maxValue < compareValue)
268 maxValue = compareValue;
269 maxRowIndex = rowIndex;
273 if (maxValue < vtkm::Epsilon<T>())
279 if (maxRowIndex != topCornerIndex)
288 permutation[maxRowIndex] = permutation[topCornerIndex];
289 permutation[topCornerIndex] = maxOriginalRowIndex;
292 inversionParity = -inversionParity;
297 template <
typename T, vtkm::IdComponent Size>
303 if (A(topCornerIndex, topCornerIndex) == 0)
313 T rAdiag = 1 / A(topCornerIndex, topCornerIndex);
314 for (
vtkm::IdComponent colIndex = topCornerIndex + 1; colIndex < Size; colIndex++)
316 A(topCornerIndex, colIndex) *= rAdiag;
321 for (
vtkm::IdComponent rowIndex = topCornerIndex + 1; rowIndex < Size; rowIndex++)
323 for (
vtkm::IdComponent colIndex = topCornerIndex + 1; colIndex < Size; colIndex++)
325 A(rowIndex, colIndex) -= A(rowIndex, topCornerIndex) * A(topCornerIndex, colIndex);
360 template <
typename T, vtkm::IdComponent Size>
369 permutation[index] = index;
371 inversionParity = T(1);
376 MatrixLUPFactorFindPivot(A, permutation, rowIndex, inversionParity, valid);
381 MatrixLUPFactorFindUpperTriangleElements(A, rowIndex, valid);
393 template <
typename T, vtkm::IdComponent Size>
408 y[rowIndex] = b[permutation[rowIndex]];
412 y[rowIndex] -= LU(rowIndex, colIndex) * y[colIndex];
414 if (LU(rowIndex, rowIndex) == 0)
416 y[rowIndex] = std::numeric_limits<T>::quiet_NaN();
420 y[rowIndex] /= LU(rowIndex, rowIndex);
430 x[rowIndex] = y[rowIndex];
433 x[rowIndex] -= LU(rowIndex, colIndex) * x[colIndex];
445 template <
typename T, vtkm::IdComponent Size>
454 detail::MatrixLUPFactor(LU, permutation, inversionParity, valid);
457 return detail::MatrixLUPSolve(LU, permutation, b);
463 template <
typename T, vtkm::IdComponent Size>
471 detail::MatrixLUPFactor(LU, permutation, inversionParity, valid);
490 template <
typename T, vtkm::IdComponent Size>
498 detail::MatrixLUPFactor(LU, permutation, inversionParity, valid);
511 T product = inversionParity;
514 product *= LU(index, index);
521 template <
typename T>
527 template <
typename T>
533 template <
typename T>
536 return A(0, 0) * A(1, 1) * A(2, 2) + A(1, 0) * A(2, 1) * A(0, 2) + A(2, 0) * A(0, 1) * A(1, 2) -
537 A(0, 0) * A(2, 1) * A(1, 2) - A(1, 0) * A(0, 1) * A(2, 2) - A(2, 0) * A(1, 1) * A(0, 2);
550 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
565 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
586 return matrix(rowIndex, colIndex);
593 return matrix(rowIndex, colIndex);
601 template <
typename NewComponentType>
604 template <
typename NewComponentType>
614 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
622 if (a(rowIndex, colIndex) != b(rowIndex, colIndex))
628 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
637 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
646 stream << mat(row, col) <<
"\t";
648 stream <<
"|" << std::endl;
654 #endif //vtk_m_Matrix_h