diff --git a/src/main/java/org/apache/commons/math4/linear/EigenDecomposition.java b/src/main/java/org/apache/commons/math4/linear/EigenDecomposition.java index aa92b5ec2..f9afeb74f 100644 --- a/src/main/java/org/apache/commons/math4/linear/EigenDecomposition.java +++ b/src/main/java/org/apache/commons/math4/linear/EigenDecomposition.java @@ -189,7 +189,7 @@ public class EigenDecomposition { if (cachedD == null) { // cache the matrix for subsequent calls - cachedD = MatrixUtils.createRealDiagonalMatrix(realEigenvalues); + cachedD = MatrixUtils.createRealMatrixWithDiagonal(realEigenvalues); for (int i = 0; i < imagEigenvalues.length; i++) { if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) > 0) { diff --git a/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java b/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java index 86d74d6e2..7b4156199 100644 --- a/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java +++ b/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java @@ -210,12 +210,31 @@ public class MatrixUtils { * The array elements will be copied. * @return a diagonal matrix instance. * + * @see #createRealMatrixWithDiagonal(double[]) * @since 2.0 */ public static DiagonalMatrix createRealDiagonalMatrix(final double[] diagonal) { return new DiagonalMatrix(diagonal, true); } + /** + * Creates a dense matrix with the specified diagonal elements. + * + * @param diagonal Diagonal elements of the matrix. + * @return a matrix instance. + * + * @see #createRealDiagonalMatrix(double[]) + * @since 4.0 + */ + public static RealMatrix createRealMatrixWithDiagonal(final double[] diagonal) { + final int size = diagonal.length; + final RealMatrix m = createRealMatrix(size, size); + for (int i = 0; i < size; i++) { + m.setEntry(i, i, diagonal[i]); + } + return m; + } + /** * Returns a diagonal matrix with specified elements. *