// ******************************************************************** // Multiply matrix 'm1' by 'm2' to give result in this // // Usage: A.mult (A1, A2); multiply A1 by A2 giving A // // ******************************************************************** public Matrix mult(Matrix m1, Matrix m2) { if (m1.c == m2.r) { resize(m1.r, m2.c); int m1_col = m1.c; for (int i = 0; i < r; i++) { for (int j = 0; j < m2.c; j++) { var sum = new SimpleComplex(0.0, 0.0); for (int k = 0; k < m1_col; k++) { sum = SimpleComplex.complexAdd(sum, SimpleComplex.complexMult(m1[i, k], m2[k, j])); } this[i, j] = sum; } } return(this); } else { throw new EMatrixSizeError("Incompatible matrix operands to multiply"); } }
// --------------------------------------------------------------------- // Create an array from an array argument using the labels to label the matrix // --------------------------------------------------------------------- public Matrix(double[,] M, string[] rowNames, string[] columnNames) { r = M.GetLength(0); c = M.GetLength(1); mxx = new SimpleComplex[r][]; for (int i = 0; i < r; i++) { mxx[i] = new SimpleComplex[c]; } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { mxx[i][j] = new SimpleComplex(M[i, j], 0.0); } } for (int i = 0; i < r; i++) { this.rowNames.Add(rowNames[i]); } for (int i = 0; i < c; i++) { this.columnNames.Add(columnNames[i]); } }
// --------------------------------------------------------------------- // Create a matrix from a jagged array argument // --------------------------------------------------------------------- public Matrix(double[][] M) { r = M.GetLength(0); if (r > 0) { c = M[0].GetLength(0); } else { c = 0; } mxx = new SimpleComplex[r][]; for (int i = 0; i < r; i++) { mxx[i] = new SimpleComplex[c]; } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { mxx[i][j] = new SimpleComplex(M[i][j], 0.0); } } setDefaultLabels(); }
public Matrix(Matrix cpy) : base() { r = cpy.r; c = cpy.c; mxx = new SimpleComplex[cpy.r][]; for (int i = 0; i < cpy.r; i++) { mxx[i] = new SimpleComplex[cpy.c]; } for (int i = 0; i < cpy.r; i++) { for (int j = 0; j < cpy.c; j++) { mxx[i][j] = cpy.mxx[i][j]; } } for (int i = 0; i < cpy.r; i++) { rowNames.Add(cpy.rowNames[i]); } for (int i = 0; i < cpy.c; i++) { columnNames.Add(cpy.columnNames[i]); } }
// ******************************************************************** // Multiply the diagonal of the matrix 'm1' by complex number z to give // result in this // // Usage: A.multDiag (A1, A2); multiply A1 by A2 giving A // // ******************************************************************** public Matrix multDiag(Matrix m, SimpleComplex z) { for (int i = 0; i < m.r; i++) { this[i, i] = SimpleComplex.complexMult(m[i, i], z); } return(this); }
// --------------------------------------------------------------------- // Subtract a scalar value from a matrix // Usage: // m.sub (3.1415); // --------------------------------------------------------------------- public void sub(double k) { for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { this[i, j] = SimpleComplex.complexSub(this[i, j], new SimpleComplex(k, 0.0)); } } }
// ******************************************************************** // Multiply matrix 'm1' by complex number z to give result in this // // Usage: A.mult (A1, z); multiply A1 by z giving A // // ******************************************************************** public Matrix mult(Matrix m, SimpleComplex z) { for (int i = 0; i < m.r; i++) { for (int j = 0; j < m.c; j++) { this[i, j] = SimpleComplex.complexMult(m[i, j], z); } } return(this); }
// ******************************************************************** // Multiply matrix 'm1' by scalar k to give result in Self // // Usage: A.mult (A1, A2); multiply A1 by A2 giving A // // ******************************************************************** public Matrix mult(Matrix m, double k) { for (int i = 0; i < m.r; i++) { for (int j = 0; j < m.c; j++) { this[i, j] = SimpleComplex.complexMult(m[i, j], new SimpleComplex(k, 0.0)); } } return(this); }
// --------------------------------------------------------------------- // Statics for generating predefined matrix types // Usage: // m = Matrix.Identity(); // --------------------------------------------------------------------- public static Matrix Identity(int n) { var m = new Matrix(n, n); for (int i = 0; i < n; i++) { m[i, i] = new SimpleComplex(1.0, 0.0); } m.setDefaultLabels(); return(m); }
// --------------------------------------------------------------------- // Subtract a scalar value from a matrix and return a new matrix // Usage: // Matrix m1 = Matrix.sub (m2, 2.718); // --------------------------------------------------------------------- public static Matrix sub(Matrix x, double k) { var result = new Matrix(x.r, x.c); for (int i = 0; i < x.r; i++) { for (int j = 0; j < x.c; j++) { result[i, j] = SimpleComplex.complexSub(x[i, j], new SimpleComplex(k, 0.0)); } } return(result); }
// --------------------------------------------------------------------- // Swap rows, i and j // --------------------------------------------------------------------- public void swapRows(int r1, int r2) { var tr = new SimpleComplex[c]; tr = mxx[r1]; mxx[r1] = mxx[r2]; mxx[r2] = tr; var tmp = (string)rowNames[r1]; rowNames[r1] = rowNames[r2]; rowNames[r2] = tmp; }
// --------------------------------------------------------------------- // One of the main constructors. Creates an empty matrix of size r by c // Usage: // Matrix m = new Matrix(4,4); // --------------------------------------------------------------------- // Worth checking for negative values? public Matrix(int r, int c) : base() { this.r = r; this.c = c; mxx = new SimpleComplex[r][]; for (int i = 0; i < r; i++) { mxx[i] = new SimpleComplex[c]; for (int j = 0; j < c; j++) mxx[i][j] = new SimpleComplex(); } setDefaultLabels(); }
// --------------------------------------------------------------------- // One of the main constructors. Creates an empty matrix of size r by c // Usage: // Matrix m = new Matrix(4,4); // --------------------------------------------------------------------- // Worth checking for negative values? public Matrix(int r, int c) : base() { this.r = r; this.c = c; mxx = new SimpleComplex[r][]; for (int i = 0; i < r; i++) { mxx[i] = new SimpleComplex[c]; for (int j = 0; j < c; j++) { mxx[i][j] = new SimpleComplex(); } } setDefaultLabels(); }
// --------------------------------------------------------------------- // Subtract a matrix from this. this is modified // Usage: // m.sub (m1); // --------------------------------------------------------------------- public void sub(Matrix x) { if (sameDimensions(this, x)) { for (int i = 0; i < x.r; i++) { for (int j = 0; j < x.c; j++) { this[i, j] = SimpleComplex.complexSub(this[i, j], x[i, j]); } } } else { throw new EMatrixException("Matrices must be the same dimension to perform subtraction"); } }
public static Matrix Random(int r, int c) { var m = new Matrix(r, c); m.name = "rm"; var rnd = new Random(); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { m[i, j] = new SimpleComplex(rnd.NextDouble(), 0.0); } } m.setDefaultLabels(); return(m); }
// Sub Methods: // Methods that return a new matrix // m = Matrix.sub (m1, m2); // m = Matrix.sub (m1, 2.3); // Methods that modify the matrix object // m.sub (m1); // m.sub (2.3); // --------------------------------------------------------------------- // Subtract two matrices together and return the result as a new matrix // Usage: // Matrix m3 = Matrix.sub (m1, m2); // --------------------------------------------------------------------- public static Matrix sub(Matrix x, Matrix y) { if (sameDimensions(x, y)) { var result = new Matrix(x.r, x.c); for (int i = 0; i < x.r; i++) { for (int j = 0; j < x.c; j++) { result[i, j] = SimpleComplex.complexSub(x[i, j], y[i, j]); } } return(result); } else { throw new EMatrixException("Matrices must be the same dimension to perform addition"); } }
// --------------------------------------------------------------------- // Create a matrix from an array argument // --------------------------------------------------------------------- public Matrix(double[,] M) { r = M.GetLength(0); c = M.GetLength(1); mxx = new SimpleComplex[r][]; for (int i = 0; i < r; i++) { mxx[i] = new SimpleComplex[c]; } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { mxx[i][j] = new SimpleComplex(M[i, j], 0.0); } } setDefaultLabels(); }
// --------------------------------------------------------------------- // Creates a matrix of size r by c filled with scalar value d // Usage: // Matrix m = new Matrix (2,2,Math.Pi); // --------------------------------------------------------------------- public Matrix(int r, int c, double d) : base() { this.r = r; this.c = c; mxx = new SimpleComplex[r][]; for (int i = 0; i < r; i++) { mxx[i] = new SimpleComplex[c]; } setDefaultLabels(); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { this[i, j] = new SimpleComplex(d, 0.0); } } }
public Matrix(Matrix cpy) : base() { r = cpy.r; c = cpy.c; mxx = new SimpleComplex[cpy.r][]; for (int i = 0; i < cpy.r; i++) mxx[i] = new SimpleComplex[cpy.c]; for (int i = 0; i < cpy.r; i++) for (int j = 0; j < cpy.c; j++) mxx[i][j] = cpy.mxx[i][j]; for (int i = 0; i < cpy.r; i++) rowNames.Add(cpy.rowNames[i]); for (int i = 0; i < cpy.c; i++) columnNames.Add(cpy.columnNames[i]); }
// --------------------------------------------------------------------- // Swap rows, i and j // --------------------------------------------------------------------- public void swapRows(int r1, int r2) { var tr = new SimpleComplex[c]; tr = mxx[r1]; mxx[r1] = mxx[r2]; mxx[r2] = tr; var tmp = (string) rowNames[r1]; rowNames[r1] = rowNames[r2]; rowNames[r2] = tmp; }
// ******************************************************************** // Multiply matrix 'm1' by 'm2' to give result in this // // Usage: A.mult (A1, A2); multiply A1 by A2 giving A // // ******************************************************************** public Matrix mult(Matrix m1, Matrix m2) { if (m1.c == m2.r) { resize(m1.r, m2.c); int m1_col = m1.c; for (int i = 0; i < r; i++) for (int j = 0; j < m2.c; j++) { var sum = new SimpleComplex(0.0, 0.0); for (int k = 0; k < m1_col; k++) sum = SimpleComplex.complexAdd(sum, SimpleComplex.complexMult(m1[i, k], m2[k, j])); this[i, j] = sum; } return this; } else throw new EMatrixSizeError("Incompatible matrix operands to multiply"); }
// ******************************************************************** // Multiply the diagonal of the matrix 'm1' by complex number z to give // result in this // // Usage: A.multDiag (A1, A2); multiply A1 by A2 giving A // // ******************************************************************** public Matrix multDiag(Matrix m, SimpleComplex z) { for (int i = 0; i < m.r; i++) this[i, i] = SimpleComplex.complexMult(m[i, i], z); return this; }
public static Matrix Random(int r, int c) { var m = new Matrix(r, c); m.name = "rm"; var rnd = new Random(); for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) m[i, j] = new SimpleComplex(rnd.NextDouble(), 0.0); m.setDefaultLabels(); return m; }
// ******************************************************************** // Multiply matrix 'm1' by complex number z to give result in this // // Usage: A.mult (A1, z); multiply A1 by z giving A // // ******************************************************************** public Matrix mult(Matrix m, SimpleComplex z) { for (int i = 0; i < m.r; i++) for (int j = 0; j < m.c; j++) this[i, j] = SimpleComplex.complexMult(m[i, j], z); return this; }
// --------------------------------------------------------------------- // Statics for generating predefined matrix types // Usage: // m = Matrix.Identity(); // --------------------------------------------------------------------- public static Matrix Identity(int n) { var m = new Matrix(n, n); for (int i = 0; i < n; i++) m[i, i] = new SimpleComplex(1.0, 0.0); m.setDefaultLabels(); return m; }
// --------------------------------------------------------------------- // Creates a matrix of size r by c filled with scalar value d // Usage: // Matrix m = new Matrix (2,2,Math.Pi); // --------------------------------------------------------------------- public Matrix(int r, int c, double d) : base() { this.r = r; this.c = c; mxx = new SimpleComplex[r][]; for (int i = 0; i < r; i++) mxx[i] = new SimpleComplex[c]; setDefaultLabels(); for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) this[i, j] = new SimpleComplex(d, 0.0); }
// --------------------------------------------------------------------- // Create an array from an array argument using the labels to label the matrix // --------------------------------------------------------------------- public Matrix(double[,] M, string[] rowNames, string[] columnNames) { r = M.GetLength(0); c = M.GetLength(1); mxx = new SimpleComplex[r][]; for (int i = 0; i < r; i++) mxx[i] = new SimpleComplex[c]; for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) mxx[i][j] = new SimpleComplex(M[i, j], 0.0); for (int i = 0; i < r; i++) this.rowNames.Add(rowNames[i]); for (int i = 0; i < c; i++) this.columnNames.Add(columnNames[i]); }
// --------------------------------------------------------------------- // Create a matrix from a jagged array argument // --------------------------------------------------------------------- public Matrix(double[][] M) { r = M.GetLength(0); if (r > 0) c = M[0].GetLength(0); else c = 0; mxx = new SimpleComplex[r][]; for (int i = 0; i < r; i++) mxx[i] = new SimpleComplex[c]; for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) mxx[i][j] = new SimpleComplex(M[i][j], 0.0); setDefaultLabels(); }
// --------------------------------------------------------------------- // Create a matrix from an array argument // --------------------------------------------------------------------- public Matrix(double[,] M) { r = M.GetLength(0); c = M.GetLength(1); mxx = new SimpleComplex[r][]; for (int i = 0; i < r; i++) mxx[i] = new SimpleComplex[c]; for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) mxx[i][j] = new SimpleComplex(M[i, j], 0.0); setDefaultLabels(); }