001/******************************************************************************* 002 * This software is provided as a supplement to the authors' textbooks on digital 003 * image processing published by Springer-Verlag in various languages and editions. 004 * Permission to use and distribute this software is granted under the BSD 2-Clause 005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved. 007 * Visit https://imagingbook.com for additional details. 008 ******************************************************************************/ 009package imagingbook.common.math.eigen; 010 011import org.apache.commons.math3.linear.EigenDecomposition; 012import org.apache.commons.math3.linear.RealMatrix; 013import org.apache.commons.math3.linear.RealVector; 014 015/** 016 * An implementation of {@link RealEigenDecomposition}, which merely wraps an instance of {@link EigenDecomposition} as 017 * provided by the Apache Commons Math library. Note this works only for square matrices and symmetry and zero 018 * tolerances cannot be specified, default values are always used. The Jama version ({@link EigenDecompositionJama}) 019 * generally appears to be more flexible and robust. 020 * 021 * @author WB 022 * @version 2022/11/24 023 * @see EigenDecompositionJama 024 * @see Eigensolver2x2 025 */ 026public class EigenDecompositionApache implements RealEigenDecomposition { 027 028 // instance of org.apache.commons.math3.linear.EigenDecomposition 029 private final EigenDecomposition decomp; 030 031 /** 032 * Constructor, calculates the eigen decomposition of the specified matrix, which must be square but does not need 033 * to be symmetric. 034 * 035 * @param M the matrix to decompose 036 */ 037 public EigenDecompositionApache(RealMatrix M) { 038 this.decomp = new EigenDecomposition(M); 039 } 040 041 @Override 042 public boolean hasComplexEigenvalues() { 043 return decomp.hasComplexEigenvalues(); 044 } 045 046 @Override 047 public double getRealEigenvalue(int k) { 048 return decomp.getRealEigenvalue(k); 049 } 050 051 @Override 052 public double[] getRealEigenvalues() { 053 return decomp.getRealEigenvalues(); 054 } 055 056 @Override 057 public RealVector getEigenvector(int k) { 058 return decomp.getEigenvector(k); 059 } 060 061 @Override 062 public RealMatrix getV() { 063 return decomp.getV(); 064 } 065 066 /** 067 * Returns the underlying Apache eigendecomposition instance, mainly for debugging. 068 * 069 * @return the Apache eigendecomposition instance 070 */ 071 public EigenDecomposition getInternalDecomposition() { 072 return decomp; 073 } 074 075}