10 #ifndef vtk_m_Matrix_h
11 #define vtk_m_Matrix_h
32 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
83 return (*
this)[rowIndex][colIndex];
96 return (*
this)[rowIndex][colIndex];
106 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
111 return matrix[rowIndex];
117 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
124 columnValues[rowIndex] = matrix(rowIndex, columnIndex);
131 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
136 matrix[rowIndex] = rowValues;
141 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
148 matrix(rowIndex, columnIndex) = columnValues[rowIndex];
154 template <
typename T,
167 T sum = T(leftFactor(rowIndex, 0) * rightFactor(0, colIndex));
168 for (
vtkm::IdComponent internalIndex = 1; internalIndex < NumInternal; internalIndex++)
170 sum = T(sum + (leftFactor(rowIndex, internalIndex) * rightFactor(internalIndex, colIndex)));
172 result(rowIndex, colIndex) = sum;
180 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
195 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
210 template <
typename T, vtkm::IdComponent Size>
216 result(index, index) = T(1.0);
223 template <
typename T, vtkm::IdComponent Size>
226 matrix = vtkm::MatrixIdentity<T, Size>();
231 template <
typename T, vtkm::IdComponent NumRows, vtkm::IdComponent NumCols>
256 template <
typename T, vtkm::IdComponent Size>
264 T maxValue = vtkm::Abs(A(maxRowIndex, topCornerIndex));
265 for (
vtkm::IdComponent rowIndex = topCornerIndex + 1; rowIndex < Size; rowIndex++)
267 T compareValue = vtkm::Abs(A(rowIndex, topCornerIndex));
268 if (maxValue < compareValue)
270 maxValue = compareValue;
271 maxRowIndex = rowIndex;
275 if (maxValue < vtkm::Epsilon<T>())
281 if (maxRowIndex != topCornerIndex)
290 permutation[maxRowIndex] = permutation[topCornerIndex];
291 permutation[topCornerIndex] = maxOriginalRowIndex;
294 inversionParity = -inversionParity;
299 template <
typename T, vtkm::IdComponent Size>
305 if (A(topCornerIndex, topCornerIndex) == 0)
315 T rAdiag = 1 / A(topCornerIndex, topCornerIndex);
316 for (
vtkm::IdComponent colIndex = topCornerIndex + 1; colIndex < Size; colIndex++)
318 A(topCornerIndex, colIndex) *= rAdiag;
323 for (
vtkm::IdComponent rowIndex = topCornerIndex + 1; rowIndex < Size; rowIndex++)
325 for (
vtkm::IdComponent colIndex = topCornerIndex + 1; colIndex < Size; colIndex++)
327 A(rowIndex, colIndex) -= A(rowIndex, topCornerIndex) * A(topCornerIndex, colIndex);
362 template <
typename T, vtkm::IdComponent Size>
371 permutation[index] = index;
373 inversionParity = T(1);
378 MatrixLUPFactorFindPivot(A, permutation, rowIndex, inversionParity, valid);
383 MatrixLUPFactorFindUpperTriangleElements(A, rowIndex, valid);
395 template <
typename T, vtkm::IdComponent Size>
410 y[rowIndex] = b[permutation[rowIndex]];
414 y[rowIndex] -= LU(rowIndex, colIndex) * y[colIndex];
416 if (LU(rowIndex, rowIndex) == 0)
418 y[rowIndex] = std::numeric_limits<T>::quiet_NaN();
422 y[rowIndex] /= LU(rowIndex, rowIndex);
432 x[rowIndex] = y[rowIndex];
435 x[rowIndex] -= LU(rowIndex, colIndex) * x[colIndex];
447 template <
typename T, vtkm::IdComponent Size>
456 detail::MatrixLUPFactor(LU, permutation, inversionParity, valid);
459 return detail::MatrixLUPSolve(LU, permutation, b);
465 template <
typename T, vtkm::IdComponent Size>
473 detail::MatrixLUPFactor(LU, permutation, inversionParity, valid);
492 template <
typename T, vtkm::IdComponent Size>
500 detail::MatrixLUPFactor(LU, permutation, inversionParity, valid);
513 T product = inversionParity;
516 product *= LU(index, index);
523 template <
typename T>
529 template <
typename T>
535 template <
typename T>
538 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) -
539 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);
552 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
567 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
588 return matrix(rowIndex, colIndex);
595 return matrix(rowIndex, colIndex);
603 template <
typename NewComponentType>
606 template <
typename NewComponentType>
616 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
624 if (a(rowIndex, colIndex) != b(rowIndex, colIndex))
630 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
639 template <
typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
648 stream << mat(row, col) <<
"\t";
650 stream <<
"|" << std::endl;
656 #endif //vtk_m_Matrix_h