コード例 #1
0
ファイル: Matlab.NumericSolver.cs プロジェクト: htna/explsolv
            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));
            }
コード例 #2
0
ファイル: Matlab.GetILinAlg.cs プロジェクト: htna/explsolv
 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);
     }
 }
コード例 #3
0
ファイル: Matlab.GetILinAlg.cs プロジェクト: htna/explsolv
 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);
     }
 }
コード例 #4
0
ファイル: Matlab.GetILinAlg.cs プロジェクト: htna/explsolv
 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);
     }
 }
コード例 #5
0
ファイル: Matlab.GetILinAlg.cs プロジェクト: htna/explsolv
 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);
     }
 }
コード例 #6
0
ファイル: Matlab.NumericSolver.cs プロジェクト: htna/explsolv
 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);
 }
コード例 #7
0
ファイル: Matlab.NumericSolver.cs プロジェクト: htna/explsolv
 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);
 }
コード例 #8
0
ファイル: Matlab.NumericSolver.cs プロジェクト: htna/explsolv
 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));
 }
コード例 #9
0
ファイル: Matlab.NumericSolver.cs プロジェクト: htna/explsolv
 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));
 }
コード例 #10
0
ファイル: Matlab.NumericSolver.cs プロジェクト: htna/explsolv
 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);
 }
コード例 #11
0
ファイル: Matlab.GetILinAlg.cs プロジェクト: htna/explsolv
 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);
     }
 }
コード例 #12
0
ファイル: Matlab.NumericSolver.cs プロジェクト: htna/explsolv
 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));
 }
コード例 #13
0
ファイル: Matlab.NumericSolver.cs プロジェクト: htna/explsolv
 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));
 }
コード例 #14
0
ファイル: Matlab.NumericSolver.cs プロジェクト: htna/explsolv
 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));
 }
コード例 #15
0
ファイル: Matlab.GetILinAlg.cs プロジェクト: htna/explsolv
 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));
     }
 }