internal T DeterminantLaplace(int diagLength) { if (diagLength == 1) { return(ConstantsAndFunctions <T> .Forward(this.GetValueNoCheck(0, 0))); } var det = ConstantsAndFunctions <T> .CreateZero(); var sign = ConstantsAndFunctions <T> .CreateOne(); var temp = SquareMatrixFactory <T> .GetMatrix(diagLength); for (int i = 0; i < diagLength; i++) { GetCofactor(this, temp, 0, i, diagLength); det = ConstantsAndFunctions <T> .Add(det, ConstantsAndFunctions <T> .Multiply( sign, ConstantsAndFunctions <T> .Multiply( this.GetValueNoCheck(0, i), temp.DeterminantLaplace(diagLength - 1) )) ); sign = ConstantsAndFunctions <T> .Negate(sign); } return(det); }
/// <summary> /// Returns adjugate matrix /// /// O(N^5) /// </summary> public GenTensor <T> Adjoint() { #if ALLOW_EXCEPTIONS if (!IsSquareMatrix) { throw new InvalidShapeException("Matrix should be square"); } #endif var diagLength = Shape.shape[0]; var res = GenTensor <T> .CreateSquareMatrix(diagLength); var temp = SquareMatrixFactory <T> .GetMatrix(diagLength); if (diagLength == 1) { res.SetValueNoCheck(ConstantsAndFunctions <T> .CreateOne(), 0, 0); return(res); } var toNegate = false; for (int x = 0; x < diagLength; x++) { for (int y = 0; y < diagLength; y++) { GetCofactor(this, temp, x, y, diagLength); toNegate = (x + y) % 2 == 1; var det = temp.DeterminantGaussianSafeDivision(diagLength - 1); if (toNegate) { res.SetValueNoCheck(ConstantsAndFunctions <T> .Negate(det), y, x); } else { res.SetValueNoCheck(det, y, x); } } } return(res); }