コード例 #1
0
        public static decimal[,] GetTriangleGaussMatrix(decimal[,] matrix)
        {
            var strCount    = matrix.GetLength(0);
            var columnCount = matrix.GetLength(1);

            if (strCount + 1 != columnCount)
            {
                throw new IncorrectMatrixException();
            }

            var outMatrix = matrix.Clone() as decimal[, ];

            for (var i = 0; i < strCount; i++)
            {
                var maxIndex = MaxAbsStringIndex(outMatrix, i, i, strCount - 1);

                if (outMatrix[i, maxIndex] == 0)
                {
                    throw new IncorrectMatrixException();
                }

                SwapString(outMatrix, i, maxIndex);

                for (var j = i + 1; j < strCount; j++)
                {
                    var koeff = -(outMatrix[j, i] / outMatrix[i, i]);
                    StringSum(outMatrix, i, j, koeff);
                }

                DivideString(outMatrix, i, outMatrix[i, i]);
            }

            return(outMatrix);
        }
コード例 #2
0
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        public object Clone()
        {
            var clone = new QrDecompositionD();

            clone.qr    = (decimal[, ])qr.Clone();
            clone.Rdiag = (decimal[])Rdiag.Clone();
            return(clone);
        }
コード例 #3
0
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        ///
        public object Clone()
        {
            var clone = new CholeskyDecompositionD();

            clone.L                = (decimal[, ])L.Clone();
            clone.D                = (decimal[])D.Clone();
            clone.n                = n;
            clone.robust           = robust;
            clone.positiveDefinite = positiveDefinite;
            clone.symmetric        = symmetric;
            return(clone);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: MarkCsharp0/MO_Lab3
        // Умножает матрицу на число
        public static decimal[,] Multiply(decimal numb, decimal[,] matrix, int size)
        {
            var newMatrix = (decimal[, ])matrix.Clone();

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    newMatrix[i, j] *= numb;
                }
            }
            return(newMatrix);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: MarkCsharp0/MO_Lab3
        // Вычисляет и возвращает транспонированную матрицу
        public static decimal[,] Transpose(decimal[,] matrix, int size)
        {
            var newMatrix = (decimal[, ])matrix.Clone();

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    newMatrix[j, i] = matrix[i, j];
                }
            }
            return(newMatrix);
        }
コード例 #6
0
        private static decimal[,] NormalizeMatrix(decimal[,] matrix, decimal[] average, int item, int user)
        {
            var result = (decimal[, ])matrix.Clone();

            for (var i = 0; i < item; i++)
            {
                for (var j = 0; j < user; j++)
                {
                    var value = result[i, j];
                    if (value != 0)
                    {
                        result[i, j] = value - average[i];
                    }
                }
            }

            return(result);
        }
