示例#1
0
文件: ILinAlg.cs 项目: htna/explsolv
        public ILinAlgMat Mul(params ILinAlgMat[] mats)
        {
            Func <ILinAlgMat[], ILinAlgMat> func = _Mul;

            if (HDebug.Selftest(func))
            {
                ILinAlgMat tA = ToILMat(new double[, ] {
                    { 1, 2 }, { 3, 4 }, { 5, 6 }
                });
                ILinAlgMat tB = ToILMat(new double[, ] {
                    { 7, 8 }, { 9, 1 }
                });
                ILinAlgMat tC = ToILMat(new double[, ] {
                    { 2, 3, 4 }, { 5, 6, 7 }
                });
                ILinAlgMat tR  = Mul(tA, tB, tC);
                ILinAlgMat tRR = ToILMat(new double[, ] {
                    { 100, 135, 170 }, { 254, 339, 424 }, { 408, 543, 678 }
                });
                double terr = (tR - tRR).ToArray().HAbs().HMax();
                if (terr > 0.00000001)
                {
                    Exit("Mul, (A * B * C) is wrong");
                }
            }
            return(func(mats));
        }
示例#2
0
文件: ILinAlg.cs 项目: htna/explsolv
        public ILinAlgMat Diag(ILinAlgMat mat)
        {
            Func <ILinAlgMat, ILinAlgMat> func = _Diag;

            if (HDebug.Selftest(func))
            {
                ILinAlgMat tA = ToILMat(new double[, ] {
                    { 1, 2, 3 }, { 2, 4, 5 }, { 3, 5, 6 }
                });
                ILinAlgMat tADiag0 = Diag(tA);
                ILinAlgMat tADiag1 = ToILMat(new double[] { 1, 4, 6 });
                double     tAerr   = (tADiag1 - tADiag0).ToArray().HAbs().HMax();
                if (tAerr > 0.00000001)
                {
                    Exit("Diag(A) is wrong");
                }

                ILinAlgMat tB      = ToILMat(new double[] { 1, 2, 3 });
                ILinAlgMat tBDiag0 = Diag(tB);
                ILinAlgMat tBDiag1 = ToILMat(new double[, ] {
                    { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 3 }
                });
                double tBerr = (tADiag1 - tADiag0).ToArray().HAbs().HMax();
                if (tBerr > 0.00000001)
                {
                    Exit("Diag(B) is wrong");
                }
            }
            return(func(mat));
        }
示例#3
0
文件: ILinAlg.cs 项目: htna/explsolv
        public Tuple <ILinAlgMat, double[]> EigSymm(ILinAlgMat A)
        {
            Func <ILinAlgMat, Tuple <ILinAlgMat, double[]> > func = _EigSymm;

            if (HDebug.Selftest(func))
            {
                ILinAlgMat tA = ToILMat(new double[, ] {
                    { 1, 2, 3 }, { 2, 4, 5 }, { 3, 5, 6 }
                });
                var        tVD = EigSymm(tA);
                ILinAlgMat tV  = tVD.Item1;
                double[]   tD  = tVD.Item2;
                ILinAlgMat tDD = Diag(ToILMat(tD));

                ILinAlgMat tAA = Mul(tV, tDD, tV.Tr);
                double     err = (tAA - tA).ToArray().HAbs().HMax();
                if (err > 0.00000001)
                {
                    Exit("EigSymm, A != V D V'");
                }
                ILinAlgMat tV_Vt = Mul(tV, tV.Tr);
                ILinAlgMat tI    = Eye(3);
                err = (tV_Vt - tI).ToArray().HAbs().HMax();
                if (err > 0.00000001)
                {
                    Exit("EigSymm, I != V V'");
                }
            }
            return(func(A));
        }
