public static void Main(String[] args) { const int N = 5; UTMatrix ut1 = new UTMatrix(N); UTMatrix ut2 = new UTMatrix(N); for (int r = 0; r < N; r++) { ut1.set(r, r, 1); for (int c = r; c < N; c++) { ut2.set(r, c, 1); } } UTMatrix ut3 = ut1 + ut2; UTMatrixEnumerator ie = ut3.GetEnumerator(); while (ie.MoveNext()) { Console.Write(ie.Current + " "); } Console.WriteLine(); foreach (int v in ut3) { Console.Write(v + " "); } Console.WriteLine(); }
// Returns an upper triangular matrix that is the summation of a & b. // Throws an error if a and b are incompatible. public static UTMatrix operator +(UTMatrix a, UTMatrix b) { if (a.N != b.N) { throw new ArgumentException("Matrices must have same dimension", "a"); } UTMatrix sum = new UTMatrix(a.getSize()); for (int i = 0; i < sum.getSize() * (sum.getSize() + 1) / 2; i++) { sum.data[i] = a.data[i] + b.data[i]; } return(sum); }
// Returns an upper triangular matrix that is the summation of a & b. // Throws an error if a and b are incompatible. public static UTMatrix operator +(UTMatrix a, UTMatrix b) { if (a.row != b.row || a.column != b.column) { // raise Error throw new InvalidOperationException(); } UTMatrix result = new UTMatrix(a.row); for (int i = 0; i < a.data.Length; i++) { result.data[i] = a.data[i] + b.data[i]; } return(result); }
// Returns an upper triangular matrix that is the summation of a & b. // Throws an error if a and b are incompatible. public static UTMatrix operator +(UTMatrix a, UTMatrix b) { if (a.getSize() != b.getSize()) { throw new ArgumentException("Cannot add matrices of different dimensions"); } UTMatrix summed = new UTMatrix(a.getSize()); for (int row = 0; row < a.getSize(); row++) { for (int col = row; col < a.getSize(); col++) { summed.set(row, col, (a.get(row, col) + b.get(row, col))); } } return(summed); }
public UTMatrixEnumerator(UTMatrix mat) { this.matrix = mat; Reset(); }
public UTMatrixEnumerator(UTMatrix matrix) { pointer = -1; numbers = matrix.data; }