如何在 SymPy 中为符号分配属性并将它们放在同一个域中? [英] How to assign properties to symbols in SymPy and have them in the same domain?

查看:46
本文介绍了如何在 SymPy 中为符号分配属性并将它们放在同一个域中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展 SymPy 中的 Symbols 类,以便我可以添加一个布尔属性.我可以为单个符号完成此操作(请参阅我的问题 此处,以及其他人的问题 此处).完成此操作的代码重复如下:

from sympy.core.symbol import Symbol类状态(符号):def __init__(self, name, boolean_attr):self.boolean_attr = boolean_attrsuper(State, self).__init__(name)

然而,这个解决方案的问题在于,当我定义多项式或某种涉及多个 State 的表达式时,这是我对 Symbol 的扩展> 如上所示,当我评估它时,我需要它们都在同一个域中:

单独定义的符号不能用数字计算:

x=sympy.symbols('x')y=sympy.symbols('y')some_poly = Poly(x+y)打印 some_poly.evalf(subs=dict(zip([sympy.symbols('x, y')],[1,4])))>>>Poly(x + y, x, y, domain='ZZ')

定义在同一个域中的符号可以用数字计算:

x, y = sympy.symbols('x, y')some_poly = Poly(x+y)打印 some_poly.evalf(subs=dict(zip(sympy.symbols('x,y'),[1,1])))>>>2.00000

这是我的问题:如何在我的 class State 中实现相同的行为?理想情况下,它的工作方式如下:

x=State('x', boolean_attr=True)y=State('y', boolean_attr=False)states_poly = Poly(x+y)打印 states_poly.evalf(subs=dict(zip(States('x,y'),[1,1])))>>>2.00000

但这不起作用,因为 Sympy 将 x 和 y 解释为在不同的域中.我该怎么做:

  • get Sympyxy 解释为在同一个域中或
  • 扩展 State 类以便能够在同一域中定义符号,例如:

    x, y =State('x, y', boolean_attr=[真, 假])

如何允许对使用扩展类定义的多项式进行数值计算?

解决方案

在您的第一个示例中,您将 symbols 放在列表中,因此您没有使用值 1、4 压缩 x 和 y:

<预><代码>>>>Poly(x+y).evalf(subs=dict(zip(sympy.symbols('x, y'),[1,4])))5.00000000000000

如果您使用与您定义的相同的状态符号,您将获得所需的结果

<预><代码>>>>x=State('x', boolean_attr=True)... y=State('y', boolean_attr=False)... states_poly = Poly(x+y)... states_poly.evalf(subs=dict(zip((x,y),[1,1])))2.00000000000000

(在您提议的语法中,您使用了未定义的 States.即使它确实有效,这样的例程也不会将 True 用于 boolean_attr另一个并且由于符号匹配属性,替换将失败.)

I want to extend the Symbols class in SymPy so that I can add a Boolean attribute. I’m able to accomplish this for a single symbol (see my question here, and also someone else’s question here). And the code to accomplish this is repeated below:

from sympy.core.symbol import Symbol
class State(Symbol):
    def __init__(self, name, boolean_attr):
        self.boolean_attr = boolean_attr
        super(State, self).__init__(name)

However, the problem with this solution is that when I am defining a polynomial, or some kind of expression involving more than one State, which is my extension of the Symbol class as you can see above, I need them all to be in the same domain when I evaluate it:

symbols defined separately cannot be evaluated numerically:

x=sympy.symbols('x')
y=sympy.symbols('y')
some_poly = Poly(x+y)
print some_poly.evalf(subs=dict(zip([sympy.symbols('x, y')],[1,4])))
>>> Poly(x + y, x, y, domain='ZZ')

symbols defined in the same domain can be evaluated numerically:

x, y = sympy.symbols('x, y')
some_poly = Poly(x+y)
print some_poly.evalf(subs=dict(zip(sympy.symbols('x,y'),[1,1])))
>>> 2.00000

Here is my question: How do I achieve this same behavior in my class State? Ideally it would work as follows:

x=State('x', boolean_attr=True)
y=State('y', boolean_attr=False)
states_poly = Poly(x+y)
print states_poly.evalf(subs=dict(zip(States('x,y'),[1,1])))
>>> 2.00000

But that doesn’t work because Sympy interprets x and y as being in different domains. How do I either:

  • get Sympy to interpret x and y as being in the same domain OR
  • extend the State class to be able to define symbols in the same domain, e.g.:

    x, y =State('x, y', boolean_attr=[True, False])

How do I allow my polynomials defined using my extended class to be evaluated numerically?

解决方案

In your first example you put symbols in a list so you didn't zip x and y with the values 1, 4:

>>> Poly(x+y).evalf(subs=dict(zip(sympy.symbols('x, y'),[1,4])))
5.00000000000000

You will get the desired result if you use the same State symbols that you defined

>>> x=State('x', boolean_attr=True)
... y=State('y', boolean_attr=False)
... states_poly = Poly(x+y)
... states_poly.evalf(subs=dict(zip((x,y),[1,1])))
2.00000000000000

(In your proposed syntax you used States which was undefined. Even if it did work, such a routine wouldn't have put True for one boolean_attr and False for the other and since Symbols match on attributes, the substitution would have failed.)

这篇关于如何在 SymPy 中为符号分配属性并将它们放在同一个域中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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