Пример #1
0
        public static Matrix operator -(Matrix A, Matrix B)
        {
            if (A.n != B.n || A.m != B.m)
            {
                var e = new InvalidMatrixOperationException(A, B, $"Matrix must have equal number of columns and raws." +
                                                            $"A({A.n}, {A.m}), B({B.n},{B.m})");
                throw e;
            }
            Matrix C = A.Clone() as Matrix;

            for (int i = 0; i < C.n; i++)
            {
                for (int j = 0; j < C.m; j++)
                {
                    C.Elem[i][j] -= B.Elem[i][j];
                }
            }
            return(C);
        }
Пример #2
0
        public static Matrix operator *(Matrix A, Matrix B)
        {
            if (A.m != B.n)
            {
                var e = new InvalidMatrixOperationException($"{A.m} (columns of A) differs from {B.n} (raws of B) when they must be equal");
                throw e;
            }
            Matrix C    = new Matrix(A.n, B.m, CreationType.Empty);
            double temp = 0;

            for (int i = 0; i < A.n; i++)
            {
                for (int j = 0; j < B.m; j++)
                {
                    for (int k = 0; k < B.n; k++)
                    {
                        temp += A.Elem[i][k] * B.Elem[k][j];
                    }
                    C.Elem[i][j] = temp;
                    temp         = 0;
                }
            }
            return(C);
        }