`S` 在 sympy 中是什么意思 [英] What does `S` signify in sympy

查看:34
本文介绍了`S` 在 sympy 中是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 sympy 的新手,我不知道

I am new to sympy, I can't figure out

from sympy.core import S

S 究竟是什么?S.true 是什么意思?

What actually S is? And what does S.true mean?

推荐答案

有点混乱,因为 S 实际上是两件事.

There's a bit of confusion because S is actually two things.

首先是SingletonRegistry. SymPy 中的几个类经常出现以至于它们被singletonized,也就是说,使用一些元编程使它们只能被实例化一次.例如,每次创建 Integer(0) 时,都会返回相同的实例 Zero.所有单例实例都是 S 对象的属性,所以 Integer(0) 也可以作为 S.Zero 访问.

The first thing it is is the SingletonRegistry. Several classes in SymPy appear so often that they are singletonized, that is, using some metaprogramming they are made so that they can only be instantiated once. For instance, every time you create Integer(0), this will return the same instance, Zero. All singleton instances are attributes of the S object, so Integer(0) can also be accessed as S.Zero.

Singletonization 有两个优点:节省内存,并且允许快速比较.它节省了内存,因为无论单个对象在内存中的表达式中出现多少次,它们都指向内存中的同一个单个实例.快速比较的原因在于,您可以使用 is 来比较 Python 中的精确实例(通常,您需要使用 == 来比较事物).因此,您可以测试 a is S.Zero 来检查 a 是否是 Integer(0) 实例.

Singletonization offers two advantages: it saves memory, and it allows fast comparison. It saves memory because no matter how many times the singletonized objects appear in expressions in memory, they all point to the same single instance in memory. The fast comparison comes from the fact that you can use is to compare exact instances in Python (usually, you need to use == to compare things). Hence, you can test a is S.Zero to check if a is the Integer(0) instance.

在大多数情况下,某些对象是单例化的这一事实是您无需担心的实现细节.对于最终用户来说,S 的主要优势是可以方便地访问某些难以键入的实例,例如 S.Half(而不是 Rational(1,2)) 或 S.true(旁注:S.trueTrue 的 SymPy 版本.不同于 True,它不是 int 的子类,所以你可以写类似 ~S.true (不是真的),它会给 S.false(与 ~True 形成对比,它给出 -2,它不是 false 作为布尔值).

For the most part, the fact that certain objects are singletonized is an implementation detail that you shouldn't need to worry about. The primary advantage of S for end users is the convenient access to certain instances that are otherwise difficult to type, like S.Half (instead of Rational(1, 2)), or S.true (side note: S.true is the SymPy version of True. Unlike True, it does not subclass from int, so you can write things like ~S.true (not true) and it will give S.false (contrast that with ~True, which gives -2, which isn't false as a boolean).

第二件事是sympify的快捷方式. sympify是转换Python对象的函数,例如int(1) 转化为 SymPy 对象,例如 Integer(1).它还将表达式的字符串形式转换为 SymPy 表达式,如 sympify(x**2) ->符号(x")**2.S(1)sympify(1) 相同(基本上,S.__call__ 已被定义为调用 sympify).

The second thing it is is a shortcut for sympify. sympify is the function that converts Python objects such as int(1) into SymPy objects such as Integer(1). It also converts the string form of an expression into a SymPy expression like sympify(x**2) -> Symbol("x")**2. S(1) is the same thing as sympify(1) (basically, S.__call__ has been defined to call sympify).

这是为了方便,因为 S 是单个字母.它主要用于定义有理数.考虑像 x + 1/2 这样的表达式.如果你直接在 Python 中输入它,它会计算 1/2 并给出 0.5(或者在 Python 2 中只是 0,因为整数除法),因为两个参数都是 ints.但是,在 SymPy 中,您通常希望两个整数的商给出一个精确的有理数.Python 求值的工作方式,运算符的至少一侧需要是一个 SymPy 对象,以便 SymPy 求值接管.您可以将其写为 x + Rational(1, 2),但这需要更多的输入.较短的版本是 x + S(1)/2.由于 S(1) 返回 Integer(1),除法将返回一个 Rational 类型,因为它将调用 Integer.__div__,它知道如何返回一个 Rational.

This is for convenience, since S is a single letter. It's mostly useful for defining rational numbers. Consider an expression like x + 1/2. If you enter this directly in Python, it will evaluate the 1/2 and give 0.5 (or just 0 in Python 2, because of integer division), because both arguments are ints. However, in SymPy, you usually want the quotient of two integers to give an exact rational number. The way Python's evaluation works, at least one side of an operator needs to be a SymPy object for the SymPy evaluation to take over. You could write this as x + Rational(1, 2), but this is a lot more typing. A shorter version is x + S(1)/2. Since S(1) returns Integer(1), the division will return a Rational type, since it will call Integer.__div__, which knows how to return a Rational.

这篇关于`S` 在 sympy 中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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