source: src/main/java/agents/anac/y2019/harddealer/math3/linear/RRQRDecomposition.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 8.6 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package agents.anac.y2019.harddealer.math3.linear;
19
20import agents.anac.y2019.harddealer.math3.util.FastMath;
21
22
23/**
24 * Calculates the rank-revealing QR-decomposition of a matrix, with column pivoting.
25 * <p>The rank-revealing QR-decomposition of a matrix A consists of three matrices Q,
26 * R and P such that AP=QR. Q is orthogonal (Q<sup>T</sup>Q = I), and R is upper triangular.
27 * If A is m&times;n, Q is m&times;m and R is m&times;n and P is n&times;n.</p>
28 * <p>QR decomposition with column pivoting produces a rank-revealing QR
29 * decomposition and the {@link #getRank(double)} method may be used to return the rank of the
30 * input matrix A.</p>
31 * <p>This class compute the decomposition using Householder reflectors.</p>
32 * <p>For efficiency purposes, the decomposition in packed form is transposed.
33 * This allows inner loop to iterate inside rows, which is much more cache-efficient
34 * in Java.</p>
35 * <p>This class is based on the class with similar name from the
36 * <a href="http://math.nist.gov/javanumerics/jama/">JAMA</a> library, with the
37 * following changes:</p>
38 * <ul>
39 * <li>a {@link #getQT() getQT} method has been added,</li>
40 * <li>the {@code solve} and {@code isFullRank} methods have been replaced
41 * by a {@link #getSolver() getSolver} method and the equivalent methods
42 * provided by the returned {@link DecompositionSolver}.</li>
43 * </ul>
44 *
45 * @see <a href="http://mathworld.wolfram.com/QRDecomposition.html">MathWorld</a>
46 * @see <a href="http://en.wikipedia.org/wiki/QR_decomposition">Wikipedia</a>
47 *
48 * @since 3.2
49 */
50public class RRQRDecomposition extends QRDecomposition {
51
52 /** An array to record the column pivoting for later creation of P. */
53 private int[] p;
54
55 /** Cached value of P. */
56 private RealMatrix cachedP;
57
58
59 /**
60 * Calculates the QR-decomposition of the given matrix.
61 * The singularity threshold defaults to zero.
62 *
63 * @param matrix The matrix to decompose.
64 *
65 * @see #RRQRDecomposition(RealMatrix, double)
66 */
67 public RRQRDecomposition(RealMatrix matrix) {
68 this(matrix, 0d);
69 }
70
71 /**
72 * Calculates the QR-decomposition of the given matrix.
73 *
74 * @param matrix The matrix to decompose.
75 * @param threshold Singularity threshold.
76 * @see #RRQRDecomposition(RealMatrix)
77 */
78 public RRQRDecomposition(RealMatrix matrix, double threshold) {
79 super(matrix, threshold);
80 }
81
82 /** Decompose matrix.
83 * @param qrt transposed matrix
84 */
85 @Override
86 protected void decompose(double[][] qrt) {
87 p = new int[qrt.length];
88 for (int i = 0; i < p.length; i++) {
89 p[i] = i;
90 }
91 super.decompose(qrt);
92 }
93
94 /** Perform Householder reflection for a minor A(minor, minor) of A.
95 * @param minor minor index
96 * @param qrt transposed matrix
97 */
98 @Override
99 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}
Note: See TracBrowser for help on using the repository browser.