public static DMatrixSparseTriplet convert(DMatrixRMaj src, DMatrixSparseTriplet dst, double tol) { if (dst == null) { dst = new DMatrixSparseTriplet(src.numRows, src.numCols, src.numRows * src.numCols); } else { dst.reshape(src.numRows, src.numCols); } int index = 0; for (int row = 0; row < src.numRows; row++) { for (int col = 0; col < src.numCols; col++) { double value = src.data[index++]; if (Math.Abs(value) > tol) { dst.addItem(row, col, value); } } } return(dst); }
public static DMatrixSparseTriplet convert(DMatrixSparseCSC src, DMatrixSparseTriplet dst) { if (dst == null) { dst = new DMatrixSparseTriplet(src.numRows, src.numCols, src.nz_length); } else { dst.reshape(src.numRows, src.numCols); } int i0 = src.col_idx[0]; for (int col = 0; col < src.numCols; col++) { int i1 = src.col_idx[col + 1]; for (int i = i0; i < i1; i++) { int row = src.nz_rows[i]; dst.addItem(row, col, src.nz_values[i]); } i0 = i1; } return(dst); }
public static DMatrixSparseTriplet convert(DMatrix src, DMatrixSparseTriplet dst, double tol) { if (dst == null) { dst = new DMatrixSparseTriplet(src.getNumRows(), src.getNumCols(), 1); } else { dst.reshape(src.getNumRows(), src.getNumCols()); } for (int row = 0; row < src.getNumRows(); row++) { for (int col = 0; col < src.getNumCols(); col++) { double value = src.unsafe_get(row, col); if (Math.Abs(value) > tol) { dst.addItem(row, col, value); } } } return(dst); }
/** * Converts DMatrixSparseTriplet into a DMatrixSparseCSC. Duplicate elements in triplet will result in an * illegal matrix in output having duplicate elements. * * @param src Original matrix which is to be copied. Not modified. * @param dst Destination. Will be a copy. Modified. * @param histStorage Workspace. Can be null. */ public static DMatrixSparseCSC convert(DMatrixSparseTriplet src, DMatrixSparseCSC dst, IGrowArray histStorage) { dst = UtilEjml.reshapeOrDeclare(dst, src.numRows, src.numCols, src.nz_length); int[] hist = UtilEjml.adjustClear(histStorage, src.numCols); // compute the number of elements in each columns for (int i = 0; i < src.nz_length; i++) { hist[src.nz_rowcol.data[i * 2 + 1]]++; } // define col_idx dst.histogramToStructure(hist); System.Array.Copy(dst.col_idx, 0, hist, 0, dst.numCols); // now write the row indexes and the values for (int i = 0; i < src.nz_length; i++) { int row = src.nz_rowcol.data[i * 2]; int col = src.nz_rowcol.data[i * 2 + 1]; double value = src.nz_value.data[i]; int index = hist[col]++; dst.nz_rows[index] = row; dst.nz_values[index] = value; } dst.indicesSorted = false; return(dst); }
/** * Creates a random symmetric matrix. The entire matrix will be filled in, not just a triangular * portion. * * @param N Number of rows and columns * @param nz_total Number of nonzero elements in the triangular portion of the matrix * @param min Minimum element value, inclusive * @param max Maximum element value, inclusive * @param rand Random number generator * @return Randomly generated matrix */ public static DMatrixSparseCSC symmetric(int N, int nz_total, double min, double max, IMersenneTwister rand) { // compute the number of elements in the triangle, including diagonal int Ntriagle = (N * N + N) / 2; // create a list of open elements int[] open = new int[Ntriagle]; for (int row = 0, index = 0; row < N; row++) { for (int col = row; col < N; col++, index++) { open[index] = row * N + col; } } // perform a random draw UtilEjml.shuffle(open, open.Length, 0, nz_total, rand); Array.Sort(open, 0, nz_total); // construct the matrix DMatrixSparseTriplet A = new DMatrixSparseTriplet(N, N, nz_total * 2); for (int i = 0; i < nz_total; i++) { int index = open[i]; int row = index / N; int col = index % N; double value = rand.NextDouble() * (max - min) + min; if (row == col) { A.addItem(row, col, value); } else { A.addItem(row, col, value); A.addItem(col, row, value); } } DMatrixSparseCSC B = new DMatrixSparseCSC(N, N, A.nz_length); ConvertDMatrixStruct.convert(A, B); return(B); }
/** * Converts SMatrixTriplet_64 into a SMatrixCC_64. * * @param src Original matrix which is to be copied. Not modified. * @param dst Destination. Will be a copy. Modified. * @param hist Workspace. Should be at least as long as the number of columns. Can be null. */ public static DMatrixSparseCSC convert(DMatrixSparseTriplet src, DMatrixSparseCSC dst, int[] hist) { if (dst == null) { dst = new DMatrixSparseCSC(src.numRows, src.numCols, src.nz_length); } else { dst.reshape(src.numRows, src.numCols, src.nz_length); } if (hist == null) { hist = new int[src.numCols]; } else if (hist.Length >= src.numCols) { Array.Clear(hist, 0, src.numCols); } else { throw new ArgumentException("Length of hist must be at least numCols"); } // compute the number of elements in each columns for (int i = 0; i < src.nz_length; i++) { hist[src.nz_data[i].col]++; } // define col_idx dst.colsum(hist); // now write the row indexes and the values for (int i = 0; i < src.nz_length; i++) { DMatrixSparseTriplet.Element e = src.nz_data[i]; int index = hist[e.col]++; dst.nz_rows[index] = e.row; dst.nz_values[index] = e.value; } dst.indicesSorted = false; return(dst); }
public static bool isEquals(DMatrixSparseTriplet a, DMatrixSparseTriplet b, double tol) { if (!isSameShape(a, b)) { return(false); } for (int i = 0; i < a.nz_length; i++) { DMatrixSparseTriplet.Element ea = a.nz_data[i]; DMatrixSparseTriplet.Element eb = b.nz_data[b.nz_index(ea.row, ea.col)]; if (eb == null || Math.Abs(ea.value - eb.value) > tol) { return(false); } } return(true); }
/** * Randomly generates matrix with the specified number of matrix elements filled with values from min to max. * * @param numRows Number of rows * @param numCols Number of columns * @param nz_total Total number of non-zero elements in the matrix * @param min Minimum value * @param max maximum value * @param rand Random number generated * @return Randomly generated matrix */ public static DMatrixSparseTriplet uniform(int numRows, int numCols, int nz_total, double min, double max, IMersenneTwister rand) { // Create a list of all the possible element values int N = numCols * numRows; if (N < 0) { throw new ArgumentException("matrix size is too large"); } nz_total = Math.Min(N, nz_total); int[] selected = new int[N]; for (int i = 0; i < N; i++) { selected[i] = i; } for (int i = 0; i < nz_total; i++) { int s = rand.NextInt(N); int tmp = selected[s]; selected[s] = selected[i]; selected[i] = tmp; } // Create a sparse matrix DMatrixSparseTriplet ret = new DMatrixSparseTriplet(numRows, numCols, nz_total); for (int i = 0; i < nz_total; i++) { int row = selected[i] / numCols; int col = selected[i] % numCols; double value = rand.NextDouble() * (max - min) + min; ret.addItem(row, col, value); } return(ret); }
public static DMatrixRMaj convert(DMatrixSparseTriplet src, DMatrixRMaj dst) { if (dst == null) { dst = new DMatrixRMaj(src.numRows, src.numCols); } else { dst.reshape(src.numRows, src.numCols); dst.zero(); } for (int i = 0; i < src.nz_length; i++) { DMatrixSparseTriplet.Element e = src.nz_data[i]; dst.unsafe_set(e.row, e.col, e.value); } return(dst); }
public static DMatrixRMaj convert(DMatrixSparseTriplet src, DMatrixRMaj dst) { if (dst == null) { dst = new DMatrixRMaj(src.numRows, src.numCols); } else { dst.reshape(src.numRows, src.numCols); dst.zero(); } for (int i = 0; i < src.nz_length; i++) { int row = src.nz_rowcol.data[i * 2]; int col = src.nz_rowcol.data[i * 2 + 1]; double value = src.nz_value.data[i]; dst.unsafe_set(row, col, value); } return(dst); }
public static DMatrixSparseCSC convert(DMatrixSparseTriplet src, DMatrixSparseCSC dst) { return(convert(src, dst, null)); }
public static bool isSameShape(DMatrixSparseTriplet a, DMatrixSparseTriplet b) { return(a.numRows == b.numRows && a.numCols == b.numCols && a.nz_length == b.nz_length); }
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"); }