示例#4
0
文件: ILinAlg.cs 项目: htna/explsolv
        public ILinAlgMat PInv(ILinAlgMat A)
        {
            Func <ILinAlgMat, ILinAlgMat> func = _PInv;

            if (HDebug.Selftest(func))
            {
                ILinAlgMat tA = ToILMat(new double[, ] {
                    { 1, 2, 3 }, { 2, 3, 5 }, { 3, 4, 5 }
                });
                ILinAlgMat tiA  = PInv(tA);
                ILinAlgMat tiAA = ToILMat(new double[, ] {
                    { -2.5, 1.0, 0.5 }, { 2.5, -2.0, 0.5 }, { -0.5, 1.0, -0.5 }
                });
                double err0 = (tiA - tiAA).ToArray().HAbs().HMax();
                if (err0 > 0.00000001)
                {
                    Exit("Pinv, is incorrect");
                }
                ILinAlgMat tI   = Eye(3);
                double     err1 = (tA * tiA - tI).ToArray().HAbs().HMax();
                if (err1 > 0.00000001)
                {
                    Exit("Pinv, A * invA != I");
                }
                double err2 = (tiA * tA - tI).ToArray().HAbs().HMax();
                if (err2 > 0.00000001)
                {
                    Exit("Pinv, invA * A != I");
                }
            }
            return(func(A));
        }
示例#5
0
 public override ILinAlgMat _Inv(ILinAlgMat A)
 {
     using (new Matlab.NamedLock("LA"))
     {
         Matlab.PutMatrix("LA.A", A.ToArray());
         CMatrix inv = Matlab.GetMatrix("inv(LA.A)");
         Matlab.Clear();
         return(inv);
     }
 }
示例#6
0
 public override double _Det(ILinAlgMat mat)
 {
     using (new Matlab.NamedLock("LA"))
     {
         Matlab.PutMatrix("LA.A", mat.ToArray());
         double det = Matlab.GetValue("det(LA.A)");
         Matlab.Clear();
         return(det);
     }
 }
示例#7
0
 public override ILinAlgMat _Diag(ILinAlgMat mat)
 {
     using (new Matlab.NamedLock("LA"))
     {
         Matlab.PutMatrix("LA.A", mat.ToArray());
         CMatrix diag = Matlab.GetMatrix("diag(LA.A)");
         Matlab.Clear();
         return(diag);
     }
 }
示例#8
0
 public override ILinAlgMat _LinSolve(ILinAlgMat A, ILinAlgMat B)
 {
     using (new Matlab.NamedLock("LA"))
     {
         Matlab.PutMatrix("LA.A", A.ToArray());
         Matlab.PutMatrix("LA.B", B.ToArray());
         CMatrix X = Matlab.GetMatrix(@"LA.A \ LA.B"); /// A X = B   =>   X = A\B
         Matlab.Clear();
         return(X);
     }
 }
示例#9
0
        public static Matrix HInv(this ILinAlg ila, Matrix A, string invtype, params object[] invopt)
        {
            Matrix invA;

            {
                ILinAlgMat AA    = ila.ToILMat(A);
                ILinAlgMat invAA = ila.HInv(AA, invtype, invopt);
                invA = invAA.ToMatrix();
                AA.Dispose();
                invAA.Dispose();
            }
            GC.Collect();
            return(invA);
        }
示例#10
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);
        }
示例#11
0
文件: ILinAlg.cs 项目: htna/explsolv
        public double Det(ILinAlgMat mat)
        {
            Func <ILinAlgMat, double> func = _Det;

            if (HDebug.Selftest(func))
            {
                ILinAlgMat tA = ToILMat(new double[, ] {
                    { 1, 2, 3 }, { 2, 4, 5 }, { 3, 5, 6 }
                });
                double tdet = Det(tA);
                if (Math.Abs(tdet - (-1.0)) > 0.00000001)
                {
                    Exit("Det(A) is wrong");
                }
            }
            return(func(mat));
        }
