/// <summary> /// Calculate the determinant of a Matrix /// </summary> /// <returns>The determinant</returns> /// <exception cref="GeometryException">Thrown, if the Matrix is not a Square Matrix</exception> /// <remarks>Based on code by Rajitha Wimalasooriya (http://www.thecodeproject.com/csharp/rtwmatrix.asp)</remarks> public double Determinant() { if (this.Rows != this.Columns) { throw new GeometryException("You can only compute the Determinant of a Square Matrix. (" + ToString() + ")"); } double d = 0; //get the determinent of a 2x2 matrix if (this.Rows == 2 && this.Columns == 2) { d = (this[0, 0] * this[1, 1]) - (this[0, 1] * this[1, 0]); return(d); } //get the determinent of a 3x3 matrix if (this.Rows == 3 && this.Columns == 3) { d = (this[0, 0] * this[1, 1] * this[2, 2]) + (this[0, 1] * this[1, 2] * this[2, 0]) + (this[0, 2] * this[1, 0] * this[2, 1]) - (this[0, 2] * this[1, 1] * this[2, 0]) - (this[0, 1] * this[1, 0] * this[2, 2]) - (this[0, 0] * this[1, 2] * this[2, 1]); return(d); } Matrixd tempMtx = new Matrixd(this.Rows - 1, this.Columns - 1); //find the determinent with respect to the first row for (int j = 0; j < this.Columns; j++) { tempMtx = this.Minor(0, j); //recursively add the determinents d += (int)Math.Pow(-1, j) * this[0, j] * tempMtx.Determinant(); } return(d); }
/// <summary> /// Calculate the Adjoint of a Matrix /// </summary> /// <returns>The adjoint</returns> /// <exception cref="GeometryException">Thrown if <see cref="Rows"/> or <see cref="Columns"/> is less than 2.</exception> /// <remarks>Based on code by Rajitha Wimalasooriya (http://www.thecodeproject.com/csharp/rtwmatrix.asp)</remarks> public Matrixd Adjoint() { if (this.Rows < 2 || this.Columns < 2) { throw new GeometryException("Adjoint matrix is not available. (" + ToString() + ")"); } Matrixd tempMtx = new Matrixd(this.Rows - 1, this.Columns - 1); Matrixd adjMtx = new Matrixd(this.Columns, this.Rows); for (int i = 0; i < this.Rows; i++) { for (int j = 0; j < this.Columns; j++) { tempMtx = this.Minor(i, j); //put the determinent of the minor in the transposed position adjMtx[j, i] = (int)Math.Pow(-1, i + j) * tempMtx.Determinant(); } } return(adjMtx); }