Skip to content

Commit

Permalink
Remove generic python introduction from gotchas.rst
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Mar 7, 2015
1 parent bdf8ba0 commit d77e296
Showing 1 changed file with 0 additions and 209 deletions.
209 changes: 0 additions & 209 deletions doc/src/gotchas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -573,212 +573,3 @@ Special Symbols
The symbols ``[]``, ``{}``, ``=``, and ``()`` have special meanings in
Python, and thus in SymPy. See the Python docs linked to above for
additional information.

.. _lists:

Lists
-----

Square brackets ``[]`` denote a list. A list is a container that holds
any number of different objects. A list can contain anything, including
items of different types. Lists are mutable, which means that you can
change the elements of a list after it has been created. You access the
items of a list also using square brackets, placing them after the list
or list variable. Items are numbered using the space before the item.

.. note::

List indexes begin at 0.

Example:

>>> a = [x, 1] # A simple list of two items
>>> a
[x, 1]
>>> a[0] # This is the first item
x
>>> a[0] = 2 # You can change values of lists after they have been created
>>> print(a)
[2, 1]
>>> print(solve(x**2 + 2*x - 1, x)) # Some functions return lists
[-1 + sqrt(2), -sqrt(2) - 1]


.. note::
See the Python docs for more information on lists and the square
bracket notation for accessing elements of a list.

Dictionaries
------------

Curly brackets ``{}`` denote a dictionary, or a dict for short. A
dictionary is an unordered list of non-duplicate keys and values. The
syntax is ``{key: value}``. You can access values of keys using square
bracket notation.

>>> d = {'a': 1, 'b': 2} # A dictionary.
>>> d
{'a': 1, 'b': 2}
>>> d['a'] # How to access items in a dict
1
>>> roots((x - 1)**2*(x - 2), x) # Some functions return dicts
{1: 2, 2: 1}
>>> # Some SymPy functions return dictionaries. For example,
>>> # roots returns a dictionary of root:multiplicity items.
>>> roots((x - 5)**2*(x + 3), x)
{-3: 1, 5: 2}
>>> # This means that the root -3 occurs once and the root 5 occurs twice.

.. note::

See the Python docs for more information on dictionaries.

Tuples
------

Parentheses ``()``, aside from changing operator precedence and their
use in function calls, (like ``cos(x)``), are also used for tuples. A
``tuple`` is identical to a :ref:`list <lists>`, except that it is not
mutable. That means that you can not change their values after they
have been created. In general, you will not need tuples in SymPy, but
sometimes it can be more convenient to type parentheses instead of
square brackets.

>>> t = (1, 2, x) # Tuples are like lists
>>> t
(1, 2, x)
>>> t[0]
1
>>> t[0] = 4 # Except you can not change them after they have been created
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

Single element tuples, unlike lists, must have a comma in them:

>>> (x,)
(x,)

Without the comma, a single expression without a comma is not a tuple:

>>> (x)
x

integrate takes a sequence as the second argument if you want to integrate
with limits (and a tuple or list will work):

>>> integrate(x**2, (x, 0, 1))
1/3
>>> integrate(x**2, [x, 0, 1])
1/3


.. note::

See the Python docs for more information on tuples.

.. _keyword-arguments:

Keyword Arguments
-----------------

Aside from the usage described :ref:`above <equals-signs>`, equals signs
(``=``) are also used to give named arguments to functions. Any
function that has ``key=value`` in its parameters list (see below on how
to find this out), then ``key`` is set to ``value`` by default. You can
change the value of the key by supplying your own value using the equals
sign in the function call. Also, functions that have ``**`` followed by
a name in the parameters list (usually ``**kwargs`` or
``**assumptions``) allow you to add any number of ``key=value`` pairs
that you want, and they will all be evaluated according to the function.

``sqrt(x**2)`` doesn't auto simplify to x because x is assumed to be
complex by default, and, for example, ``sqrt((-1)**2) == sqrt(1) == 1 != -1``:

>>> sqrt(x**2)
sqrt(x**2)

Giving assumptions to Symbols is an example of using the keyword argument:

>>> x = Symbol('x', positive=True)

The square root will now simplify since it knows that ``x >= 0``:

>>> sqrt(x**2)
x

powsimp has a default argument of ``combine='all'``:

>>> pprint(powsimp(x**n*x**m*y**n*y**m))
m + n
(x*y)

Setting combine to the default value is the same as not setting it.

>>> pprint(powsimp(x**n*x**m*y**n*y**m, combine='all'))
m + n
(x*y)

The non-default options are ``'exp'``, which combines exponents...

>>> pprint(powsimp(x**n*x**m*y**n*y**m, combine='exp'))
m + n m + n
x *y

...and 'base', which combines bases.

>>> pprint(powsimp(x**n*x**m*y**n*y**m, combine='base'))
m n
(x*y) *(x*y)

.. note::

See the Python docs for more information on function parameters.

Getting help from within SymPy
==============================

help()
------

Although all docs are available at `docs.sympy.org <http://docs.sympy.org/>`_ or on the
`SymPy Wiki <http://wiki.sympy.org/>`_, you can also get info on functions from within the
Python interpreter that runs SymPy. The easiest way to do this is to do
``help(function)``, or ``function?`` if you are using :command:`ipython`::

In [1]: help(powsimp) # help() works everywhere

In [2]: # But in ipython, you can also use ?, which is better because it
In [3]: # it gives you more information
In [4]: powsimp?

These will give you the function parameters and docstring for
:func:`powsimp`. The output will look something like this:

.. module:: sympy.simplify.simplify
.. autofunction:noindex: powsimp
source()
--------

Another useful option is the :func:`source` function. This will print
the source code of a function, including any docstring that it may have.
You can also do ``function??`` in :command:`ipython`. For example,
from SymPy 0.6.5:

>>> source(simplify) # simplify() is actually only 2 lines of code. #doctest: +SKIP
In file: ./sympy/simplify/simplify.py
def simplify(expr):
"""Naively simplifies the given expression.
...
Simplification is not a well defined term and the exact strategies
this function tries can change in the future versions of SymPy. If
your algorithm relies on "simplification" (whatever it is), try to
determine what you need exactly - is it powsimp()? radsimp()?
together()?, logcombine()?, or something else? And use this particular
function directly, because those are well defined and thus your algorithm
will be robust.
...
"""
expr = Poly.cancel(powsimp(expr))
return powsimp(together(expr.expand()), combine='exp', deep=True)

0 comments on commit d77e296

Please sign in to comment.