Beispiel #1
0
        public static Matrix Mul(this ILinAlg ila, params Matrix[] mats)
        {
            ILinAlgMat[] MATS = new ILinAlgMat[mats.Length];
            for (int i = 0; i < mats.Length; i++)
            {
                MATS[i] = ila.ToILMat(mats[i]);
            }
            ILinAlgMat MUL = ila.Mul(MATS);
            Matrix     mul = ToMatrix(MUL);

            MUL.Dispose();
            foreach (var MAT in MATS)
            {
                MAT.Dispose();
            }
            return(mul);
        }
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();
            }
        }