public double GetDeterminant() { int columnsCount = GetColumnsCount(); int rowsCount = GetRowsCount(); if (columnsCount != rowsCount) { throw new InvalidOperationException("Матрица должна быть квадратная."); } if (columnsCount == 1) { return(rows[0].GetComponent(0)); } if (columnsCount == 2) { return(rows[0].GetComponent(0) * rows[1].GetComponent(1) - rows[0].GetComponent(1) * rows[1].GetComponent(0)); } double determinant = 0; for (int i = 0; i < rowsCount; i++) { Matrix newMatrix = new Matrix(columnsCount - 1, rowsCount - 1); for (int j = 1; j < columnsCount; j++) { for (int k = 0, newIndexY = 0; k < rowsCount; k++, newIndexY++) { if (k == i) { newIndexY--; continue; } newMatrix.rows[j - 1].SetComponent(newIndexY, rows[j].GetComponent(k)); } } if (i % 2 == 0) { determinant += rows[0].GetComponent(i) * newMatrix.GetDeterminant(); } else { determinant += -rows[0].GetComponent(i) * newMatrix.GetDeterminant(); } } return(determinant); }
static void Main(string[] args) { Matrix matrix1 = new Matrix(5, 7); Matrix matrix2 = new Matrix(matrix1); Matrix matrix3 = new Matrix(new double[, ] { { 5, 6 }, { 3, 2 }, { 5, 6 } }); Matrix matrix4 = new Matrix(new Vector[] { new Vector(new double[] { 5.6, 6.4 }), new Vector(new double[] { 3.7, 6.5, 7.8 }) }); Matrix matrix5 = new Matrix(matrix4); Console.WriteLine("Размер матрицы {0} = {1} на {2}", matrix1, matrix1.GetColumnsCount(), matrix1.GetRowsCount()); Console.WriteLine(); Console.WriteLine("Размер матрицы {0} = {1} на {2}", matrix2, matrix2.GetColumnsCount(), matrix2.GetRowsCount()); Console.WriteLine(); Console.WriteLine("Размер матрицы {0} = {1} на {2}", matrix3, matrix3.GetColumnsCount(), matrix3.GetRowsCount()); Console.WriteLine(); Console.WriteLine("Размер матрицы {0} = {1} на {2}", matrix4, matrix4.GetColumnsCount(), matrix4.GetRowsCount()); Console.WriteLine(); Console.WriteLine(); int index = 1; Console.WriteLine("Горизонтальный вектор номер {0} = {1}", index, matrix3.GetRow(index)); Console.WriteLine(); matrix3.SetRow(1, new Vector(new double[] { 2, 3 })); Console.WriteLine("Теперь горизонтальный вектор номер {0} = {1}", index, matrix3.GetRow(index)); Console.WriteLine(); Console.WriteLine("Вертикальный вектор номер {0} = {1}", index, matrix3.GetColumn(index)); Console.WriteLine(); Console.WriteLine(); Console.Write("Результат транспонирования матрицы {0} = ", matrix4); matrix4.Transpose(); Console.WriteLine(matrix4); Console.WriteLine(); Console.Write("Результат умножения матрицы {0} на 10 = ", matrix3); matrix3.MultiplyByScalar(10); Console.WriteLine(matrix3); Console.WriteLine(); Matrix matrix6 = new Matrix(new double[, ] { { 5, 6 }, { 3, 2 } }); Console.WriteLine("Определитель матрицы {0} = {1}", matrix6, matrix6.GetDeterminant()); Console.WriteLine(); Vector vector = new Vector(new double[] { -1, -10 }); Console.Write("Результат умножения матрицы {0} на вектор {1} = {2}", matrix3, vector, matrix3.MultiplyByVector(vector)); Console.WriteLine(); Console.Write("Результат сложения матриц {0} и {1} = ", matrix3, matrix4); matrix3.AddMatrix(matrix4); Console.WriteLine(matrix3); Console.WriteLine(); Console.Write("Разность матриц {0} и {1} = ", matrix3, matrix4); matrix3.SubtractMatrix(matrix4); Console.WriteLine(matrix3); Console.WriteLine(); Console.WriteLine("Результат сложения матриц {0} и {1} = {2}", matrix4, matrix3, Matrix.AddMatrices(matrix4, matrix3)); Console.WriteLine(); Console.WriteLine("Разность матриц {0} и {1} = {2}", matrix4, matrix3, Matrix.SubtractMatrices(matrix4, matrix3)); Console.WriteLine(); Console.WriteLine("Результат умножения матриц {0} и {1} = {2}", matrix6, matrix5, Matrix.MultiplyMatrices(matrix6, matrix5)); Console.ReadKey(); }
static void Main(string[] args) { Matrix a = new Matrix(3, 5); Console.WriteLine("a"); Console.WriteLine(a); Matrix b = new Matrix(a); Console.WriteLine("b"); Console.WriteLine(b); double[,] z = new double[, ] { { 1, 2, 3, 4, 5, 6, 7 }, { 0, 0, 1, 0, 0, 0, 0 }, { 1, 1, 0, 2, 3, 9, 9 }, { 1, 2, 3, 8, 9, 7, 6 }, { 2, 3, 4, 5, 6, 7, 1 } }; Matrix c = new Matrix(z); Console.WriteLine("c"); Console.WriteLine(c); double[] p = { 1, 2, 3, 4, 5, 6, 7 }; Vector v1 = new Vector(4, p); double[] o = { 0, 1, 0, 0, 0, 0 }; Vector v2 = new Vector(4, o); double[] u = { 1, 1, 0, 2, 3, 9, 9 }; Vector v3 = new Vector(3, u); double[] y = { 1, 2, 3, 8, 9, 7, 6 }; Vector v4 = new Vector(y); double[] t = { 2, 3, 4, 5, 6, 7, 1 }; Vector v5 = new Vector(t); Vector[] x = new Vector[6] { v1, v2, v3, v4, v3, v3 }; Matrix d = new Matrix(x); Console.WriteLine("d"); Console.WriteLine(d); Console.WriteLine("d.GetColumnsNumber()"); Console.WriteLine(d.GetColumnsNumber()); Console.WriteLine("d.GetRowsNumber()"); Console.WriteLine(d.GetRowsNumber()); Console.WriteLine("GetVectorByIndex 5"); Console.WriteLine(d.GetRowByIndex(5)); d.SetRowByIndex(5, v5); Console.WriteLine(d); Console.WriteLine("GetColumnByIndex 6"); Console.WriteLine(d.GetColumnByIndex(6)); Console.WriteLine("оригинальная матрица"); Console.WriteLine(d); d.Transpose(); Console.WriteLine("перевернутая матрица"); Console.WriteLine(d); Console.WriteLine("Умножение на скаляр 2"); d.MultiplicationOnScalar(2); Console.WriteLine(d); double[,] f = new double[, ] { { 2, -1, 3, 2, 0 }, { 3, 1, 7, 0, 1 }, { -4, -1, 2, 1, 0 }, { -6, 7, 1, -1, 1 }, { -3, -1, 2, 8, 0 } }; Matrix g = new Matrix(f); Console.WriteLine("Определитель матрицы"); Console.WriteLine(g); Console.WriteLine(" = {0}", g.GetDeterminant()); double[] l = new double[] { 2, 3, -1, 4, 9 }; Vector r = new Vector(l); Console.WriteLine("MultiplicationOnVector"); Console.WriteLine("матрица"); Console.WriteLine(g); Console.WriteLine("вектор"); Console.WriteLine(r); Console.WriteLine("Результат"); Console.WriteLine(g.MultiplicationOnVector(r)); double[,] mu1 = new double[, ] { { 1, 2, 2 }, { 3, 1, 1 } }; double[,] mu2 = new double[, ] { { 4, 2 }, { 3, 1 }, { 1, 5 } }; Matrix mul1 = new Matrix(mu1); Matrix mul2 = new Matrix(mu2); Console.WriteLine("Умножение матриц"); Console.WriteLine("матрица 1 {0}", mul1); Console.WriteLine("матрица 2 {0}", mul2); Console.WriteLine(Matrix.Multiplication(mul1, mul2)); }