Using System;
using System.Collections.Generic;
public class Matrix
{
private double[,] Data;
public double this[int i, int j]
{
get { return Data[i, j]; }
set { Data[i, j] = value; }
}
public Matrix(double[,] data) { this.Data = data; }
public override string ToString()
{
var sb = new System.Text.StringBuilder();
for (var i = Data.GetLowerBound(0); i <= Data.GetUpperBound(0); i++)<br> {
for (var j = Data.GetLowerBound(1); j <= Data.GetUpperBound(1); j++)<br> {
sb.AppendFormat("{0} ", Data[i, j]);
}
sb.AppendLine();
}
return sb.ToString();
}
public int GetUpperBound(int i) => this.Data.GetUpperBound(i);
public static Matrix operator -(Matrix a, Matrix b)
{
var c = new double[1 + Math.Min(a.GetUpperBound(0), b.GetUpperBound(0)), 1 + Math.Min(a.GetUpperBound(1), b.GetUpperBound(1))];
for (var i = 0; i <= c.GetUpperBound(0); i++)<br> {
for (var j = 0; j <= c.GetUpperBound(1); j++)<br> {
c[i, j] = a[i, j] - b[i, j];
}
};
return new Matrix(c);
}
}
class Program
{
static void Main()
{
var n = 5;
var a = new Matrix(new double[n, n]);
var b = new Matrix(new double[n, n]);
for (var i = 0; i < n; i++)
{
for (var j = 0; j < n; j++)
{
a[i, j] = i + j;
b[i, j] = i;
}
}
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(a - b);
Console.ReadKey();
}
}