/** * <p> * Performs an in-place solve operation where T is contained in a single block.<br> * <br> * B = T<sup>-1</sup> B<br> * <br> * where T is a triangular matrix contained in an inner block. T or B can be transposed. T must be a single complete inner block * and B is either a column block vector or row block vector. * </p> * * @param blockLength Size of the inner blocks in the block matrix. * @param upper If T is upper or lower triangular. * @param T An upper or lower triangular matrix that is contained in an inner block. Not modified. * @param B A block aligned row or column submatrix. Modified. * @param transT If T is transposed or not. * @param transB If B is transposed or not. */ public static void solveBlock(int blockLength, bool upper, DSubmatrixD1 T, DSubmatrixD1 B, bool transT, bool transB) { int Trows = T.row1 - T.row0; if (Trows > blockLength) { throw new ArgumentException("T can be at most the size of a block"); } // number of rows in a block. The submatrix can be smaller than a block int blockT_rows = Math.Min(blockLength, T.original.numRows - T.row0); int blockT_cols = Math.Min(blockLength, T.original.numCols - T.col0); int offsetT = T.row0 * T.original.numCols + blockT_rows * T.col0; double[] dataT = T.original.data; double[] dataB = B.original.data; if (transB) { if (upper) { if (transT) { throw new ArgumentException("Operation not yet supported"); } else { throw new ArgumentException("Operation not yet supported"); } } else { if (transT) { throw new ArgumentException("Operation not yet supported"); } else { //CONCURRENT_BELOW EjmlConcurrency.loopFor(B.row0, B.row1, blockLength, i -> { for (int i = B.row0; i < B.row1; i += blockLength) { int N = Math.Min(B.row1, i + blockLength) - i; int offsetB = i * B.original.numCols + N * B.col0; InnerTriangularSolver_DDRB.solveLTransB(dataT, dataB, blockT_rows, N, blockT_rows, offsetT, offsetB); } //CONCURRENT_ABOVE }); } } } else { if (Trows != B.row1 - B.row0) { throw new ArgumentException("T and B must have the same number of rows."); } if (upper) { if (transT) { //CONCURRENT_BELOW EjmlConcurrency.loopFor(B.col0, B.col1, blockLength, i -> { for (int i = B.col0; i < B.col1; i += blockLength) { int offsetB = B.row0 * B.original.numCols + Trows * i; int N = Math.Min(B.col1, i + blockLength) - i; InnerTriangularSolver_DDRB.solveTransU(dataT, dataB, Trows, N, Trows, offsetT, offsetB); } //CONCURRENT_ABOVE }); } else { //CONCURRENT_BELOW EjmlConcurrency.loopFor(B.col0, B.col1, blockLength, i -> { for (int i = B.col0; i < B.col1; i += blockLength) { int offsetB = B.row0 * B.original.numCols + Trows * i; int N = Math.Min(B.col1, i + blockLength) - i; InnerTriangularSolver_DDRB.solveU(dataT, dataB, Trows, N, Trows, offsetT, offsetB); } //CONCURRENT_ABOVE }); } } else { if (transT) { //CONCURRENT_BELOW EjmlConcurrency.loopFor(B.col0, B.col1, blockLength, i -> { for (int i = B.col0; i < B.col1; i += blockLength) { int offsetB = B.row0 * B.original.numCols + Trows * i; int N = Math.Min(B.col1, i + blockLength) - i; InnerTriangularSolver_DDRB.solveTransL(dataT, dataB, Trows, N, blockT_cols, offsetT, offsetB); } //CONCURRENT_ABOVE }); } else { //CONCURRENT_BELOW EjmlConcurrency.loopFor(B.col0, B.col1, blockLength, i -> { for (int i = B.col0; i < B.col1; i += blockLength) { int offsetB = B.row0 * B.original.numCols + Trows * i; int N = Math.Min(B.col1, i + blockLength) - i; InnerTriangularSolver_DDRB.solveL(dataT, dataB, Trows, N, blockT_cols, offsetT, offsetB); } //CONCURRENT_ABOVE }); } } } }
/** * Inverts an upper or lower triangular block submatrix. Uses a row-oriented approach. * * @param upper Is it upper or lower triangular. * @param T Triangular matrix that is to be inverted. Must be block aligned. Not Modified. * @param T_inv Where the inverse is stored. This can be the same as T. Modified. * @param workspace Work space variable that is size blockLength*blockLength. */ public static void invert(int blockLength, bool upper, DSubmatrixD1 T, DSubmatrixD1 T_inv, GrowArray <DGrowArray> workspace) { if (upper) { throw new ArgumentException("Upper triangular matrices not supported yet"); } //CONCURRENT_INLINE if (T.original == T_inv.original) //CONCURRENT_INLINE throw new ArgumentException("Same instance not allowed for concurrent"); //if (workspace == null) // workspace = new GrowArray<DGrowArray>(new DGrowArray()); //else // workspace.reset(); if (T.row0 != T_inv.row0 || T.row1 != T_inv.row1 || T.col0 != T_inv.col0 || T.col1 != T_inv.col1) { throw new ArgumentException("T and T_inv must be at the same elements in the matrix"); } int blockSize = blockLength * blockLength; //CONCURRENT_REMOVE_BELOW double[] temp = workspace.grow().reshape(blockSize).data; int M = T.row1 - T.row0; double[] dataT = T.original.data; double[] dataX = T_inv.original.data; int offsetT = T.row0 * T.original.numCols + M * T.col0; for (int rowT = 0; rowT < M; rowT += blockLength) { int _rowT = rowT; // Needed for concurrent lambdas int heightT = Math.Min(T.row1 - (rowT + T.row0), blockLength); int indexII = offsetT + T.original.numCols * (rowT + T.row0) + heightT * (rowT + T.col0); //CONCURRENT_BELOW EjmlConcurrency.loopFor(0, rowT, blockLength, workspace, ( work, colT ) -> { for (int colT = 0; colT < rowT; colT += blockLength) { //CONCURRENT_INLINE double[] temp = work.reshape(blockSize).data; int widthX = Math.Min(T.col1 - (colT + T.col0), blockLength); Array.Fill(temp, 0); for (int k = colT; k < _rowT; k += blockLength) { int widthT = Math.Min(T.col1 - (k + T.col0), blockLength); int indexL2 = offsetT + T.original.numCols * (_rowT + T.row0) + heightT * (k + T.col0); int indexX2 = offsetT + T.original.numCols * (k + T.row0) + widthT * (colT + T.col0); InnerMultiplication_DDRB.blockMultMinus(dataT, dataX, temp, indexL2, indexX2, 0, heightT, widthT, widthX); } int indexX = offsetT + T.original.numCols * (_rowT + T.row0) + heightT * (colT + T.col0); InnerTriangularSolver_DDRB.solveL(dataT, temp, heightT, widthX, heightT, indexII, 0); System.Array.Copy(temp, 0, dataX, indexX, widthX * heightT); } //CONCURRENT_ABOVE }); InnerTriangularSolver_DDRB.invertLower(dataT, dataX, heightT, indexII, indexII); } }
//CONCURRENT_OMIT_BEGIN /** * Inverts an upper or lower triangular block submatrix. Uses a row oriented approach. * * @param upper Is it upper or lower triangular. * @param T Triangular matrix that is to be inverted. Overwritten with solution. Modified. * @param workspace Work space variable that is size blockLength*blockLength. */ public static void invert(int blockLength, bool upper, DSubmatrixD1 T, GrowArray <DGrowArray> workspace) { if (upper) { throw new ArgumentException("Upper triangular matrices not supported yet"); } // FORCE to disable workspace = null; if (workspace == null) { //workspace = new GrowArray<>(DGrowArray::new); } else { workspace.reset(); } int blockSize = blockLength * blockLength; double[] temp = workspace.grow().reshape(blockSize).data; int M = T.row1 - T.row0; double[] dataT = T.original.data; int offsetT = T.row0 * T.original.numCols + M * T.col0; for (int i = 0; i < M; i += blockLength) { int heightT = Math.Min(T.row1 - (i + T.row0), blockLength); int indexII = offsetT + T.original.numCols * (i + T.row0) + heightT * (i + T.col0); for (int j = 0; j < i; j += blockLength) { int widthX = Math.Min(T.col1 - (j + T.col0), blockLength); Array.Fill(temp, 0); for (int k = j; k < i; k += blockLength) { int widthT = Math.Min(T.col1 - (k + T.col0), blockLength); int indexL2 = offsetT + T.original.numCols * (i + T.row0) + heightT * (k + T.col0); int indexX2 = offsetT + T.original.numCols * (k + T.row0) + widthT * (j + T.col0); InnerMultiplication_DDRB.blockMultMinus(dataT, dataT, temp, indexL2, indexX2, 0, heightT, widthT, widthX); } int indexX = offsetT + T.original.numCols * (i + T.row0) + heightT * (j + T.col0); InnerTriangularSolver_DDRB.solveL(dataT, temp, heightT, widthX, heightT, indexII, 0); System.Array.Copy(temp, 0, dataT, indexX, widthX * heightT); } InnerTriangularSolver_DDRB.invertLower(dataT, heightT, indexII); } }