MatrixUtils { public static boolean anyNaNorInf(Matrix matrix) { for (double[] arrLine : matrix.getArray()) { for (double d : arrLine) { if (Double.isNaN(d) || Double.isInfinite(d)) return true; } } return false; }  static boolean anyNaNorInf(Matrix matrix); static double maxAbsDiag(Matrix matrix); static double minAbsDiag(Matrix matrix); static Matrix sqrt(Matrix matrix); static Matrix pseudoInverse(Matrix matrix); static Matrix invSqrtSym(Matrix matrix); static Matrix abs(Matrix mat); static boolean equals(Matrix m1, Matrix m2, double eps); static Matrix pow(Matrix mat, double exp); static String toString(Matrix mat); static double sum(Matrix mat); static void zero(Matrix m); static EigenValueVectorPair symmetricEig2x2(Matrix m); static EigenValueVectorPair eig2x2(Matrix m); static Matrix matrixFromFloat(float[][] data); static Matrix reduceRank(Matrix m, int rank); static Matrix convert(DenseMatrix mjt); static Matrix convert(DenseMatrix mjt, int nrows, int ncols); static Matrix reverseColumns(Matrix m); static Matrix reverseColumnsInplace(Matrix m); static Matrix reverseRows(Matrix m); static Matrix reverseRowsInplace(Matrix m); static Matrix diag(double[] s); static Matrix setColumn(Matrix m, int c, double v); static Matrix setRow(Matrix m, int r, double v); static Matrix fill(Matrix m, double v); static Matrix minus(Matrix m, double v); static Matrix plus(Matrix m, double v); static Matrix reshape(Matrix m, int newRows); static Matrix reshape(Matrix m, int newRows,boolean columnMajor); static double sumColumn(Matrix m, int col); static double sumRow(Matrix m, int row); static Matrix incrColumn(Matrix m, int col, double value); static Matrix incrRow(Matrix m, int row, double value); static Matrix round(Matrix times); static Matrix min(Matrix A, Matrix B); static Matrix rangePow(double d, double ... range); static Matrix range(double ... range); static Matrix range(int ... range); static Matrix ntuples(Matrix A, Matrix B); static Matrix repmat(Matrix x, int i, int j); static Matrix hstack(Matrix ... x); static Matrix plusEqualsRow(Matrix mat, Matrix rows, int[] rowIndex); static Matrix lessThan(Matrix x, double val); static Matrix greaterThan(Matrix x, double val); static Matrix and(Matrix ... x); static Matrix ones(int rowDimension, int columnDimension); static Matrix all(Matrix x); static int[] valsToIndex(double[] vals); static Matrix greaterThanSet(Matrix x, int val, int toset); static Matrix lessThanSet(Matrix x, int val, int toset);  }
@Test public void testAnyNaNorInf() { Matrix X = new Matrix( new double[][] { { 5, -4}, {-4, 6}, }); assertFalse(MatrixUtils.anyNaNorInf(X)); X = new Matrix( new double[][] { { Double.NEGATIVE_INFINITY, -4}, {-4, 6}, }); assertTrue(MatrixUtils.anyNaNorInf(X)); X = new Matrix( new double[][] { { 1, Double.POSITIVE_INFINITY}, {-4, 6}, }); assertTrue(MatrixUtils.anyNaNorInf(X)); X = new Matrix( new double[][] { { 5, -4}, {Double.NaN, 6}, }); assertTrue(MatrixUtils.anyNaNorInf(X)); }