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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.linear;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.util.FastMath;
  25. import org.hipparchus.util.Precision;

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

  54.     /**
  55.      * Build the transformation to Hessenberg form of a general matrix.
  56.      *
  57.      * @param matrix matrix to transform
  58.      * @throws MathIllegalArgumentException if the matrix is not square
  59.      */
  60.     public HessenbergTransformer(final RealMatrix matrix) {
  61.         if (!matrix.isSquare()) {
  62.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NON_SQUARE_MATRIX,
  63.                                                    matrix.getRowDimension(), matrix.getColumnDimension());
  64.         }

  65.         final int m = matrix.getRowDimension();
  66.         householderVectors = matrix.getData();
  67.         ort = new double[m];
  68.         cachedP = null;
  69.         cachedPt = null;
  70.         cachedH = null;

  71.         // transform matrix
  72.         transform();
  73.     }

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

  85.             for (int i = 0; i < n; i++) {
  86.                 for (int j = 0; j < n; j++) {
  87.                     pa[i][j] = (i == j) ? 1 : 0;
  88.                 }
  89.             }

  90.             for (int m = high - 1; m >= 1; m--) {
  91.                 if (householderVectors[m][m - 1] != 0.0) {
  92.                     for (int i = m + 1; i <= high; i++) {
  93.                         ort[i] = householderVectors[i][m - 1];
  94.                     }

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

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

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

  102.                         for (int i = m; i <= high; i++) {
  103.                             pa[i][j] += g * ort[i];
  104.                         }
  105.                     }
  106.                 }
  107.             }

  108.             cachedP = MatrixUtils.createRealMatrix(pa);
  109.         }
  110.         return cachedP;
  111.     }

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

  122.         // return the cached matrix
  123.         return cachedPt;
  124.     }

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

  139.                 // copy upper triangular part of the matrix
  140.                 System.arraycopy(householderVectors[i], i, h[i], i, m - i);
  141.             }
  142.             cachedH = MatrixUtils.createRealMatrix(h);
  143.         }

  144.         // return the cached matrix
  145.         return cachedH;
  146.     }

  147.     /**
  148.      * Get the Householder vectors of the transform.
  149.      * <p>Note that since this class is only intended for internal use, it returns
  150.      * directly a reference to its internal arrays, not a copy.</p>
  151.      *
  152.      * @return the main diagonal elements of the B matrix
  153.      */
  154.     double[][] getHouseholderVectorsRef() {
  155.         return householderVectors; // NOPMD - returning an internal array is intentional and documented here
  156.     }

  157.     /**
  158.      * Transform original matrix to Hessenberg form.
  159.      * <p>Transformation is done using Householder transforms.</p>
  160.      */
  161.     private void transform() {
  162.         final int n = householderVectors.length;
  163.         final int high = n - 1;

  164.         for (int m = 1; m <= high - 1; m++) {
  165.             // Scale column.
  166.             double scale = 0;
  167.             for (int i = m; i <= high; i++) {
  168.                 scale += FastMath.abs(householderVectors[i][m - 1]);
  169.             }

  170.             if (!Precision.equals(scale, 0)) {
  171.                 // Compute Householder transformation.
  172.                 double h = 0;
  173.                 for (int i = high; i >= m; i--) {
  174.                     ort[i] = householderVectors[i][m - 1] / scale;
  175.                     h += ort[i] * ort[i];
  176.                 }
  177.                 final double g = (ort[m] > 0) ? -FastMath.sqrt(h) : FastMath.sqrt(h);

  178.                 h -= ort[m] * g;
  179.                 ort[m] -= g;

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

  182.                 for (int j = m; j < n; j++) {
  183.                     double f = 0;
  184.                     for (int i = high; i >= m; i--) {
  185.                         f += ort[i] * householderVectors[i][j];
  186.                     }
  187.                     f /= h;
  188.                     for (int i = m; i <= high; i++) {
  189.                         householderVectors[i][j] -= f * ort[i];
  190.                     }
  191.                 }

  192.                 for (int i = 0; i <= high; i++) {
  193.                     double f = 0;
  194.                     for (int j = high; j >= m; j--) {
  195.                         f += ort[j] * householderVectors[i][j];
  196.                     }
  197.                     f /= h;
  198.                     for (int j = m; j <= high; j++) {
  199.                         householderVectors[i][j] -= f * ort[j];
  200.                     }
  201.                 }

  202.                 ort[m] = scale * ort[m];
  203.                 householderVectors[m][m - 1] = scale * g;
  204.             }
  205.         }
  206.     }
  207. }