示例#1
0
 /// <summary>
 /// Get the inverse of the decomposed matrix.
 /// </summary>
 /// <returns>the inverse matrix.</returns>
 public RealMatrix getInverse()
 {
     return(solve(MatrixUtils.createRealIdentityMatrix(lTData.Length)));
 }
        /// <inheritdoc/>
        public RealMatrix power(int p)
        {
            if (p < 0)
            {
                throw new NotPositiveException <Int32>(new LocalizedFormats("NOT_POSITIVE_EXPONENT"), p);
            }

            if (!isSquare())
            {
                throw new NonSquareMatrixException(getRowDimension(), getColumnDimension());
            }

            if (p == 0)
            {
                return(MatrixUtils.createRealIdentityMatrix(this.getRowDimension()));
            }

            if (p == 1)
            {
                return(this.copy());
            }

            int power = p - 1;

            /*
             * Only log_2(p) operations is used by doing as follows:
             * 5^214 = 5^128 * 5^64 * 5^16 * 5^4 * 5^2
             *
             * In general, the same approach is used for A^p.
             */

            char[]       binaryRepresentation = Convert.ToString(power, 2).ToCharArray();
            List <Int32> nonZeroPositions     = new List <Int32>();
            int          maxI = -1;

            for (int i = 0; i < binaryRepresentation.Length; ++i)
            {
                if (binaryRepresentation[i] == '1')
                {
                    int pos = binaryRepresentation.Length - i - 1;
                    nonZeroPositions.Add(pos);

                    // The positions are taken in turn, so maxI is only changed once
                    if (maxI == -1)
                    {
                        maxI = pos;
                    }
                }
            }

            RealMatrix[] results = new RealMatrix[maxI + 1];
            results[0] = this.copy();

            for (int i = 1; i <= maxI; ++i)
            {
                results[i] = results[i - 1].multiply(results[i - 1]);
            }

            RealMatrix result = this.copy();

            foreach (Int32 i in nonZeroPositions)
            {
                result = result.multiply(results[i]);
            }

            return(result);
        }