在 Pari-GP 中嵌套特定的递归 [英] Nesting a specific recursion in Pari-GP

查看:104
本文介绍了在 Pari-GP 中嵌套特定的递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家!

我已经发布了一个类似的问题,最初是在 Stackexchange 上;它被移到这里,可以在链接中找到:Declaring afunctional recursive sequence inMatlab 我试图在这篇文章中做一些类似的事情,但我认为 Matlab 不是这样做的地方.我将不得不使用 Pari-GP;在这一点上,没有两种方法.

I've posted a similar problem, initially on Stackexchange; it was moved here and can be found at the link: Declaring a functional recursive sequence in Matlab I'm trying to do something similar in this post, but I've figured that Matlab isn't the place to do it. I'll have to use Pari-GP; and at this point there's no two ways about it.

这本质上是我为自己制作的一个编码项目;这是为了有效地数值评估 Tetration 函数的某个构造.我已经能够在 MatLab 中很好地编写代码;问题是,因为我们正在处理像 e^e^e^e^e^e^e 这样的大数;MatLab 中的这些短路.Pari-GP 对通常会导致溢出的数字有更好的理解;我现在完全明白为什么 Tetration 社区一直在使用它.

This is essentially a coding project I've made for myself; which is to effectively numerically evaluate a certain construction of the Tetration function. I've been able to code it fairly well in MatLab; the trouble is, as we are dealing with large numbers like e^e^e^e^e^e^e; these short circuit in MatLab. Pari-GP has a much better understanding of numbers which would usually cause overflows; and I'm now fully realizing why it's used consistently by the Tetration community.

尽管如此,Matlab 代码适用于小数字和对它们有利的数字(放置得当的假想参数).为了这个问题的完整性;matlab 代码的 github 存储库是 https://github.com/JmsNxn92/Recursive_Tetration 这不是't 我们想要的代码;我进一步优化了它;这完全不是最新的.但就这个问题而言,这就足够了.

Nonetheless, the Matlab code works for small numbers and numbers with a niceness to them (well placed imaginary argument). For completeness of this question; the github repository for the matlab code is https://github.com/JmsNxn92/Recursive_Tetration This isn't the code we want though; I've optimized it further; this isn't up to date totally. But for the purpose of this question, it's enough.

现在,我不是最好的程序员.我从 09 年开始就没有编程了,也许吧;但我仍然知道我的方式.但更重要的是我有编码的框架;而实际的语法更少.想象一下自己熟悉法国哲学和法国写作;但是在订购咖啡馆时会绊倒你的话.

Now, I'm not the best programmer. I haven't programmed since '09, maybe; but I still know my way around. But it's more that I have the framework of coding; and less the actual syntax. Imagine being well acquainted with french philosophy and french writing; but stumbling your words when ordering a cafe.

至于那个,我将停止绕圈子,进入问题.

As to that, I'll stop beating around the bush, and get to the question.

如果我在 Pari-GP 中定义一个函数 beta_function 并将其写为,

If I define a function beta_function in Pari-GP and write it as,

beta_function(z,l,n) =
{
    out = 0;
    for(i=0,n-1,
        out = exp(out)/(exp(l*(n-i-z)) +1));
    out;
}

一切都很好,而且有效.现在 MatLab 中 beta_function 的代码没有太大的不同.没有什么更复杂的添加.正如我最初要求使用 MatLab,我再次要求使用 Pari-GP.这是如何编写函数tau_K(z,l,n,k);这是完全可行的.我只是遗漏了一些明显的东西.

Everything is good, and it works. Now the code for beta_function in MatLab isn't very different. There's nothing more complex being added. As I originally asked for MatLab, I'm asking again for Pari-GP. This is how to code a function tau_K(z,l,n,k); which is perfectly doable. I'm just missing something obvious.

下面附上 MatLab 中 tau_K(z,l,n,k) 的代码.这里的一位友好人士解释了如何在 MatLab 中执行此操作;对于那些感兴趣的人,我并没有真正事先正确定义递归.这是我正在使用的当前 MatLab 代码,

The code for tau_K(z,l,n,k) in MatLab is attached below. A friendly person on here explained how to do this in MatLab; for those of you interested, I wasn't really defining the recursion properly beforehand. Here is the current MatLab code I'm using,

