Создать тип Matrix, инкапсулирующий двумерный массив-матрицу блок реального (двойного) типа на c#
У меня есть задание,Создать тип Matrix, инкапсулирующий двумерный массив-матрицу блок реального (двойного) типа. В своем коде я дошла только до половины. И теперь топчусь на месте. Вот что мне удалось решить :
```
using System;
namespace MatrixLibrary
{
class MatrixException : Exception
{
public int Rows { get; }
public int Columns { get; }
public override string Message
{
get
{
return "incorrect quantity of rows/columns";
}
}
public MatrixException(int rows, int columns)
{
this.Rows = rows;
this.Columns = columns;
}
}
// TODO: Create custom exception "MatrixException"
public class Matrix : ICloneable
{
/// <summary>
/// Number of rows.
/// </summary>
public int Rows
{
get;
set;
}
/// <summary>
/// Number of columns.
/// </summary>
public int Columns
{
get;
set;
}
/// <summary>
/// Gets an array of floating-point values that represents the elements of this Matrix.
/// </summary>
public double[,] Array
{
get; set;
}
/// <summary>
/// Initializes a new instance of the <see cref="Matrix"/> class.
/// </summary>
/// <param name="rows"></param>
/// <param name="columns"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public Matrix(int rows, int columns)
{
Rows = rows;
Columns = columns;
Array = new double[Rows, Columns];
}
/// <summary>
/// Initializes a new instance of the <see cref="Matrix"/> class with the specified elements.
/// </summary>
/// <param name="array">An array of floating-point values that represents the elements of this Matrix.</param>
/// <exception cref="ArgumentNullException"></exception>
public Matrix(double[,] array)
{
this.Rows = array.GetLength(0);
this.Columns = array.GetLength(1);
this.Array = array;
}
/// <summary>
/// Allows instances of a Matrix to be indexed just like arrays.
/// </summary>
/// <param name="row"></param>
/// <param name="column"></param>
/// <exception cref="ArgumentException"></exception>
public double this[int row, int column]
{
get
{
return this.Array[row, column];
}
set
{
this.Array[row, column] = value;
}
}
```
теперь я пытаюсь решить вот этот :
/// <summary>
/// Creates a deep copy of this Matrix.
/// </summary>
/// <returns>A deep copy of the current object.</returns>
public object Clone()
{
// if (obj.GetType() != this.GetType()) return false;
//var other = (Matrix)obj;
// return (this.rows == other.rows) && (this.columns == other.columns);
}
/// <summary>
/// Adds two matrices.
/// </summary>
/// <param name="matrix1"></param>
/// <param name="matrix2"></param>
/// <returns>New <see cref="Matrix"/> object which is sum of two matrices.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="MatrixException"></exception>
public static Matrix operator +(Matrix matrix1, Matrix matrix2)
{
// Matrix matrix1 = new Matrix(matrix1, row, colum);
// Matrix matrix2 = new Matrix(matrix2, row, colum);
// Boolean result;
// result is true.
//result = (matrix1 == matrix2);
// result is false.
//result = (matrix1 != matrix2);
}
/// <summary>
/// Subtracts two matrices.
/// </summary>
/// <param name="matrix1"></param>
/// <param name="matrix2"></param>
/// <returns>New <see cref="Matrix"/> object which is subtraction of two matrices</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="MatrixException"></exception>
public static Matrix operator -(Matrix matrix1, Matrix matrix2)
{
throw new NotImplementedException();
}
/// <summary>
/// Multiplies two matrices.
/// </summary>
/// <param name="matrix1"></param>
/// <param name="matrix2"></param>
/// <returns>New <see cref="Matrix"/> object which is multiplication of two matrices.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="MatrixException"></exception>
public static Matrix operator *(Matrix matrix1, Matrix matrix2)
{
throw new NotImplementedException();
}
/// <summary>
/// Adds <see cref="Matrix"/> to the current matrix.
/// </summary>
/// <param name="matrix"><see cref="Matrix"/> for adding.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="MatrixException"></exception>
public Matrix Add(Matrix matrix)
{
throw new NotImplementedException();
}
/// <summary>
/// Subtracts <see cref="Matrix"/> from the current matrix.
/// </summary>
/// <param name="matrix"><see cref="Matrix"/> for subtracting.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="MatrixException"></exception>
public Matrix Subtract(Matrix matrix)
{
throw new NotImplementedException();
}
/// <summary>
/// Multiplies <see cref="Matrix"/> on the current matrix.
/// </summary>
/// <param name="matrix"><see cref="Matrix"/> for multiplying.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="MatrixException"></exception>
public Matrix Multiply(Matrix matrix)
{
Matrix result = matrix.Multiply(array);
return result;
}
/// <summary>
/// Tests if <see cref="Matrix"/> is identical to this Matrix.
/// </summary>
/// <param name="obj">Object to compare with. (Can be null)</param>
/// <returns>True if matrices are equal, false if are not equal.</returns>
public override bool Equals(object obj)
{
throw new NotImplementedException();
}
public override int GetHashCode() => base.GetHashCode();
}
}
у меня ничего не выходит. нужна помощь
if (matrix1.Columns == matrix2.Columns)
for (int i = 0; i < matrix1.Rows; i++)
{
for (int j = 0; j < matrix1.Columns; j++)
{
matrix1.Array[i, j] = matrix1.Array[i, j] + matrix2.Array[0, j];
}
}
return matrix1;-этот код работает. я ошиблась в параметрах. нужно было с заглавной буквы
public static Matrix operator +(Matrix matrix1, Matrix matrix2)
{
if (matrix1.Columns != matrix2.Columns && matrix1.Rows != matrix2.Rows)
throw new ArgumentException("Количества столбцов и колонок должны быть равны");
double[,] result = new double[matrix1.Columns, matrix1.Rows];
for (int i = 0; i < matrix1.Rows; i++)
for (int j = 0; j < matrix1.Columns; j++)
result[i, j] = matrix1.Array[i, j] + matrix2.Array[i, j];
return new Matrix(result);
}
я изменила код но тесты не проходят
public static Matrix operator +(Matrix matrix1, Matrix matrix2)
{
if (matrix1.column == matrix2.column)
for (int i = 0; i < matrix1.row; i++)
{
for (int j = 0; j < matrix1.column; j++)
{
matrix1.array[i, j] = matrix1.array[i, j] + matrix2.array[0, j];
}
}
else Console.WriteLine("Количества столбцов должны быть равны");
return matrix1;
}
что тут делаю не так?