Реализовать класс для матриц. В этом классе реализовать интерфейс, содержащий методы для выполнения операций - C#
Формулировка задачи:
Реализовать класс для матриц. В этом классе реализовать интерфейс, содержащий методы для выполнения следующих операций:
- сложение
- умножение
- транспонирование.
Решение задачи: «Реализовать класс для матриц. В этом классе реализовать интерфейс, содержащий методы для выполнения операций»
textual
Листинг программы
class Program
{
/*
* real (row x col) matrix
*/
public class RMatrix
{
private int nRows;
private int nCols;
private double[,] matrix;
public RMatrix(int nRows, int nCols)
{
this.nRows = nRows;
this.nCols = nCols;
this.matrix = new double[nRows, nCols];
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
matrix[i, j] = 0.0;
}
}
}
public RMatrix(double[,] matrix)
{
this.nRows = matrix.GetLength(0);
this.nCols = matrix.GetLength(1);
this.matrix = matrix;
}
public RMatrix(RMatrix m)
{
nRows = m.GetnRows;
nCols = m.GetnCols;
matrix = m.matrix;
}
public RMatrix IdentityMatrix()
{
RMatrix m = new RMatrix(nRows, nCols);
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
if (i == j)
{
m[i, j] = 1;
}
}
}
return m;
}
public double this[int m, int n]
{
get
{
if (m < 0 || m > nRows)
{
throw new Exception("m-th row is out of range!");
}
if (n < 0 || n > nCols)
{
throw new Exception("n-th col is out of range!");
}
return matrix[m, n];
}
set { matrix[m, n] = value; }
}
public int GetnRows
{
get { return nRows; }
}
public int GetnCols
{
get { return nCols; }
}
public override string ToString()
{
string strMatrix = "(";
for (int i = 0; i < nRows; i++)
{
string str = "";
for (int j = 0; j < nCols - 1; j++)
{
str += matrix[i, j].ToString() + ", ";
}
str += matrix[i, nCols - 1].ToString();
if (i != nRows - 1 && i == 0)
strMatrix += str + "\n";
else if (i != nRows - 1 && i != 0)
strMatrix += " " + str + "\n";
else
strMatrix += " " + str + ")";
}
return strMatrix;
}
public static bool operator ==(RMatrix m1, RMatrix m2)
{
return m1.Equals(m2);
}
public static bool operator !=(RMatrix m1, RMatrix m2)
{
return !m1.Equals(m2);
}
public static RMatrix operator +(RMatrix m)
{
return m;
}
public static RMatrix operator +(RMatrix m1, RMatrix m2)
{
RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols);
for (int i = 0; i < m1.GetnRows; i++)
{
for (int j = 0; j < m1.GetnCols; j++)
{
result[i, j] = m1[i, j] + m2[i, j];
}
}
return result;
}
public static RMatrix operator -(RMatrix m)
{
for (int i = 0; i < m.GetnRows; i++)
{
for (int j = 0; j < m.GetnCols; j++)
{
m[i, j] = -m[i, j];
}
}
return m;
}
public static RMatrix operator -(RMatrix m1, RMatrix m2)
{
RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols);
for (int i = 0; i < m1.GetnRows; i++)
{
for (int j = 0; j < m1.GetnCols; j++)
{
result[i, j] = m1[i, j] - m2[i, j];
}
}
return result;
}
public static RMatrix operator *(RMatrix m, double d)
{
RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
for (int i = 0; i < m.GetnRows; i++)
{
for (int j = 0; j < m.GetnCols; j++)
{
result[i, j] = m[i, j] * d;
}
}
return result;
}
public static RMatrix operator *(double d, RMatrix m)
{
RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
for (int i = 0; i < m.GetnRows; i++)
{
for (int j = 0; j < m.GetnCols; j++)
{
result[i, j] = m[i, j] * d;
}
}
return result;
}
public static RMatrix operator /(RMatrix m, double d)
{
RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
for (int i = 0; i < m.GetnRows; i++)
{
for (int j = 0; j < m.GetnCols; j++)
{
result[i, j] = m[i, j] / d;
}
}
return result;
}
public static RMatrix operator /(double d, RMatrix m)
{
RMatrix result = new RMatrix(m.GetnRows, m.GetnCols);
for (int i = 0; i < m.GetnRows; i++)
{
for (int j = 0; j < m.GetnCols; j++)
{
result[i, j] = m[i, j] / d;
}
}
return result;
}
public static RMatrix operator *(RMatrix m1, RMatrix m2)
{
if (m1.GetnCols != m2.GetnRows)
{
throw new Exception("The numbers of columns of the" +
" first matrix must be equal to the number of " +
" rows of the second matrix!");
}
double tmp;
RMatrix result = new RMatrix(m1.GetnRows, m2.GetnCols);
for (int i = 0; i < m1.GetnRows; i++)
{
for (int j = 0; j < m2.GetnCols; j++)
{
tmp = result[i, j];
for (int k = 0; k < result.GetnRows; k++)
{
tmp += m1[i, k] * m2[k, j];
}
result[i, j] = tmp;
}
}
return result;
}
public RMatrix Transpose()
{
RMatrix m = new RMatrix(nCols, nRows);
for (int i = 0; i < nRows; i++)
{
for (int j = 0; j < nCols; j++)
{
m[j, i] = matrix[i, j];
}
}
return m;
}
}
static void Main(string[] args)
{
RMatrix m1 = new RMatrix(new double[3, 3] { {3.0, 2.0, 7.0},
{9.0, 3.0, 5.0},
{4.0, 1.0, 0.0}});
RMatrix m2 = new RMatrix(3, 3);
m2[0, 0] = 23.0; m2[0, 1] = 6.0; m2[0, 2] = 8.0;
m2[1, 0] = 2.0; m2[1, 1] = 55.0; m2[1, 2] = 39.0;
m2[2, 0] = 1.0; m2[2, 1] = 4.0; m2[2, 2] = 10.0;
Console.WriteLine("\nOriginal matrix: m1 = \n{0}", m1);
Console.WriteLine("\nOriginal matrix: m2 = \n{0}", m2);
Console.WriteLine("\nm1 + m2 = \n{0}", (m1 + m2));
Console.WriteLine("\nm1 - m2 = \n{0}", (m1 - m2));
Console.WriteLine("\nm1 * m2 = \n{0}", (m1 * m2));
Console.WriteLine("\nTranspose of m2 = \n{0}", m2.Transpose());
Console.ReadKey();
}
}