Exemple #1
0
        public static void Call(string functionbody, string retval, params string[] parvals)
        {
            string currdir = HEnvironment.CurrentDirectory;

            {
                string funcpath = System.IO.Path.GetTempFileName();
                System.IO.File.WriteAllText(funcpath, functionbody);
                System.IO.FileInfo finfo = new System.IO.FileInfo(funcpath);
                HEnvironment.CurrentDirectory = finfo.DirectoryName;
                {
                    string command = finfo.Name + "(";
                    foreach (string parval in parvals)
                    {
                        command += "," + parval;
                    }
                    command = command.Replace("(,", "(");
                    command = command + ");";
                    if (retval != null)
                    {
                        command = retval + " = " + command;
                    }
                    Matlab.Execute(command);
                }
                System.IO.File.Delete(funcpath);
            }
            HEnvironment.CurrentDirectory = currdir;
        }
            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));
            }
Exemple #3
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);
     }
 }
Exemple #4
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);
     }
 }
Exemple #5
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);
     }
 }
Exemple #6
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);
     }
 }
 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);
 }
 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);
 }
Exemple #9
0
        static void Main(string[] args)
        {
            double x = 0;

            for (int i = 0; i < 100000; i++)
            {
                Matlab.PutValue("x", x);
                Matlab.Execute("x=x+1;");
                x = Matlab.GetValue("x");
                //matlab.Execute("clear x");
                System.Console.WriteLine(x);
            }
        }
Exemple #10
0
 protected override bool RankImpl(Matrix mat, out int rank)
 {
     NamedLock.FuncO <int, bool> func = delegate(out int lrank)
     {
         string varname = "HTLib2_Matlab_RankImpl";
         Matlab.Clear(varname);
         Matlab.PutMatrix(varname, mat.ToArray());
         lrank = Matlab.GetValueInt("rank(" + varname + ")");
         Matlab.Clear(varname);
         return(true);
     };
     //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.RankImpl(Matrix, out int)", func, out rank);
     return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_RankImpl"), func, out rank));
 }
Exemple #11
0
 public override ILinAlgMat _Mul(params ILinAlgMat[] mats)
 {
     using (new Matlab.NamedLock("LA"))
     {
         Matlab.PutMatrix("LA.mul", mats[0].ToArray(), true);
         for (int i = 1; i < mats.Length; i++)
         {
             Matlab.PutMatrix("LA.tmp", mats[i].ToArray(), true);
             Matlab.Execute("LA.mul = LA.mul * LA.tmp;");
         }
         CMatrix mul = Matlab.GetMatrix("LA.mul", true);
         Matlab.Clear();
         return(mul);
     }
 }
Exemple #12
0
 protected override bool CorrImpl(Vector vec1, Vector vec2, out double corr)
 {
     NamedLock.FuncO <double, bool> func = delegate(out double lcorr)
     {
         Matlab.Clear("HTLib2_Matlab_CorrImpl");
         Matlab.PutVector("HTLib2_Matlab_CorrImpl.vec1", vec1.ToArray());
         Matlab.PutVector("HTLib2_Matlab_CorrImpl.vec2", vec2.ToArray());
         Matlab.Execute("HTLib2_Matlab_CorrImpl.corr = corr(HTLib2_Matlab_CorrImpl.vec1, HTLib2_Matlab_CorrImpl.vec2);");
         lcorr = Matlab.GetValue("HTLib2_Matlab_CorrImpl.corr");
         Matlab.Clear("HTLib2_Matlab_CorrImpl");
         return(true);
     };
     //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.CorrImpl(Vector, Vector, out double)", func, out corr);
     return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_CorrImpl"), func, out corr));
 }
Exemple #13
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);
 }
Exemple #14
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));
 }
Exemple #15
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));
 }
Exemple #16
0
 protected override bool PinvImpl(Matrix mat, out Matrix pinv, InfoPack extra)
 {
     NamedLock.FuncO <Matrix, bool> func = delegate(out Matrix lpinv)
     {
         Matlab.Clear("HTLib2_Matlab_PinvImpl");
         Matlab.PutMatrix("HTLib2_Matlab_PinvImpl", mat.ToArray());
         if (extra != null)
         {
             extra.SetValue("rank", Matlab.GetValueInt("rank(HTLib2_Matlab_PinvImpl)"));
         }
         Matlab.Execute("HTLib2_Matlab_PinvImpl = pinv(HTLib2_Matlab_PinvImpl);");
         lpinv = Matlab.GetMatrix("HTLib2_Matlab_PinvImpl");
         Matlab.Clear("HTLib2_Matlab_PinvImpl");
         return(true);
     };
     //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.PinvImpl(Matrix, out Matrix, InfoPack)", func, out pinv);
     return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_PinvImpl"), func, out pinv));
 }
