// For 0 <= r < numRows and 0 <= c < numCols, element (r,c) is 1 and // all others are 0. If either of r or c is invalid, the zero matrix // is created. This is a convenience for creating the standard // Euclidean basis matrices; see also MakeUnit(int,int) and // Unit(int,int). public GMatrix(int numRows, int numCols, int r, int c) { this.numRows = numRows; this.numCols = numCols; this.elements = new GVector(numRows * numCols); this.MakeUnit(r, c); }
// Initialization to a diagonal matrix whose diagonal entries are the // components of D, even when nonsquare. public static void MakeDiagonal(GVector d, GMatrix m) { int numRows = m.NumRows; int numCols = m.NumCols; int numDiagonal = numRows <= numCols ? numRows : numCols; m.MakeZero(); for (int i = 0; i < numDiagonal; ++i) { m[i, i] = d[i]; } }
// U*V^T, U is N-by-1, V is M-by-1, result is N-by-M. public static GMatrix OuterProduct(GVector u, GVector v) { GMatrix result = new GMatrix(u.Size, v.Size); for (int r = 0; r < result.NumRows; ++r) { for (int c = 0; c < result.NumCols; ++c) { result[r, c] = u[r] * v[c]; } } return(result); }
public GVector GetCol(int c) { if (0 <= c && c < this.numCols) { GVector vec = new GVector(this.numRows); for (int r = 0; r < this.numRows; ++r) { vec[r] = this[r, c]; } return(vec); } Debug.Assert(false, "Invalid index."); return(new GVector(0)); }
public GVector GetRow(int r) { if (0 <= r && r < this.numRows) { GVector vec = new GVector(this.numCols); for (int c = 0; c < this.numCols; ++c) { vec[c] = this[r, c]; } return(vec); } Debug.Assert(false, "Invalid index."); return(new GVector(0)); }
// D*M, D is square diagonal (stored as vector) public static GMatrix MultiplyDM(GVector d, GMatrix m) { if (d.Size == m.NumRows) { GMatrix result = new GMatrix(m.NumRows, m.NumCols); for (int r = 0; r < result.NumRows; ++r) { for (int c = 0; c < result.NumCols; ++c) { result[r, c] = d[r] * m[r, c]; } } return(result); } Debug.Assert(false, "Mismatched sizes."); return(new GMatrix(0, 0)); }
public void SetCol(int c, GVector vec) { if (0 <= c && c < this.numCols) { if (vec.Size == this.NumRows) { for (int r = 0; r < this.numRows; ++r) { this[r, c] = vec[r]; } return; } Debug.Assert(false, "Mismatched sizes."); } Debug.Assert(false, "Invalid index."); }
// Member access by rows or by columns. The input vectors must have // the correct number of elements for the matrix size. public void SetRow(int r, GVector vec) { if (0 <= r && r < this.numRows) { if (vec.Size == this.NumCols) { for (int c = 0; c < this.numCols; ++c) { this[r, c] = vec[c]; } return; } Debug.Assert(false, "Mismatched sizes."); } Debug.Assert(false, "Invalid index."); }
// V^T*M public static GVector operator *(GVector v, GMatrix m) { if (v.Size == m.NumRows) { GVector result = new GVector(m.NumCols); for (int c = 0; c < m.NumCols; ++c) { result[c] = 0.0; for (int r = 0; r < m.NumRows; ++r) { result[c] += v[r] * m[r, c]; } } return(result); } Debug.Assert(false, "Mismatched sizes."); return(new GVector(0)); }
public GMatrix(int numRows, int numCols, double[] elements) { this.numRows = numRows; this.numCols = numCols; this.elements = new GVector(elements); }
//// The table is length zero and mNumRows and mNumCols are set to zero. //public GMatrix() //{ //} // The table is length numRows*numCols and the elements are // initialized to zero. public GMatrix(int numRows, int numCols) { this.numRows = numRows; this.numCols = numCols; this.elements = new GVector(numRows * numCols); }