当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Defining a range of symbols whose bounds are OTHER symbols

问题描述:

I'm trying to express a summation over an arbitrary (but finite) number of symbols, which I wish to be given by another symbol. For instance, is it possible to say:

N,ci,cj = symbols('N,c_i,c_j')

# pseudocode

k = sum(ci+cj,(ci,0,N),(cj,0,N))

or, more literally,

k = sum(ci+cj, (ci != cj))

My instinct is that it isn't, but I do wish sympy would implement support for it!

UPDATE

It appears sympy offers provisions for indexed variables. Namely:

x = IndexedBase('x')

i,j = symbols('i j',cls=Idx)

however, the you can an error when attempting:

y = Sum(x[i], (i, 0, 2))

Which is:

ValueError: Invalid limits given: ((i, 1, 5),)

网友答案:

You can use a Function, like x = symbols('x', cls=Function) and x(i). Indexed should also work, but it looks like Sum has a bug that disallows Idx. It works if you just use i = symbols('i'), though.

分享给朋友:
您可能感兴趣的文章:
随机阅读: