SymPy:如何实现索引系数乘以索引函数的求和?

发布于 2025-01-10 10:32:54 字数 487 浏览 1 评论 0原文

更新:

我想运行一个generate_function(3),例如得到以下输出:

c_0 * F(0) + c_1 * F(1) + c_2 * F(2) + c_3 * F(3)

其中 c_i 只是一个符号,而 F(i) 是一个函数或一个对象,我稍后可以将其用于其余代码。


我只是想使用 SymPy 来实现求和:

求和(从 i = 0 到 n)c_i * f(i)

其中 c_i 是索引常量(符号),f 是参数为 i 的函数。

我尝试了很多次,但都失败了。

def generate(n):
    coeff = sym.symbols('c0:{}'.format(n))
    def f(i):
        return i
    return sym.Sum(coeff(i) * f(i),(i,0,n))

我得到:“元组”对象不可调用

感谢您的帮助

Update:

I want to run a generate_function(3) for example to have an output of:

c_0 * F(0) + c_1 * F(1) + c_2 * F(2) + c_3 * F(3)

where c_i is just a symbol, while F(i) is a function or an object that I can use later for the rest of code.


I simply want to use SymPy to implement the summation:

summation (from i = 0 to n) c_i * f(i)

where c_i is indexed constant (symbol) and f is a function with argument of i.

I tried many times and failed.

def generate(n):
    coeff = sym.symbols('c0:{}'.format(n))
    def f(i):
        return i
    return sym.Sum(coeff(i) * f(i),(i,0,n))

I got: 'tuple' object is not callable

Thanks for help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街九尾狐 2025-01-17 10:32:54

目前还不完全清楚你想要什么,但也许就是这样:

In [31]: C = IndexedBase('C')

In [32]: f = Function('f')

In [33]: i, n = symbols('i, n')

In [34]: s = Sum(C[i] * f(i), (i, 0, n))

In [35]: s
Out[35]: 
  n            
 ___           
 ╲             
  ╲            
  ╱   f(i)⋅C[i]
 ╱             
 ‾‾‾           
i = 0 

It's not completely clear what you want but maybe this is it:

In [31]: C = IndexedBase('C')

In [32]: f = Function('f')

In [33]: i, n = symbols('i, n')

In [34]: s = Sum(C[i] * f(i), (i, 0, n))

In [35]: s
Out[35]: 
  n            
 ___           
 ╲             
  ╲            
  ╱   f(i)⋅C[i]
 ╱             
 ‾‾‾           
i = 0 
白云不回头 2025-01-17 10:32:54

您创建了一个符号元组:

In [8]: coeff = symbols("c0:{}".format(n))

In [9]: coeff
Out[9]: (c0a, c0b, c0c, c0d, c0e, c0f, c0g, c0h, c0i, c0j, c0k, c0l, c0m, c0n)

将这样的元组视为一个函数(如 f(i) 中所示)是行不通的。这就是基本的Python!虽然索引对于创建许多符号来说是一个很好的简短,但结果与执行 symbols('xy z') 没有什么不同

In [10]: coeff(0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [10], in <module>
----> 1 coeff(0)

TypeError: 'tuple' object is not callable

您可以索引元组

In [11]: coeff[0]
Out[11]: c0a

您可以使用列表理解来创建表达式列表:

In [14]: [coeff[i] * f(i) for i in range(5)]
Out[14]: [0, c0b, 2⋅c0c, 3⋅c0d, 4⋅c0e]

甚至应用基本 sum 函数来创建 sympy.Add 表达式:

In [16]: sum([coeff[i] * f(i) for i in range(5)])
Out[16]: c0b + 2⋅c0c + 3⋅c0d + 4⋅c0e

You created a tuple of symbols:

In [8]: coeff = symbols("c0:{}".format(n))

In [9]: coeff
Out[9]: (c0a, c0b, c0c, c0d, c0e, c0f, c0g, c0h, c0i, c0j, c0k, c0l, c0m, c0n)

Treating such a tuple as though it were a function, as in f(i), does not work. That's basic Python! While the indexing is a nice short to making many symbols, the result is no different from doing symbols('x y z')

In [10]: coeff(0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [10], in <module>
----> 1 coeff(0)

TypeError: 'tuple' object is not callable

You can index the tuple

In [11]: coeff[0]
Out[11]: c0a

You could use a list comprehension to make a list of expressions:

In [14]: [coeff[i] * f(i) for i in range(5)]
Out[14]: [0, c0b, 2⋅c0c, 3⋅c0d, 4⋅c0e]

and even apply the base sum function to create a sympy.Add expression:

In [16]: sum([coeff[i] * f(i) for i in range(5)])
Out[16]: c0b + 2⋅c0c + 3⋅c0d + 4⋅c0e
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文