/// <summary> /// Subtracts a matrix from a matrix and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="MatrixF"/> instance to subtract from.</param> /// <param name="b">A <see cref="MatrixF"/> instance to subtract.</param> /// <param name="result">A <see cref="MatrixF"/> instance to hold the result.</param> /// <remarks>result[x][y] = a[x][y] - b[x][y]</remarks> /// <exception cref="System.ArgumentException">Matrix dimentions do not match.</exception> public static void Subtract(MatrixF a, MatrixF b, MatrixF result) { if ((!MatrixF.EqualDimentions(a, b)) && (!MatrixF.EqualDimentions(a, result))) { throw new ArgumentException("Matrix dimentions do not match."); } for (int r = 0; r < a.Rows; r++) { for (int c = 0; c < a.Columns; c++) { result._data[r][c] = a._data[r][c] - b._data[r][c]; } } }
/// <summary> /// Subtracts a matrix from a matrix. /// </summary> /// <param name="a">A <see cref="MatrixF"/> instance to subtract from.</param> /// <param name="b">A <see cref="MatrixF"/> instance to subtract.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the difference.</returns> /// <remarks>result[x][y] = a[x][y] - b[x][y]</remarks> /// <exception cref="System.ArgumentException">Matrix dimentions do not match.</exception> public static MatrixF Subtract(MatrixF a, MatrixF b) { if (!MatrixF.EqualDimentions(a, b)) { throw new ArgumentException("Matrix dimentions do not match."); } MatrixF result = new MatrixF(a.Rows, a.Columns); for (int r = 0; r < a.Rows; r++) { for (int c = 0; c < a.Columns; c++) { result._data[r][c] = a._data[r][c] - b._data[r][c]; } } return(result); }
/// <summary> /// Returns a value indicating whether this instance is equal to /// the specified object. /// </summary> /// <param name="obj">An object to compare to this instance.</param> /// <returns><see langword="true"/> if <paramref name="obj"/> is a <see cref="MatrixF"/> and has the same values as this instance; otherwise, <see langword="false"/>.</returns> public override bool Equals(object obj) { if (obj is MatrixF) { MatrixF m = (MatrixF)obj; for (int i = 0; i < m.Rows; i++) { for (int j = 0; j < m.Columns; j++) { if (_data[i][j] != m._data[i][j]) { return(false); } } } return(true); } return(false); }
/// <summary> /// Extracts a sub matrix from the current matrix. /// </summary> /// <param name="rowStart">Row index to start extraction from.</param> /// <param name="rowEnd">Row index to end extraction at.</param> /// <param name="colStart">Column index to start extraction from.</param> /// <param name="colEnd">Column index to end extraction at.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the extracted sub matrix.</returns> public MatrixF Submatrix(int rowStart, int rowEnd, int colStart, int colEnd) { MatrixF result = new MatrixF( (rowEnd-rowStart)+1 , (colEnd-colStart)+1 ); for (int r = rowStart; r <= rowEnd; r++) { for (int c = colStart; c <= colEnd; c++) { result._data[r - rowStart][c - colStart] = _data[r][c]; } } return result; }
/// <summary> /// Transposes a matrix. /// </summary> /// <param name="m">A <see cref="MatrixF"/> instance.</param> /// <returns>A new <see cref="MatrixF"/> instance transposed matrix.</returns> public static MatrixF Transpose(MatrixF m) { MatrixF result = new MatrixF(m.Columns, m.Rows); for(int r = 0; r < m.Rows; r++) { for(int c = 0; c < m.Columns; c++) { result._data[c][r] = m._data[r][c]; } } return result; }
/// <summary> /// Subtracts a matrix from a matrix and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="MatrixF"/> instance to subtract from.</param> /// <param name="b">A <see cref="MatrixF"/> instance to subtract.</param> /// <param name="result">A <see cref="MatrixF"/> instance to hold the result.</param> /// <remarks>result[x][y] = a[x][y] - b[x][y]</remarks> /// <exception cref="System.ArgumentException">Matrix dimentions do not match.</exception> public static void Subtract(MatrixF a, MatrixF b, MatrixF result) { if ((!MatrixF.EqualDimentions(a,b)) && (!MatrixF.EqualDimentions(a,result))) { throw new ArgumentException("Matrix dimentions do not match."); } for(int r = 0; r < a.Rows; r++) { for(int c = 0; c < a.Columns; c++) { result._data[r][c] = a._data[r][c] - b._data[r][c]; } } }
/// <summary> /// Subtracts a matrix from a matrix. /// </summary> /// <param name="a">A <see cref="MatrixF"/> instance to subtract from.</param> /// <param name="b">A <see cref="MatrixF"/> instance to subtract.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the difference.</returns> /// <remarks>result[x][y] = a[x][y] - b[x][y]</remarks> /// <exception cref="System.ArgumentException">Matrix dimentions do not match.</exception> public static MatrixF Subtract(MatrixF a, MatrixF b) { if (!MatrixF.EqualDimentions(a,b)) { throw new ArgumentException("Matrix dimentions do not match."); } MatrixF result = new MatrixF(a.Rows, a.Columns); for(int r = 0; r < a.Rows; r++) { for(int c = 0; c < a.Columns; c++) { result._data[r][c] = a._data[r][c] - b._data[r][c]; } } return result; }
/// <summary> /// Negates a matrix. /// </summary> /// <param name="m">A <see cref="MatrixF"/> instance.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the result.</returns> public static MatrixF Negate(MatrixF m) { MatrixF result = new MatrixF(m.Rows, m.Columns); for(int r = 0; r < result.Rows; r++) { for(int c = 0; c < result.Columns; c++) { result._data[r][c] = -m._data[r][c]; } } return result; }
/// <summary> /// Extracts a sub matrix from the current matrix. /// </summary> /// <param name="rowIndices">An array of row indices.</param> /// <param name="colStart">Column index to start extraction from.</param> /// <param name="colEnd">Column index to end extraction at.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the extracted sub matrix.</returns> public MatrixF Submatrix(int[] rowIndices, int colStart, int colEnd) { MatrixF result = new MatrixF( rowIndices.Length, (colEnd-colStart)+1 ); for(int r = 0; r < rowIndices.Length; r++) { for(int c = colStart; c <= colEnd; c++) { result._data[r][c - colStart] = _data[rowIndices[r]][c]; } } return result; }
/// <summary> /// Tests whether the dimentions of two given matrices are equal. /// </summary> /// <param name="a">A <see cref="MatrixF"/> instance.</param> /// <param name="b">A <see cref="MatrixF"/> instance.</param> /// <returns><see langword="true"/> if the dimentions of the two matrices are equal; otherwise, <see langword="false"/>.</returns> public static bool EqualDimentions(MatrixF a, MatrixF b) { if (a.Rows != b.Rows) return false; if (a.Columns != b.Columns) return false; return true; }
/// <summary> /// Initializes a new instance of the <see cref="MatrixF"/> using a given matrix. /// </summary> /// <param name="matrix">A <see cref="MatrixF"/> instance.</param> public MatrixF(MatrixF matrix) { _rows = matrix._rows; _columns = matrix._columns; _data = (float[][])matrix._data.Clone(); }
/// <summary> /// Multiplies a matrix by a scalar. /// </summary> /// <param name="m">A <see cref="MatrixF"/> instance.</param> /// <param name="s">A single-precision floating point value.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the result.</returns> public static MatrixF operator*(float s, MatrixF m) { return(MatrixF.Multiply(m, s)); }
/// <summary> /// Adds two matrices. /// </summary> /// <param name="a">A <see cref="MatrixF"/> instance.</param> /// <param name="b">A <see cref="MatrixF"/> instance.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the sum.</returns> public static MatrixF operator+(MatrixF a, MatrixF b) { return(MatrixF.Add(a, b)); }
/// <summary> /// Negates the values of a matrix. /// </summary> /// <param name="m">A <see cref="MatrixF"/> instance.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the result.</returns> public static MatrixF operator-(MatrixF m) { return(MatrixF.Negate(m)); }
/// <summary> /// Extracts a sub matrix from the current matrix. /// </summary> /// <param name="rowIndices">An array of row indices.</param> /// <param name="colIndices">An array of column indices.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the extracted sub matrix.</returns> public MatrixF Submatrix(int[] rowIndices, int[] colIndices) { MatrixF result = new MatrixF(rowIndices.Length, colIndices.Length); for(int r = 0; r < rowIndices.Length; r++) { for(int c = 0; c < colIndices.Length; c++) { result._data[r][c] = _data[rowIndices[r]][colIndices[c]]; } } return result; }
/// <summary> /// Extracts a sub matrix from the current matrix. /// </summary> /// <param name="rowStart">Row index to start extraction from.</param> /// <param name="rowEnd">Row index to end extraction at.</param> /// <param name="colIndices">An array of column indices.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the extracted sub matrix.</returns> public MatrixF Submatrix(int rowStart, int rowEnd, int[] colIndices) { MatrixF result = new MatrixF( (rowEnd-rowStart)+1, colIndices.Length ); for(int r = rowStart; r <= rowEnd; r++) { for(int c = 0; c < colIndices.Length; c++) { result._data[r - rowStart][c] = _data[r][colIndices[c]]; } } return result; }
/// <summary> /// Multiplies a matrix by a scalar. /// </summary> /// <param name="m">A <see cref="MatrixF"/> instance.</param> /// <param name="s">A single-precision floating point value.</param> /// <returns>A new <see cref="MatrixF"/> instance containing the result.</returns> public static MatrixF Multiply(MatrixF m, float s) { MatrixF result = new MatrixF(m); for (int i = 0; i < result.Rows; i++) { for (int j = 0; j < result.Columns; j++) { result._data[i][j] *= s; } } return result; }
/// <summary> /// Initializes a new instance of the <see cref="MatrixF"/> using a given matrix. /// </summary> /// <param name="matrix">A <see cref="MatrixF"/> instance.</param> public MatrixF(MatrixF matrix) { _rows = matrix._rows; _columns= matrix._columns; _data = (float[][])matrix._data.Clone(); }
/// <summary> /// Multiplies a matrix by a scalar and put the result in a given matrix instance. /// </summary> /// <param name="m">A <see cref="MatrixF"/> instance.</param> /// <param name="s">A single-precision floating point value.</param> /// <param name="result">A <see cref="MatrixF"/> instance to hold the result.</param> public static void Multiply(MatrixF m, float s, MatrixF result) { Debug.Assert(result.Rows == m.Rows); Debug.Assert(result.Columns == m.Columns); for (int i = 0; i < result.Rows; i++) { for (int j = 0; j < result.Columns; j++) { result._data[i][j] = m._data[i][j] * s; } } }