001package ch.akuhn.matrix.eigenvalues;
002
003import ch.akuhn.matrix.Matrix;
004import ch.akuhn.matrix.Vector;
005
006/**
007 * The eigen-decomposition of a matrix.
008 * 
009 * @author Adrian Kuhn
010 * 
011 */
012public class Eigenvalues {
013
014        /**
015         * The eigenvalues
016         */
017        public double[] value;
018
019        /**
020         * The eigenvectors
021         */
022        public Vector[] vector;
023
024        protected int n;
025        protected int nev;
026
027        /**
028         * Construct with the given dimensions
029         * 
030         * @param n
031         */
032        public Eigenvalues(int n) {
033                this.n = n;
034                this.nev = n;
035        }
036
037        /**
038         * Get an object that can compute the eigendecomposition of the given
039         * matrix. If the matrix has fewer than 10 columns, this will be an
040         * {@link AllEigenvalues}, otherwise it will be a {@link FewEigenvalues}.
041         * 
042         * @param A
043         * @return the object to compute the eigen decomposition
044         */
045        public static Eigenvalues of(Matrix A) {
046                if (A.columnCount() == 0) {
047                        final Eigenvalues eigen = new Eigenvalues(0);
048                        eigen.value = new double[0];
049                        eigen.vector = new Vector[0];
050                        return eigen;
051                }
052                if (A.columnCount() == 1) {
053                        final Eigenvalues eigen = new Eigenvalues(0);
054                        eigen.value = new double[] { A.get(0, 0) };
055                        eigen.vector = new Vector[] { Vector.from(1.0) };
056                        return eigen;
057                }
058                if (A.columnCount() < 10)
059                        return new AllEigenvalues(A);
060                return FewEigenvalues.of(A);
061        }
062
063        /**
064         * Configure to compute the largest <code>nev</code> values/vectors.
065         * 
066         * @param nev
067         * @return this
068         */
069        public Eigenvalues largest(int nev) {
070                this.nev = nev;
071                return this;
072        }
073
074        /**
075         * Run the decomposition algorithm. Subclasses should override as necessary.
076         * 
077         * @return this
078         */
079        public Eigenvalues run() {
080                return this;
081        }
082
083        /**
084         * @return the total number of possible eigen vectors (i.e. the number of
085         *         rows of the input)
086         */
087        public int getN() {
088                return n;
089        }
090}