public static void print(MatrixClass a) { for (int i = 0; i < a.Matrix.GetLength(0); i++) { for (int j = 0; j < a.Matrix.GetLength(1); j++) { Console.Write("{0} ", a[i, j]); } Console.WriteLine(); } }
/// <summary> /// Конструктор /// </summary> /// <param name="value">Объект типа MatrixClass</param> public MatrixClass(MatrixClass value) { // if (value == null) { throw new ArgumentException("null value"); } matrix = new double[value.Matrix.GetLength(0), value.Matrix.GetLength(1)]; for (int i = 0; i < value.Matrix.GetLength(0); i++) { for (int j = 0; j < value.Matrix.GetLength(1); j++) { this.matrix[i, j] = value[i,j]; } } }
public void Test2() { MatrixClass a = new MatrixClass(2, 2, 1, 1); }
public void Test1() { MatrixClass a = new MatrixClass(0,0,1,1,1,2,3); }
public void OperatorTest1() { MatrixClass a = new MatrixClass(2, 3, 1, 1, 1, 2, 2, 5); MatrixClass b = new MatrixClass(4, 2, 1, 1, 1, 2, 9, 8, 7, 6); MatrixClass c = a * b; }
public void InverseMatrixTest() { MatrixClass a = new MatrixClass(2, 3, 1, 1, 1, 2, 2, 5); MatrixClass c = MatrixClass.InverseMatrix(a); }
public void CompareToTest2() { MatrixClass a = new MatrixClass(2, 2, 1, 1, 1, 2); MatrixClass b = new MatrixClass(2, 2, 1, 1, 1, 2); Assert.AreEqual(0, a.CompareTo(b)); }
public void CompareToTest1() { MatrixClass a = new MatrixClass(2, 2, 1, 1, 1, 2); a.CompareTo("Hello world"); }
static void Main(string[] args) { MatrixClass prim = new MatrixClass(3,3,3,4,5,6,7,8,1,1,3); MatrixClass prim2 = new MatrixClass(3, 3, 3, 4, 5, 6, 7, 8, 1, 1, 3); MatrixClass prim3; MatrixClass prim4; MatrixClass prim5; Console.WriteLine("Первая матрица:"); print(prim); Console.WriteLine("Вторая матрица:"); print(prim2); Console.WriteLine("Результат умножения матриц:"); try { prim3 = prim * prim2; print(prim3); } catch (MatrixException ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Результат сложения матриц:"); try { prim4 = prim + prim2; print(prim4); } catch (MatrixException ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Результат вычитания матриц:"); try { prim5 = prim - prim2; print(prim5); } catch (MatrixException ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Обратная матрица матрицы 1:"); try { print(MatrixClass.InverseMatrix(prim)); } catch (MatrixException ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Обратная матрица матрицы 2:"); try { print(MatrixClass.InverseMatrix(prim2)); } catch (MatrixException ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); }
/// <summary> /// Метод для нахождения обратной матрицы /// </summary> /// <param name="obj">Матрица</param> /// <returns></returns> public static MatrixClass InverseMatrix(MatrixClass obj) { // if (obj == null) { throw new ArgumentException("obj has a null value"); } if (obj.Matrix.GetLength(0) != obj.Matrix.GetLength(1)) { throw new MatrixException("Matrix must be square"); } double determ = Determinant(obj.Matrix); if (determ == 0) { throw new MatrixException("Matrix dont have inverse matrix"); } double[,] answer = new double[obj.Matrix.GetLength(0), obj.Matrix.GetLength(0)]; for (int j = 0; j < obj.Matrix.GetLength(0); j++) { answer[j, j] = 1; } for (int i = 0; i < obj.Matrix.GetLength(0); i++) { for (int t = 0; t < obj.Matrix.GetLength(0); t++) { answer[t, i] = Math.Pow(-1, i + t)* Determinant(GetMinor(obj.Matrix, i, t)); } } for (int i = 0; i < obj.Matrix.GetLength(0); i++) { for (int t = 0; t < obj.Matrix.GetLength(0); t++) { answer[t, i] = (1 / determ) * answer[t,i]; } } return new MatrixClass(answer); }
/// <summary> /// Операция умножения /// </summary> /// <param name="first">Первый объект типа MatrixClass</param> /// <param name="second">Второй объект типа MatrixClass</param> /// <returns>Объект типа MatrixClass который является произведением двух матриц</returns> public static MatrixClass operator *(MatrixClass first, MatrixClass second) { if ((object)first == null || (object)second == null) { throw new ArgumentException("Parameters cannot be null"); } if (first.Matrix.GetLength(1) != second.Matrix.GetLength(0)) { throw new MatrixException("Number of columns in the first factor must be equal to the number of rows in the second"); } if(first.Matrix.GetLength(0)<second.Matrix.GetLength(0)) { MatrixClass temp = new MatrixClass(first); first = second; second = temp; } double[] resultMatrix = new double[first.Matrix.GetLength(0) * second.Matrix.GetLength(1)]; int k = 0; for (int y = 0; y < first.matrix.GetLength(0); y++) { for (int i = 0; i < second.Matrix.GetLength(1); i++) { for (int j = 0; j < second.Matrix.GetLength(0); j++) { resultMatrix[k] += first.Matrix[y, j] * second.Matrix[j, i]; } k++; } } return new MatrixClass(first.Matrix.GetLength(0),second.Matrix.GetLength(1),resultMatrix); }