Exemplo n.º 1
0
            protected override bool InvEigImpl(Matrix mat, double?thresEigval, int?numZeroEigval, out Matrix inv, InfoPack extra)
            {
                NamedLock.FuncO <Matrix, bool> func = delegate(out Matrix linv)
                {
                    Matlab.Clear("HTLib2_Matlab_InvEigImpl");
                    Matlab.PutMatrix("HTLib2_Matlab_InvEigImpl.A", mat.ToArray());
                    Matlab.PutValue("HTLib2_Matlab_InvEigImpl.ze", numZeroEigval.GetValueOrDefault(0));
                    Matlab.PutValue("HTLib2_Matlab_InvEigImpl.th", Math.Abs(thresEigval.GetValueOrDefault(0)));
                    Matlab.Execute("[HTLib2_Matlab_InvEigImpl.V, HTLib2_Matlab_InvEigImpl.D] = eig(HTLib2_Matlab_InvEigImpl.A);");
                    Matlab.Execute("HTLib2_Matlab_InvEigImpl.D = diag(HTLib2_Matlab_InvEigImpl.D);");
                    Matlab.Execute("HTLib2_Matlab_InvEigImpl.sortAbsD = sort(abs(HTLib2_Matlab_InvEigImpl.D));");
                    Matlab.Execute("HTLib2_Matlab_InvEigImpl.sortAbsD0 = [0; HTLib2_Matlab_InvEigImpl.sortAbsD];"); // add zero to the first list, for the case ze=0 (null)
                    Matlab.Execute("HTLib2_Matlab_InvEigImpl.ze  = HTLib2_Matlab_InvEigImpl.sortAbsD0(HTLib2_Matlab_InvEigImpl.ze+1);");
                    Matlab.Execute("HTLib2_Matlab_InvEigImpl.th  = max(HTLib2_Matlab_InvEigImpl.th, HTLib2_Matlab_InvEigImpl.ze);");
                    Matlab.Execute("HTLib2_Matlab_InvEigImpl.idx = abs(HTLib2_Matlab_InvEigImpl.D) <= HTLib2_Matlab_InvEigImpl.th;");
                    Matlab.Execute("HTLib2_Matlab_InvEigImpl.invD = ones(size(HTLib2_Matlab_InvEigImpl.D)) ./ HTLib2_Matlab_InvEigImpl.D;");
                    Matlab.Execute("HTLib2_Matlab_InvEigImpl.invD(HTLib2_Matlab_InvEigImpl.idx) = 0;");
                    Matlab.Execute("HTLib2_Matlab_InvEigImpl.invD = diag(HTLib2_Matlab_InvEigImpl.invD);");
                    Matlab.Execute("HTLib2_Matlab_InvEigImpl.invA = HTLib2_Matlab_InvEigImpl.V * HTLib2_Matlab_InvEigImpl.invD * inv(HTLib2_Matlab_InvEigImpl.V);");
                    linv = Matlab.GetMatrix("HTLib2_Matlab_InvEigImpl.invA");
                    if (extra != null)
                    {
                        int num_zero_eigvals = Matlab.GetValueInt("sum(HTLib2_Matlab_InvEigImpl.idx)");
                        HDebug.AssertIf(numZeroEigval != null, numZeroEigval.GetValueOrDefault() <= num_zero_eigvals);
                        extra["num_zero_eigvals"] = num_zero_eigvals;
                        extra["eigenvalues"]      = Matlab.GetVector("HTLib2_Matlab_InvEigImpl.D");
                    }
                    Matlab.Clear("HTLib2_Matlab_InvEigImpl");

                    return(true);
                };
                //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.InvEigImpl(Matrix, double?, int?, out Matrix, InfoPack)", func, out inv);
                return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_InvEigImpl"), func, out inv));
            }
