Esempio n. 1
0
        internal static T DeterminantLaplace(GenTensor <T, TWrapper> t, int diagLength)
        {
            if (diagLength == 1)
            {
                return(t.GetValueNoCheck(0, 0));
            }
            var det  = default(TWrapper).CreateZero();
            var sign = default(TWrapper).CreateOne();
            var temp = SquareMatrixFactory <T, TWrapper> .GetMatrix(diagLength - 1);

            for (int i = 0; i < diagLength; i++)
            {
                Inversion <T, TWrapper> .GetCofactorMatrix(t, temp, 0, i, diagLength);

                det = default(TWrapper).Add(det,
                                            default(TWrapper).Multiply(
                                                sign,
                                                default(TWrapper).Multiply(
                                                    t.GetValueNoCheck(0, i),
                                                    DeterminantLaplace(temp, diagLength - 1)
                                                    ))
                                            );
                sign = default(TWrapper).Negate(sign);
            }
            return(det);
        }
Esempio n. 2
0
        public static GenTensor <T, TWrapper> Adjoint(GenTensor <T, TWrapper> t)
        {
            #if ALLOW_EXCEPTIONS
            if (!t.IsSquareMatrix)
            {
                throw new InvalidShapeException("Matrix should be square");
            }
            #endif
            var diagLength = t.Shape.shape[0];

            if (diagLength is 1)
            {
                return(GenTensor <T, TWrapper> .CreateIdentityMatrix(1));
            }

            var res = GenTensor <T, TWrapper> .CreateSquareMatrix(diagLength);

            var temp = SquareMatrixFactory <T, TWrapper> .GetMatrix(diagLength - 1);

            if (diagLength == 1)
            {
                res.SetValueNoCheck(default(TWrapper).CreateOne(), 0, 0);
                return(res);
            }

            var toNegate = false;

            for (int x = 0; x < diagLength; x++)
            {
                for (int y = 0; y < diagLength; y++)
                {
                    GetCofactorMatrix(t, temp, x, y, diagLength);

                    var cofactor = Determinant <T, TWrapper> .DeterminantGaussianSafeDivision(temp, diagLength - 1);

                    // TODO: is this statement correct?
                    toNegate = (x + y) % 2 == 1;
                    var minor = toNegate ? default(TWrapper).Negate(cofactor) : cofactor;

                    res.SetValueNoCheck(minor, y, x);
                }
            }

            return(res);
        }