예제 #1
0
        /// <summary>Returns a sub matrix extracted from the current matrix.</summary>
        /// <param name="r">Array of row indices</param>
        /// <param name="j0">Start column index</param>
        /// <param name="j1">End column index</param>
        public MatrixV Submatrix(int[] r, int j0, int j1)
        {
            if ((j0 > j1) || (j0 < 0) || (j0 >= columns) || (j1 < 0) || (j1 >= columns))
            {
                throw new ArgumentException("Argument out of range.");
            }

            MatrixV X = new MatrixV(r.Length, j1 - j0 + 1);

            double[][] x = X.Array;
            for (int i = 0; i < r.Length; i++)
            {
                for (int j = j0; j <= j1; j++)
                {
                    if ((r[i] < 0) || (r[i] >= this.rows))
                    {
                        throw new ArgumentException("Argument out of range.");
                    }

                    x[i][j - j0] = data[r[i]][j];
                }
            }

            return(X);
        }
예제 #2
0
        /// <summary>Determines weather two instances are equal.</summary>
        public static bool Equals(MatrixV left, MatrixV right)
        {
            if (((object)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);
        }
예제 #3
0
        /// <summary>Unary minus.</summary>
        public static MatrixV Negate(MatrixV value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

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

            double[][] data = value.Array;

            MatrixV X = new MatrixV(rows, columns);

            double[][] x = X.Array;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    x[i][j] = -data[i][j];
                }
            }

            return(X);
        }
예제 #4
0
        /// <summary>Matrix subtraction.</summary>
        public static MatrixV Subtract(MatrixV left, MatrixV right)
        {
            if (left == null)
            {
                throw new ArgumentNullException("left");
            }

            if (right == null)
            {
                throw new ArgumentNullException("right");
            }

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

            double[][] data = left.Array;

            if ((rows != right.Rows) || (columns != right.Columns))
            {
                throw new ArgumentException("Matrix dimension do not match.");
            }

            MatrixV X = new MatrixV(rows, columns);

            double[][] x = X.Array;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    x[i][j] = data[i][j] - right[i, j];
                }
            }
            return(X);
        }
예제 #5
0
        /// <summary>Construct a QR decomposition.</summary>
        public QrDecomposition(MatrixV value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

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

            this.Rdiag = new double[n];

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

                if (nrm != 0.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;
            }
        }
예제 #6
0
        /// <summary>Matrix-scalar multiplication.</summary>
        public static MatrixV Multiply(MatrixV left, double right)
        {
            if (left == null)
            {
                throw new ArgumentNullException("left");
            }

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

            double[][] data = left.Array;

            MatrixV X = new MatrixV(rows, columns);

            double[][] x = X.Array;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    x[i][j] = data[i][j] * right;
                }
            }

            return(X);
        }
예제 #7
0
        /// <summary>Returns a sub matrix extracted from the current matrix.</summary>
        /// <param name="i0">Starttial row index</param>
        /// <param name="i1">End row index</param>
        /// <param name="c">Array of row indices</param>
        public MatrixV Submatrix(int i0, int i1, int[] c)
        {
            if ((i0 > i1) || (i0 < 0) || (i0 >= this.rows) || (i1 < 0) || (i1 >= this.rows))
            {
                throw new ArgumentException("Argument out of range.");
            }

            MatrixV X = new MatrixV(i1 - i0 + 1, c.Length);

            double[][] x = X.Array;
            for (int i = i0; i <= i1; i++)
            {
                for (int j = 0; j < c.Length; j++)
                {
                    if ((c[j] < 0) || (c[j] >= columns))
                    {
                        throw new ArgumentException("Argument out of range.");
                    }

                    x[i - i0][j] = data[i][c[j]];
                }
            }

            return(X);
        }
