Exemplo n.º 1
0
        public static DoubleMatrix2D Solve(DoubleMatrix2D A, DoubleMatrix2D B)
        {
            LUDecomposition lu = new LUDecomposition(A);
            QRDecomposition qr = new QRDecomposition(B);

            return(A.Rows == A.Columns ? (lu.Solve(B)) : (qr.Solve(B)));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the results of <i>ToString(A)</i> and additionally the results of all sorts of decompositions applied to the given matrix.
        /// Useful for debugging or to quickly get the rough picture.
        /// </summary>
        /// <param name="matrix"></param>
        /// <returns></returns>
        /// <example>
        /// <pre>
        /// A = 3 x 3 matrix
        /// 249  66  68
        /// 104 214 108
        /// 144 146 293
        ///
        /// cond         : 3.931600417472078
        /// det          : 9638870.0
        /// norm1        : 497.0
        /// norm2        : 473.34508217011404
        /// normF        : 516.873292016525
        /// normInfinity : 583.0
        /// rank         : 3
        /// trace        : 756.0
        ///
        /// density                      : 1.0
        /// isDiagonal                   : false
        /// isDiagonallyDominantByColumn : true
        /// isDiagonallyDominantByRow    : true
        /// isIdentity                   : false
        /// isLowerBidiagonal            : false
        /// isLowerTriangular            : false
        /// isNonNegative                : true
        /// isOrthogonal                 : false
        /// isPositive                   : true
        /// isSingular                   : false
        /// isSkewSymmetric              : false
        /// isSquare                     : true
        /// isStrictlyLowerTriangular    : false
        /// isStrictlyTriangular         : false
        /// isStrictlyUpperTriangular    : false
        /// isSymmetric                  : false
        /// isTriangular                 : false
        /// isTridiagonal                : false
        /// isUnitTriangular             : false
        /// isUpperBidiagonal            : false
        /// isUpperTriangular            : false
        /// isZero                       : false
        /// lowerBandwidth               : 2
        /// semiBandwidth                : 3
        /// upperBandwidth               : 2
        ///
        /// -----------------------------------------------------------------------------
        /// LUDecompositionQuick(A) --> isNonSingular(A), det(A), pivot, L, U, inverse(A)
        /// -----------------------------------------------------------------------------
        /// isNonSingular = true
        /// det = 9638870.0
        /// pivot = [0, 1, 2]
        ///
        /// L = 3 x 3 matrix
        /// 1        0       0
        /// 0.417671 1       0
        /// 0.578313 0.57839 1
        ///
        /// U = 3 x 3 matrix
        /// 249  66         68
        ///   0 186.433735  79.598394
        ///   0   0        207.635819
        ///
        /// inverse(A) = 3 x 3 matrix
        ///  0.004869 -0.000976 -0.00077
        /// -0.001548  0.006553 -0.002056
        /// -0.001622 -0.002786  0.004816
        ///
        /// -----------------------------------------------------------------
        /// QRDecomposition(A) --> hasFullRank(A), H, Q, R, pseudo inverse(A)
        /// -----------------------------------------------------------------
        /// hasFullRank = true
        ///
        /// H = 3 x 3 matrix
        /// 1.814086 0        0
        /// 0.34002  1.903675 0
        /// 0.470797 0.428218 2
        ///
        /// Q = 3 x 3 matrix
        /// -0.814086  0.508871  0.279845
        /// -0.34002  -0.808296  0.48067
        /// -0.470797 -0.296154 -0.831049
        ///
        /// R = 3 x 3 matrix
        /// -305.864349 -195.230337 -230.023539
        ///    0        -182.628353  467.703164
        ///    0           0        -309.13388
        ///
        /// pseudo inverse(A) = 3 x 3 matrix
        ///  0.006601  0.001998 -0.005912
        /// -0.005105  0.000444  0.008506
        /// -0.000905 -0.001555  0.002688
        ///
        /// --------------------------------------------------------------------------
        /// CholeskyDecomposition(A) --> isSymmetricPositiveDefinite(A), L, inverse(A)
        /// --------------------------------------------------------------------------
        /// isSymmetricPositiveDefinite = false
        ///
        /// L = 3 x 3 matrix
        /// 15.779734  0         0
        ///  6.590732 13.059948  0
        ///  9.125629  6.573948 12.903724
        ///
        /// inverse(A) = Illegal operation or error: Matrix is not symmetric positive definite.
        ///
        /// ---------------------------------------------------------------------
        /// EigenvalueDecomposition(A) --> D, V, realEigenvalues, imagEigenvalues
        /// ---------------------------------------------------------------------
        /// realEigenvalues = 1 x 3 matrix
        /// 462.796507 172.382058 120.821435
        /// imagEigenvalues = 1 x 3 matrix
        /// 0 0 0
        ///
        /// D = 3 x 3 matrix
        /// 462.796507   0          0
        ///   0        172.382058   0
        ///   0          0        120.821435
        ///
        /// V = 3 x 3 matrix
        /// -0.398877 -0.778282  0.094294
        /// -0.500327  0.217793 -0.806319
        /// -0.768485  0.66553   0.604862
        ///
        /// ---------------------------------------------------------------------
        /// SingularValueDecomposition(A) --> cond(A), rank(A), norm2(A), U, S, V
        /// ---------------------------------------------------------------------
        /// cond = 3.931600417472078
        /// rank = 3
        /// norm2 = 473.34508217011404
        ///
        /// U = 3 x 3 matrix
        /// 0.46657  -0.877519  0.110777
        /// 0.50486   0.161382 -0.847982
        /// 0.726243  0.45157   0.51832
        ///
        /// S = 3 x 3 matrix
        /// 473.345082   0          0
        ///   0        169.137441   0
        ///   0          0        120.395013
        ///
        /// V = 3 x 3 matrix
        /// 0.577296 -0.808174  0.116546
        /// 0.517308  0.251562 -0.817991
        /// 0.631761  0.532513  0.563301
        /// </pre>
        ///</example>
        public static String ToVerboseString(DoubleMatrix2D matrix)
        {
            /*
             *  StringBuilder buf = new StringBuilder();
             *  String unknown = "Illegal operation or error: ";
             *  String constructionException = "Illegal operation or error upon construction: ";
             *
             *  buf.Append("------------------------------------------------------------------\n");
             *  buf.Append("LUDecomposition(A) --> isNonSingular, det, pivot, L, U, inverse(A)\n");
             *  buf.Append("------------------------------------------------------------------\n");
             */

            String        constructionException = "Illegal operation or error upon construction of ";
            StringBuilder buf = new StringBuilder();

            buf.Append("A = ");
            buf.Append(matrix);

            buf.Append("\n\n" + ToString(matrix));
            buf.Append("\n\n" + Property.DEFAULT.ToString(matrix));

            LUDecomposition lu = null;

            try { lu = new LUDecomposition(matrix); }
            catch (ArgumentException exc)
            {
                buf.Append("\n\n" + constructionException + " LUDecomposition: " + exc.Message);
            }
            if (lu != null)
            {
                buf.Append("\n\n" + lu.ToString());
            }

            QRDecomposition qr = null;

            try { qr = new QRDecomposition(matrix); }
            catch (ArgumentException exc)
            {
                buf.Append("\n\n" + constructionException + " QRDecomposition: " + exc.Message);
            }
            if (qr != null)
            {
                buf.Append("\n\n" + qr.ToString());
            }

            CholeskyDecomposition chol = null;

            try { chol = new CholeskyDecomposition(matrix); }
            catch (ArgumentException exc)
            {
                buf.Append("\n\n" + constructionException + " CholeskyDecomposition: " + exc.Message);
            }
            if (chol != null)
            {
                buf.Append("\n\n" + chol.ToString());
            }

            EigenvalueDecomposition eig = null;

            try { eig = new EigenvalueDecomposition(matrix); }
            catch (ArgumentException exc)
            {
                buf.Append("\n\n" + constructionException + " EigenvalueDecomposition: " + exc.Message);
            }
            if (eig != null)
            {
                buf.Append("\n\n" + eig.ToString());
            }

            SingularValueDecomposition svd = null;

            try { svd = new SingularValueDecomposition(matrix); }
            catch (ArgumentException exc)
            {
                buf.Append("\n\n" + constructionException + " SingularValueDecomposition: " + exc.Message);
            }
            if (svd != null)
            {
                buf.Append("\n\n" + svd.ToString());
            }

            return(buf.ToString());
        }