Описать класс, реализующий тип данных «вещественная матрица» и работу с ними - C#
Формулировка задачи:
Ребята помогите пожалуйста)
Описать класс, реализующий тип данных «вещественная матрица» и работу с ними. Класс должен реализовывать следующие операции над матрицами:
• умножение, деление (как на другую матрицу, так и на число);
• комбинированные операции присваивания (*=, /=);
• операцию возведения в степень;
• методы вычисления детерминанта и нормы;
• доступ к элементу по индексам.
Написать программу, демонстрирующую все разработанные элементы класса
Решение задачи: «Описать класс, реализующий тип данных «вещественная матрица» и работу с ними»
textual
Листинг программы
using System;
namespace problem_3
{
class SquareMatrix
{
double[,] a;
//constructors
public SquareMatrix(double[,] _a)
{
a = new double[_a.GetLength(0), _a.GetLength(0)];
a = _a;
}
//destructor
~SquareMatrix()
{
a = null;
}
//since we have a square matrix
public int getSize()
{
return a.GetLength(0);
}
public static Boolean isSquare()
{
return a.GetLength(0) == a.GetLength(1);
}
//prints a matrix
public void printMatrix()
{
for (int i = 0; i < getSize(); i++)
{
for (int j = 0; j < getSize(); j++)
{
Console.Write(a[i, j] + " ");
}
Console.WriteLine();
}
}
//this one deletes specific row and column out of a matrix
public SquareMatrix excludeRowAndColumn(int rowToRemove, int columnToRemove)
{
double[,] t = new double[getSize() - 1, getSize() - 1];
for (int i = 0, j = 0; i < getSize(); i++)
{
if (i == rowToRemove) continue;
for (int k = 0, u = 0; k < getSize(); k++)
{
if (k == columnToRemove) continue;
t[j, u] = a[i, k];
u++;
}
j++;
}
return new SquareMatrix(t);
}
//returns a matrix of cofactors
public SquareMatrix getCofactorsMatrix()
{
double[,] t = new double[getSize(), getSize()];
for (int i = 0; i < getSize(); i++)
{
for (int j = 0; j < getSize(); j++)
{
t[i, j] = excludeRowAndColumn(i, j).getDetermiant() * Math.Pow(-1, i + j);
}
}
return new SquareMatrix(t);
}
//just transposes a matrix
public SquareMatrix getTransposeMatrix()
{
double[,] t = new double[getSize(), getSize()];
for (int i = 0; i < getSize(); i++)
{
for (int j = 0; j < getSize(); j++)
{
t[j, i] = a[i, j];
}
}
return new SquareMatrix(t);
}
//returns a determiant of a matrix
//uses LU - decomposition algorithm
public double getDetermiant()
{
double sum, det = 1;
double[,] L = new double[getSize(), getSize()];
double[,] U = new double[getSize(), getSize()];
for (int i = 0; i < getSize(); i++)
{
for (int j = 0; j < getSize(); j++)
{
U[0, i] = a[0, i];
L[i, 0] = a[i, 0] / U[0, 0];
sum = 0;
for (int k = 0; k < i; k++)
{
sum += L[i, k] * U[k, j];
}
U[i, j] = a[i, j] - sum;
if (i > j)
{
L[j, i] = 0;
}
else
{
sum = 0;
for (int k = 0; k < i; k++)
{
sum += L[j, k] * U[k, i];
}
L[j, i] = (a[j, i] - sum) / U[i, i];
}
}
}
for (int i = 0; i < getSize(); i++)
det *= U[i, i];
return det;
}
//returns an inversed matrix
public SquareMatrix getInverseMatrix()
{
double[,] t = new double[getSize(), getSize()];
for (int i = 0; i < getSize(); i++)
{
for (int j = 0; j < getSize(); j++)
{
t[i, j] = (1 / getDetermiant()) * getCofactorsMatrix().getTransposeMatrix().a[i, j];
}
}
return new SquareMatrix(t);
}
//operator overloading (+,-,*,/)
public static SquareMatrix operator +(SquareMatrix a, SquareMatrix b)
{
if (isSquare())
{
double[,] c = new double[a.getSize(), a.getSize()];
for (int i = 0; i < a.getSize(); i++)
for (int j = 0; j < a.getSize(); j++)
c[i, j] = a.a[i, j] + b.a[i, j];
return new SquareMatrix(c);
}
else
return a;
}
public static SquareMatrix operator -(SquareMatrix a, SquareMatrix b)
{
if (isSquare())
{
double[,] c = new double[a.getSize(), a.getSize()];
for (int i = 0; i < a.getSize(); i++)
for (int j = 0; j < a.getSize(); j++)
c[i, j] = a.a[i, j] - b.a[i, j];
return new SquareMatrix(c);
}
else
return a;
}
public static SquareMatrix operator *(SquareMatrix a, SquareMatrix b)
{
if (isSquare())
{
double[,] c = new double[a.getSize(), a.getSize()];
for (int i = 0; i < a.getSize(); i++)
{
for (int j = 0; j < a.getSize(); j++)
{
double sum = 0;
for (int k = 0; k < a.getSize(); k++)
sum += a.a[i, k] * b.a[k, j];
c[i, j] = sum;
}
}
return new SquareMatrix(c);
}
else
return a;
}
//this one can be deleted
public static SquareMatrix operator *(double t, SquareMatrix a)
{
double[,] b = new double[a.getSize(), a.getSize()];
for (int i = 0; i < a.getSize(); i++)
for (int j = 0; j < a.getSize(); j++)
b[i, j] = a.a[i, j] * t;
return new SquareMatrix(b);
}
public static SquareMatrix operator /(SquareMatrix a, SquareMatrix b)
{
if (isSquare())
return a * b.getInverseMatrix();
else
return a;
}
}
class MainClass
{
public static void Main(string[] args)
{
SquareMatrix a = new SquareMatrix(new double[,] { { -1, 2, 3 }, { 2, 4, 8 }, { 2, 6, 1 } });
SquareMatrix b = new SquareMatrix(new double[,] { { 1, -2, 2 }, { 2, 4, 8 }, { 2, 6, 1 } });
SquareMatrix c;
c = a / b;
c.printMatrix();
//Console.WriteLine(a.excludeMatrix(1, 1, a).getDetermiant());
Console.ReadKey();
}
}
}