예제 #8
0
        public static Matrix4d Invert(Matrix4d source)
        {
            //Matrix4d test = ConvertDX.ToMatrix4d(Microsoft.DirectX.Matrix.Invert(ConvertDX.FromMatrix4d(source)));
            MatrixV  rightHandSide = MatrixV.Diagonal(4, 4, 1.0);
            Matrix4d solution      = new Matrix4d(new LuDecomposition(source.m_MapackMat).Solve(rightHandSide));

            return(solution);
        }
예제 #9
0
 private Matrix4d(MatrixV mapackMat)
 {
     if (mapackMat.Rows != 4 || mapackMat.Columns != 4)
     {
         throw new ApplicationException("Only 4x4 matrices supported in Matrix4d constructor.");
     }
     m_MapackMat = mapackMat;
 }
예제 #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 * U * X = B</c>.</returns>
        public MatrixV Solve(MatrixV 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;
            MatrixV X     = value.Submatrix(pivotVector, 0, count - 1);

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

            double[][] 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);
        }
예제 #11
0
        /// <summary>Returns a diagonal matrix of the given size.</summary>
        public static MatrixV Diagonal(int rows, int columns, double value)
        {
            MatrixV X = new MatrixV(rows, columns);

            double[][] x = X.Array;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    x[i][j] = ((i == j) ? value : 0.0);
                }
            }
            return(X);
        }
예제 #12
0
 public Matrix4d(
     double _M11, double _M12, double _M13, double _M14,
     double _M21, double _M22, double _M23, double _M24,
     double _M31, double _M32, double _M33, double _M34,
     double _M41, double _M42, double _M43, double _M44
     )
 {
     m_MapackMat = new MatrixV(new double[][] {
         new double[] { _M11, _M12, _M13, _M14 },
         new double[] { _M21, _M22, _M23, _M24 },
         new double[] { _M31, _M32, _M33, _M34 },
         new double[] { _M41, _M42, _M43, _M44 }
     });
 }
예제 #13
0
        /// <summary>Returns a matrix filled with random values.</summary>
        public static MatrixV Random(int rows, int columns)
        {
            MatrixV X = new MatrixV(rows, columns);

            double[][] x = X.Array;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    x[i][j] = random.NextDouble();
                }
            }
            return(X);
        }
예제 #14
0
        /// <summary>Matrix-matrix multiplication.</summary>
        public static MatrixV Multiply(MatrixV left, MatrixV right)
        {
            if (left == null)
            {
                throw new ArgumentNullException("left");
            }

            if (right == null)
            {
                throw new ArgumentNullException("right");
            }

            int rows = left.Rows;

            double[][] data = left.Array;

            if (right.Rows != left.columns)
            {
                throw new ArgumentException("Matrix dimensions are not valid.");
            }

            int     columns = right.Columns;
            MatrixV X       = new MatrixV(rows, columns);

            double[][] x = X.Array;

            int size = left.columns;

            double[] column = new double[size];
            for (int j = 0; j < columns; j++)
            {
                for (int k = 0; k < size; k++)
                {
                    column[k] = right[k, j];
                }
                for (int i = 0; i < rows; i++)
                {
                    double[] row = data[i];
                    double   s   = 0;
                    for (int k = 0; k < size; k++)
                    {
                        s += row[k] * column[k];
                    }
                    x[i][j] = s;
                }
            }

            return(X);
        }
예제 #15
0
        /// <summary>Creates a copy of the matrix.</summary>
        public MatrixV Clone()
        {
            MatrixV X = new MatrixV(rows, columns);

            double[][] x = X.Array;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    x[i][j] = data[i][j];
                }
            }

            return(X);
        }
예제 #16
0
        /// <summary>Returns the transposed matrix.</summary>
        public MatrixV Transpose()
        {
            MatrixV X = new MatrixV(columns, rows);

            double[][] x = X.Array;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    x[j][i] = data[i][j];
                }
            }

            return(X);
        }