function f = tau_K(z,l,n,k)
    if k == 1
        f = -log(1+exp(-l*z));
        return
    end

    f = log(1 + tau_K(z+1,l,n,k-1)./beta_function(z+1,l,n)) - log(1+exp(-l*z));
end

问题很简单.如何在 Pari-GP 中定义这种递归;你如何在 Pari-GP 中编码?

The question is simple. How would one define this recursion in Pari-GP; how do you code this in Pari-GP?

当我尝试直接翻译这段代码时,一切似乎都朝着 0 处的返回值崩溃.对上帝诚实;我知道这只是因为我在如何将输出调用到下一次迭代中犯了一些语法错误.我已经尝试了所有我能想到的.而教程,他们似乎没有帮助.我已经尝试了一切.在这一点上,我知道我在语法上遗漏了一些愚蠢的东西.

Everything seems to be collapsing towards a return value at 0, when I try to directly translate this code. And honest to god; I know it's just because I'm making some syntax error in how I'm calling the output into the next iteration. I've tried everything I could think of. And the tutorials, they don't seem to be helping. I've tried next to everything. And at this point, I know I'm missing something stupid syntactically.

我只是希望这里有人能像我在幼儿园一样向我解释这一点.我听说尾递归在这里很重要.如果是这样,我将如何编码?只需添加一个变量来跟踪所有内容?

I'm just hoping someone here would be so helpful as to explain this to me like I'm in kindergarten. I've heard that tail recursion is important here. And if so, how would I code this in? Simply add in a variable which keeps track of everything?

再次感谢您对问题的深入了解.

Again, thanks if you got this far into the question.

推荐答案

在提问时,如果您为某些指定的给定参数提供预期的输出会有所帮助,否则很难测试.我不懂 MATLAB,但您的函数可以用 PARI 编写:

When asking questions, it would help if you would provide expected output for some specified given arguments, otherwise it is hard to test. I don't know MATLAB, but your functions could be written in PARI:

beta_function(z,l,n)={
    my(out = 0);
    for(i=0,n-1,
        out = exp(out)/(exp(l*(n-i-z)) +1));
    out;
}

tau_K(z,l,n,k)={
    if(k == 1, 
      -log(1+exp(-l*z)), 
      log(1 + tau_K(z+1,l,n,k-1)/beta_function(z+1,l,n)) - log(1+exp(-l*z))
    )
}

beta_function 中,将my() 放在out = 0 周围很重要.这使变量保持在函数的局部.如果不这样做,意味着 out 将是一个全局变量,并且可能会出现许多细微的错误.

In the beta_function, it is important to put my() around out = 0. This keeps the variable local to the function. Failure to do this, means that out will be a global variable, and many subtle bugs can arise.

PARI 是一种函数式编程语言,这意味着您通常不需要将事物显式分配给临时变量.例如 if 将返回一个值,这可以从您的 tau_K 函数返回(在您的 MATLAB 代码中,您分配给一个临时变量 f,但在 PARI 中这不是必需的).

PARI is a functional programming language which means you often don't need to assign things explicitly to temporary variables. For example if will return a value and this can be returned from your tau_K function (in your MATLAB code you assign to a temporary variable f, but in PARI this is not necessary).

递归调用函数没有问题.在这种情况下,tau_K 可以根据需要调用自己.

There are no issues with calling a function recursively. In this case, tau_K can just call itself as needed.

在 MATLAB 程序中,您有 ./.我不知道这意味着什么 - 我已经替换为 / ,它只是普通的除法运算符.

In the MATLAB program you have ./. I don't know what this means - I have replaced by / which is just the normal division operator.

在运行之前,您需要为数字运算设置一些精度.实现此目的的最简单方法是在 PARI-GP 提示符下输入 \p100.(或 \p1000 如果您需要 1000 个十进制数字的精度).如果您需要以高精度执行某些部分的计算而其他部分以较低的精度执行,或者精度需要依赖于n,则可以动态控制精度.

Before running you will need to set some precision for the numeric operations. The easiest way to achieve this is to enter \p100 at the PARI-GP prompt. (or \p1000 if you need a 1000 decimal digits of precision). It is possible to control precision dynamically, if you need some part of the calculation performed at high precision and other parts at a lower precision or if the precision needs to be dependent on n.

这篇关于在 Pari-GP 中嵌套特定的递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