001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.linear;
019
020import org.apache.commons.math3.util.FastMath;
021
022
023/**
024 * Calculates the rank-revealing QR-decomposition of a matrix, with column pivoting.
025 * <p>The rank-revealing QR-decomposition of a matrix A consists of three matrices Q,
026 * R and P such that AP=QR.  Q is orthogonal (Q<sup>T</sup>Q = I), and R is upper triangular.
027 * If A is m&times;n, Q is m&times;m and R is m&times;n and P is n&times;n.</p>
028 * <p>QR decomposition with column pivoting produces a rank-revealing QR
029 * decomposition and the {@link #getRank(double)} method may be used to return the rank of the
030 * input matrix A.</p>
031 * <p>This class compute the decomposition using Householder reflectors.</p>
032 * <p>For efficiency purposes, the decomposition in packed form is transposed.
033 * This allows inner loop to iterate inside rows, which is much more cache-efficient
034 * in Java.</p>
035 * <p>This class is based on the class with similar name from the
036 * <a href="http://math.nist.gov/javanumerics/jama/">JAMA</a> library, with the
037 * following changes:</p>
038 * <ul>
039 *   <li>a {@link #getQT() getQT} method has been added,</li>
040 *   <li>the {@code solve} and {@code isFullRank} methods have been replaced
041 *   by a {@link #getSolver() getSolver} method and the equivalent methods
042 *   provided by the returned {@link DecompositionSolver}.</li>
043 * </ul>
044 *
045 * @see <a href="http://mathworld.wolfram.com/QRDecomposition.html">MathWorld</a>
046 * @see <a href="http://en.wikipedia.org/wiki/QR_decomposition">Wikipedia</a>
047 *
048 * @since 3.2
049 */
050public class RRQRDecomposition extends QRDecomposition {
051
052    /** An array to record the column pivoting for later creation of P. */
053    private int[] p;
054
055    /** Cached value of P. */
056    private RealMatrix cachedP;
057
058
059    /**
060     * Calculates the QR-decomposition of the given matrix.
061     * The singularity threshold defaults to zero.
062     *
063     * @param matrix The matrix to decompose.
064     *
065     * @see #RRQRDecomposition(RealMatrix, double)
066     */
067    public RRQRDecomposition(RealMatrix matrix) {
068        this(matrix, 0d);
069    }
070
071   /**
072     * Calculates the QR-decomposition of the given matrix.
073     *
074     * @param matrix The matrix to decompose.
075     * @param threshold Singularity threshold.
076     * @see #RRQRDecomposition(RealMatrix)
077     */
078    public RRQRDecomposition(RealMatrix matrix,  double threshold) {
079        super(matrix, threshold);
080    }
081
082    /** Decompose matrix.
083     * @param qrt transposed matrix
084     */
085    @Override
086    protected void decompose(double[][] qrt) {
087        p = new int[qrt.length];
088        for (int i = 0; i < p.length; i++) {
089            p[i] = i;
090        }
091        super.decompose(qrt);
092    }
093
094    /** Perform Householder reflection for a minor A(minor, minor) of A.
095     * @param minor minor index
096     * @param qrt transposed matrix
097     */
098    @Override
099    protected void performHouseholderReflection(int minor, double[][] qrt) {
100
101        double l2NormSquaredMax = 0;
102        // Find the unreduced column with the greatest L2-Norm
103        int l2NormSquaredMaxIndex = minor;
104        for (int i = minor; i < qrt.length; i++) {
105            double l2NormSquared = 0;
106            for (int j = 0; j < qrt[i].length; j++) {
107                l2NormSquared += qrt[i][j] * qrt[i][j];
108            }
109            if (l2NormSquared > l2NormSquaredMax) {
110                l2NormSquaredMax = l2NormSquared;
111                l2NormSquaredMaxIndex = i;
112            }
113        }
114        // swap the current column with that with the greated L2-Norm and record in p
115        if (l2NormSquaredMaxIndex != minor) {
116            double[] tmp1 = qrt[minor];
117            qrt[minor] = qrt[l2NormSquaredMaxIndex];
118            qrt[l2NormSquaredMaxIndex] = tmp1;
119            int tmp2 = p[minor];
120            p[minor] = p[l2NormSquaredMaxIndex];
121            p[l2NormSquaredMaxIndex] = tmp2;
122        }
123
124        super.performHouseholderReflection(minor, qrt);
125
126    }
127
128
129    /**
130     * Returns the pivot matrix, P, used in the QR Decomposition of matrix A such that AP = QR.
131     *
132     * If no pivoting is used in this decomposition then P is equal to the identity matrix.
133     *
134     * @return a permutation matrix.
135     */
136    public RealMatrix getP() {
137        if (cachedP == null) {
138            int n = p.length;
139            cachedP = MatrixUtils.createRealMatrix(n,n);
140            for (int i = 0; i < n; i++) {
141                cachedP.setEntry(p[i], i, 1);
142            }
143        }
144        return cachedP ;
145    }
146
147    /**
148     * Return the effective numerical matrix rank.
149     * <p>The effective numerical rank is the number of non-negligible
150     * singular values.</p>
151     * <p>This implementation looks at Frobenius norms of the sequence of
152     * bottom right submatrices.  When a large fall in norm is seen,
153     * the rank is returned. The drop is computed as:</p>
154     * <pre>
155     *   (thisNorm/lastNorm) * rNorm < dropThreshold
156     * </pre>
157     * <p>
158     * where thisNorm is the Frobenius norm of the current submatrix,
159     * lastNorm is the Frobenius norm of the previous submatrix,
160     * rNorm is is the Frobenius norm of the complete matrix
161     * </p>
162     *
163     * @param dropThreshold threshold triggering rank computation
164     * @return effective numerical matrix rank
165     */
166    public int getRank(final double dropThreshold) {
167        RealMatrix r    = getR();
168        int rows        = r.getRowDimension();
169        int columns     = r.getColumnDimension();
170        int rank        = 1;
171        double lastNorm = r.getFrobeniusNorm();
172        double rNorm    = lastNorm;
173        while (rank < FastMath.min(rows, columns)) {
174            double thisNorm = r.getSubMatrix(rank, rows - 1, rank, columns - 1).getFrobeniusNorm();
175            if (thisNorm == 0 || (thisNorm / lastNorm) * rNorm < dropThreshold) {
176                break;
177            }
178            lastNorm = thisNorm;
179            rank++;
180        }
181        return rank;
182    }
183
184    /**
185     * Get a solver for finding the A &times; X = B solution in least square sense.
186     * <p>
187     * Least Square sense means a solver can be computed for an overdetermined system,
188     * (i.e. a system with more equations than unknowns, which corresponds to a tall A
189     * matrix with more rows than columns). In any case, if the matrix is singular
190     * within the tolerance set at {@link RRQRDecomposition#RRQRDecomposition(RealMatrix,
191     * double) construction}, an error will be triggered when
192     * the {@link DecompositionSolver#solve(RealVector) solve} method will be called.
193     * </p>
194     * @return a solver
195     */
196    @Override
197    public DecompositionSolver getSolver() {
198        return new Solver(super.getSolver(), this.getP());
199    }
200
201    /** Specialized solver. */
202    private static class Solver implements DecompositionSolver {
203
204        /** Upper level solver. */
205        private final DecompositionSolver upper;
206
207        /** A permutation matrix for the pivots used in the QR decomposition */
208        private RealMatrix p;
209
210        /**
211         * Build a solver from decomposed matrix.
212         *
213         * @param upper upper level solver.
214         * @param p permutation matrix
215         */
216        private Solver(final DecompositionSolver upper, final RealMatrix p) {
217            this.upper = upper;
218            this.p     = p;
219        }
220
221        /** {@inheritDoc} */
222        public boolean isNonSingular() {
223            return upper.isNonSingular();
224        }
225
226        /** {@inheritDoc} */
227        public RealVector solve(RealVector b) {
228            return p.operate(upper.solve(b));
229        }
230
231        /** {@inheritDoc} */
232        public RealMatrix solve(RealMatrix b) {
233            return p.multiply(upper.solve(b));
234        }
235
236        /**
237         * {@inheritDoc}
238         * @throws SingularMatrixException if the decomposed matrix is singular.
239         */
240        public RealMatrix getInverse() {
241            return solve(MatrixUtils.createRealIdentityMatrix(p.getRowDimension()));
242        }
243    }
244}