コード例 #7
0
ファイル: LuDecompositionD.cs プロジェクト: s76/Accord.NET
        /// <summary>
        ///   Constructs a new LU decomposition.
        /// </summary>
        /// <param name="value">The matrix A to be decomposed.</param>
        /// <param name="transpose">True if the decomposition should be performed on
        /// the transpose of A rather than A itself, false otherwise. Default is false.</param>
        /// <param name="inPlace">True if the decomposition should be performed over the
        /// <paramref name="value"/> matrix rather than on a copy of it. If true, the
        /// matrix will be destroyed during the decomposition. Default is false.</param>
        ///
        public LuDecompositionD(decimal[,] value, bool transpose, bool inPlace)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value", "Matrix cannot be null.");
            }

            if (transpose)
            {
                this.lu = value.Transpose(inPlace);
            }
            else
            {
                this.lu = inPlace ? value : (decimal[, ])value.Clone();
            }

            this.rows      = lu.GetLength(0);
            this.cols      = lu.GetLength(1);
            this.pivotSign = 1;

            this.pivotVector = new int[rows];
            for (int i = 0; i < rows; i++)
            {
                pivotVector[i] = i;
            }

            var LUcolj = new decimal[rows];


            unsafe
            {
                fixed(decimal *LU = lu)
                {
                    // Outer loop.
                    for (int j = 0; j < cols; j++)
                    {
                        // Make a copy of the j-th column to localize references.
                        for (int i = 0; i < rows; i++)
                        {
                            LUcolj[i] = lu[i, j];
                        }

                        // Apply previous transformations.
                        for (int i = 0; i < rows; i++)
                        {
                            decimal s = 0;

                            // Most of the time is spent in
                            // the following dot product:
                            int      kmax   = Math.Min(i, j);
                            decimal *LUrowi = &LU[i * cols];
                            for (int k = 0; k < kmax; k++)
                            {
                                s += LUrowi[k] * LUcolj[k];
                            }

                            LUrowi[j] = LUcolj[i] -= s;
                        }

                        // Find pivot and exchange if necessary.
                        int p = j;
                        for (int i = j + 1; i < rows; i++)
                        {
                            if (Math.Abs(LUcolj[i]) > Math.Abs(LUcolj[p]))
                            {
                                p = i;
                            }
                        }

                        if (p != j)
                        {
                            for (int k = 0; k < cols; k++)
                            {
                                var t = lu[p, k];
                                lu[p, k] = lu[j, k];
                                lu[j, k] = t;
                            }

                            int v = pivotVector[p];
                            pivotVector[p] = pivotVector[j];
                            pivotVector[j] = v;

                            pivotSign = -pivotSign;
                        }

                        // Compute multipliers.
                        if (j < rows && lu[j, j] != 0)
                        {
                            for (int i = j + 1; i < rows; i++)
                            {
                                lu[i, j] /= lu[j, j];
                            }
                        }
                    }
                }
            }
        }
コード例 #8
0
        /// <summary>Constructs a QR decomposition.</summary>
        /// <param name="value">The matrix A to be decomposed.</param>
        /// <param name="transpose">True if the decomposition should be performed on
        /// the transpose of A rather than A itself, false otherwise. Default is false.</param>
        public QrDecompositionD(decimal[,] value, bool transpose)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value", "Matrix cannot be null.");
            }

            if ((!transpose && value.GetLength(0) < value.GetLength(1)) ||
                (transpose && value.GetLength(1) < value.GetLength(0)))
            {
                throw new ArgumentException("Matrix has more columns than rows.", "value");
            }

            this.qr = transpose ? value.Transpose() : (decimal[, ])value.Clone();

            int rows = qr.GetLength(0);
            int cols = qr.GetLength(1);

            this.Rdiag = new decimal[cols];

            for (int k = 0; k < cols; k++)
            {
                // Compute 2-norm of k-th column without under/overflow.
                decimal nrm = 0;
                for (int i = k; i < rows; i++)
                {
                    nrm = Tools.Hypotenuse(nrm, qr[i, k]);
                }

                if (nrm != 0)
                {
                    // Form k-th Householder vector.
                    if (qr[k, k] < 0)
                    {
                        nrm = -nrm;
                    }

                    for (int i = k; i < rows; i++)
                    {
                        qr[i, k] /= nrm;
                    }

                    qr[k, k] += 1;

                    // Apply transformation to remaining columns.
                    for (int j = k + 1; j < cols; j++)
                    {
                        decimal s = 0;

                        for (int i = k; i < rows; i++)
                        {
                            s += qr[i, k] * qr[i, j];
                        }

                        s = -s / qr[k, k];

                        for (int i = k; i < rows; i++)
                        {
                            qr[i, j] += s * qr[i, k];
                        }
                    }
                }

                this.Rdiag[k] = -nrm;
            }
        }