Exemple #17
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));
     }
 }
Exemple #18
0
 public NamedLock(string name)
     : base(GetName(name))
 {
     this.name = name;
     Matlab.Execute("clear;");
 }
Exemple #19
0
 public static string Pwd()
 {
     return(new string(Matlab.GetVectorChar("pwd")));
 }
Exemple #20
0
 public static void Cd(string path)
 {
     Matlab.Execute("cd '" + path + "';");
 }
Exemple #21
0
        public static void PlotScatter
            (IEnumerable <double> x
            , IEnumerable <double> y
            , IEnumerable <double> size = null
            , double?sizeall            = null
            , IEnumerable <ValueTuple <double, double, double> > RGB = null
            , ValueTuple <double, double, double>?RGBall             = null
            , string xlabel     = null
            , string ylabel     = null
            , string title      = null
            , bool filled       = false
            , bool init_figure  = true
            , bool init_holdon  = true
            , bool last_holdoff = true
            )
        {
            Matlab.PutVector("htlib2_matlab_PlotScatter.x", x.ToArray());
            Matlab.PutVector("htlib2_matlab_PlotScatter.y", y.ToArray());
            if (size != null)
            {
                Matlab.PutVector("htlib2_matlab_PlotScatter.sz", size.ToArray());
            }
            if (RGB != null)
            {
                Matlab.PutVector("htlib2_matlab_PlotScatter.R", RGB.HListItem1().ToArray());
            }
            if (RGB != null)
            {
                Matlab.PutVector("htlib2_matlab_PlotScatter.G", RGB.HListItem2().ToArray());
            }
            if (RGB != null)
            {
                Matlab.PutVector("htlib2_matlab_PlotScatter.B", RGB.HListItem3().ToArray());
            }
            if (RGB != null)
            {
                Matlab.Execute("htlib2_matlab_PlotScatter.RGB = [htlib2_matlab_PlotScatter.R; htlib2_matlab_PlotScatter.G; htlib2_matlab_PlotScatter.B];");
            }
            if (init_figure)
            {
                Matlab.Execute("figure;");
            }
            if (init_holdon)
            {
                Matlab.Execute("hold on;");
            }
            string script;

            {
                script = "scatter" +
                         "( htlib2_matlab_PlotScatter.x" +
                         ", htlib2_matlab_PlotScatter.y";
                // size
                if (sizeall != null)
                {
                    script += ", " + sizeall.Value;
                }
                else if (size != null)
                {
                    script += ", htlib2_matlab_PlotScatter.sz";
                }
                else
                {
                    script += ", []";
                }
                // color
                if (RGBall != null)
                {
                    script += string.Format(", [{0},{1},{2}]", RGBall.Value.Item1, RGBall.Value.Item2, RGBall.Value.Item3);
                }
                else if (RGB != null)
                {
                    script += ", htlib2_matlab_PlotScatter.RGB";
                }
                // filled
                if (filled)
                {
                    script += ", 'filled'";
                }
                // close
                script += ");";
            }
            Matlab.Execute(script);
            if (xlabel != null)
            {
                Matlab.Execute("xlabel('" + xlabel + "');");
            }
            if (ylabel != null)
            {
                Matlab.Execute("ylabel('" + ylabel + "');");
            }
            if (title != null)
            {
                Matlab.Execute("title('" + title + "');");
            }
            if (last_holdoff)
            {
                Matlab.Execute("hold off;");
            }
            Matlab.Execute("clear htlib2_matlab_PlotScatter;");
        }
Exemple #22
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,
            });
        }
Exemple #23
0
            public override ILinAlgMat _Ones(int colsize, int rowsize)
            {
                CMatrix zeros = Matlab.GetMatrix(string.Format("ones({0},{1})", colsize, rowsize));

                return(zeros);
            }
Exemple #24
0
            public override ILinAlgMat _Eye(int size)
            {
                CMatrix eye = Matlab.GetMatrix(string.Format("eye({0})", size));

                return(eye);
            }
Exemple #25
0
 public override void Dispose()
 {
     Matlab.Execute("clear;");
     base.Dispose();
 }