/// <summary> /// Constructs translation matrix for a vector in R x R /// </summary> /// <param name="x"> X coordinate. </param> /// <param name="y"> Y coordinate.</param> /// <returns> Returns translation matrix. </returns> public static Matrix Translation(double x, double y) { Matrix translatioMatrix = new Matrix(Matrix.IdentityMatrix(3)); translatioMatrix[0, 2] = x; translatioMatrix[1, 2] = y; return(translatioMatrix); }
/// <summary> /// Constructs inverse matrix for instance if the latter is invertible. /// </summary> /// <returns> Returns inverse matrix. </returns> public Matrix Inverse() { if (!this.IsSquare()) { throw new Exception("Matrix must be square to have inverse matrix."); } Matrix res = new Matrix(this); //this is identity matrix //which will be transformed to inverse matrix //if the given matrix is invertible Matrix idM = new Matrix(Matrix.IdentityMatrix(res.Rows)); double temp; int i, j; //performing matrix inverse using Gauss-Jordan method for (j = 0; j < this.Rows; j++) { for (i = j; i < this.Rows; i++) { if (res[i, j] != 0) { for (int k = 0; k < this.Rows; k++) { temp = res[j, k]; res[j, k] = res[i, k]; res[i, k] = temp; temp = idM[j, k]; idM[j, k] = idM[i, k]; idM[i, k] = temp; } temp = 1 / res[j, j]; for (int k = 0; k < this.Rows; k++) { res[j, k] *= temp; idM[j, k] *= temp; } for (int l = 0; l < this.Rows; l++) { if (l != j) { temp = (-res[l, j]); for (int k = 0; k < this.Rows; k++) { res[l, k] = res[l, k] + temp * res[j, k]; idM[l, k] = idM[l, k] + temp * idM[j, k]; } } } break; } //if detected a row with 0s throw exception //because in that case matrix is non-invertible if (res[i, j] == 0) { throw new Exception("Non-invertible matrix."); } } } return(idM); }
/// <summary> /// Constructs translation matrix for a vector in R x R x R /// </summary> /// <param name="x"> X coordinate. </param> /// <param name="y"> Y coordinate. </param> /// <param name="z"> Z coordinate.</param> /// <returns> Returns translation matrix. </returns> public static Matrix Translation(double x, double y, double z) { Matrix translationMatrix = new Matrix(Matrix.IdentityMatrix(4)); translationMatrix[0, 3] = x; translationMatrix[1, 3] = y; translationMatrix[2, 3] = z; return(translationMatrix); }
/// <summary> /// Constructs translation matrix for a vector in R x R x ... x R /// </summary> /// <param name="VecCoord"> Coordinates for translation. </param> /// <returns> Returns translation matrix. </returns> public static Matrix Translation(params double[] VecCoord) { Matrix translationMatrix = Matrix.IdentityMatrix(VecCoord.Length + 1); for (int i = 0; i < VecCoord.Length; i++) { translationMatrix[i, VecCoord.Length] = VecCoord[i]; } return(translationMatrix); }
/// <summary> /// Checks if matrix is orthogonal. /// </summary> /// <returns> Returns true if matrix is orthogonal;otherwis:false. </returns> public bool IsOrthogonal() { if (this.IsSquare()) { Matrix res = new Matrix(this); //Matrix is orthogonal if and only if it is square matrix //and the multiplication of matrix and its transpose matrix is identity matrix //calculating the multiplication res = res.Mul(res.Transpose()); return(res.Equals(Matrix.IdentityMatrix(res.Rows))); } else { //if matrix is not square matrix return false return(false); } }