Exemplo n.º 2
0
 protected override bool QuadraticProgrammingConstrainedImpl(out Vector x
                                                             , Matrix H, Vector f
                                                             , Matrix A       = null, Vector b   = null
                                                             , Matrix Aeq     = null, Vector beq = null
                                                             , Vector lb      = null, Vector ub  = null
                                                             , Vector x0      = null
                                                             , string options = null
                                                             )
 {
     using (new Matlab.NamedLock("NUMSLV"))
     {
         Matlab.Clear("NUMSLV");
         Matlab.PutMatrix("NUMSLV.H", H.ToArray());
         Matlab.PutVector("NUMSLV.f", f.ToArray());
         Matlab.Execute("NUMSLV.A   = [];"); if (A != null)
         {
             Matlab.PutMatrix("NUMSLV.A", A.ToArray());
         }
         Matlab.Execute("NUMSLV.b   = [];"); if (b != null)
         {
             Matlab.PutVector("NUMSLV.b", b.ToArray());
         }
         Matlab.Execute("NUMSLV.Aeq = [];"); if (Aeq != null)
         {
             Matlab.PutMatrix("NUMSLV.Aeq", Aeq.ToArray());
         }
         Matlab.Execute("NUMSLV.beq = [];"); if (beq != null)
         {
             Matlab.PutVector("NUMSLV.beq", beq.ToArray());
         }
         Matlab.Execute("NUMSLV.lb  = [];"); if (lb != null)
         {
             Matlab.PutVector("NUMSLV.lb", lb.ToArray());
         }
         Matlab.Execute("NUMSLV.ub  = [];"); if (ub != null)
         {
             Matlab.PutVector("NUMSLV.ub", ub.ToArray());
         }
         Matlab.Execute("NUMSLV.x0  = [];"); if (x0 != null)
         {
             Matlab.PutVector("NUMSLV.x0", x0.ToArray());
         }
         if (options == null)
         {
             options = "[]";
         }
         Matlab.Execute("NUMSLV.x = quadprog(NUMSLV.H, NUMSLV.f, NUMSLV.A, NUMSLV.b, NUMSLV.Aeq, NUMSLV.beq, NUMSLV.lb, NUMSLV.ub, NUMSLV.x0, " + options + ");", true);
         x = Matlab.GetVector("NUMSLV.x");
         Matlab.Clear("NUMSLV");
     }
     return(true);
 }
Exemplo n.º 3
0
 protected override bool LeastSquareConstrainedImpl(out Vector x
                                                    , Matrix C, Vector d
                                                    , Matrix A       = null, Vector b   = null
                                                    , Matrix Aeq     = null, Vector beq = null
                                                    , Vector lb      = null, Vector ub  = null
                                                    , Vector x0      = null
                                                    , string options = null
                                                    )
 {
     using (new Matlab.NamedLock("NUMSLV"))
     {
         Matlab.Clear("NUMSLV");
         Matlab.PutMatrix("NUMSLV.C", C.ToArray());
         Matlab.PutVector("NUMSLV.d", d.ToArray());
         Matlab.Execute("NUMSLV.A   = [];"); if (A != null)
         {
             Matlab.PutMatrix("NUMSLV.A", A.ToArray());
         }
         Matlab.Execute("NUMSLV.b   = [];"); if (b != null)
         {
             Matlab.PutVector("NUMSLV.b", b.ToArray());
         }
         Matlab.Execute("NUMSLV.Aeq = [];"); if (Aeq != null)
         {
             Matlab.PutMatrix("NUMSLV.Aeq", Aeq.ToArray());
         }
         Matlab.Execute("NUMSLV.beq = [];"); if (beq != null)
         {
             Matlab.PutVector("NUMSLV.beq", beq.ToArray());
         }
         Matlab.Execute("NUMSLV.lb  = [];"); if (lb != null)
         {
             Matlab.PutVector("NUMSLV.lb", lb.ToArray());
         }
         Matlab.Execute("NUMSLV.ub  = [];"); if (ub != null)
         {
             Matlab.PutVector("NUMSLV.ub", ub.ToArray());
         }
         Matlab.Execute("NUMSLV.x0  = [];"); if (x0 != null)
         {
             Matlab.PutVector("NUMSLV.x0", x0.ToArray());
         }
         if (options == null)
         {
             options = "[]";
         }
         Matlab.Execute("NUMSLV.x = lsqlin(NUMSLV.C, NUMSLV.d, NUMSLV.A, NUMSLV.b, NUMSLV.Aeq, NUMSLV.beq, NUMSLV.lb, NUMSLV.ub, NUMSLV.x0, " + options + ");", true);
         x = Matlab.GetVector("NUMSLV.x");
         Matlab.Clear("NUMSLV");
     }
     return(true);
 }
