示例#1
0
        public static double?GetRootNewton(Func <double, double> func, Func <double, double> dfunc, double init)
        {
            double x1, x2 = init;
            double dx1       = 0;
            double dx2       = 1000000;
            int    iteration = 100;

            while (iteration > 0)
            {
                iteration--;

                x1  = x2;
                x2  = x1 - (func(x1) / dfunc(x1));
                dx1 = dx2;
                dx2 = x2 - x1;
                if (Math.Abs(dx2 - dx1) < 0.00005)
                {
                    /* debug */ if (HDebug.IsDebuggerAttached)
                    /* debug */ {
                        /* debug */ double error = func(x2);
                        /* debug */ HDebug.AssertIf(false, Math.Abs(error) < 0.000001);
                        /* debug */ }
                    return(x2);
                }
                if (double.IsInfinity(x2) || double.IsNaN(x2))
                {
                    return(null);
                }
            }
            ;
            return(x2);
        }
示例#2
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));
            }
示例#3
0
文件: Matlab.cs 项目: htna/explsolv
        public static string Execute(string script, bool ignoreAssert, params string[] replaces)
        {
            string nscript = CompleteScript(script, replaces);
            string msg     = matlab.Execute(nscript);

            if (ignoreAssert == false)
            {
                HDebug.AssertIf(!ignoreAssert, msg == "");
                if (msg != "")
                {
                    System.Console.Error.WriteLine("Matlab error:\n" + msg);
                    throw new HException(string.Format("exception at Matlab.Execute(\"{0}\")", script));
                }
            }
            return(msg);
        }