/** * Performs matrix addition:<br> * C = αA + βB * * @param alpha scalar value multiplied against A * @param A Matrix * @param beta scalar value multiplied against B * @param B Matrix * @param C Output matrix. * @param gw (Optional) Storage for internal workspace. Can be null. * @param gx (Optional) Storage for internal workspace. Can be null. */ public static void add(double alpha, DMatrixSparseCSC A, double beta, DMatrixSparseCSC B, DMatrixSparseCSC C, IGrowArray gw, DGrowArray gx) { double[] x = TriangularSolver_DSCC.adjust(gx, A.numRows); int[] w = TriangularSolver_DSCC.adjust(gw, A.numRows, A.numRows); C.indicesSorted = false; C.nz_length = 0; for (int col = 0; col < A.numCols; col++) { C.col_idx[col] = C.nz_length; ImplSparseSparseMult_DSCC.multAddColA(A, col, alpha, C, col + 1, x, w); ImplSparseSparseMult_DSCC.multAddColA(B, col, beta, C, col + 1, x, w); // take the values in the dense vector 'x' and put them into 'C' int idxC0 = C.col_idx[col]; int idxC1 = C.col_idx[col + 1]; for (int i = idxC0; i < idxC1; i++) { C.nz_values[i] = x[C.nz_rows[i]]; } } }
/** * <p> * Checks to see if a matrix is orthogonal or isometric. * </p> * * @param Q The matrix being tested. Not modified. * @param tol Tolerance. * @return True if it passes the test. */ public static bool isOrthogonal(DMatrixSparseCSC Q, double tol) { if (Q.numRows < Q.numCols) { throw new ArgumentException("The number of rows must be more than or equal to the number of columns"); } IGrowArray gw = new IGrowArray(); DGrowArray gx = new DGrowArray(); for (int i = 0; i < Q.numRows; i++) { for (int j = i + 1; j < Q.numCols; j++) { double val = CommonOps_DSCC.dotInnerColumns(Q, i, Q, j, gw, gx); if (!(Math.Abs(val) <= tol)) { return(false); } } } return(true); }
/** * Computes the solution to the triangular system. * * @param G (Input) Lower or upper triangular matrix. diagonal elements must be non-zero. Not modified. * @param lower true for lower triangular and false for upper * @param B (Input) Matrix. Not modified. * @param X (Output) Solution * @param g_x (Optional) Storage for workspace. * @param g_xi (Optional) Storage for workspace. * @param g_w (Optional) Storage for workspace. */ public static void solve(DMatrixSparseCSC G, bool lower, DMatrixSparseCSC B, DMatrixSparseCSC X, DGrowArray g_x, IGrowArray g_xi, IGrowArray g_w) { double[] x = adjust(g_x, G.numRows); if (g_xi == null) { g_xi = new IGrowArray(); } int[] xi = adjust(g_xi, G.numRows); X.nz_length = 0; X.col_idx[0] = 0; X.indicesSorted = false; for (int colB = 0; colB < B.numCols; colB++) { int top = solve(G, lower, B, colB, x, null, g_xi, g_w); int nz_count = X.numRows - top; if (X.nz_values.Length < X.nz_length + nz_count) { X.growMaxLength(X.nz_length * 2 + nz_count, true); } for (int p = top; p < X.numRows; p++, X.nz_length++) { X.nz_rows[X.nz_length] = xi[p]; X.nz_values[X.nz_length] = x[xi[p]]; } X.col_idx[colB + 1] = X.nz_length; } }
/** * Resizes the array to ensure that it is at least of length desired and returns its internal array */ public static double[] adjust(DGrowArray gwork, int desired) { if (gwork == null) { gwork = new DGrowArray(); } gwork.reshape(desired); return(gwork.data); }
/** * Performs an element-wise multiplication.<br> * C[i,j] = A[i,j]*B[i,j]<br> * All matrices must have the same shape. * * @param A (Input) Matrix. * @param B (Input) Matrix * @param C (Ouptut) Matrix. * @param gw (Optional) Storage for internal workspace. Can be null. * @param gx (Optional) Storage for internal workspace. Can be null. */ public static void elementMult(DMatrixSparseCSC A, DMatrixSparseCSC B, DMatrixSparseCSC C, IGrowArray gw, DGrowArray gx) { if (A.numCols != B.numCols || A.numRows != B.numRows || A.numCols != C.numCols || A.numRows != C.numRows) { throw new ArgumentException("All inputs must have the same number of rows and columns"); } ImplCommonOps_DSCC.elementMult(A, B, C, gw, gx); }
/** * Performs matrix addition:<br> * C = αA + βB * * @param alpha scalar value multiplied against A * @param A Matrix * @param beta scalar value multiplied against B * @param B Matrix * @param C Output matrix. * @param gw (Optional) Storage for internal workspace. Can be null. * @param gx (Optional) Storage for internal workspace. Can be null. */ public static void add(double alpha, DMatrixSparseCSC A, double beta, DMatrixSparseCSC B, DMatrixSparseCSC C, IGrowArray gw, DGrowArray gx) { if (A.numRows != B.numRows || A.numCols != B.numCols || A.numRows != C.numRows || A.numCols != C.numCols) { throw new ArgumentException("Inconsistent matrix shapes"); } ImplCommonOps_DSCC.add(alpha, A, beta, B, C, gw, gx); }
public static void multTransA(DMatrixSparseCSC A, DMatrixSparseCSC B, DMatrixSparseCSC C, IGrowArray gw, DGrowArray gx) { if (A.numCols != C.numRows || B.numCols != C.numCols) { throw new ArgumentException("Inconsistent matrix shapes"); } ImplSparseSparseMult_DSCC.multTransA(A, B, C, gw, gx); }
/** * Performs matrix multiplication. C = A*B<sup>T</sup></sup> * * @param A Matrix * @param B Matrix * @param C Storage for results. Data length is increased if increased if insufficient. * @param gw (Optional) Storage for internal workspace. Can be null. * @param gx (Optional) Storage for internal workspace. Can be null. */ public static void multTransB(DMatrixSparseCSC A, DMatrixSparseCSC B, DMatrixSparseCSC C, IGrowArray gw, DGrowArray gx) { if (!B.isIndicesSorted()) { throw new ArgumentException("B must have its indices sorted."); } else if (!CommonOps_DSCC.checkIndicesSorted(B)) { throw new ArgumentException("Crap. Not really sorted"); } double[] x = TriangularSolver_DSCC.adjust(gx, A.numRows); int[] w = TriangularSolver_DSCC.adjust(gw, A.numRows + B.numCols, A.numRows); C.growMaxLength(A.nz_length + B.nz_length, false); C.indicesSorted = false; C.nz_length = 0; C.col_idx[0] = 0; // initialize w is the first index in each column of B int locationB = A.numRows; Array.Copy(B.col_idx, 0, w, locationB, B.numCols); for (int colC = 0; colC < B.numRows; colC++) { C.col_idx[colC + 1] = C.nz_length; // needs a value of B has nothing in the row // find the column in the transposed B int mark = colC + 1; for (int colB = 0; colB < B.numCols; colB++) { int bi = w[locationB + colB]; if (bi < B.col_idx[colB + 1]) { int row = B.nz_rows[bi]; if (row == colC) { multAddColA(A, colB, B.nz_values[bi], C, mark, x, w); w[locationB + colB]++; } } } // take the values in the dense vector 'x' and put them into 'C' int idxC0 = C.col_idx[colC]; int idxC1 = C.col_idx[colC + 1]; for (int i = idxC0; i < idxC1; i++) { C.nz_values[i] = x[C.nz_rows[i]]; } } }
/** * Performs element-wise multiplication:<br> * C_ij = A_ij * B_ij * * @param A (Input) Matrix * @param B (Input) Matrix * @param C (Output) matrix. * @param gw (Optional) Storage for internal workspace. Can be null. * @param gx (Optional) Storage for internal workspace. Can be null. */ public static void elementMult(DMatrixSparseCSC A, DMatrixSparseCSC B, DMatrixSparseCSC C, IGrowArray gw, DGrowArray gx) { double[] x = TriangularSolver_DSCC.adjust(gx, A.numRows); int[] w = TriangularSolver_DSCC.adjust(gw, A.numRows); //Arrays.fill(w, 0, A.numRows, -1); // fill with -1. This will be a value less than column for (var i = 0; i < A.numRows; i++) { w[i] = -1; } C.indicesSorted = false; // Hmm I think if B is storted then C will be sorted... C.nz_length = 0; for (int col = 0; col < A.numCols; col++) { int idxA0 = A.col_idx[col]; int idxA1 = A.col_idx[col + 1]; int idxB0 = B.col_idx[col]; int idxB1 = B.col_idx[col + 1]; // compute the maximum number of elements that there can be in this row int maxInRow = Math.Min(idxA1 - idxA0, idxB1 - idxB0); // make sure there are enough non-zero elements in C if (C.nz_length + maxInRow > C.nz_values.Length) { C.growMaxLength(C.nz_values.Length + maxInRow, true); } // update the structure of C C.col_idx[col] = C.nz_length; // mark the rows that appear in A and save their value for (int i = idxA0; i < idxA1; i++) { int row = A.nz_rows[i]; w[row] = col; x[row] = A.nz_values[i]; } // If a row appears in A and B, multiply and set as an element in C for (int i = idxB0; i < idxB1; i++) { int row = B.nz_rows[i]; if (w[row] == col) { C.nz_values[C.nz_length] = x[row] * B.nz_values[i]; C.nz_rows[C.nz_length++] = row; } } } C.col_idx[C.numCols] = C.nz_length; }
/** * Performs matrix multiplication. C = A*B<sup>T</sup>. B needs to be sorted and will be sorted if it * has not already been sorted. * * @param A (Input) Matrix. Not modified. * @param B (Input) Matrix. Value not modified but indicies will be sorted if not sorted already. * @param C (Output) Storage for results. Data length is increased if increased if insufficient. * @param gw (Optional) Storage for internal workspace. Can be null. * @param gx (Optional) Storage for internal workspace. Can be null. */ public static void multTransB(DMatrixSparseCSC A, DMatrixSparseCSC B, DMatrixSparseCSC C, IGrowArray gw, DGrowArray gx) { if (A.numRows != C.numRows || B.numRows != C.numCols) { throw new ArgumentException("Inconsistent matrix shapes"); } if (!B.isIndicesSorted()) { B.sortIndices(null); } ImplSparseSparseMult_DSCC.multTransB(A, B, C, gw, gx); }
/** * Performs matrix multiplication. C = A*B * * @param A Matrix * @param B Matrix * @param C Storage for results. Data length is increased if increased if insufficient. * @param gw (Optional) Storage for internal workspace. Can be null. * @param gx (Optional) Storage for internal workspace. Can be null. */ public static void mult(DMatrixSparseCSC A, DMatrixSparseCSC B, DMatrixSparseCSC C, IGrowArray gw, DGrowArray gx) { double[] x = TriangularSolver_DSCC.adjust(gx, A.numRows); int[] w = TriangularSolver_DSCC.adjust(gw, A.numRows, A.numRows); C.growMaxLength(A.nz_length + B.nz_length, false); C.indicesSorted = false; C.nz_length = 0; // C(i,j) = sum_k A(i,k) * B(k,j) int idx0 = B.col_idx[0]; for (int bj = 1; bj <= B.numCols; bj++) { int colB = bj - 1; int idx1 = B.col_idx[bj]; C.col_idx[bj] = C.nz_length; if (idx0 == idx1) { continue; } // C(:,j) = sum_k A(:,k)*B(k,j) for (int bi = idx0; bi < idx1; bi++) { int rowB = B.nz_rows[bi]; double valB = B.nz_values[bi]; // B(k,j) k=rowB j=colB multAddColA(A, rowB, valB, C, colB + 1, x, w); } // take the values in the dense vector 'x' and put them into 'C' int idxC0 = C.col_idx[colB]; int idxC1 = C.col_idx[colB + 1]; for (int i = idxC0; i < idxC1; i++) { C.nz_values[i] = x[C.nz_rows[i]]; } idx0 = idx1; } }
/** * Computes the inner product of two column vectors taken from the input matrices. * * <p>dot = A(:,colA)'*B(:,colB)</p> * * @param A Matrix * @param colA Column in A * @param B Matrix * @param colB Column in B * @return Dot product */ public static double dotInnerColumns(DMatrixSparseCSC A, int colA, DMatrixSparseCSC B, int colB, IGrowArray gw, DGrowArray gx) { if (A.numRows != B.numRows) { throw new ArgumentException("Number of rows must match."); } int[] w = TriangularSolver_DSCC.adjust(gw, A.numRows); //Arrays.fill(w,0,A.numRows,-1); for (var i = 0; i < A.numRows; i++) { w[i] = -1; } double[] x = TriangularSolver_DSCC.adjust(gx, A.numRows); int length = 0; int idx0 = A.col_idx[colA]; int idx1 = A.col_idx[colA + 1]; for (int i = idx0; i < idx1; i++) { int row = A.nz_rows[i]; x[length] = A.nz_values[i]; w[row] = length++; } double dot = 0; idx0 = B.col_idx[colB]; idx1 = B.col_idx[colB + 1]; for (int i = idx0; i < idx1; i++) { int row = B.nz_rows[i]; if (w[row] != -1) { dot += x[w[row]] * B.nz_values[i]; } } return(dot); }
/** * <p> * Performs a rank-1 update operation on the submatrix specified by V with the multiply on the right.<br> * <br> * C = (I - γ*v*v<sup>T</sup>)*A<br> * </p> * <p> * The order that matrix multiplies are performed has been carefully selected * to minimize the number of operations. * </p> * * <p> * Before this can become a truly generic operation the submatrix specification needs * to be made more generic. * </p> */ public static void rank1UpdateMultR(DMatrixSparseCSC V, int colV, double gamma, DMatrixSparseCSC A, DMatrixSparseCSC C, IGrowArray gw, DGrowArray gx) { if (V.numRows != A.numRows) { throw new ArgumentException("Number of rows in V and A must match"); } C.nz_length = 0; C.numRows = V.numRows; C.numCols = 0; for (int i = 0; i < A.numCols; i++) { double tau = CommonOps_DSCC.dotInnerColumns(V, colV, A, i, gw, gx); ImplCommonOps_DSCC.addColAppend(1.0, A, i, -gamma * tau, V, colV, C, gw); } }
/** * Performs matrix multiplication. C = A<sup>T</sup></sup>*B * * @param A Matrix * @param B Matrix * @param C Storage for results. Data length is increased if increased if insufficient. * @param gw (Optional) Storage for internal workspace. Can be null. * @param gx (Optional) Storage for internal workspace. Can be null. */ public static void multTransA(DMatrixSparseCSC A, DMatrixSparseCSC B, DMatrixSparseCSC C, IGrowArray gw, DGrowArray gx) { double[] x = TriangularSolver_DSCC.adjust(gx, A.numRows); int[] w = TriangularSolver_DSCC.adjust(gw, A.numRows, A.numRows); C.growMaxLength(A.nz_length + B.nz_length, false); C.indicesSorted = true; C.nz_length = 0; C.col_idx[0] = 0; int idxB0 = B.col_idx[0]; for (int bj = 1; bj <= B.numCols; bj++) { int idxB1 = B.col_idx[bj]; C.col_idx[bj] = C.nz_length; if (idxB0 == idxB1) { continue; } // convert the column of B into a dense format and mark which rows are used for (int bi = idxB0; bi < idxB1; bi++) { int rowB = B.nz_rows[bi]; x[rowB] = B.nz_values[bi]; w[rowB] = bj; } // C(colA,colB) = A(:,colA)*B(:,colB) for (int colA = 0; colA < A.numCols; colA++) { int idxA0 = A.col_idx[colA]; int idxA1 = A.col_idx[colA + 1]; double sum = 0; for (int ai = idxA0; ai < idxA1; ai++) { int rowA = A.nz_rows[ai]; if (w[rowA] == bj) { sum += x[rowA] * A.nz_values[ai]; } } if (sum != 0) { if (C.nz_length == C.nz_values.Length) { C.growMaxLength(C.nz_length * 2 + 1, true); } C.nz_values[C.nz_length] = sum; C.nz_rows[C.nz_length++] = colA; } } C.col_idx[bj] = C.nz_length; idxB0 = idxB1; } }
/** * Computes the inner product of two column vectors taken from the input matrices. * * <p>dot = A(:,colA)'*B(:,colB)</p> * * @param A Matrix * @param colA Column in A * @param B Matrix * @param colB Column in B * @return Dot product */ public static double dotInnerColumns(DMatrixSparseCSC A, int colA, DMatrixSparseCSC B, int colB, IGrowArray gw, DGrowArray gx) { return(ImplSparseSparseMult_DSCC.dotInnerColumns(A, colA, B, colB, gw, gx)); }
public static void main(String[] args) { IMersenneTwister rand = new MersenneTwisterFast(234); // easy to work with sparse format, but hard to do computations with DMatrixSparseTriplet work = new DMatrixSparseTriplet(5, 4, 5); work.addItem(0, 1, 1.2); work.addItem(3, 0, 3); work.addItem(1, 1, 22.21234); work.addItem(2, 3, 6); // convert into a format that's easier to perform math with DMatrixSparseCSC Z = ConvertDMatrixStruct.convert(work, (DMatrixSparseCSC)null); // print the matrix to standard out in two different formats Z.print(); Console.WriteLine(); Z.printNonZero(); Console.WriteLine(); // Create a large matrix that is 5% filled DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(ROWS, COLS, (int)(ROWS * COLS * 0.05), rand); // large vector that is 70% filled DMatrixSparseCSC x = RandomMatrices_DSCC.rectangle(COLS, XCOLS, (int)(XCOLS * COLS * 0.7), rand); Console.WriteLine("Done generating random matrices"); // storage for the initial solution DMatrixSparseCSC y = new DMatrixSparseCSC(ROWS, XCOLS, 0); DMatrixSparseCSC z = new DMatrixSparseCSC(ROWS, XCOLS, 0); // To demonstration how to perform sparse math let's multiply: // y=A*x // Optional storage is set to null so that it will declare it internally long before = DateTimeHelper.CurrentTimeMilliseconds; IGrowArray workA = new IGrowArray(A.numRows); DGrowArray workB = new DGrowArray(A.numRows); for (int i = 0; i < 100; i++) { CommonOps_DSCC.mult(A, x, y, workA, workB); CommonOps_DSCC.add(1.5, y, 0.75, y, z, workA, workB); } long after = DateTimeHelper.CurrentTimeMilliseconds; Console.WriteLine("norm = " + NormOps_DSCC.fastNormF(y) + " sparse time = " + (after - before) + " ms"); DMatrixRMaj Ad = ConvertDMatrixStruct.convert(A, (DMatrixRMaj)null); DMatrixRMaj xd = ConvertDMatrixStruct.convert(x, (DMatrixRMaj)null); DMatrixRMaj yd = new DMatrixRMaj(y.numRows, y.numCols); DMatrixRMaj zd = new DMatrixRMaj(y.numRows, y.numCols); before = DateTimeHelper.CurrentTimeMilliseconds; for (int i = 0; i < 100; i++) { CommonOps_DDRM.mult(Ad, xd, yd); CommonOps_DDRM.add(1.5, yd, 0.75, yd, zd); } after = DateTimeHelper.CurrentTimeMilliseconds; Console.WriteLine("norm = " + NormOps_DDRM.fastNormF(yd) + " dense time = " + (after - before) + " ms"); }