What does `S` signify in sympy

这一生的挚爱 提交于 2019-12-07 03:01:22

问题


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

from sympy.core import S

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


回答1:


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

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 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.

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).

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).

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.




回答2:


As many have linked, S is for accessing singletons. Per the source, S is an instance of class SingletonRegistry. S does two things:

  • gives you access to instances such as S.One and S.Zero, which represent fundamental constants
  • lets you create or access singletons representing constants based on their values (e.g., S(1)).

The reason to use a singleton is so that you can compare objects with is rather than ==. is is faster, but only works if you know for sure that your 1 is the same as the 1 you are testing against. The code gives an example:

>>> Basic() is Basic()
False
>>> MySingleton() is MySingleton()
True
 >>> S.MySingleton is MySingleton()
True


来源:https://stackoverflow.com/questions/41860294/what-does-s-signify-in-sympy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!