matrixwithtest/MatrixProject/Matrix.cs

75 lines
1.7 KiB
C#
Raw Normal View History

2024-10-08 13:20:27 +00:00
using MatrixProject.Exceptions;
namespace MatrixProject;
2024-10-08 10:42:11 +00:00
public struct Matrix
{
private double[,] _data;
public int Rows { get => _data.GetLength(0);}
public int Columns { get => _data.GetLength(1);}
2024-10-08 12:24:58 +00:00
2024-10-08 10:42:11 +00:00
public Matrix(int rows)
{
_data = new double[rows, rows];
FillMatrixByNumber(0);
}
public Matrix(int rows, int columns) {
_data = new double[rows,columns];
FillMatrixByNumber(0);
}
public Matrix(int rows, int columns, int fillNumber)
{
_data = new double[rows, columns];
FillMatrixByNumber(fillNumber);
}
2024-10-08 12:24:58 +00:00
public static Matrix operator +(Matrix matrix, Matrix matrixTwo) {
2024-10-08 13:20:27 +00:00
if (matrix.Rows != matrixTwo.Rows ||
matrix.Columns != matrixTwo.Columns)
throw new AdditionException("Строки и столбцы не совпадают");
for (int i = 0; i < matrix.Rows; i++) {
for (int j = 0; j < matrix.Columns; j++){
matrix[i, j] += matrixTwo[i, j];
}
}
return matrix;
2024-10-08 12:24:58 +00:00
}
public static Matrix operator *(Matrix matrix, int scalar)
{
for (int i = 0; i < matrix.Rows; i++)
{
for (int j = 0; j < matrix.Columns; j++)
{
matrix[i, j] *= scalar;
}
}
return matrix;
}
2024-10-08 10:42:11 +00:00
public double this[int row, int column] {
get => _data[row,column];
2024-10-08 12:24:58 +00:00
set => _data[row,column] = value;
2024-10-08 10:42:11 +00:00
}
private void FillMatrixByNumber(int number) {
for (int i = 0; i < Rows; i++)
{
for (int j= 0; j < Columns; j++)
{
_data[i, j] = number;
}
}
}
}