/** * Creates a new SimpleMatrix which is a copy of the Matrix. * * @param orig The original matrix whose value is copied. Not modified. */ public SimpleMatrixD(DMatrixRBlock orig) { if (orig == null) { throw new ArgumentNullException(); } DMatrixRMaj a = new DMatrixRMaj(orig.getNumRows(), orig.getNumCols()); ConvertDMatrixStruct.convert((DMatrixRBlock)orig, a); setMatrix(a); }
public static void main(string[] args) { // declare the matrix DMatrix3x3 a = new DMatrix3x3(); DMatrix3x3 b = new DMatrix3x3(); // Can assign values the usual way for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { a.set(i, j, i + j + 1); } } // Direct manipulation of each value is the fastest way to assign/read values a.a11 = 12; a.a23 = 64; // can print the usual way too a.print(); // most of the standard operations are support CommonOps_DDF3.transpose(a, b); b.print(); Console.WriteLine("Determinant = " + CommonOps_DDF3.det(a)); // matrix-vector operations are also supported // Constructors for vectors and matrices can be used to initialize its value DMatrix3 v = new DMatrix3(1, 2, 3); DMatrix3 result = new DMatrix3(); CommonOps_DDF3.mult(a, v, result); // Conversion into DMatrixRMaj can also be done DMatrixRMaj dm = ConvertDMatrixStruct.convert(a, null); dm.print(); // This can be useful if you need do more advanced operations SimpleMatrix <DMatrixRMaj> sv = SimpleMatrix <DMatrixRMaj> .wrap(dm).svd().getV() as SimpleMatrix <DMatrixRMaj>; // can then convert it back into a fixed matrix DMatrix3x3 fv = ConvertDMatrixStruct.convert(sv.getDDRM(), (DMatrix3x3)null); Console.WriteLine("Original simple matrix and converted fixed matrix"); sv.print(); fv.print(); }
/** * 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); }
/** * Creates a new SimpleMatrix which is a copy of the Matrix. * * @param orig The original matrix whose value is copied. Not modified. */ public SimpleMatrix(Matrix orig) { Matrix mat; if (orig is DMatrixRBlock) { DMatrixRMaj a = new DMatrixRMaj(orig.getNumRows(), orig.getNumCols()); ConvertDMatrixStruct.convert((DMatrixRBlock)orig, a); mat = a; } else if (orig is FMatrixRBlock) { FMatrixRMaj a = new FMatrixRMaj(orig.getNumRows(), orig.getNumCols()); ConvertFMatrixStruct.convert((FMatrixRBlock)orig, a); mat = a; } else { mat = orig.copy(); } setMatrix((T)mat); }
/** * Converts a row major block matrix into a row major matrix. * * @param src Original DMatrixRBlock.. Not modified. * @param dst Equivalent DMatrixRMaj. Modified. */ public static DMatrixRMaj convert(DMatrixRBlock src, DMatrixRMaj dst) { return(ConvertDMatrixStruct.convert(src, dst)); }
/** * Converts a row major matrix into a row major block matrix. * * @param src Original DMatrixRMaj. Not modified. * @param dst Equivalent DMatrixRBlock. Modified. */ public static void convert(DMatrixRMaj src, DMatrixRBlock dst) { ConvertDMatrixStruct.convert(src, dst); }
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"); }
/** * <p>Converts the block matrix into a SimpleMatrix.</p> * * @param A Block matrix that is being converted. Not modified. * @return Equivalent SimpleMatrix. */ public static SimpleMatrix <T> convertSimple <T>(DMatrixRBlock A) where T : class, Matrix { var B = ConvertDMatrixStruct.convert(A, null) as T; return(SimpleMatrix <T> .wrap(B)); }
public static DMatrixSparseCSC parse_DSCC(string s, int numColumns) { DMatrixRMaj tmp = parse_DDRM(s, numColumns); return(ConvertDMatrixStruct.convert(tmp, (DMatrixSparseCSC)null, 0)); }