コード例 #9
0
        /// <summary>Least squares solution of <c>A * X = B</c></summary>
        /// <param name="value">Right-hand-side matrix with as many rows as <c>A</c> and any number of columns.</param>
        /// <returns>A matrix that minimized the two norm of <c>Q * R * X - B</c>.</returns>
        /// <exception cref="T:System.ArgumentException">Matrix row dimensions must be the same.</exception>
        /// <exception cref="T:System.InvalidOperationException">Matrix is rank deficient.</exception>
        public decimal[,] Solve(decimal[,] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value", "Matrix cannot be null.");
            }

            if (value.GetLength(0) != qr.GetLength(0))
            {
                throw new ArgumentException("Matrix row dimensions must agree.");
            }

            if (!this.FullRank)
            {
                throw new InvalidOperationException("Matrix is rank deficient.");
            }

            // Copy right hand side
            int count = value.GetLength(1);
            var X     = (decimal[, ])value.Clone();
            int m     = qr.GetLength(0);
            int n     = qr.GetLength(1);

            // Compute Y = transpose(Q)*B
            for (int k = 0; k < n; k++)
            {
                for (int j = 0; j < count; j++)
                {
                    decimal s = 0;

                    for (int i = k; i < m; i++)
                    {
                        s += qr[i, k] * X[i, j];
                    }

                    s = -s / qr[k, k];

                    for (int i = k; i < m; i++)
                    {
                        X[i, j] += s * qr[i, k];
                    }
                }
            }

            // Solve R*X = Y;
            for (int k = n - 1; k >= 0; k--)
            {
                for (int j = 0; j < count; j++)
                {
                    X[k, j] /= Rdiag[k];
                }

                for (int i = 0; i < k; i++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        X[i, j] -= X[k, j] * qr[i, k];
                    }
                }
            }

            var r = new decimal[n, count];

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < count; j++)
                {
                    r[i, j] = X[i, j];
                }
            }

            return(r);
        }
コード例 #10
0
        /// <summary>Solves a set of equation systems of type <c>A * X = B</c>.</summary>
        /// <param name="value">Right hand side matrix with as many rows as <c>A</c> and any number of columns.</param>
        /// <returns>Matrix <c>X</c> so that <c>L * L' * X = B</c>.</returns>
        /// <exception cref="T:System.ArgumentException">Matrix dimensions do not match.</exception>
        /// <exception cref="T:System.NonSymmetricMatrixException">Matrix is not symmetric.</exception>
        /// <exception cref="T:System.NonPositiveDefiniteMatrixException">Matrix is not positive-definite.</exception>
        /// <param name="inPlace">True to compute the solving in place, false otherwise.</param>
        ///
        public decimal[,] Solve(decimal[,] value, bool inPlace)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.GetLength(0) != n)
            {
                throw new ArgumentException("Argument matrix should have the same number of rows as the decomposed matrix.", "value");
            }

            if (!symmetric)
            {
                throw new NonSymmetricMatrixException("Decomposed matrix is not symmetric.");
            }

            if (!robust && !positiveDefinite)
            {
                throw new NonPositiveDefiniteMatrixException("Decomposed matrix is not positive definite.");
            }


            int count = value.GetLength(1);

            decimal[,] B = inPlace ? value : (decimal[, ])value.Clone();


            // Solve L*Y = B;
            for (int k = 0; k < n; k++)
            {
                for (int j = 0; j < count; j++)
                {
                    for (int i = 0; i < k; i++)
                    {
                        B[k, j] -= B[i, j] * L[k, i];
                    }

                    B[k, j] /= L[k, k];
                }
            }

            if (robust)
            {
                for (int k = 0; k < n; k++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        B[k, j] /= D[k];
                    }
                }
            }

            // Solve L'*X = Y;
            for (int k = n - 1; k >= 0; k--)
            {
                for (int j = 0; j < count; j++)
                {
                    for (int i = k + 1; i < n; i++)
                    {
                        B[k, j] -= B[i, j] * L[i, k];
                    }

                    B[k, j] /= L[k, k];
                }
            }

            return(B);
        }