Esempio n. 1
0
 /// <summary>
 /// Computes a QR decomposition of the matrix.
 /// </summary>
 /// <returns>A QR decomposition of the matrix.</returns>
 /// <seealso cref="Matrices.QRDecomposition"/>
 public SquareQRDecomposition QRDecomposition()
 {
     double[] rStore  = MatrixAlgorithms.Copy(store, offset, rowStride, colStride, dimension, dimension);
     double[] qtStore = SquareMatrixAlgorithms.CreateUnitMatrix(dimension);
     MatrixAlgorithms.QRDecompose(rStore, qtStore, dimension, dimension);
     return(new SquareQRDecomposition(qtStore, rStore, dimension));
 }
 /// <summary>
 /// Computes the eigenvalues and eigenvectors of the matrix.
 /// </summary>
 /// <returns>A representation of the eigenvalues and eigenvectors of the matrix.</returns>
 /// <remarks>
 /// <para>For a generic vector v and matrix M, Mv = u will point in some direction with no particular relationship to v.
 /// The eigenvectors of a matrix M are vectors z that satisfy Mz = &#x3BB;z, i.e. multiplying an eigenvector by a
 /// matrix reproduces the same vector, up to a prortionality constant &#x3BB; called the eigenvalue.</para>
 /// <para>For v to be an eigenvector of M with eigenvalue &#x3BB;, (M - &#x3BB;I)z = 0. But for a matrix to
 /// anihilate any non-zero vector, that matrix must have determinant, so det(M - &#x3BB;I)=0. For a matrix of
 /// order N, this is an equation for the roots of a polynomial of order N. Since an order-N polynomial always has exactly
 /// N roots, an order-N matrix always has exactly N eigenvalues.</para>
 /// <para>An alternative way of expressing the same relationship is to say that the eigenvalues of a matrix are its
 /// diagonal elements when the matrix is expressed in a basis that diagonalizes it. That is, given Z such that Z<sup>-1</sup>MZ = D,
 /// where D is diagonal, the columns of Z are the eigenvectors of M and the diagonal elements of D are the eigenvalues.</para>
 /// <para>Note that the eigenvectors of a matrix are not entirely unique. Given an eigenvector z, any scaled vector &#x3B1;z
 /// is an eigenvector with the same eigenvalue, so eigenvectors are at most unique up to a rescaling. If an eigenvalue
 /// is degenerate, i.e. there are two or more linearly independent eigenvectors with the same eigenvalue, then any linear
 /// combination of the eigenvectors is also an eigenvector with that eigenvalue, and in fact any set of vectors that span the
 /// same subspace could be taken as the eigenvector set corresponding to that eigenvalue.</para>
 /// <para>The eigenvectors of a symmetric matrix are always orthogonal and the eigenvalues are always real. The transformation
 /// matrix Z is thus orthogonal (Z<sup>-1</sup> = Z<sup>T</sup>).</para>
 /// <para>Finding the eigenvalues and eigenvectors of a symmetric matrix is an O(N<sup>3</sup>) operation.</para>
 /// <para>If you require only the eigenvalues, not the eigenvectors, of the matrix, the <see cref="Eigenvalues"/> method
 /// will produce them faster than this method.</para>
 /// </remarks>
 public RealEigensystem Eigensystem()
 {
     double[][] A = SymmetricMatrixAlgorithms.Copy(values, dimension);
     double[]   V = SquareMatrixAlgorithms.CreateUnitMatrix(dimension);
     SymmetricMatrixAlgorithms.JacobiEigensystem(A, V, dimension);
     return(new RealEigensystem(dimension, SymmetricMatrixAlgorithms.GetDiagonal(A, dimension), V));
 }
Esempio n. 3
0
 public static double[] AccumulateBidiagonalV(double[] store, int rows, int cols)
 {
     //  Q_1 * ... (Q_{n-1} * (Q_n * 1))
     double[] result = SquareMatrixAlgorithms.CreateUnitMatrix(cols);
     for (int k = cols - 2; k >= 0; k--)
     {
         // apply Householder reflection to each column from the left
         for (int j = k + 1; j < cols; j++)
         {
             VectorAlgorithms.ApplyHouseholderReflection(store, (k + 1) * rows + k, rows, result, j * cols + (k + 1), 1, cols - k - 1);
         }
     }
     return(result);
 }
Esempio n. 4
0
        // complicated specific operations

        /// <summary>
        /// Computes the QR decomposition of the matrix.
        /// </summary>
        /// <returns>The QR decomposition of the matrix.</returns>
        /// <remarks>
        /// <para>Only matrices with a number of rows greater than or equal to the number of columns can be QR decomposed. If your
        /// matrix has more columns than rows, you can QR decompose its transpose.</para>
        /// </remarks>
        /// <seealso cref="QRDecomposition"/>
        public QRDecomposition QRDecomposition()
        {
            if (rows < cols)
            {
                throw new InvalidOperationException();
            }

            double[] rStore = MatrixAlgorithms.Copy(store, offset, rowStride, colStride, rows, cols);

            double[] qtStore = SquareMatrixAlgorithms.CreateUnitMatrix(rows);

            MatrixAlgorithms.QRDecompose(rStore, qtStore, rows, cols);

            return(new QRDecomposition(qtStore, rStore, rows, cols));
        }
Esempio n. 5
0
        public static double[] AccumulateBidiagonalU(double[] store, int rows, int cols)
        {
            // ((1 *  Q_n) * Q_{n-1}) ... * Q_1
            double[] result = SquareMatrixAlgorithms.CreateUnitMatrix(rows);

            // iterate over Householder reflections
            for (int k = cols - 1; k >= 0; k--)
            {
                // apply Householder reflection to each row from the right
                for (int j = k; j < rows; j++)
                {
                    VectorAlgorithms.ApplyHouseholderReflection(store, k * rows + k, 1, result, k * rows + j, rows, rows - k);
                }
            }

            return(result);
        }
Esempio n. 6
0
 /// <summary>
 /// Computes the matrix raised to the given power.
 /// </summary>
 /// <param name="n">The power to which to raise the matrix, which must be positive.</param>
 /// <returns>The matrix A<sup>n</sup>.</returns>
 /// <remarks>
 /// <para>This method uses exponentiation-by-squaring, so typically only of order log(n) matrix multiplications are required.
 /// This improves not only the speed with which the matrix power is computed, but also the accuracy.</para>
 /// </remarks>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="n"/> is negative.</exception>
 /// <seealso href="https://en.wikipedia.org/wiki/Exponentiation_by_squaring"/>
 public SquareMatrix Power(int n)
 {
     if (n < 0)
     {
         throw new ArgumentOutOfRangeException(nameof(n));
     }
     else if (n == 0)
     {
         return(new SquareMatrix(SquareMatrixAlgorithms.CreateUnitMatrix(dimension), dimension));
     }
     else if (n == 1)
     {
         return(this.Copy());
     }
     else
     {
         double[] pStore = SquareMatrixAlgorithms.Power(store, dimension, n);
         return(new SquareMatrix(pStore, dimension));
     }
 }
Esempio n. 7
0
 /// <summary>
 /// Returns a square matrix with unit matrix entries.
 /// </summary>
 /// <returns>An independent, write-able copy of the unit matrix.</returns>
 public SquareMatrix ToSquareMatrix()
 {
     double[] storage = SquareMatrixAlgorithms.CreateUnitMatrix(n);
     return(new SquareMatrix(storage, n));
 }