Exemplo n.º 4
0
 protected override bool SvdImpl(Matrix X, out Matrix U, out Vector S, out Matrix V)
 {
     using (new Matlab.NamedLock("NUMSLV"))
     {
         Matlab.Clear("NUMSLV");
         Matlab.PutMatrix("NUMSLV.X", X.ToArray());
         Matlab.Execute("[NUMSLV.U, NUMSLV.S, NUMSLV.V] = svd(NUMSLV.X);");
         Matlab.Execute("NUMSLV.S = diag(NUMSLV.S);");
         U = Matlab.GetMatrix("NUMSLV.U");
         S = Matlab.GetVector("NUMSLV.S");
         V = Matlab.GetMatrix("NUMSLV.V");
         Matlab.Clear("NUMSLV");
     }
     return(true);
 }
Exemplo n.º 5
0
 protected override bool EigImpl(Matrix A, Matrix B, out Matrix eigvec, out Vector eigval)
 {
     NamedLock.FuncOO <Matrix, Vector, bool> func = delegate(out Matrix leigvec, out Vector leigval)
     {
         Matlab.Clear("HTLib2_Matlab_EigImpl");
         Matlab.PutMatrix("HTLib2_Matlab_EigImpl.A", A.ToArray());
         Matlab.PutMatrix("HTLib2_Matlab_EigImpl.B", B.ToArray());
         Matlab.Execute("[HTLib2_Matlab_EigImpl.V, HTLib2_Matlab_EigImpl.D] = eig(HTLib2_Matlab_EigImpl.A, HTLib2_Matlab_EigImpl.B);");
         Matlab.Execute("HTLib2_Matlab_EigImpl.D = diag(HTLib2_Matlab_EigImpl.D);");
         leigvec = Matlab.GetMatrix("HTLib2_Matlab_EigImpl.V");
         leigval = Matlab.GetVector("HTLib2_Matlab_EigImpl.D");
         Matlab.Clear("HTLib2_Matlab_EigImpl");
         return(true);
     };
     //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.EigImpl(Matrix, Matrix, out Matrix, out Vector)", func, out eigvec, out eigval);
     return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_EigImpl"), func, out eigvec, out eigval));
 }
Exemplo n.º 6
0
 protected override bool EigImpl(Matrix mat, out Matrix eigvec, out Vector eigval)
 {
     NamedLock.FuncOO <Matrix, Vector, bool> func = delegate(out Matrix leigvec, out Vector leigval)
     {
         bool bUseFile = (mat.ColSize * mat.ColSize > 1000 * 1000);
         Matlab.Clear("HTLib2_Matlab_EigImpl");
         Matlab.PutMatrix("HTLib2_Matlab_EigImpl.A", mat.ToArray(), bUseFile);
         Matlab.Execute("[HTLib2_Matlab_EigImpl.V, HTLib2_Matlab_EigImpl.D] = eig(HTLib2_Matlab_EigImpl.A);");
         Matlab.Execute("HTLib2_Matlab_EigImpl.D = diag(HTLib2_Matlab_EigImpl.D);");
         leigvec = Matlab.GetMatrix("HTLib2_Matlab_EigImpl.V", bUseFile);
         leigval = Matlab.GetVector("HTLib2_Matlab_EigImpl.D");
         Matlab.Clear("HTLib2_Matlab_EigImpl");
         return(true);
     };
     //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.EigImpl(Matrix, out Matrix, out Vector)", func, out eigvec, out eigval);
     return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_EigImpl"), func, out eigvec, out eigval));
 }
Exemplo n.º 7
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));
     }
 }
