Example #1
0
        public static MatrixInt Identity(int size)
        {
            MatrixInt m = new MatrixInt(size, size);

            for (int r = 0; r < size; r++)
            {
                for (int c = 0; c < size; c++)
                {
                    m[r, c] = (r == c ? 1 : 0);
                }
            }

            return(m);
        }
Example #2
0
        public static MatrixInt Power(MatrixInt m, int e)
        {
            Debug.Assert(m.rows == m.cols, "Cannot raise non-square matrix to a power");

            MatrixInt result = Identity(m.rows);

            while (e > 0)
            {
                if ((e & 1) != 0)
                {
                    result *= m;
                    e      -= 1;
                }
                m *= m;
                e /= 2;
            }
            return(result);
        }
Example #3
0
        public static MatrixInt operator*(MatrixInt a, MatrixInt b)
        {
            Debug.Assert(a.cols == b.rows, "Invalid arguments, unable to multiply mismatched matrix sizes.");

            MatrixInt c = new MatrixInt(a.rows, b.cols);

            for (int row = 0; row < c.rows; row++)
            {
                for (int col = 0; col < c.cols; col++)
                {
                    for (int i = 0; i < a.cols; i++)
                    {
                        c[row, col] += a[row, i] * b[i, col];
                    }
                }
            }

            return(c);
        }