This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 899c86b5090fd3f96067a653e5fdb54c5b39f908 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:26:32 2026 +0100 feat(Math): add Matrix.symmetricEigen --- .../main/org/apache/sis/maths/Matrix.java | 99 ++++++++++++++++++++++ .../test/org/apache/sis/maths/MatrixTest.java | 82 ++++++++++++++++++ 2 files changed, 181 insertions(+) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix.java index 7ab8ea138d..aea6341368 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix.java @@ -16,6 +16,9 @@ */ package org.apache.sis.maths; +import java.util.Comparator; +import java.util.stream.IntStream; + /** * @@ -153,4 +156,100 @@ public interface Matrix<T extends Matrix<T>> extends ReadOnly.Matrix<T>, org.ope default org.opengis.referencing.operation.Matrix clone() { return copy(); } + + /** + * Eigenvalues and eigenvectors of a symmetric matrix. + * + * @param values the eigenvalues, in increasing order. + * @param vectors the unit eigenvector of each eigenvalue, in the same order. + * + * @see #symmetricEigen() + */ + record Eigen(double[] values, Vector<?>[] vectors) { + } + + /** + * Computes the eigenvalues and eigenvectors of this matrix, which shall be symmetric. + * For example, the eigenvector of the smallest eigenvalue of a covariance matrix + * is the normal of the plane best fitting the positions. + * + * <ul> + * <li>Computed with the cyclic Jacobi algorithm, which is accurate for symmetric matrices.</li> + * <li>Only symmetric matrices are supported: the result is meaningless for other matrices.</li> + * </ul> + * + * @return the eigenvalues in increasing order and their eigenvectors. + * @throws IllegalStateException if this matrix is not square. + * + * @see "https://en.wikipedia.org/wiki/Jacobi_eigenvalue_algorithm" + */ + default Eigen symmetricEigen() { + if (!isSquare()) { + throw new IllegalStateException("The eigenvalues are computed on square matrices only."); + } + final int n = getNumRow(); + final double[][] a = toArray2Double(ROW_ORDER); + final double[][] v = new double[n][n]; + double norm = 0; + for (int i = 0; i < n; i++) { + v[i][i] = 1; + for (int j = 0; j < n; j++) { + norm += a[i][j] * a[i][j]; + } + } + final double threshold = norm * Math.ulp(1.0) * Math.ulp(1.0); + for (int sweep = 0; sweep < 50; sweep++) { + double off = 0; + for (int p = 0; p < n; p++) { + for (int q = p + 1; q < n; q++) { + off += a[p][q] * a[p][q]; + } + } + if (off <= threshold) { + break; + } + for (int p = 0; p < n; p++) { + for (int q = p + 1; q < n; q++) { + if (a[p][q] == 0) { + continue; + } + // Rotation in the (p,q) plane cancelling a[p][q]. + final double theta = (a[q][q] - a[p][p]) / (2 * a[p][q]); + final double t = (theta == 0) ? 1 : Math.signum(theta) / (Math.abs(theta) + Math.sqrt(theta * theta + 1)); + final double c = 1 / Math.sqrt(t * t + 1); + final double s = t * c; + for (int k = 0; k < n; k++) { + final double kp = a[k][p], kq = a[k][q]; + a[k][p] = c * kp - s * kq; + a[k][q] = s * kp + c * kq; + } + for (int k = 0; k < n; k++) { + final double pk = a[p][k], qk = a[q][k]; + a[p][k] = c * pk - s * qk; + a[q][k] = s * pk + c * qk; + } + for (int k = 0; k < n; k++) { + final double kp = v[k][p], kq = v[k][q]; + v[k][p] = c * kp - s * kq; + v[k][q] = s * kp + c * kq; + } + } + } + } + final int[] order = IntStream.range(0, n).boxed() + .sorted(Comparator.comparingDouble((Integer i) -> a[i][i])) + .mapToInt(Integer::intValue).toArray(); + final double[] values = new double[n]; + final Vector<?>[] vectors = new Vector<?>[n]; + for (int i = 0; i < n; i++) { + final int column = order[i]; + values[i] = a[column][column]; + final double[] vector = new double[n]; + for (int k = 0; k < n; k++) { + vector[k] = v[k][column]; + } + vectors[i] = Vector.of(vector); + } + return new Eigen(values, vectors); + } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/MatrixTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/MatrixTest.java new file mode 100644 index 0000000000..55b673f012 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/MatrixTest.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.maths; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; + + +/** + * Tests the default methods of {@link Matrix}. + * + * @author Johann Sorel (Geomatys) + */ +public class MatrixTest { + + private static final double TOLERANCE = 1E-12; + + private static final double HALF_SQRT2 = Math.sqrt(0.5); + + /** + * The eigenvalues of a diagonal matrix are its diagonal, and its eigenvectors the coordinate axes. + */ + @Test + public void testSymmetricEigenOfDiagonal() { + final Matrix.Eigen eigen = new Matrix3D(3, 0, 0, 0, 1, 0, 0, 0, 2).symmetricEigen(); + assertArrayEquals(new double[] {1, 2, 3}, eigen.values(), TOLERANCE); + assertArrayEquals(new double[] {0, 1, 0}, eigen.vectors()[0].toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {0, 0, 1}, eigen.vectors()[1].toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {1, 0, 0}, eigen.vectors()[2].toArrayDouble(), TOLERANCE); + } + + /** + * A symmetric matrix coupling the two first axes, whose eigenvectors are the diagonals of the (x,y) plane. + */ + @Test + public void testSymmetricEigen() { + final Matrix.Eigen eigen = new Matrix3D(2, 1, 0, 1, 2, 0, 0, 0, 5).symmetricEigen(); + assertArrayEquals(new double[] {1, 3, 5}, eigen.values(), TOLERANCE); + assertArrayEquals(new double[] {HALF_SQRT2, -HALF_SQRT2, 0}, eigen.vectors()[0].toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {HALF_SQRT2, HALF_SQRT2, 0}, eigen.vectors()[1].toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {0, 0, 1}, eigen.vectors()[2].toArrayDouble(), TOLERANCE); + } + + /** + * A matrix of 5 × 5 made of two blocks: the 2 × 2 block of {@link #testSymmetricEigen()} + * and a diagonal of 7, 6, 4. + */ + @Test + public void testSymmetricEigenND() { + final MatrixND matrix = new MatrixND(new double[][] { + {2, 1, 0, 0, 0}, + {1, 2, 0, 0, 0}, + {0, 0, 7, 0, 0}, + {0, 0, 0, 6, 0}, + {0, 0, 0, 0, 4}}); + assertArrayEquals(new double[] {1, 3, 4, 6, 7}, matrix.symmetricEigen().values(), TOLERANCE); + } + + /** + * Only square matrices have eigenvalues. + */ + @Test + public void testSymmetricEigenOfNonSquare() { + assertThrows(IllegalStateException.class, () -> new MatrixND(2, 3).symmetricEigen()); + } +}
