matrixwithtest/MatrixProject/Matrix.cs
2024-10-08 16:20:27 +03:00

75 lines
1.7 KiB
C#

using MatrixProject.Exceptions;
namespace MatrixProject;
public struct Matrix
{
private double[,] _data;
public int Rows { get => _data.GetLength(0);}
public int Columns { get => _data.GetLength(1);}
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);
}
public static Matrix operator +(Matrix matrix, Matrix matrixTwo) {
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;
}
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;
}
public double this[int row, int column] {
get => _data[row,column];
set => _data[row,column] = value;
}
private void FillMatrixByNumber(int number) {
for (int i = 0; i < Rows; i++)
{
for (int j= 0; j < Columns; j++)
{
_data[i, j] = number;
}
}
}
}