示例#12
0
 public override Tuple <ILinAlgMat, double[]> _EigSymm(ILinAlgMat A)
 {
     using (new Matlab.NamedLock("LA"))
     {
         Matlab.PutMatrix("LA.A", A.ToArray());
         Matlab.Execute("LA.A = (LA.A + LA.A)/2;");
         Matlab.Execute("[LA.V, LA.D] = eig(LA.A);");
         Matlab.Execute("LA.D = diag(LA.D);");
         CMatrix  V = Matlab.GetMatrix("LA.V");
         double[] D = Matlab.GetVector("LA.D");
         if (HDebug.IsDebuggerAttached)
         {
             Matlab.Execute("LA.ERR = LA.A - (LA.V * diag(LA.D) * LA.V');");
             Matlab.Execute("LA.ERR = max(max(abs(LA.ERR)));");
             double err = Matlab.GetValue("LA.ERR");
             HDebug.AssertTolerance(0.00000001, err);
         }
         Matlab.Clear();
         return(new Tuple <ILinAlgMat, double[]>(V, D));
     }
 }
示例#13
0
文件: ILinAlg.cs 项目: htna/explsolv
        public ILinAlgMat InvSymm(ILinAlgMat A)
        {
            Func <ILinAlgMat, ILinAlgMat> func = _InvSymm;

            if (HDebug.Selftest(func))
            {
                ILinAlgMat tA = ToILMat(new double[, ] {
                    { 1, 2, 3 }, { 2, 4, 5 }, { 3, 5, 6 }
                });
                ILinAlgMat tI = ToILMat(new double[, ] {
                    { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
                });
                ILinAlgMat tInvA = func(tA);
                double     err   = (tInvA * tA - tI).ToArray().HAbs().HMax();
                if (err > 0.00000001)
                {
                    Exit("LinSolve, is incorrect");
                }
            }
            return(func(A));
        }
示例#14
0
文件: ILinAlg.cs 项目: htna/explsolv
        public ILinAlgMat LinSolve(ILinAlgMat A, ILinAlgMat B)
        {
            Func <ILinAlgMat, ILinAlgMat, ILinAlgMat> func = _LinSolve;

            if (HDebug.Selftest(func))
            {
                ILinAlgMat tA = ToILMat(new double[, ] {
                    { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }
                });
                ILinAlgMat tB = ToILMat(new double[, ] {
                    { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 }
                });
                ILinAlgMat tX  = _LinSolve(tA, tB);
                double     err = (tA * tX - tB).ToArray().HAbs().HMax();
                if (err > 0.00000001)
                {
                    Exit("LinSolve, is incorrect");
                }
            }
            return(func(A, B));
        }
示例#15
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();
            }
        }
示例#16
0
文件: ILinAlg.cs 项目: htna/explsolv
 public abstract double _Det(ILinAlgMat mat);
示例#17
0
文件: ILinAlg.cs 项目: htna/explsolv
 public abstract ILinAlgMat _InvSymm(ILinAlgMat A);
示例#18
0
文件: ILinAlg.cs 项目: htna/explsolv
 public abstract ILinAlgMat _Inv(ILinAlgMat A);
示例#19
0
文件: ILinAlg.cs 项目: htna/explsolv
 public abstract ILinAlgMat _LinSolve(ILinAlgMat A, ILinAlgMat B);
示例#20
0
文件: ILinAlg.cs 项目: htna/explsolv
 public abstract ILinAlgMat _Diag(ILinAlgMat mat);
示例#21
0
文件: ILinAlg.cs 项目: htna/explsolv
 public abstract Tuple <ILinAlgMat, double[]> _EigSymm(ILinAlgMat A);
示例#22
0
 public override ILinAlgMat op_Mul(/*ILinAlgMat mat1, */ ILinAlgMat mat2)
 {
     return(new CMatrix(matrix * (mat2 as CMatrix).matrix));
 }
示例#23
0
文件: ILinAlg.cs 项目: htna/explsolv
 public abstract ILinAlgMat op_Mul(/*ILinAlgMat mat1, */ ILinAlgMat mat2);