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); }
/** * Generic, but slow, conversion function. * * @param input Input matrix. * @param output Output matrix. */ public static void convert(DMatrix input, DMatrix output) { if (output is ReshapeMatrix) { ((ReshapeMatrix)output).reshape(input.NumRows, input.NumCols); } else { if (input.NumRows != output.NumRows) { throw new ArgumentException("Number of rows do not match"); } if (input.NumCols != output.NumCols) { throw new ArgumentException("Number of columns do not match"); } } for (int i = 0; i < input.NumRows; i++) { for (int j = 0; j < input.NumCols; j++) { output.unsafe_set(i, j, input.unsafe_get(i, j)); } } }
/** * Generic, but slow, conversion function. * * @param input Input matrix. * @param output Output matrix. */ public static void convert(DMatrix input, DMatrix output) { if (input.getNumRows() != output.getNumRows()) { throw new ArgumentException("Number of rows do not match"); } if (input.getNumCols() != output.getNumCols()) { throw new ArgumentException("Number of columns do not match"); } for (int i = 0; i < input.getNumRows(); i++) { for (int j = 0; j < input.getNumCols(); j++) { output.unsafe_set(i, j, input.unsafe_get(i, j)); } } }