创建未知数量的以编程方式定义的变量 [英] Create an unknown number of programmatically defined variables

查看:61
本文介绍了创建未知数量的以编程方式定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个递归函数,它可以产生难以知道数量的表达式,每个表达式都需要乘以一个新变量.这些变量稍后将通过涉及积分或残差的计算去除.

I have a recursive function that can produce a difficult-to-know number of expressions, each needing a new variable multiplied to it. These variables will later be removed out by calculations involving integration or residue.

如何开发这些未知数量的变量?也许索引?我在互联网上看到的所有示例都使用具有确定大小的先验已知对象,例如中的项目"如何通过 while 循环动态创建变量?在 Python 'for' 循环中访问索引

How can I develop these unknown number of variables? Maybe indexed? All examples I've seen on the internet are working with an a priori known object of a definite size, e.g. "item" in How can you dynamically create variables via a while loop? or Accessing the index in Python 'for' loops

我想我可以把它归结为这个简单的例子,以便在我的真实脚本中使用:

I think I can boil it down to this simple example to use in my real script:

import sympy as s
p0,p1,p2,p3,p4=s.symbols('p0 p1 p2 p3 p4')
l = [p0, p1, p2, p3, p4]

def f(n):
    if n == 0:
        return l[n]
    elif n == 1: 
        return l[n]
    else:
        return f(n-1)*l[n]+f(n-2)

f(3) # works
f(6) # doesnt' work - need to define ahead of time the 
     # dummy variables l[6], l[5], .... 
     # even if they are just symbols for (much) later numerical evaluation.

我需要上面的代码片段来提前实际生成所需的未知数.

I need this above snippet to actually generate the needed unknowns ahead of time.

我看到了一些关于熊猫的提及,但找不到满足我需要的好例子,甚至不确定这是否是最佳路线.还看到了诸如...未知行数 [文件]..."或...未知数量的参数..."之类的内容,但这些似乎不适用.

I saw some mentions of pandas, but couldn't find a good example for my need, nor even sure if that was the best route. Also saw things like, "...an unknown number of lines [file]...", or "...unknown number of arguments...", but those are, seemingly, not applicable.

推荐答案

索引对象代表一个抽象的东西,索引可以取任何值,对索引的大小没有限制.

Indexed objects represent an abstract thing with an index taking any values, with no restriction on how large the index can be.

import sympy as s
p = s.IndexedBase("p")

def f(n):
    if n == 0 or n == 1:
        return p[n]
    else:
        return f(n-1)*p[n] + f(n-2)

print(f(7))

输出

(p[0] + p[1]*p[2])*p[3] + (((p[0] + p[1]*p[2])*p[3] + p[1])*p[4] + p[0] + p[1]*p[2])*p[5] + (((p[0] + p[1]*p[2])*p[3] + p[1])*p[4] + ((p[0] + p[1]*p[2])*p[3] + (((p[0] + p[1]*p[2])*p[3] + p[1])*p[4] + p[0] + p[1]*p[2])*p[5] + p[1])*p[6] + p[0] + p[1]*p[2])*p[7] + p[1]

<小时>

顺便说一句,p0,p1,p2,p3,p4=s.symbols('p0 p1 p2 p3 p4') 之类的事情可以使用 syms = s 更轻松地完成.symbols('p0:5') 甚至


As an aside, things like p0,p1,p2,p3,p4=s.symbols('p0 p1 p2 p3 p4') can be done more easily with syms = s.symbols('p0:5') or even

n = ...
syms = s.symbols('p0:{}'.format(n))

这会创建单个符号,而不是索引对象,因此在创建时必须知道数字 n.但仍然比列出 p0 p1 等容易.

This creates individual symbols, not an indexed object, so the number n has to be known at the time of creation. But still easier than listing p0 p1 and so on.

这篇关于创建未知数量的以编程方式定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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