Exemplo n.º 1
0
        // Перегружаем бинарный оператор +
        public static MatrixT <T> operator +(MatrixT <T> A, MatrixT <T> B)
        {
            if (Paral)
            {
                MatrixT <T> ans = new MatrixT <T>(new T[A.Elements.GetLength(0), A.Elements.GetLength(1)]);
                Parallel.For(0, A.Elements.GetLength(0), (i) =>
                {
                    for (int j = 0; j < A.Elements.GetLength(1); j++)
                    {
                        ans.Elements[i, j] = (dynamic)(A.Elements[i, j]) + (dynamic)(B.Elements[i, j]);
                    }
                });

                return(ans);
            }
            else
            {
                MatrixT <T> ans = new MatrixT <T>(new T[A.Elements.GetLength(0), A.Elements.GetLength(1)]);
                for (int i = 0; i < A.Elements.GetLength(0); i++)
                {
                    for (int j = 0; j < A.Elements.GetLength(1); j++)
                    {
                        ans.Elements[i, j] = (dynamic)(A.Elements[i, j]) + (dynamic)(B.Elements[i, j]);
                    }
                }

                return(ans);
            }
        }
Exemplo n.º 2
0
        // Перегружаем бинарный оператор *
        public static MatrixT <T> operator *(MatrixT <T> A, MatrixT <T> B)
        {
            if (Paral)
            {
                MatrixT <T> ans = new MatrixT <T>(new T[A.Elements.GetLength(0), B.Elements.GetLength(1)]);
                Parallel.For(0, A.Elements.GetLength(0), (i) =>
                {
                    for (int j = 0; j < B.Elements.GetLength(1); j++)
                    {
                        ans.Elements[i, j] = (dynamic)0;
                        for (int k = 0; k < A.Elements.GetLength(1); k++)
                        {
                            ans.Elements[i, j] += (dynamic)A.Elements[i, k] * (dynamic)B.Elements[k, j];
                        }
                    }
                });

                return(ans);
            }
            else
            {
                MatrixT <T> ans = new MatrixT <T>(new T[A.Elements.GetLength(0), B.Elements.GetLength(1)]);
                for (int i = 0; i < A.Elements.GetLength(0); i++)
                {
                    for (int j = 0; j < B.Elements.GetLength(1); j++)
                    {
                        ans.Elements[i, j] = (dynamic)0;
                        for (int k = 0; k < A.Elements.GetLength(1); k++)
                        {
                            ans.Elements[i, j] += (dynamic)A.Elements[i, k] * (dynamic)B.Elements[k, j];
                        }
                    }
                }

                return(ans);
            }
        }