Create an unknown number of programmatically defined variables

半腔热情 提交于 2020-02-05 05:44:05

问题


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.

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.


回答1:


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))

Output

(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]

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))

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.



来源:https://stackoverflow.com/questions/48372808/create-an-unknown-number-of-programmatically-defined-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!