예제 #17
0
        /// <summary>Returns a sub matrix extracted from the current matrix.</summary>
        /// <param name="startRow">Start row index</param>
        /// <param name="endRow">End row index</param>
        /// <param name="startColumn">Start column index</param>
        /// <param name="endColumn">End column index</param>
        public MatrixV Submatrix(int startRow, int endRow, int startColumn, int endColumn)
        {
            if ((startRow > endRow) || (startColumn > endColumn) || (startRow < 0) || (startRow >= this.rows) || (endRow < 0) || (endRow >= this.rows) || (startColumn < 0) || (startColumn >= this.columns) || (endColumn < 0) || (endColumn >= this.columns))
            {
                throw new ArgumentException("Argument out of range.");
            }

            MatrixV X = new MatrixV(endRow - startRow + 1, endColumn - startColumn + 1);

            double[][] x = X.Array;
            for (int i = startRow; i <= endRow; i++)
            {
                for (int j = startColumn; j <= endColumn; j++)
                {
                    x[i - startRow][j - startColumn] = data[i][j];
                }
            }

            return(X);
        }
예제 #18
0
        /// <summary>Returns a sub matrix extracted from the current matrix.</summary>
        /// <param name="rowIndexes">Array of row indices</param>
        /// <param name="columnIndexes">Array of column indices</param>
        public MatrixV Submatrix(int[] rowIndexes, int[] columnIndexes)
        {
            MatrixV X = new MatrixV(rowIndexes.Length, columnIndexes.Length);

            double[][] x = X.Array;
            for (int i = 0; i < rowIndexes.Length; i++)
            {
                for (int j = 0; j < columnIndexes.Length; j++)
                {
                    if ((rowIndexes[i] < 0) || (rowIndexes[i] >= rows) || (columnIndexes[j] < 0) || (columnIndexes[j] >= columns))
                    {
                        throw new ArgumentException("Argument out of range.");
                    }

                    x[i][j] = data[rowIndexes[i]][columnIndexes[j]];
                }
            }

            return(X);
        }
예제 #19
0
 /// <summary>Returns the LHS solution vetor if the matrix is square or the least squares solution otherwise.</summary>
 public MatrixV Solve(MatrixV rightHandSide)
 {
     return((rows == columns) ? new LuDecomposition(this).Solve(rightHandSide) : new QrDecomposition(this).Solve(rightHandSide));
 }
예제 #20
0
        /// <summary>Least squares solution of <c>A * X = B</c></summary>
        /// <param name="value">Right-hand-side MatrixV with as many rows as <c>A</c> and any number of columns.</param>
        /// <returns>A MatrixV that minimized the two norm of <c>Q * R * X - B</c>.</returns>
        /// <exception cref="T:System.ArgumentException">MatrixV row dimensions must be the same.</exception>
        /// <exception cref="T:System.InvalidOperationException">MatrixV is rank deficient.</exception>
        public MatrixV Solve(MatrixV value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.Rows != QR.Rows)
            {
                throw new ArgumentException("MatrixV row dimensions must agree.");
            }

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

            // Copy right hand side
            int     count = value.Columns;
            MatrixV X     = value.Clone();
            int     m     = QR.Rows;
            int     n     = QR.Columns;

            double[][] qr = QR.Array;

            // Compute Y = transpose(Q)*B
            for (int k = 0; k < n; k++)
            {
                for (int j = 0; j < count; j++)
                {
                    double s = 0.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];
                    }
                }
            }

            return(X.Submatrix(0, n - 1, 0, count - 1));
        }
예제 #21
0
        /// <summary>Construct a LU decomposition.</summary>
        public LuDecomposition(MatrixV value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            this.LU = (MatrixV)value.Clone();
            double[][] 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;
            double[] LUrowi;
            double[] LUcolj = new double[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);
                    double s    = 0.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 (Math.Abs(LUcolj[i]) > Math.Abs(LUcolj[p]))
                    {
                        p = i;
                    }
                }

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