예제 #1
0
        /// <summary>
        /// Returns a new matrix as submatrix of current matrix.
        /// </summary>
        /// <param name="rows">Number of rows in submatrix.</param>
        /// <param name="columns">Number of columns in submatrix.</param>
        /// <returns></returns>
        public MyMatrix GetSubMatrix(int rows, int columns)
        {
            if (rows > Rows || columns > Columns || rows < 0 || columns < 0)
            {
                throw new ArgumentException();
            }

            MyMatrix result = new MyMatrix(rows, columns);

            for (int i = 0; i < result.Rows; i++)
            {
                for (int j = 0; j < result.Columns; j++)
                {
                    result[i, j] = _matrix[i, j];
                }
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Multiplies the matrix by the number.
        /// </summary>
        /// <param name="m1">Matrix to multiply.</param>
        /// <param name="number">Number to multiply.</param>
        /// <returns>New matrix.</returns>
        public static MyMatrix operator *(MyMatrix m1, int number)
        {
            if (m1 is null)
            {
                throw new ArgumentNullException();
            }

            if (m1.Length == 0)
            {
                throw new ArgumentException();
            }

            MyMatrix result = new MyMatrix(m1.Rows, m1.Columns);

            for (int i = 0; i < result.Rows; i++)
            {
                for (int j = 0; j < result.Columns; j++)
                {
                    result[i, j] = m1[i, j] * number;
                }
            }

            return(result);
        }
예제 #3
0
        /// <summary>
        /// Subtracts two matrixes.
        /// </summary>
        /// <param name="m1">First matrix to subtract.</param>
        /// <param name="m2">Second matrix to subtract.</param>
        /// <returns>New matrix.</returns>
        public static MyMatrix operator -(MyMatrix m1, MyMatrix m2)
        {
            if (m1 is null || m2 is null)
            {
                throw new ArgumentNullException();
            }

            if (m1.Rows != m2.Rows || m1.Columns != m2.Columns || m2.Length == 0 || m1.Length == 0)
            {
                throw new ArgumentException();
            }

            MyMatrix result = new MyMatrix(m1.Rows, m1.Columns);

            for (int i = 0; i < result.Rows; i++)
            {
                for (int j = 0; j < result.Columns; j++)
                {
                    result[i, j] = m1[i, j] - m2[i, j];
                }
            }

            return(result);
        }