Beispiel #1
0
        public static Tuple <Matrix, Vector> EigSymm(this ILinAlg ila, Matrix A)
        {
            var    AA   = ila.ToILMat(A);
            var    VVDD = ila.EigSymm(AA);
            Matrix V    = VVDD.Item1.ToMatrix();
            Vector D    = VVDD.Item2;

            AA.Dispose();
            VVDD.Item1.Dispose();
            GC.Collect();
            return(new Tuple <Matrix, Vector>(V, D));
        }
Beispiel #2
0
        public static ILinAlgMat HInv(this ILinAlg ila, ILinAlgMat AA, string invtype, params object[] invopt)
        {
            if (invtype == null)
            {
                invtype = "inv";
            }
            object optparam = null;

            switch (invtype)
            {
            case "inv":
                return(ila.Inv(AA));

            case "pinv":
                return(ila.PInv(AA));

            case "eig-zerothres":
            {
                var VVDD = ila.EigSymm(AA);

                double threshold = (double)invopt[0];
                for (int i = 0; i < VVDD.Item2.Length; i++)
                {
                    if (Math.Abs(VVDD.Item2[i]) < threshold)
                    {
                        VVDD.Item2[i] = 0;
                    }
                }

                optparam = VVDD;
            }
                goto case "eig-VVDD";

            case "eig-zerocount":
            {
                var VVDD = ila.EigSymm(AA);

                int zerocount = (int)invopt[0];
                for (int i = 0; i < zerocount; i++)
                {
                    VVDD.Item2[i] = 0;
                }

                optparam = VVDD;
            }
                goto case "eig-VVDD";

            case "eig-VVDD":
            {
                Tuple <ILinAlgMat, double[]> VVDD = optparam as Tuple <ILinAlgMat, double[]>;
                for (int i = 0; i < VVDD.Item2.Length; i++)
                {
                    if (VVDD.Item2[i] != 0)
                    {
                        VVDD.Item2[i] = 1 / VVDD.Item2[i];
                    }
                }
                var invAA = ila.Mul(VVDD.Item1, ila.ToILMat(VVDD.Item2).Diag(), VVDD.Item1.Tr);
                if (HDebug.IsDebuggerAttached)
                {
                    var check = AA * invAA;
                    check.Dispose();
                }
                VVDD.Item1.Dispose();
                return(invAA);
            }

            default:
                throw new NotImplementedException();
            }
        }