【问题标题】:Evaluate sympy polynomial with an array用数组评估 sympy 多项式
【发布时间】:2019-10-06 19:59:51
【问题描述】:

我需要有关 Sympy 的帮助,我实际上有一个多项式列表,比如说 2 个变量 s0s1,但是有些多项式在 s0 中 而已,有的只有 s1,有的只是常数,有的有两个变量,例如:

poly = [1 , s0 , s0+3*s1 , .... ]

我想用相同的命令来评估它们中的任何一个,比如说 s0=2s1=1。例如评估我尝试过的列表的第三个多项式

s = sympy.symbols('s0:%d'2)
poly[2].subs(s,[2,0])

# s is containing (s0, s1), but this won't work at all

poly[2].eval([2,0])

# this one works only if the polynomial uses the two variables, so it will throw an error for poly[0] and poly[1]

事实是我不知道哪个变量有哪个变量,我也不知道先验有多少变量,这最后的信息是函数的输入。因此需要对列表中的所有多项式使用数组和相同的命令。

【问题讨论】:

    标签: python-3.x sympy


    【解决方案1】:

    您可以使用带有 subs 的 dict:

    In [12]: from sympy import *                                                                                                                                  
    
    In [13]: s0, s1 = symbols('s:2')                                                                                                                              
    
    In [14]: polys = [1 , s0 , s0+3*s1]                                                                                                                           
    
    In [15]: [S(p).subs(values) for p in polys]                                                                                                                   
    Out[15]: [1, 2, 5]
    

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多