/** * <p> * Computes W from the householder reflectors stored in the columns of the row block * submatrix Y. * </p> * * <p> * Y = v<sup>(1)</sup><br> * W = -β<sub>1</sub>v<sup>(1)</sup><br> * for j=2:r<br> * z = -β(I +WY<sup>T</sup>)v<sup>(j)</sup> <br> * W = [W z]<br> * Y = [Y v<sup>(j)</sup>]<br> * end<br> * <br> * where v<sup>(.)</sup> are the house holder vectors, and r is the block length. Note that * Y already contains the householder vectors so it does not need to be modified. * </p> * * <p> * Y and W are assumed to have the same number of rows and columns. * </p> */ public static void computeW_row(int blockLength, DSubmatrixD1 Y, DSubmatrixD1 W, double[] beta, int betaIndex) { int heightY = Y.row1 - Y.row0; CommonOps_DDRM.fill(W.original, 0); // W = -beta*v(1) BlockHouseHolder_DDRB.scale_row(blockLength, Y, W, 0, 1, -beta[betaIndex++]); int min = Math.Min(heightY, W.col1 - W.col0); // set up rest of the rows for (int i = 1; i < min; i++) { // w=-beta*(I + W*Y^T)*u double b = -beta[betaIndex++]; // w = w -beta*W*(Y^T*u) for (int j = 0; j < i; j++) { double yv = BlockHouseHolder_DDRB.innerProdRow(blockLength, Y, i, Y, j, 1); VectorOps_DDRB.add_row(blockLength, W, i, 1, W, j, b * yv, W, i, 1, Y.col1 - Y.col0); } //w=w -beta*u + stuff above BlockHouseHolder_DDRB.add_row(blockLength, Y, i, b, W, i, 1, W, i, 1, Y.col1 - Y.col0); } }
/** * <p> * Performs a tridiagonal decomposition on the upper row only. * </p> * * <p> * For each row 'a' in 'A': * Compute 'u' the householder reflector. * y(:) = A*u * v(i) = y - (1/2)*(y^T*u)*u * a(i+1) = a(i) - u*γ*v^T - v*u^t * </p> * * @param blockLength Size of a block * @param A is the row block being decomposed. Modified. * @param gammas Householder gammas. * @param V Where computed 'v' are stored in a row block. Modified. */ public static void tridiagUpperRow(int blockLength, DSubmatrixD1 A, double[] gammas, DSubmatrixD1 V) { int blockHeight = Math.Min(blockLength, A.row1 - A.row0); if (blockHeight <= 1) { return; } int width = A.col1 - A.col0; int num = Math.Min(width - 1, blockHeight); int applyIndex = Math.Min(width, blockHeight); // step through rows in the block for (int i = 0; i < num; i++) { // compute the new reflector and save it in a row in 'A' BlockHouseHolder_DDRB.computeHouseHolderRow(blockLength, A, gammas, i); double gamma = gammas[A.row0 + i]; // compute y computeY(blockLength, A, V, i, gamma); // compute v from y computeRowOfV(blockLength, A, V, i, gamma); // Apply the reflectors to the next row in 'A' only if (i + 1 < applyIndex) { applyReflectorsToRow(blockLength, A, V, i + 1); } } }
/** * <p> * Computes the 'y' vector and stores the result in 'v'<br> * <br> * y = -γ(A + U*V^T + V*U^T)u * </p> * * @param blockLength * @param A Contains the reflectors and the row being updated. * @param V Contains previously computed 'v' vectors. * @param row The row of 'A' that is to be updated. */ public static void computeY(int blockLength, DSubmatrixD1 A, DSubmatrixD1 V, int row, double gamma) { // Elements in 'y' before 'row' are known to be zero and the element at 'row' // is not used. Thus only elements after row and after are computed. // y = A*u multA_u(blockLength, A, V, row); for (int i = 0; i < row; i++) { // y = y + u_i*v_i^t*u + v_i*u_i^t*u // v_i^t*u double dot_v_u = BlockHouseHolder_DDRB.innerProdRow(blockLength, A, row, V, i, 1); // u_i^t*u double dot_u_u = BlockHouseHolder_DDRB.innerProdRow(blockLength, A, row, A, i, 1); // y = y + u_i*(v_i^t*u) // the ones in these 'u' are skipped over since the next submatrix of A // is only updated VectorOps_DDRB.add_row(blockLength, V, row, 1, A, i, dot_v_u, V, row, row + 1, A.col1 - A.col0); // y = y + v_i*(u_i^t*u) // the 1 in U is taken account above VectorOps_DDRB.add_row(blockLength, V, row, 1, V, i, dot_u_u, V, row, row + 1, A.col1 - A.col0); } // y = -gamma*y VectorOps_DDRB.scale_row(blockLength, V, row, -gamma, V, row, row + 1, V.col1 - V.col0); }
/** * Performs a standard bidiagonal decomposition just on the outer blocks of the provided matrix * * @param blockLength * @param A * @param gammasU */ public static bool bidiagOuterBlocks(int blockLength, DSubmatrixD1 A, double[] gammasU, double[] gammasV) { // Console.WriteLine("---------- Orig"); // A.original.print(); int width = Math.Min(blockLength, A.col1 - A.col0); int height = Math.Min(blockLength, A.row1 - A.row0); int min = Math.Min(width, height); for (int i = 0; i < min; i++) { //--- Apply reflector to the column // compute the householder vector if (!BlockHouseHolder_DDRB.computeHouseHolderCol(blockLength, A, gammasU, i)) { return(false); } // apply to rest of the columns in the column block BlockHouseHolder_DDRB.rank1UpdateMultR_Col(blockLength, A, i, gammasU[A.col0 + i]); // apply to the top row block BlockHouseHolder_DDRB.rank1UpdateMultR_TopRow(blockLength, A, i, gammasU[A.col0 + i]); Console.WriteLine("After column stuff"); A.original.print(); //-- Apply reflector to the row if (!BlockHouseHolder_DDRB.computeHouseHolderRow(blockLength, A, gammasV, i)) { return(false); } // apply to rest of the rows in the row block BlockHouseHolder_DDRB.rank1UpdateMultL_Row(blockLength, A, i, i + 1, gammasV[A.row0 + i]); Console.WriteLine("After update row"); A.original.print(); // apply to the left column block // TODO THIS WON'T WORK!!!!!!!!!!!!! // Needs the whole matrix to have been updated by the left reflector to compute the correct solution // rank1UpdateMultL_LeftCol(blockLength,A,i,i+1,gammasV[A.row0+i]); Console.WriteLine("After row stuff"); A.original.print(); } return(true); }
/** * <p> * Final computation for a single row of 'v':<br> * <br> * v = y -(1/2)γ(y^T*u)*u * </p> * * @param blockLength * @param A * @param V * @param row * @param gamma */ public static void computeRowOfV(int blockLength, DSubmatrixD1 A, DSubmatrixD1 V, int row, double gamma) { // val=(y^T*u) double val = BlockHouseHolder_DDRB.innerProdRow(blockLength, A, row, V, row, 1); // take in account the one double before = A.get(row, row + 1); A.set(row, row + 1, 1); // v = y - (1/2)gamma*val * u VectorOps_DDRB.add_row(blockLength, V, row, 1, A, row, -0.5 * gamma * val, V, row, row + 1, A.col1 - A.col0); A.set(row, row + 1, before); }