TriDiagonalTransformer.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 java.util.Arrays;

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


  26. /**
  27.  * Class transforming a symmetrical matrix to tridiagonal shape.
  28.  * <p>A symmetrical m &times; m matrix A can be written as the product of three matrices:
  29.  * A = Q &times; T &times; Q<sup>T</sup> with Q an orthogonal matrix and T a symmetrical
  30.  * tridiagonal matrix. Both Q and T are m &times; m matrices.</p>
  31.  * <p>This implementation only uses the upper part of the matrix, the part below the
  32.  * diagonal is not accessed at all.</p>
  33.  * <p>Transformation to tridiagonal shape is often not a goal by itself, but it is
  34.  * an intermediate step in more general decomposition algorithms like {@link
  35.  * EigenDecompositionSymmetric eigen decomposition}. This class is therefore intended for internal
  36.  * use by the library and is not public. As a consequence of this explicitly limited scope,
  37.  * many methods directly returns references to internal arrays, not copies.</p>
  38.  */
  39. class TriDiagonalTransformer {
  40.     /** Householder vectors. */
  41.     private final double[][] householderVectors;
  42.     /** Main diagonal. */
  43.     private final double[] main;
  44.     /** Secondary diagonal. */
  45.     private final double[] secondary;
  46.     /** Cached value of Q. */
  47.     private RealMatrix cachedQ;
  48.     /** Cached value of Qt. */
  49.     private RealMatrix cachedQt;
  50.     /** Cached value of T. */
  51.     private RealMatrix cachedT;

  52.     /**
  53.      * Build the transformation to tridiagonal shape of a symmetrical matrix.
  54.      * <p>The specified matrix is assumed to be symmetrical without any check.
  55.      * Only the upper triangular part of the matrix is used.</p>
  56.      *
  57.      * @param matrix Symmetrical matrix to transform.
  58.      * @throws MathIllegalArgumentException if the matrix is not square.
  59.      */
  60.     TriDiagonalTransformer(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.         main      = new double[m];
  68.         secondary = new double[m - 1];
  69.         cachedQ   = null;
  70.         cachedQt  = null;
  71.         cachedT   = null;

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

  75.     /**
  76.      * Returns the matrix Q of the transform.
  77.      * <p>Q is an orthogonal matrix, i.e. its transpose is also its inverse.</p>
  78.      * @return the Q matrix
  79.      */
  80.     public RealMatrix getQ() {
  81.         if (cachedQ == null) {
  82.             cachedQ = getQT().transpose();
  83.         }
  84.         return cachedQ;
  85.     }

  86.     /**
  87.      * Returns the transpose of the matrix Q of the transform.
  88.      * <p>Q is an orthogonal matrix, i.e. its transpose is also its inverse.</p>
  89.      * @return the Q matrix
  90.      */
  91.     public RealMatrix getQT() {
  92.         if (cachedQt == null) {
  93.             final int m = householderVectors.length;
  94.             double[][] qta = new double[m][m];

  95.             // build up first part of the matrix by applying Householder transforms
  96.             for (int k = m - 1; k >= 1; --k) {
  97.                 final double[] hK = householderVectors[k - 1];
  98.                 qta[k][k] = 1;
  99.                 if (hK[k] != 0.0) {
  100.                     final double inv = 1.0 / (secondary[k - 1] * hK[k]);
  101.                     double beta = 1.0 / secondary[k - 1];
  102.                     qta[k][k] = 1 + beta * hK[k];
  103.                     for (int i = k + 1; i < m; ++i) {
  104.                         qta[k][i] = beta * hK[i];
  105.                     }
  106.                     for (int j = k + 1; j < m; ++j) {
  107.                         beta = 0;
  108.                         for (int i = k + 1; i < m; ++i) {
  109.                             beta += qta[j][i] * hK[i];
  110.                         }
  111.                         beta *= inv;
  112.                         qta[j][k] = beta * hK[k];
  113.                         for (int i = k + 1; i < m; ++i) {
  114.                             qta[j][i] += beta * hK[i];
  115.                         }
  116.                     }
  117.                 }
  118.             }
  119.             qta[0][0] = 1;
  120.             cachedQt = MatrixUtils.createRealMatrix(qta);
  121.         }

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

  125.     /**
  126.      * Returns the tridiagonal matrix T of the transform.
  127.      * @return the T matrix
  128.      */
  129.     public RealMatrix getT() {
  130.         if (cachedT == null) {
  131.             final int m = main.length;
  132.             double[][] ta = new double[m][m];
  133.             for (int i = 0; i < m; ++i) {
  134.                 ta[i][i] = main[i];
  135.                 if (i > 0) {
  136.                     ta[i][i - 1] = secondary[i - 1];
  137.                 }
  138.                 if (i < main.length - 1) {
  139.                     ta[i][i + 1] = secondary[i];
  140.                 }
  141.             }
  142.             cachedT = MatrixUtils.createRealMatrix(ta);
  143.         }

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

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

  156.     /**
  157.      * Get the main diagonal elements of the matrix T of the transform.
  158.      * <p>Note that since this class is only intended for internal use,
  159.      * it returns directly a reference to its internal arrays, not a copy.</p>
  160.      * @return the main diagonal elements of the T matrix
  161.      */
  162.     double[] getMainDiagonalRef() {
  163.         return main; // NOPMD - returning an internal array is intentional and documented here
  164.     }

  165.     /**
  166.      * Get the secondary diagonal elements of the matrix T of the transform.
  167.      * <p>Note that since this class is only intended for internal use,
  168.      * it returns directly a reference to its internal arrays, not a copy.</p>
  169.      * @return the secondary diagonal elements of the T matrix
  170.      */
  171.     double[] getSecondaryDiagonalRef() {
  172.         return secondary; // NOPMD - returning an internal array is intentional and documented here
  173.     }

  174.     /**
  175.      * Transform original matrix to tridiagonal form.
  176.      * <p>Transformation is done using Householder transforms.</p>
  177.      */
  178.     private void transform() {
  179.         final int m = householderVectors.length;
  180.         final double[] z = new double[m];
  181.         for (int k = 0; k < m - 1; k++) {

  182.             //zero-out a row and a column simultaneously
  183.             final double[] hK = householderVectors[k];
  184.             main[k] = hK[k];
  185.             double xNormSqr = 0;
  186.             for (int j = k + 1; j < m; ++j) {
  187.                 final double c = hK[j];
  188.                 xNormSqr += c * c;
  189.             }
  190.             final double a = (hK[k + 1] > 0) ? -FastMath.sqrt(xNormSqr) : FastMath.sqrt(xNormSqr);
  191.             secondary[k] = a;
  192.             if (a != 0.0) {
  193.                 // apply Householder transform from left and right simultaneously

  194.                 hK[k + 1] -= a;
  195.                 final double beta = -1 / (a * hK[k + 1]);

  196.                 // compute a = beta A v, where v is the Householder vector
  197.                 // this loop is written in such a way
  198.                 //   1) only the upper triangular part of the matrix is accessed
  199.                 //   2) access is cache-friendly for a matrix stored in rows
  200.                 Arrays.fill(z, k + 1, m, 0);
  201.                 for (int i = k + 1; i < m; ++i) {
  202.                     final double[] hI = householderVectors[i];
  203.                     final double hKI = hK[i];
  204.                     double zI = hI[i] * hKI;
  205.                     for (int j = i + 1; j < m; ++j) {
  206.                         final double hIJ = hI[j];
  207.                         zI   += hIJ * hK[j];
  208.                         z[j] += hIJ * hKI;
  209.                     }
  210.                     z[i] = beta * (z[i] + zI);
  211.                 }

  212.                 // compute gamma = beta vT z / 2
  213.                 double gamma = 0;
  214.                 for (int i = k + 1; i < m; ++i) {
  215.                     gamma += z[i] * hK[i];
  216.                 }
  217.                 gamma *= beta / 2;

  218.                 // compute z = z - gamma v
  219.                 for (int i = k + 1; i < m; ++i) {
  220.                     z[i] -= gamma * hK[i];
  221.                 }

  222.                 // update matrix: A = A - v zT - z vT
  223.                 // only the upper triangular part of the matrix is updated
  224.                 for (int i = k + 1; i < m; ++i) {
  225.                     final double[] hI = householderVectors[i];
  226.                     for (int j = i; j < m; ++j) {
  227.                         hI[j] -= hK[i] * z[j] + z[i] * hK[j];
  228.                     }
  229.                 }
  230.             }
  231.         }
  232.         main[m - 1] = householderVectors[m - 1][m - 1];
  233.     }
  234. }