MatrixUtils { public static Matrix sqrt(Matrix matrix) { final EigenvalueDecomposition evd = matrix.eig(); final Matrix v = evd.getV(); final Matrix d = evd.getD(); for (int r = 0; r < d.getRowDimension(); r++) for (int c = 0; c < d.getColumnDimension(); c++) d.set(r, c, Math.sqrt(d.get(r, c))); final Matrix a = v.inverse(); final Matrix b = v.times(d).inverse(); return a.solve(b).inverse(); } private  MatrixUtils(); 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); static Matrix minusRow(Matrix in, double[] row); static Matrix minusCol(Matrix in, Matrix col); static Matrix plusEquals(Matrix result, Matrix add); static Matrix times(Matrix m, double val); static double[][] mtjToDoubleArray(no.uib.cipr.matrix.DenseMatrix mat); static Matrix sumRows(Matrix m); static Matrix sumCols(Matrix m); static Matrix randGaussian(int rows, int cols); static double sparsity(SparseMatrix matrix); static Matrix diag(Matrix cv); static double[] diagVector(Matrix cv); static String toMatlabString(Matrix mat); static String toPythonString(Matrix mat); static double trace(Matrix mat); static double[] solveHomogeneousSystem(Matrix A); static double[] solveHomogeneousSystem(double[][] A); static double[] solveHomogeneousSystem(DenseMatrix A); static String toJavaString(Matrix mat); static Matrix fromRowPacked(double[] vector, int ncols); static Matrix covariance(Matrix m); static Matrix sign(Matrix m); static Matrix exp(Matrix m); static Matrix tanh(Matrix m);  }
@Test public void testSqrt() { Matrix X = new Matrix( new double[][] { { 5, -4, 1, 0, 0}, {-4, 6, -4, 1, 0}, { 1, -4, 6, -4, 1}, { 0, 1, -4, 6, -4}, { 0, 0, 1, -4, 5}, }); Matrix expected = new Matrix( new double[][] { { 2, -1, -0, -0, -0}, {-1, 2, -1, 0, -0}, { 0, -1, 2, -1, 0}, {-0, 0, -1, 2, -1}, {-0, -0, -0, -1, 2}, }); Matrix Y = MatrixUtils.sqrt(X); assertTrue(MatrixUtils.equals(expected, Y, 0.00001)); }