/// <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="MatrixD"/> instance containing the extracted sub matrix.</returns> public MatrixD Submatrix(int[] rowIndices, int colStart, int colEnd) { MatrixD result = new MatrixD( 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> /// Initializes a new instance of the <see cref="MatrixD"/> using a given matrix. /// </summary> /// <param name="matrix">A <see cref="MatrixD"/> instance.</param> public MatrixD(MatrixD matrix) { _rows = matrix._rows; _columns= matrix._columns; _data = (double[][])matrix._data.Clone(); }
/// <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="MatrixD"/> instance containing the extracted sub matrix.</returns> public MatrixD Submatrix(int[] rowIndices, int[] colIndices) { MatrixD result = new MatrixD(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="MatrixD"/> instance containing the extracted sub matrix.</returns> public MatrixD Submatrix(int rowStart, int rowEnd, int[] colIndices) { MatrixD result = new MatrixD( (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> /// Transposes a matrix. /// </summary> /// <param name="m">A <see cref="MatrixD"/> instance.</param> /// <returns>A new <see cref="MatrixD"/> instance transposed matrix.</returns> public static MatrixD Transpose(MatrixD m) { MatrixD result = new MatrixD(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> /// 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="MatrixD"/> instance containing the extracted sub matrix.</returns> public MatrixD Submatrix(int rowStart, int rowEnd, int colStart, int colEnd) { MatrixD result = new MatrixD( (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> /// Subtracts a matrix from a matrix. /// </summary> /// <param name="a">A <see cref="MatrixD"/> instance to subtract from.</param> /// <param name="b">A <see cref="MatrixD"/> instance to subtract.</param> /// <returns>A new <see cref="MatrixD"/> 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 MatrixD Subtract(MatrixD a, MatrixD b) { if (!MatrixD.EqualDimentions(a,b)) { throw new ArgumentException("Matrix dimentions do not match."); } MatrixD result = new MatrixD(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> /// Subtracts a matrix from a matrix and put the result in a third matrix. /// </summary> /// <param name="a">A <see cref="MatrixD"/> instance to subtract from.</param> /// <param name="b">A <see cref="MatrixD"/> instance to subtract.</param> /// <param name="result">A <see cref="MatrixD"/> 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(MatrixD a, MatrixD b, MatrixD result) { if ((!MatrixD.EqualDimentions(a,b)) && (!MatrixD.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> /// Multiplies a matrix by a scalar and put the result in a given matrix instance. /// </summary> /// <param name="m">A <see cref="MatrixD"/> instance.</param> /// <param name="s">A double-precision floating point value.</param> /// <param name="result">A <see cref="MatrixD"/> instance to hold the result.</param> public static void Multiply(MatrixD m, double s, MatrixD 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; } } }
/// <summary> /// Negates a matrix. /// </summary> /// <param name="m">A <see cref="MatrixD"/> instance.</param> /// <returns>A new <see cref="MatrixD"/> instance containing the result.</returns> public static MatrixD Negate(MatrixD m) { MatrixD result = new MatrixD(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> /// Multiplies a matrix by a scalar. /// </summary> /// <param name="m">A <see cref="MatrixD"/> instance.</param> /// <param name="s">A double-precision floating point value.</param> /// <returns>A new <see cref="MatrixD"/> instance containing the result.</returns> public static MatrixD Multiply(MatrixD m, double s) { MatrixD result = new MatrixD(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> /// Tests whether the dimentions of two given matrices are equal. /// </summary> /// <param name="a">A <see cref="MatrixD"/> instance.</param> /// <param name="b">A <see cref="MatrixD"/> instance.</param> /// <returns><see langword="true"/> if the dimentions of the two matrices are equal; otherwise, <see langword="false"/>.</returns> public static bool EqualDimentions(MatrixD a, MatrixD b) { if (a.Rows != b.Rows) return false; if (a.Columns != b.Columns) return false; return true; }
/// <summary> /// Multiplies a matrix by a scalar. /// </summary> /// <param name="m">A <see cref="MatrixD"/> instance.</param> /// <param name="s">A double-precision floating point value.</param> /// <returns>A new <see cref="MatrixD"/> instance containing the result.</returns> public static MatrixD operator*(double s, MatrixD m) { return(MatrixD.Multiply(m, s)); }
/// <summary> /// Adds two matrices. /// </summary> /// <param name="a">A <see cref="MatrixD"/> instance.</param> /// <param name="b">A <see cref="MatrixD"/> instance.</param> /// <returns>A new <see cref="MatrixD"/> instance containing the sum.</returns> public static MatrixD operator+(MatrixD a, MatrixD b) { return(MatrixD.Add(a, b)); }
/// <summary> /// Negates the values of a matrix. /// </summary> /// <param name="m">A <see cref="MatrixD"/> instance.</param> /// <returns>A new <see cref="MatrixD"/> instance containing the result.</returns> public static MatrixD operator-(MatrixD m) { return(MatrixD.Negate(m)); }