Base class for all objects in sympy.
Conventions:
1) When you want to access parameters of some instance, always use .args: Example:
>>> from sympy import symbols, cot
>>> x, y = symbols('xy')
>>> cot(x).args
(x,)
>>> cot(x).args[0]
x
>>> (x*y).args
(x, y)
>>> (x*y).args[1]
y
2) Never use internal methods or variables (the ones prefixed with “_”). Example:
>>> cot(x)._args #don't use this, use cot(x).args instead
(x,)
Converts polynomial to a valid sympy expression.
>>> from sympy import *
>>> x,y = symbols('xy')
>>> p = (x**2 + x*y).as_poly(x, y)
>>> p.as_basic()
x*y + x**2
>>> f = sin(x)
>>> f.as_basic()
sin(x)
Extracts symbolic coefficient at the given expression. In other words, this functions separates ‘self’ into product of ‘expr’ and ‘expr’-free coefficient. If such separation is not possible it will return None.
>>> from sympy import *
>>> x, y = symbols('xy')
>>> E.as_coefficient(E)
1
>>> (2*E).as_coefficient(E)
2
>>> (2*E + x).as_coefficient(E)
>>> (2*sin(E)*E).as_coefficient(E)
>>> (2*pi*I).as_coefficient(pi*I)
2
>>> (2*I).as_coefficient(pi*I)
Returns a pair with separated parts of a given expression independent of specified symbols in the first place and dependent on them in the other. Both parts are valid SymPy expressions.
>>> from sympy import *
>>> x, y = symbols('xy')
>>> (2*x*sin(x)+y+x).as_independent(x)
(y, x + 2*x*sin(x))
>>> (x*sin(x)*cos(y)).as_independent(x)
(cos(y), x*sin(x))
All other expressions are multiplicative:
>>> (sin(x)).as_independent(x)
(1, sin(x))
>>> (sin(x)).as_independent(y)
(sin(x), 1)
Converts ‘self’ to a polynomial or returns None.
When constructing a polynomial an exception will be raised in case the input expression is not convertible to a polynomial. There are situations when it is easier (simpler or prettier) to receive None on failure.
If no symbols were given and ‘self’ isn’t already a polynomial then all available symbols will be collected and used to form a new polynomial.
>>> from sympy import *
>>> x,y = symbols('xy')
>>> print (x**2 + x*y).as_poly()
Poly(x**2 + x*y, x, y)
>>> print (x**2 + x*y).as_poly(x, y)
Poly(x**2 + x*y, x, y)
>>> print (x**2 + sin(y)).as_poly(x, y)
None
Performs complex expansion on ‘self’ and returns a tuple containing collected both real and imaginary parts. This method can’t be confused with re() and im() functions, which does not perform complex expansion at evaluation.
However it is possible to expand both re() and im() functions and get exactly the same results as with a single call to this function.
>>> from sympy import *
>>> x, y = symbols('xy', real=True)
>>> (x + y*I).as_real_imag()
(x, y)
>>> z, w = symbols('zw')
>>> (z + w*I).as_real_imag()
(-im(w) + re(z), im(z) + re(w))
Returns the atoms that form the current object.
>>> from sympy import *
>>> x,y = symbols('xy')
>>> sorted((x+y**2 + 2*x*y).atoms())
[2, x, y]
You can also filter the results by a given type(s) of object:
>>> sorted((x+y+2+y**2*sin(x)).atoms(Symbol))
[x, y]
>>> sorted((x+y+2+y**3*sin(x)).atoms(Number))
[2, 3]
>>> sorted((x+y+2+y**2*sin(x)).atoms(Symbol, Number))
[2, x, y]
Or by a type of on object in an impliciy way:
>>> sorted((x+y+2+y**2*sin(x)).atoms(x))
[x, y]
Is a>b in the sense of ordering in printing?
yes ..... return 1 no ...... return -1 equal ... return 0
Strategy:
It uses Basic.compare as a fallback, but improves it in many cases, like x**3, x**4, O(x**3) etc. In those simple cases, it just parses the expression and returns the “sane” ordering such as:
1 < x < x**2 < x**3 < O(x**4) etc.
Canonical way to choose an element in the set {e, -e} where e is any expression. If the canonical element is e, we have e.could_extract_minus_sign() == True, else e.could_extract_minus_sign() == False.
For any expression, the set {e.could_extract_minus_sign(), (-e).could_extract_minus_sign()} must be {True, False}.
>>> from sympy import *
>>> x, y = symbols("xy")
>>> (x-y).could_extract_minus_sign() != (y-x).could_extract_minus_sign()
True
Return the number of operations in expressions.
Examples: >>> (1+a+b**2).count_ops() POW + 2 * ADD >>> (sin(x)*x+sin(x)**2).count_ops() ADD + MUL + POW + 2 * SIN
Evaluate objects that are not evaluated by default like limits, integrals, sums and products. All objects of this kind will be evaluated unless some species were excluded via ‘hints’.
>>> from sympy import *
>>> x, y = symbols('xy')
>>> 2*Integral(x, x)
2*Integral(x, x)
>>> (2*Integral(x, x)).doit()
x**2
Evaluate the given formula to an accuracy of n digits. Optional keyword arguments:
- subs=<dict>
- Substitute numerical values for symbols, e.g. subs={x:3, y:1+pi}.
- maxprec=N
- Allow a maximum temporary working precision of N digits (default=100)
- chop=<bool>
- Replace tiny real or imaginary parts in subresults by exact zeros (default=False)
- strict=<bool>
- Raise PrecisionExhausted if any subresult fails to evaluate to full accuracy, given the available maxprec (default=False)
- quad=<str>
- Choose algorithm for numerical quadrature. By default, tanh-sinh quadrature is used. For oscillatory integrals on an infinite interval, try quad=’osc’.
- verbose=<bool>
- Print debug information (default=False)
Expand an expression using hints.
Currently supported hints are basic, power, complex, trig and func. Hints are applied with arbitrary order so your code shouldn’t depend on the way hints are passed to this method. Expand ‘basic’ is the default and run always, provided that it isn’t turned off by the user.
>>> from sympy import *
>>> x,y = symbols('xy')
>>> (y*(x + y)**2).expand()
y*x**2 + 2*x*y**2 + y**3
>>> (x+y).expand(complex=True)
I*im(x) + I*im(y) + re(x) + re(y)
Return None if it’s not possible to make self in the form something + c in a nice way, i.e. preserving the properties of arguments of self.
>>> from sympy import *
>>> x, y = symbols('xy', real=True)
>>> ((x*y)**3).extract_additively(1)
>>> (x+1).extract_additively(x)
1
>>> (x+1).extract_additively(2*x)
>>> (x+1).extract_additively(-x)
1 + 2*x
>>> (-x+1).extract_additively(2*x)
1 - 3*x
Return None if it’s not possible to make self in the form c * something in a nice way, i.e. preserving the properties of arguments of self.
>>> from sympy import *
>>> x, y = symbols('xy', real=True)
>>> ((x*y)**3).extract_multiplicatively(x**2 * y)
x*y**2
>>> ((x*y)**3).extract_multiplicatively(x**4 * y)
>>> (2*x).extract_multiplicatively(2)
x
>>> (2*x).extract_multiplicatively(3)
>>> (Rational(1,2)*x).extract_multiplicatively(3)
x/6
Return True if ‘self’ has all of the symbols.
>>> from sympy import *
>>> x,y,z = symbols('xyz')
>>> (x**2 + sin(x*y)).has_all_symbols(x, y)
True
>>> (x**2 + sin(x*y)).has_all_symbols(x, y, z)
False
Return True if ‘self’ has any of the symbols.
>>> from sympy import *
>>> x,y,z = symbols('xyz')
>>> (x**2 + sin(x*y)).has_any_symbols(z)
False
>>> (x**2 + sin(x*y)).has_any_symbols(x, y)
True
>>> (x**2 + sin(x*y)).has_any_symbols(x, y, z)
True
Pattern matching.
Wild symbols match all.
Return None when expression (self) does not match with pattern. Otherwise return a dictionary such that
pattern.subs(self.match(pattern)) == self
Helper method for match() - switches the pattern and expr.
>>> from sympy import Symbol, Wild, Integer
>>> a,b = map(Symbol, 'ab')
>>> x = Wild('x')
>>> (a+b*x).matches(Integer(0))
{x_: -a/b}
Evaluate the given formula to an accuracy of n digits. Optional keyword arguments:
- subs=<dict>
- Substitute numerical values for symbols, e.g. subs={x:3, y:1+pi}.
- maxprec=N
- Allow a maximum temporary working precision of N digits (default=100)
- chop=<bool>
- Replace tiny real or imaginary parts in subresults by exact zeros (default=False)
- strict=<bool>
- Raise PrecisionExhausted if any subresult fails to evaluate to full accuracy, given the available maxprec (default=False)
- quad=<str>
- Choose algorithm for numerical quadrature. By default, tanh-sinh quadrature is used. For oscillatory integrals on an infinite interval, try quad=’osc’.
- verbose=<bool>
- Print debug information (default=False)
create new ‘similar’ object
this is conceptually equivalent to:
type(self) (*args)
but takes type assumptions into account.
see: assumptions0
Calculates a generalized series expansion.
The difference between oseries and nseries is that nseries calculates “n” terms in the innermost expressions and then builds up the final series just by “cross-mutliplying” everything out.
Advantage – it’s fast, because we don’t have to determine how many terms we need to calculate in advance.
Disadvantage – you may endup with less terms than you may have expected, but the O(x**n) term appended will always be correct, so the result is correct, but maybe shorter.
Return the series of an expression up to given Order symbol (without the actual O term).
The general philosophy is this: simply start with the most simple Taylor (Laurent) term and calculate one be one and use order.contains(term) method to determine if your term is still significant and should be added to the series, or we should stop.
Rewrites expression containing applications of functions of one kind in terms of functions of different kind. For example you can rewrite trigonometric functions as complex exponentials or combinatorial functions as gamma function.
As a pattern this function accepts a list of functions to to rewrite (instances of DefinedFunction class). As rule you can use string or a destination function instance (in this case rewrite() will use the str() function).
There is also possibility to pass hints on how to rewrite the given expressions. For now there is only one such hint defined called ‘deep’. When ‘deep’ is set to False it will forbid functions to rewrite their contents.
>>> from sympy import *
>>> x, y = symbols('xy')
>>> sin(x).rewrite(sin, exp)
-I*(exp(I*x) - exp(-I*x))/2
Series expansion of “self” around “point”.
Returns the Taylor (Laurent or generalized) series of “self” around the point “point” (default 0) with respect to “x” until the n-th term (default n is 6).
This method is the most high level method and it returns the series including the O(x**n) term.
Internally, it executes a method oseries(), which takes an O instance as the only parameter and it is responsible for returning a series (without the O term) up to the given order.
Solve equation “eqn == rhs” with respect to some linear symbol in eqn.
Returns (symbol, solution). If eqn is nonlinear with respect to all symbols, then return trivial solution (eqn, rhs).
Substitutes an expression.
Calls either _subs_old_new, _subs_dict or _subs_list depending if you give it two arguments (old, new), a dictionary or a list.
Examples:
>>> from sympy import *
>>> x,y = symbols('xy')
>>> (1+x*y).subs(x, pi)
1 + pi*y
>>> (1+x*y).subs({x:pi, y:2})
1 + 2*pi
>>> (1+x*y).subs([(x,pi), (y,2)])
1 + 2*pi
>>> A,B = symbols('AB', commutative = False)
>>> bool(A*B != B*A)
True
>>> bool(A*B*2 == 2*A*B) == True # multiplication by scalars is commutative
True
Returns the leading term and it’s order.
Examples:
>>> (x+1+1/x**5).extract_leading_order(x)
((1/x**5, O(1/x**5)),)
>>> (1+x).extract_leading_order(x)
((1, O(1, x)),)
>>> (x+x**2).extract_leading_order(x)
((x, O(x)),)
Takes the sequence “seq” of nested Adds and returns a flatten list.
Returns: (commutative_part, noncommutative_part, order_symbols)
Applies associativity, all terms are commutable with respect to addition.