Exemplo n.º 1
0
        public static Matrix operator *(Matrix left, Matrix right)
        {
            if (left.Cols != right.Rows)
            {
                MatrixTypeCalculateError e = new MatrixTypeCalculateError(3);
                e.ShowException();
                ReadKey();
                Environment.Exit(0);
            }

            Matrix result = new Matrix($"result({left.Name} * {right.Name})", left.Rows, right.Cols);

            double temp = 0;

            for (int i = 0; i < result.Rows; i++)
            {
                for (int j = 0; j < result.Cols; j++)
                {
                    for (int k = 0; k < left.Cols; k++)
                    {
                        temp += left.Elements[i, k] * right.Elements[k, j];
                    }
                    result.Elements[i, j] = temp;
                    temp = 0;
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public static Matrix operator -(Matrix left, Matrix right)
        {
            if ((left.Rows != right.Rows) || (left.Cols != right.Cols))
            {
                MatrixTypeCalculateError e = new MatrixTypeCalculateError(2);
                e.ShowException();
                ReadKey();
                Environment.Exit(0);
            }

            Matrix result = new Matrix();

            result      = left + (-right);
            result.Name = "result(" + left.Name + " - " + right.Name + ")";
            return(result);
        }
Exemplo n.º 3
0
        public static Matrix pow(Matrix a, int x)
        {
            if (a.Cols != a.Rows)
            {
                MatrixTypeCalculateError e = new MatrixTypeCalculateError(5);
                e.ShowException();
                ReadKey();
                Environment.Exit(0);
            }

            Matrix result = new Matrix($"result({a.Name}^{x})", a.Rows, a.Cols);

            for (int i = 0; i < result.Rows; i++)
            {
                for (int j = 0; j < result.Cols; j++)
                {
                    result.Elements[i, j] = 1;
                }
            }
            if (x < 0)
            {
                MatrixPowException e = new MatrixPowException();
                WriteLine(e.Message);
                ReadKey();
                Environment.Exit(0);
            }

            if (x == 0)
            {
                return(result);
            }

            if (x == 1)
            {
                return(a);
            }

            if (x == 2)
            {
                return(a * a);
            }
            else
            {
                return(a * pow(a, x - 1));
            }
        }
Exemplo n.º 4
0
        public static Matrix operator +(Matrix left, Matrix right)
        {
            if ((left.Rows != right.Rows) || (left.Cols != right.Cols))
            {
                MatrixTypeCalculateError e = new MatrixTypeCalculateError(1);
                e.ShowException();
                ReadKey();
                Environment.Exit(0);
            }

            Matrix result = new Matrix($"result({left.Name} + {right.Name})", left.Rows, left.Cols);

            result.Count = left.Count;

            for (int i = 0; i < left.Rows; i++)
            {
                for (int j = 0; j < left.Cols; j++)
                {
                    result.Elements[i, j] = left.Elements[i, j] + right.Elements[i, j];
                }
            }

            return(result);
        }