Ejemplo n.º 1
0
        public static AbstrSquareMatrix operator *(AbstrSquareMatrix m1, AbstrSquareMatrix m2)
        {
            if (m1.N != m2.N)
            {
                throw new SizeException(m1.N, m2.N);
            }

            AbstrSquareMatrix matrixC = m1.Create(m1.N);

            for (var i = 0; i < m1.N; i++)
            {
                for (var j = 0; j < m2.N; j++)
                {
                    matrixC.Get(i, j) = 0;

                    for (var k = 0; k < m1.N; k++)
                    {
                        matrixC.Get(i, j) += m1.Get(i, k) * m2.Get(k, j);
                    }
                }
            }

            return(matrixC);
        }
Ejemplo n.º 2
0
        public AbstrSquareMatrix InverseMatrix()
        {
            double[,] res = new double[N, N * 2];
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    res[i, j] = Get(i, j);
                }
                res[i, i + N] = 1;
            }
            double[,] prev = new double[res.GetLength(0), res.GetLength(1)];
            for (int i = 0; i < res.GetLength(0); i++)
            {
                Array.Copy(res, prev, res.Length);
                for (int j = 0; j < res.GetLength(0); j++)
                {
                    for (int k = 0; k < res.GetLength(1); k++)
                    {
                        if (j == i)
                        {
                            res[j, k] /= prev[i, i];
                        }
                        else
                        {
                            res[j, k] -= (prev[j, i] * prev[i, k]) / prev[i, i];
                        }
                    }
                }
            }
            AbstrSquareMatrix inversed = Create(N);

            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1) / 2; j++)
                {
                    inversed.Get(i, j) = res[i, j + res.GetLength(1) / 2];
                }
            }
            return(inversed);
        }