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); }
public virtual void set(Matrix original) { DMatrix m = (DMatrix)original; if (m.getNumCols() == 1 && m.getNumRows() == 6) { a1 = m.get(0, 0); a2 = m.get(1, 0); a3 = m.get(2, 0); a4 = m.get(3, 0); a5 = m.get(4, 0); a6 = m.get(5, 0); } else if (m.getNumRows() == 1 && m.getNumCols() == 6) { a1 = m.get(0, 0); a2 = m.get(0, 1); a3 = m.get(0, 2); a4 = m.get(0, 3); a5 = m.get(0, 4); a6 = m.get(0, 5); } else { throw new ArgumentException("Incompatible shape"); } }
/** * <p> * Checks to see if each element in the matrix is within tolerance of each other: * </p> * * <p> * The two matrices are identical with in tolerance if:<br> * |a<sub>ij</sub> - b<sub>ij</sub>| ≤ tol * </p> * * <p> * In addition if an element is NaN or infinite in one matrix it must be the same in the other. * </p> * * @param A Matrix A * @param B Matrix B * @param tol Tolerance */ public static void assertEqualsUncountable(DMatrix A, DMatrix B, double tol) { assertShape(A, B); for (int i = 0; i < A.getNumRows(); i++) { for (int j = 0; j < A.getNumCols(); j++) { double valA = A.get(i, j); double valB = B.get(i, j); if (double.IsNaN(valA)) { assertTrue(double.IsNaN(valB), "At (" + i + "," + j + ") A = " + valA + " B = " + valB); } else if (double.IsInfinity(valA)) { assertTrue(double.IsInfinity(valB), "At (" + i + "," + j + ") A = " + valA + " B = " + valB); } else { double diff = Math.Abs(valA - valB); assertTrue(diff <= tol, "At (" + i + "," + j + ") A = " + valA + " B = " + valB); } } } }
/** * Assert equals with a relative error */ public static void assertRelativeEquals(DMatrix A, DMatrix B, double tol) { assertShape(A, B); for (int i = 0; i < A.getNumRows(); i++) { for (int j = 0; j < A.getNumCols(); j++) { double valA = A.get(i, j); double valB = B.get(i, j); if ((double.IsNaN(valA) != double.IsNaN(valB)) || (double.IsInfinity(valA) != double.IsInfinity(valB))) { throw new AssertFailedException("At (" + i + "," + j + ") A = " + valA + " B = " + valB); } double max = Math.Max(Math.Abs(valA), Math.Abs(valB)); double error = Math.Abs(valA - valB) / max; if (error > tol) { Console.WriteLine("------------ A -----------"); A.print(); Console.WriteLine("\n------------ B -----------"); B.print(); throw new AssertFailedException("At (" + i + "," + j + ") A = " + valA + " B = " + valB + " error = " + error); } } } }
public static void print(Stream output, DMatrix mat, string format) { string type = mat is ReshapeMatrix ? "dense64" : "dense64 fixed"; Console.WriteLine("Type = " + type + " real , numRows = " + mat.getNumRows() + " , numCols = " + mat.getNumCols()); format += " "; for (int y = 0; y < mat.getNumRows(); y++) { for (int x = 0; x < mat.getNumCols(); x++) { Console.Write(format, mat.get(y, x)); } Console.WriteLine(); } }
/** * 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)); } } }
public virtual void set(Matrix original) { DMatrix m = (DMatrix)original; if (m.getNumCols() == 1 && m.getNumRows() == 2) { a1 = m.get(0, 0); a2 = m.get(1, 0); } else if (m.getNumRows() == 1 && m.getNumCols() == 2) { a1 = m.get(0, 0); a2 = m.get(0, 1); } else { throw new ArgumentException("Incompatible shape"); } }
/** * Saves a matrix to disk using in a Column Space Value (CSV) format. For a * description of the format see {@link MatrixIO#loadCSV(String)}. * * @param A The matrix being saved. * @param fileName Name of the file its being saved at. * @throws java.io.IOException */ public static void saveCSV(DMatrix A, string fileName) { var sb = new StringBuilder(); sb = sb.AppendLine(A.getNumRows() + " " + A.getNumCols() + " real"); for (int i = 0; i < A.getNumRows(); i++) { for (int j = 0; j < A.getNumCols(); j++) { sb = sb.Append(A.get(i, j) + " "); } sb = sb.AppendLine(); } using (var stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)) using (var writer = new StreamWriter(stream)) { writer.Write(sb.ToString()); } }
/** * Checks to see if every element in A is countable. A doesn't have any element with * a value of NaN or infinite. * * @param A Matrix */ public static void assertCountable(DMatrix A) { for (int i = 0; i < A.getNumRows(); i++) { for (int j = 0; j < A.getNumCols(); j++) { assertTrue(!double.IsNaN(A.get(i, j)), "NaN found at " + i + " " + j); assertTrue(!double.IsInfinity(A.get(i, j)), "Infinite found at " + i + " " + j); } } }
public static void setRandom(DMatrix a, double min, double max, IMersenneTwister rand) { for (int i = 0; i < a.getNumRows(); i++) { for (int j = 0; j < a.getNumCols(); j++) { double val = rand.NextDouble() * (max - min) + min; a.set(i, j, val); } } }
/** * Creates a new DMatrixRMaj which contains the same information as the provided Matrix64F. * * @param mat Matrix whose values will be copied. Not modified. */ public DMatrixRMaj(DMatrix mat) : this(mat.getNumRows(), mat.getNumCols()) { for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { set(i, j, mat.get(i, j)); } } }
public static bool isEquivalentTriangle(bool upper, DMatrix a, DMatrix b, double tol) { if (a.getNumRows() != b.getNumRows() || a.getNumCols() != b.getNumCols()) { return(false); } if (upper) { for (int i = 0; i < a.getNumRows(); i++) { for (int j = i; j < a.getNumCols(); j++) { double diff = Math.Abs(a.get(i, j) - b.get(i, j)); if (diff > tol) { return(false); } } } } else { for (int j = 0; j < a.getNumCols(); j++) { for (int i = j; i < a.getNumRows(); i++) { double diff = Math.Abs(a.get(i, j) - b.get(i, j)); if (diff > tol) { return(false); } } } } return(true); }
public static void copy(DMatrix from, DMatrix to) { int numCols = from.getNumCols(); int numRows = from.getNumRows(); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { to.set(i, j, from.get(i, j)); } } }
/** * <p> * Checks to see if each element in the upper or lower triangular portion of the two matrices are within tolerance of * each other: tol ≥ |a<sub>ij</sub> - b<sub>ij</sub>|. * <p> * * <p> * NOTE: If any of the elements are not countable then false is returned.<br> * NOTE: If a tolerance of zero is passed in this is equivalent to calling * {@link #isEquals(DMatrixD1, DMatrixD1)} * </p> * * @param a A matrix. Not modified. * @param b A matrix. Not modified. * @param upper true of upper triangular and false for lower. * @param tol How close to being identical each element needs to be. * @return true if equals and false otherwise. */ public static bool isEqualsTriangle(DMatrix a, DMatrix b, bool upper, double tol) { if (a.getNumRows() != b.getNumRows() || a.getNumCols() != b.getNumCols()) { return(false); } if (upper) { for (int i = 0; i < a.getNumRows(); i++) { for (int j = i; j < a.getNumCols(); j++) { if (Math.Abs(a.get(i, j) - b.get(i, j)) > tol) { return(false); } } } } else { for (int i = 0; i < a.getNumRows(); i++) { int end = Math.Min(i, a.getNumCols() - 1); for (int j = 0; j <= end; j++) { if (Math.Abs(a.get(i, j) - b.get(i, j)) > tol) { return(false); } } } } return(true); }
/** * <p> * Checks to see if the transpose of B is equal to A and countable: * </p> * * <p> * |a<sub>ij</sub> - b<sub>ji</sub>| ≤ tol * </p> * * <p> * The test will fail if any element in either matrix is NaN or infinite. * </p> * * @param A Matrix A * @param B Matrix B * @param tol Tolerance */ public static void assertEqualsTrans(DMatrix A, DMatrix B, double tol) { assertShape(A, B.getNumCols(), B.getNumRows()); for (int i = 0; i < A.getNumRows(); i++) { for (int j = 0; j < A.getNumCols(); j++) { double valA = A.get(i, j); double valB = B.get(j, i); assertTrue(!double.IsNaN(valA) && !double.IsNaN(valB), "A(" + i + "," + j + ") = " + valA + ") B(" + j + "," + i + ") = " + valB); assertTrue(!double.IsInfinity(valA) && !double.IsInfinity(valB), "A(" + i + "," + j + ") = " + valA + ") B(" + j + "," + i + ") = " + valB); assertTrue(Math.Abs(valA - valB) <= tol, "A(" + i + "," + j + ") = " + valA + ") B(" + j + "," + i + ") = " + valB); } } }
/** * Returns true if the provided matrix is has a value of 1 along the diagonal * elements and zero along all the other elements. * * @param a Matrix being inspected. * @param tol How close to zero or one each element needs to be. * @return If it is within tolerance to an identity matrix. */ public static bool isIdentity(DMatrix a, double tol) { for (int i = 0; i < a.getNumRows(); i++) { for (int j = 0; j < a.getNumCols(); j++) { if (i == j) { if (Math.Abs(a.get(i, j) - 1.0) > tol) { return(false); } } else { if (Math.Abs(a.get(i, j)) > tol) { return(false); } } } } return(true); }