FAQ
From SymPy
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:
>>> 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:
>>> A, B = a*(1+b), a + a*b >>> bool(A.expand() == B.expand()) True >>> (A - B).expand() 0
If .expand() does not help, try simplify(), trigsimp(), etc, which attempt more advanced transformations. For example,
>>> trigsimp(cos(x)**2 + sin(x)**2) == 1 True
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:
>>> 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.
Where is SymPy going to? What are the nearest plans?
See our roadmap: http://wiki.sympy.org/wiki/Plan_for_SymPy_1.0
The current state of SymPy as of January 3, 2008 is summed up in the blogpost: http://ondrejcertik.blogspot.com/2008/01/sympysympycore-pure-python-up-to-5x.html
And in the email: http://groups.google.com/group/sympy/msg/da52a78c1ef9f02b
But the secret plan is to be the best CAS in Python. :)
What about Sage?
Sage will be the best opensource CAS (hopefully). More information here:
http://code.google.com/p/sympy/wiki/Motivation
How do I clear the cache?
You can see/clear the cache content as follows:
In [1]: from sympy.core.cache import *
In [2]: print_cache() ============================ <function gcd at 0xb7a96f7c> ============================ *** 0 *** ((2, 1), ()) : 1 ((1, 30), ()) : 1 ((1, 6), ()) : 1 *** 1 *** 1 : 1 ... lots of output follow ... In [3]: clear_cache()
How do I turn off caching?
Simply set the environment variable "SYMPY_USE_CACHE=no", for example:
$ SYMPY_USE_CACHE=no py.test sympy/core
This was implemented in fdca1475fb92.
Is there a method to get a list with all symbols in an expression?
Yes .atoms(type=Symbol), use it like this:
In [1]: e = x + y*sin(z**2) In [2]: e.atoms() Out[2]: set([z, y, x, 2]) In [3]: e.atoms(type=Symbol) Out[3]: set([z, y, x])
How can I make my editor highlight trailing whitespace red?
This depends on your editor.
- For Vim/Gvim, add the following to your .vimrc:
if has('gui_running')
hi WhiteSpaceEOL guibg=#FF0000
else
hi WhiteSpaceEOL ctermbg=Red
endif
match WhitespaceEOL /\s\+\%#\@<!$/
