HessenbergTransformer.java

  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. package org.apache.commons.math4.legacy.linear;

  18. import org.apache.commons.math4.core.jdkmath.JdkMath;
  19. import org.apache.commons.numbers.core.Precision;

  20. /**
  21.  * Class transforming a general real matrix to Hessenberg form.
  22.  * <p>A m &times; m matrix A can be written as the product of three matrices: A = P
  23.  * &times; H &times; P<sup>T</sup> with P an orthogonal matrix and H a Hessenberg
  24.  * matrix. Both P and H are m &times; m matrices.</p>
  25.  * <p>Transformation to Hessenberg form is often not a goal by itself, but it is an
  26.  * intermediate step in more general decomposition algorithms like
  27.  * {@link EigenDecomposition eigen decomposition}. This class is therefore
  28.  * intended for internal use by the library and is not public. As a consequence
  29.  * of this explicitly limited scope, many methods directly returns references to
  30.  * internal arrays, not copies.</p>
  31.  * <p>This class is based on the method orthes in class EigenvalueDecomposition
  32.  * from the <a href="http://math.nist.gov/javanumerics/jama/">JAMA</a> library.</p>
  33.  *
  34.  * @see <a href="http://mathworld.wolfram.com/HessenbergDecomposition.html">MathWorld</a>
  35.  * @see <a href="http://en.wikipedia.org/wiki/Householder_transformation">Householder Transformations</a>
  36.  * @since 3.1
  37.  */
  38. class HessenbergTransformer {
  39.     /** Householder vectors. */
  40.     private final double[][] householderVectors;
  41.     /** Temporary storage vector. */
  42.     private final double[] ort;
  43.     /** Cached value of P. */
  44.     private RealMatrix cachedP;
  45.     /** Cached value of Pt. */
  46.     private RealMatrix cachedPt;
  47.     /** Cached value of H. */
  48.     private RealMatrix cachedH;

  49.     /**
  50.      * Build the transformation to Hessenberg form of a general matrix.
  51.      *
  52.      * @param matrix matrix to transform
  53.      * @throws NonSquareMatrixException if the matrix is not square
  54.      */
  55.     HessenbergTransformer(final RealMatrix matrix) {
  56.         if (!matrix.isSquare()) {
  57.             throw new NonSquareMatrixException(matrix.getRowDimension(),
  58.                     matrix.getColumnDimension());
  59.         }

  60.         final int m = matrix.getRowDimension();
  61.         householderVectors = matrix.getData();
  62.         ort = new double[m];
  63.         cachedP = null;
  64.         cachedPt = null;
  65.         cachedH = null;

  66.         // transform matrix
  67.         transform();
  68.     }

  69.     /**
  70.      * Returns the matrix P of the transform.
  71.      * <p>P is an orthogonal matrix, i.e. its inverse is also its transpose.</p>
  72.      *
  73.      * @return the P matrix
  74.      */
  75.     public RealMatrix getP() {
  76.         if (cachedP == null) {
  77.             final int n = householderVectors.length;
  78.             final int high = n - 1;
  79.             final double[][] pa = new double[n][n];

  80.             for (int i = 0; i < n; i++) {
  81.                 for (int j = 0; j < n; j++) {
  82.                     pa[i][j] = (i == j) ? 1 : 0;
  83.                 }
  84.             }

  85.             for (int m = high - 1; m >= 1; m--) {
  86.                 if (householderVectors[m][m - 1] != 0.0) {
  87.                     for (int i = m + 1; i <= high; i++) {
  88.                         ort[i] = householderVectors[i][m - 1];
  89.                     }

  90.                     for (int j = m; j <= high; j++) {
  91.                         double g = 0.0;

  92.                         for (int i = m; i <= high; i++) {
  93.                             g += ort[i] * pa[i][j];
  94.                         }

  95.                         // Double division avoids possible underflow
  96.                         g = (g / ort[m]) / householderVectors[m][m - 1];

  97.                         for (int i = m; i <= high; i++) {
  98.                             pa[i][j] += g * ort[i];
  99.                         }
  100.                     }
  101.                 }
  102.             }

  103.             cachedP = MatrixUtils.createRealMatrix(pa);
  104.         }
  105.         return cachedP;
  106.     }

  107.     /**
  108.      * Returns the transpose of the matrix P of the transform.
  109.      * <p>P is an orthogonal matrix, i.e. its inverse is also its transpose.</p>
  110.      *
  111.      * @return the transpose of the P matrix
  112.      */
  113.     public RealMatrix getPT() {
  114.         if (cachedPt == null) {
  115.             cachedPt = getP().transpose();
  116.         }

  117.         // return the cached matrix
  118.         return cachedPt;
  119.     }

  120.     /**
  121.      * Returns the Hessenberg matrix H of the transform.
  122.      *
  123.      * @return the H matrix
  124.      */
  125.     public RealMatrix getH() {
  126.         if (cachedH == null) {
  127.             final int m = householderVectors.length;
  128.             final double[][] h = new double[m][m];
  129.             for (int i = 0; i < m; ++i) {
  130.                 if (i > 0) {
  131.                     // copy the entry of the lower sub-diagonal
  132.                     h[i][i - 1] = householderVectors[i][i - 1];
  133.                 }

  134.                 // copy upper triangular part of the matrix
  135.                 System.arraycopy(householderVectors[i], i, h[i], i, m - i);
  136.             }
  137.             cachedH = MatrixUtils.createRealMatrix(h);
  138.         }

  139.         // return the cached matrix
  140.         return cachedH;
  141.     }

  142.     /**
  143.      * Get the Householder vectors of the transform.
  144.      * <p>Note that since this class is only intended for internal use, it returns
  145.      * directly a reference to its internal arrays, not a copy.</p>
  146.      *
  147.      * @return the main diagonal elements of the B matrix
  148.      */
  149.     double[][] getHouseholderVectorsRef() {
  150.         return householderVectors;
  151.     }

  152.     /**
  153.      * Transform original matrix to Hessenberg form.
  154.      * <p>Transformation is done using Householder transforms.</p>
  155.      */
  156.     private void transform() {
  157.         final int n = householderVectors.length;
  158.         final int high = n - 1;

  159.         for (int m = 1; m <= high - 1; m++) {
  160.             // Scale column.
  161.             double scale = 0;
  162.             for (int i = m; i <= high; i++) {
  163.                 scale += JdkMath.abs(householderVectors[i][m - 1]);
  164.             }

  165.             if (!Precision.equals(scale, 0)) {
  166.                 // Compute Householder transformation.
  167.                 double h = 0;
  168.                 for (int i = high; i >= m; i--) {
  169.                     ort[i] = householderVectors[i][m - 1] / scale;
  170.                     h += ort[i] * ort[i];
  171.                 }
  172.                 final double g = (ort[m] > 0) ? -JdkMath.sqrt(h) : JdkMath.sqrt(h);

  173.                 h -= ort[m] * g;
  174.                 ort[m] -= g;

  175.                 // Apply Householder similarity transformation
  176.                 // H = (I - u*u' / h) * H * (I - u*u' / h)

  177.                 for (int j = m; j < n; j++) {
  178.                     double f = 0;
  179.                     for (int i = high; i >= m; i--) {
  180.                         f += ort[i] * householderVectors[i][j];
  181.                     }
  182.                     f /= h;
  183.                     for (int i = m; i <= high; i++) {
  184.                         householderVectors[i][j] -= f * ort[i];
  185.                     }
  186.                 }

  187.                 for (int i = 0; i <= high; i++) {
  188.                     double f = 0;
  189.                     for (int j = high; j >= m; j--) {
  190.                         f += ort[j] * householderVectors[i][j];
  191.                     }
  192.                     f /= h;
  193.                     for (int j = m; j <= high; j++) {
  194.                         householderVectors[i][j] -= f * ort[j];
  195.                     }
  196.                 }

  197.                 ort[m] = scale * ort[m];
  198.                 householderVectors[m][m - 1] = scale * g;
  199.             }
  200.         }
  201.     }
  202. }