public static void Main(String [] args) { const int N = 10; TriDiagonalMatrix m1 = new TriDiagonalMatrix(N); TriDiagonalMatrix m2 = new TriDiagonalMatrix(N); int x = 1; for (int row = 0; row < N; row++) { for (int col = 0; col < N; col++) { if (Math.Abs(row - col) <= 1) { m1.set(row, col, 1); m2.set(row, col, x); x++; } } } Console.WriteLine("m1:\n" + m1.CompactPrint()); Console.WriteLine("m2:\n" + m2.CompactPrint()); TriDiagonalMatrix m3 = m1 + m2; Console.WriteLine("m3:\n" + m3.CompactPrint()); }
// Constructor // Initialize m with value matrix and b with value v // Throw an ArgumentException if the size of the matrix is not equal to // the length of vector v. The exception message should have the format: // "Mismatched sizes: The size of the matrix is {0} and the length of the vector is {1}" public Solver(TriDiagonalMatrix matrix, float [] v) { if (matrix.N != v.Length) { throw new System.ArgumentException(String.Format("Mismatched sizes: The size of the matrix is {0} and the length of the vector is {1}", matrix.N, v.Length), "matrix"); } this.m = matrix; this.b = v; }
public static void Main(String [] args) { try { TriDiagonalMatrix m = new TriDiagonalMatrix(6); Console.WriteLine(m.get(-2, 5)); } catch (ArgumentException e) { Console.WriteLine(e); } }
public static void Main(String [] args) { try { TriDiagonalMatrix m = new TriDiagonalMatrix(-2); m.CompactPrint(); } catch (ArgumentException e) { Console.WriteLine(e); } }
public static void Main(String [] args) { const int N = 10; TriDiagonalMatrix m1 = new TriDiagonalMatrix(N); TriDiagonalMatrix m2 = new TriDiagonalMatrix(N); int x = 1; for (int row = 0; row < N; row++) { for (int col = 0; col < N; col++) { if (Math.Abs(row - col) <= 1) { m1.set(row, col, 1); m2.set(row, col, x); x++; } } } TriDiagonalMatrix m3 = m1 + m2; Console.WriteLine("m1:"); foreach (int v in m1) { Console.Write(v + " "); } Console.WriteLine(); Console.WriteLine("m2:"); foreach (int v in m2) { Console.Write(v + " "); } Console.WriteLine(); Console.WriteLine("m3:"); foreach (int v in m3) { Console.Write(v + " "); } Console.WriteLine(); Console.WriteLine("m3:"); TriDiagonalMatrixEnumerator ie = m3.GetEnumerator(); while (ie.MoveNext()) { Console.Write(ie.Current + " "); } Console.WriteLine(); }
public static void Main(String [] args) { try { TriDiagonalMatrix m1 = new TriDiagonalMatrix(2); TriDiagonalMatrix m2 = new TriDiagonalMatrix(3); TriDiagonalMatrix m3 = m1 + m2; m3.CompactPrint(); } catch (ArgumentException e) { Console.WriteLine(e); } }
public static void Main(String [] args) { int N = 31; TriDiagonalMatrix m = new TriDiagonalMatrix(N); float [] b = new float[N]; for (int row = 0; row < N; row++) { for (int col = 0; col < N; col++) { int diff = Math.Abs(row - col); if (diff == 0) { m.set(row, col, 2); } else if (diff == 1) { m.set(row, col, 1); } } b[row] = (float)16.0 - (float)Math.Abs((float)(row + 1) - (float)16.0); } try { Solver s = new Solver(m, b); float [] x = s.Solve(); Console.Write("x: "); for (int i = 0; i < N; i++) { Console.Write((int)Math.Round(x[i], 0) + " "); } Console.WriteLine(); } catch (ArgumentException e) { Console.WriteLine(e); } }
// Returns an upper tridiagonal matrix that is the summation of tridiagonal // matrices x and y. // Throws an ArgumentException if x and y are incompatible. The exception // message should include the sizes of x and y. public static TriDiagonalMatrix operator +(TriDiagonalMatrix x, TriDiagonalMatrix y) { // check if 2 martix are compatible if (x.N != y.N) { throw new System.ArgumentException("Two matrix are incompatible", "x"); } TriDiagonalMatrix re = new TriDiagonalMatrix(x.N); for (int i = 0; i < x.N - 1; i++) { re.d[i] = x.d[i] + y.d[i]; re.a[i] = x.a[i] + y.a[i]; re.c[i] = x.c[i] + y.c[i]; } re.d[x.N - 1] = x.d[x.N - 1] + y.d[x.N - 1]; return(re); }
public TriDiagonalMatrixEnumerator(TriDiagonalMatrix matrix) { this.matrix = matrix; Reset(); }