Exemplo n.º 8
0
        public static object LeastSquare
            (double[,] As, double[] bs
            , bool opt_get_stat = false
            )
        {
            if (HDebug.Selftest())
            {
                /// >> A = [ 1,3,2, 1 ; 4,5,6, 1 ; 7,9,9, 1 ; 11,11,12, 1 ; 13,16,15, 1 ]
                /// >> b = [1, 4, 6, 9, 12]'
                /// >> x = inv(A' * A) * (A' * b)
                ///     0.2171
                ///     0.2125
                ///     0.4205
                ///    -0.7339
                /// >> esti = A * x
                ///     0.9619
                ///     3.7203
                ///     6.4832
                ///     9.0381
                ///    11.7965
                /// >> corr(esti,b)
                ///     0.9976
                /// >> mean( (b-esti).^2 )
                ///     0.0712
                double[,] _A = new double[5, 3] {
                    { 1, 3, 2 }, { 4, 5, 6 }, { 7, 9, 9 }, { 11, 11, 12 }, { 13, 16, 15 }
                };
                double[] _b = new double[5] {
                    1, 4, 6, 9, 12
                };
                dynamic _out = LeastSquare(_A, _b, true);

                double   _matlab_corr = 0.9976;
                double   _matlab_mse  = 0.0712;
                double[] _matlab_x    = new double[] { 0.2171, 0.2125, 0.4205, -0.7339 };
                double[] _matlab_esti = new double[] { 0.9619, 3.7203, 6.4832, 9.0381, 11.7965 };

                double err1 = Math.Abs(_matlab_corr - _out.opt_estimation_corr);
                double err2 = Math.Abs(_matlab_mse - _out.opt_mean_square_err);
                double err3 = (_matlab_x - (Vector)_out.x).ToArray().MaxAbs();
                double err4 = (_matlab_esti - (Vector)_out.opt_estimation).ToArray().MaxAbs();

                HDebug.Assert(err1 < 0.0001);
                HDebug.Assert(err2 < 0.0001);
                HDebug.Assert(err3 < 0.0001);
                HDebug.Assert(err4 < 0.0001);
            }
            /// => A x = b
            ///
            /// => At A x = At b
            ///
            /// => AA * x = Ab
            /// => x = inv(AA) * Ab
            HDebug.Assert(As.GetLength(0) == bs.Length);
            int n = As.GetLength(0);
            int k = As.GetLength(1);

            Matrix A = Matrix.Zeros(n, k + 1);

            for (int c = 0; c < n; c++)
            {
                for (int r = 0; r < k; r++)
                {
                    A[c, r] = As[c, r];
                }
                A[c, k] = 1;
            }

            Matrix AA = LinAlg.MtM(A, A);
            Vector Ab = LinAlg.MtV(A, bs);

            Vector x;

            switch (k + 1)
            {
            case 2: { Matrix invAA = LinAlg.Inv2x2(AA.ToArray()); x = LinAlg.MV(invAA, Ab); } break;

            case 3: { Matrix invAA = LinAlg.Inv3x3(AA.ToArray()); x = LinAlg.MV(invAA, Ab); } break;

            case 4: { Matrix invAA = LinAlg.Inv4x4(AA.ToArray()); x = LinAlg.MV(invAA, Ab); } break;

            default:
                Matlab.PutMatrix("LinAlg_LeastSquare.AA", AA);
                Matlab.PutVector("LinAlg_LeastSquare.Ab", Ab);
                Matlab.Execute("LinAlg_LeastSquare.AA = inv(LinAlg_LeastSquare.AA);");
                Matlab.Execute("LinAlg_LeastSquare.x = LinAlg_LeastSquare.AA * LinAlg_LeastSquare.Ab;");
                x = Matlab.GetVector("LinAlg_LeastSquare.x");
                Matlab.Execute("clear LinAlg_LeastSquare;");
                break;
            }

            double?opt_mean_square_err = null;
            double?opt_estimation_corr = null;
            Vector opt_estimation      = null;

            if (opt_get_stat)
            {
                opt_estimation = new double[n];
                double avg_err2 = 0;
                for (int i = 0; i < n; i++)
                {
                    double esti = 0;
                    for (int j = 0; j < k; j++)
                    {
                        esti += As[i, j] * x[j];
                    }
                    esti += x[k];

                    opt_estimation[i] = esti;
                    avg_err2         += (esti - bs[i]) * (esti - bs[i]);
                }
                avg_err2 /= n;

                opt_mean_square_err = avg_err2;
                opt_estimation_corr = HMath.HCorr(opt_estimation, bs);
            }

            return(new
            {
                x = x,
                /// optional outputs
                opt_mean_square_err = opt_mean_square_err,
                opt_estimation_corr = opt_estimation_corr,
                opt_estimation = opt_estimation,
            });
        }