当前位置: 首页>>代码示例>>C#>>正文


C# CodeGen.Return方法代码示例

本文整理汇总了C#中CodeGen.Return方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGen.Return方法的具体用法?C# CodeGen.Return怎么用?C# CodeGen.Return使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CodeGen的用法示例。


在下文中一共展示了CodeGen.Return方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: NSolve

        // Use homotopy method with newton's method to find a solution for F(x) = 0.
        private static List<Arrow> NSolve(List<Expression> F, List<Arrow> x0, double Epsilon, int MaxIterations)
        {
            int M = F.Count;
            int N = x0.Count;

            // Compute JxF, the Jacobian of F.
            List<Dictionary<Expression, Expression>> JxF = Jacobian(F, x0.Select(i => i.Left)).ToList();

            // Define a function to evaluate JxH(x), where H = F(x) - s*F(x0).
            CodeGen code = new CodeGen();

            ParamExpr _JxH = code.Decl<double[,]>(Scope.Parameter, "JxH");
            ParamExpr _x0 = code.Decl<double[]>(Scope.Parameter, "x0");
            ParamExpr _s = code.Decl<double>(Scope.Parameter, "s");

            // Load x_j from the input array and add them to the map.
            for (int j = 0; j < N; ++j)
                code.DeclInit(x0[j].Left, LinqExpr.ArrayAccess(_x0, LinqExpr.Constant(j)));

            LinqExpr error = code.Decl<double>("error");

            // Compile the expressions to assign JxH
            for (int i = 0; i < M; ++i)
            {
                LinqExpr _i = LinqExpr.Constant(i);
                for (int j = 0; j < N; ++j)
                    code.Add(LinqExpr.Assign(
                        LinqExpr.ArrayAccess(_JxH, _i, LinqExpr.Constant(j)),
                        code.Compile(JxF[i][x0[j].Left])));
                // e = F(x) - s*F(x0)
                LinqExpr e = code.DeclInit<double>("e", LinqExpr.Subtract(code.Compile(F[i]), LinqExpr.Multiply(LinqExpr.Constant((double)F[i].Evaluate(x0)), _s)));
                code.Add(LinqExpr.Assign(LinqExpr.ArrayAccess(_JxH, _i, LinqExpr.Constant(N)), e));
                // error += e * e
                code.Add(LinqExpr.AddAssign(error, LinqExpr.Multiply(e, e)));
            }

            // return error
            code.Return(error);

            Func<double[,], double[], double, double> JxH = code.Build<Func<double[,], double[], double, double>>().Compile();

            double[] x = new double[N];

            // Remember where we last succeeded/failed.
            double s0 = 0.0;
            double s1 = 1.0;
            do
            {
                try
                {
                    // H(F, s) = F - s*F0
                    NewtonsMethod(M, N, JxH, s0, x, Epsilon, MaxIterations);

                    // Success at this s!
                    s1 = s0;
                    for (int i = 0; i < N; ++i)
                        x0[i] = Arrow.New(x0[i].Left, x[i]);

                    // Go near the goal.
                    s0 = Lerp(s0, 0.0, 0.9);
                }
                catch (FailedToConvergeException)
                {
                    // Go near the last success.
                    s0 = Lerp(s0, s1, 0.9);

                    for (int i = 0; i < N; ++i)
                        x[i] = (double)x0[i].Right;
                }
            } while (s0 > 0.0 && s1 >= s0 + 1e-6);

            // Make sure the last solution is at F itself.
            if (s0 != 0.0)
            {
                NewtonsMethod(M, N, JxH, 0.0, x, Epsilon, MaxIterations);
                for (int i = 0; i < N; ++i)
                    x0[i] = Arrow.New(x0[i].Left, x[i]);
            }

            return x0;
        }
开发者ID:JackWangCUMT,项目名称:ComputerAlgebra,代码行数:82,代码来源:NSolve.cs

示例2: DefineSimulate

        // Define a function for running the simulation of a population system S with timestep
        // dt. The number of timesteps and the data buffer are parameters of the defined function.
        static Func<int, double[, ], int> DefineSimulate(double dt, PopulationSystem S)
        {
            CodeGen code = new CodeGen();

            // Define a parameter for the current population x, and define mappings to the
            // expressions defined above.
            LinqExpr N = code.Decl<int>(Scope.Parameter, "N");
            LinqExpr Data = code.Decl<double[,]>(Scope.Parameter, "Data");

            // Loop over the sample range requested. Note that this loop is a 'runtime' loop,
            // while the rest of the loops nested in the body of this loop are 'compile time' loops.
            LinqExpr n = code.DeclInit<int>("n", 1);
            code.For(
                () => { },
                LinqExpr.LessThan(n, N),
                () => code.Add(LinqExpr.PostIncrementAssign(n)),
                () =>
            {
                // Define expressions representing the population of each species.
                List<Expression> x = new List<Expression>();
                for (int i = 0; i < S.N; ++i)
                {
                    // Define a variable xi.
                    Expression xi = "x" + i.ToString();
                    x.Add(xi);
                    // xi = Data[n, i].
                    code.DeclInit(xi, LinqExpr.ArrayAccess(Data, LinqExpr.Subtract(n, LinqExpr.Constant(1)), LinqExpr.Constant(i)));
                }

                for (int i = 0; i < S.N; ++i)
                {
                    // This list is the elements of the sum representing the i'th
                    // row of f, i.e. r_i + (A*x)_i.
                    Expression dx_dt = 1;
                    for (int j = 0; j < S.N; ++j)
                        dx_dt -= S.A[i, j] * x[j];

                    // Define dx_i/dt = x_i * f_i(x), as per the Lotka-Volterra equations.
                    dx_dt *= x[i] * S.r[i];

                    // Euler's method for x(t) is: x(t) = x(t - h) + h * x'(t - h).
                    Expression integral = x[i] + dt * dx_dt;

                    // Data[n, i] = Data[n - 1, i] + dt * dx_dt;
                    code.Add(LinqExpr.Assign(
                        LinqExpr.ArrayAccess(Data, n, LinqExpr.Constant(i)),
                        code.Compile(integral)));
                }
            });

            code.Return(N);

            // Compile the generated code.
            LinqExprs.Expression<Func<int, double[,], int>> expr = code.Build<Func<int, double[,], int>>();
            return expr.Compile();
        }
开发者ID:JackWangCUMT,项目名称:ComputerAlgebra,代码行数:58,代码来源:Program.cs


注:本文中的CodeGen.Return方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。