Clone this wiki locally

I did the same calculation twice and got different results. What's going on?

What is the best way to create symbols?

How good is SymPy's performance?

Where is SymPy going to? What are the nearest plans?

What about Sage?

How do I clear the cache?

How do I turn off caching?

Is there a method to get a list with all symbols in an expression?

How can I make my editor highlight trailing whitespace red?

How to connect to our IRC channel?

Why doesn't changing one variable change another that depends it?

When I copy and paste an expression during interactive work, why do I get a different answer?

How can I get sympy to not change what I enter?

Why does SymPy say that two equal expressions are unequal?

The equality operator (==) tests whether expressions have identical form, not whether they are mathematically equivalent.

To make equality testing useful in basic cases, SymPy tries to rewrite mathematically equivalent expressions to a canonical form when evaluating them. For example, SymPy evaluates both x+x and -(-2*x) to 2*x, and x*x to x**2.

The simplest example where the default transformations fail to generate a canonical form is for nonlinear polynomials, which can be represented in both factored and expanded form. Although clearly ( a(1+b) = a+ab ) mathematically, SymPy gives:

>>>from sympy.abc import a, b
>>>bool(a*(1+b) == a + a*b)
False

Likewise, SymPy fails to detect that the difference is zero:

>>>bool(a*(1+b) - (a+a*b) ==0)
False

If you want to determine the mathematical equivalence of nontrivial expressions, you should apply a more advanced simplification routine to both sides of the equation. In the case of polynomials, expressions can be rewritten in a canonical form by expanding them fully. This is done using the .expand() method in SymPy:

I did the same calculation twice and got different results. What's going on?

This problem is probably due to erroneous caching of assumptions. Please report the bug to the mailing list or the issue tracker.

What is the best way to create symbols?

The most convenient way to create symbols when using SymPy interactively is via var. For example:

>>>from sympy import var
>>> var('x,y')
>>> x + y
x + y

The var function uses a hack to add the symbols to the current namespace, so you don't have to type each symbol name twice. In library code, it is better to create symbols explicitly:

x = Symbol('x')
y = Symbol('y')

This makes the code much clearer.

How good is SymPy's performance?

SymPy is efficient enough for interactive use as an advanced calculator. The order-of-magnitude time to evaluate a simple algebraic operation (say, multiplying two small polynomials) is around 1/1000 of a second. Operations like limits and symbolic integration are generally much slower.

SymPy is written entirely in Python. Compared to CASes written in compiled languages (including Mathematica, Maple and Maxima), SymPy is generally at least an order of magnitude (10x) slower: This is partly due to the overhead of Python, and partly because SymPy in many cases only implements the most basic algorithm. Nonetheless, experimentation has shown that that symbolic computations can be done nearly as efficiently in Python as in compiled languages by using the right data structures and designing the code to minimize various Python overheads. The sympycore project is currently attempting to redesign SymPy's internals to provide better performance, and the results are promising so far. See sympycore wiki: PerformanceHistory and sympycore wiki: Performance for more details.

Start it, it will ask you to select a nick (select some) and connect
to a network, connect to "FreeNode" (select it from the list) and then
after you connect, type

/join #sympy

and that's it. If you have any troubles, please ask on our mailinglist.

Why doesn't changing one variable change another that depends it?

The short answer is "because it doesn't depend on it." :-)
Even though you are working with equations, you are still working with Python objects.
The equations you are typing use the values present at the time of creation to
"fill in" values, just like regular python definitions. They are not altered by
changes made afterwards. Consider the following:

>>> a = Symbol('a') # create an object with name 'a' for variable a to point to>>> b = a +1; b # create another object that refers to what 'a' refers to
a +1>>> a =4; a # a now points to the literal integer 4, not Symbol('a')4>>> b # but b is still pointing at Symbol('a')
a +1

Changing quantity a does not change b; you are not working with a set of simultaneous equations. It might be helpful to remember that the string that gets printed when you print a variable refering to
a sympy object is the string that was give to it when it was created; that string does not have to
be the same as the variable that you assign it to:

When I copy and paste an expression during interactive work, why do I get a different answer?

Even though you are working with sympy objects, not everything you type is a sympy object. If you are working with a python version earlier than 3 and have not issued a from __future__ import division command then dividing an integer by a larger integer will give 0

This could be the source of the difference that you see when you copy and paste a result. The work-around for this is to use sympify() on that copied expression which changes integers to a sympy object or just use a index to access the piece of the output that you are interested in:

How can I get sympy to not change what I enter?

Sometimes you might want a fraction in an unsimplified form (e.g. 2/4 instead of 1/2) or you might want terms raised to a power to not have the power distributed to each term (e.g. (x*y)**2 instead of x**2*y**2). Presently, the way to get the raw form is by building the expression with the evaluate=False option, e.g.