Ejemplo n.º 1
0
        /// <summary>Determines weather two instances are equal.</summary>
        public static bool Equals(MatrixField left, MatrixField right)
        {
            if (left == ((object)right))
            {
                return(true);
            }

            if ((((object)left) == null) || (((object)right) == null))
            {
                return(false);
            }

            if ((left.Rows != right.Rows) || (left.Columns != right.Columns))
            {
                return(false);
            }

            for (int i = 0; i < left.Rows; i++)
            {
                for (int j = 0; j < left.Columns; j++)
                {
                    if (left[i, j] != right[i, j])
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 2
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 * U * X = B</c>.</returns>
        public MatrixField Solve(MatrixField value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.Rows != this.LU.Rows)
            {
                throw new ArgumentException("Invalid matrix dimensions.", "value");
            }

            if (!this.NonSingular)
            {
                throw new InvalidOperationException("Matrix is singular");
            }

            // Copy right hand side with pivoting
            int         count = value.Columns;
            MatrixField X     = value.Submatrix(pivotVector, 0, count - 1);

            int rows    = LU.Rows;
            int columns = LU.Columns;

            Field[][] lu = LU.Array;

            // Solve L*Y = B(piv,:)
            for (int k = 0; k < columns; k++)
            {
                for (int i = k + 1; i < columns; i++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        X[i, j] -= X[k, j] * lu[i][k];
                    }
                }
            }

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

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

            return(X);
        }
Ejemplo n.º 3
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 MatrixField Solve(MatrixField value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.Rows != QR.Rows)
            {
                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.Columns;
            MatrixField X     = value.Clone();
            int         m     = QR.Rows;
            int         n     = QR.Columns;

            Field[][] qr = QR.Array;

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

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

                    s = (s - 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];
                    }
                }
            }

            return(X.Submatrix(0, n - 1, 0, count - 1));
        }
Ejemplo n.º 4
0
        /// <summary>Construct a QR decomposition.</summary>
        public QrDecompositionField(MatrixField value)
        {
            throw new InvalidOperationException("QrDecompositionField is not supported for Galois Fields for now :)");
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            this.QR = value.Clone();
            Field[][] qr = this.QR.Array;
            int       m  = value.Rows;
            int       n  = value.Columns;

            this.Rdiag = new Field[n];

            //for (int k = 0; k < n; k++)
            //{
            //    // Compute 2-norm of k-th column without under/overflow.
            //    Field nrm = new Field(0);
            //    for (int i = k; i < m; i++)
            //    {
            //        nrm = Hypotenuse(nrm, qr[i][k]);
            //    }

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

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

            //        qr[k][k] += 1.0;

            //        // Apply transformation to remaining columns.
            //        for (int j = k + 1; j < n; j++)
            //        {
            //            double s = 0.0;

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

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

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

            //    this.Rdiag[k] = -nrm;
            //}
        }
Ejemplo n.º 5
0
        /// <summary>Construct a LU decomposition.</summary>
        public LuDecompositionField(MatrixField value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            this.LU = value.Clone();
            Field[][] lu      = LU.Array;
            int       rows    = value.Rows;
            int       columns = value.Columns;

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

            pivotSign = 1;
            Field[] LUrowi;
            Field[] LUcolj = new Field[rows];

            // Outer loop.
            for (int j = 0; j < columns; 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++)
                {
                    LUrowi = lu[i];

                    // Most of the time is spent in the following dot product.
                    int   kmax = Math.Min(i, j);
                    Field s    = new Field(0);
                    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 (LUcolj[i].Value > LUcolj[p].Value)
                    {
                        p = i;
                    }
                }

                if (p != j)
                {
                    for (int k = 0; k < columns; k++)
                    {
                        Field 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].Value != 0)
                {
                    for (int i = j + 1; i < rows; i++)
                    {
                        lu[i][j] /= lu[j][j];
                    }
                }
            }
        }