18 #ifndef viskores_Matrix_h
19 #define viskores_Matrix_h
40 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
92 return (*
this)[rowIndex][colIndex];
105 return (*
this)[rowIndex][colIndex];
115 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
120 return matrix[rowIndex];
126 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
134 columnValues[rowIndex] = matrix(rowIndex, columnIndex);
141 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
146 matrix[rowIndex] = rowValues;
151 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
158 matrix(rowIndex, columnIndex) = columnValues[rowIndex];
164 template <
typename T,
177 T sum = T(leftFactor(rowIndex, 0) * rightFactor(0, colIndex));
180 sum = T(sum + (leftFactor(rowIndex, internalIndex) * rightFactor(internalIndex, colIndex)));
182 result(rowIndex, colIndex) = sum;
190 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
205 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
220 template <
typename T, viskores::IdComponent Size>
226 result(index, index) = T(1.0);
233 template <
typename T, viskores::IdComponent Size>
236 matrix = viskores::MatrixIdentity<T, Size>();
241 template <
typename T, viskores::IdComponent NumRows, viskores::IdComponent NumCols>
266 template <
typename T, viskores::IdComponent Size>
275 T maxValue = viskores::Abs(A(maxRowIndex, topCornerIndex));
278 T compareValue = viskores::Abs(A(rowIndex, topCornerIndex));
279 if (maxValue < compareValue)
281 maxValue = compareValue;
282 maxRowIndex = rowIndex;
286 if (maxValue < viskores::Epsilon<T>())
292 if (maxRowIndex != topCornerIndex)
301 permutation[maxRowIndex] = permutation[topCornerIndex];
302 permutation[topCornerIndex] = maxOriginalRowIndex;
305 inversionParity = -inversionParity;
310 template <
typename T, viskores::IdComponent Size>
317 if (A(topCornerIndex, topCornerIndex) == 0)
327 T rAdiag = 1 / A(topCornerIndex, topCornerIndex);
330 A(topCornerIndex, colIndex) *= rAdiag;
339 A(rowIndex, colIndex) -= A(rowIndex, topCornerIndex) * A(topCornerIndex, colIndex);
374 template <
typename T, viskores::IdComponent Size>
383 permutation[index] = index;
385 inversionParity = T(1);
390 MatrixLUPFactorFindPivot(A, permutation, rowIndex, inversionParity, valid);
395 MatrixLUPFactorFindUpperTriangleElements(A, rowIndex, valid);
407 template <
typename T, viskores::IdComponent Size>
422 y[rowIndex] = b[permutation[rowIndex]];
426 y[rowIndex] -= LU(rowIndex, colIndex) * y[colIndex];
428 if (LU(rowIndex, rowIndex) == 0)
430 y[rowIndex] = std::numeric_limits<T>::quiet_NaN();
434 y[rowIndex] /= LU(rowIndex, rowIndex);
444 x[rowIndex] = y[rowIndex];
447 x[rowIndex] -= LU(rowIndex, colIndex) * x[colIndex];
459 template <
typename T, viskores::IdComponent Size>
469 detail::MatrixLUPFactor(LU, permutation, inversionParity, valid);
472 return detail::MatrixLUPSolve(LU, permutation, b);
478 template <
typename T, viskores::IdComponent Size>
487 detail::MatrixLUPFactor(LU, permutation, inversionParity, valid);
506 template <
typename T, viskores::IdComponent Size>
514 detail::MatrixLUPFactor(LU, permutation, inversionParity, valid);
527 T product = inversionParity;
530 product *= LU(index, index);
537 template <
typename T>
543 template <
typename T>
549 template <
typename T>
552 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) -
553 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);
566 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
581 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
603 return matrix(rowIndex, colIndex);
610 return matrix(rowIndex, colIndex);
618 template <
typename NewComponentType>
621 template <
typename NewComponentType>
631 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
639 if (a(rowIndex, colIndex) != b(rowIndex, colIndex))
645 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
654 template <
typename T, viskores::IdComponent NumRow, viskores::IdComponent NumCol>
664 stream << mat(row, col) <<
"\t";
666 stream <<
"|" << std::endl;
672 #endif //viskores_Matrix_h