Inheritance diagram for nipy.modalities.fmri.formula:
A formula is basically a sympy expression for the mean of something of the form:
mean = sum([Beta(e)*e for e in expr])
Or, a linear combination of sympy expressions, with each one multiplied by its own “Beta”. The elements of expr can be instances of Term (for a linear regression formula, they would all be instances of Term). But, in general, there might be some other parameters (i.e. sympy.Symbol instances) that are not Terms.
The design matrix is made up of columns that are the derivatives of mean with respect to everything that is not a Term, evaluted at a recarray that has field names given by [str(t) for t in self.terms].
For those familiar with R’s formula syntax, if we wanted a design matrix like the following:
> s.table = read.table("http://www-stat.stanford.edu/~jtaylo/courses/stats191/data/supervisor.table", header=T)
> d = model.matrix(lm(Y ~ X1*X3, s.table)
)
> d
(Intercept) X1 X3 X1:X3
1 1 51 39 1989
2 1 64 54 3456
3 1 70 69 4830
4 1 63 47 2961
5 1 78 66 5148
6 1 55 44 2420
7 1 67 56 3752
8 1 75 55 4125
9 1 82 67 5494
10 1 61 47 2867
11 1 53 58 3074
12 1 60 39 2340
13 1 62 42 2604
14 1 83 45 3735
15 1 77 72 5544
16 1 90 72 6480
17 1 85 69 5865
18 1 60 75 4500
19 1 70 57 3990
20 1 58 54 3132
21 1 40 34 1360
22 1 61 62 3782
23 1 66 50 3300
24 1 37 58 2146
25 1 54 48 2592
26 1 77 63 4851
27 1 75 74 5550
28 1 57 45 2565
29 1 85 71 6035
30 1 82 59 4838
attr(,"assign")
[1] 0 1 2 3
>
With the Formula, it looks like this:
>>> r = np.rec.array([
... (43, 51, 30, 39, 61, 92, 45), (63, 64, 51, 54, 63, 73, 47),
... (71, 70, 68, 69, 76, 86, 48), (61, 63, 45, 47, 54, 84, 35),
... (81, 78, 56, 66, 71, 83, 47), (43, 55, 49, 44, 54, 49, 34),
... (58, 67, 42, 56, 66, 68, 35), (71, 75, 50, 55, 70, 66, 41),
... (72, 82, 72, 67, 71, 83, 31), (67, 61, 45, 47, 62, 80, 41),
... (64, 53, 53, 58, 58, 67, 34), (67, 60, 47, 39, 59, 74, 41),
... (69, 62, 57, 42, 55, 63, 25), (68, 83, 83, 45, 59, 77, 35),
... (77, 77, 54, 72, 79, 77, 46), (81, 90, 50, 72, 60, 54, 36),
... (74, 85, 64, 69, 79, 79, 63), (65, 60, 65, 75, 55, 80, 60),
... (65, 70, 46, 57, 75, 85, 46), (50, 58, 68, 54, 64, 78, 52),
... (50, 40, 33, 34, 43, 64, 33), (64, 61, 52, 62, 66, 80, 41),
... (53, 66, 52, 50, 63, 80, 37), (40, 37, 42, 58, 50, 57, 49),
... (63, 54, 42, 48, 66, 75, 33), (66, 77, 66, 63, 88, 76, 72),
... (78, 75, 58, 74, 80, 78, 49), (48, 57, 44, 45, 51, 83, 38),
... (85, 85, 71, 71, 77, 74, 55), (82, 82, 39, 59, 64, 78, 39)],
... dtype=[('y', '<i8'), ('x1', '<i8'), ('x2', '<i8'),
... ('x3', '<i8'), ('x4', '<i8'), ('x5', '<i8'),
... ('x6', '<i8')])
>>> x1 = Term('x1'); x3 = Term('x3')
>>> f = Formula([x1, x3, x1*x3]) + I
>>> f.mean
_b0*x1 + _b1*x3 + _b2*x1*x3 + _b3
The I is the “intercept” term, I have explicity not used R’s default of adding it to everything.
>>> f.design(r)
array([(51.0, 39.0, 1989.0, 1.0), (64.0, 54.0, 3456.0, 1.0),
(70.0, 69.0, 4830.0, 1.0), (63.0, 47.0, 2961.0, 1.0),
(78.0, 66.0, 5148.0, 1.0), (55.0, 44.0, 2420.0, 1.0),
(67.0, 56.0, 3752.0, 1.0), (75.0, 55.0, 4125.0, 1.0),
(82.0, 67.0, 5494.0, 1.0), (61.0, 47.0, 2867.0, 1.0),
(53.0, 58.0, 3074.0, 1.0), (60.0, 39.0, 2340.0, 1.0),
(62.0, 42.0, 2604.0, 1.0), (83.0, 45.0, 3735.0, 1.0),
(77.0, 72.0, 5544.0, 1.0), (90.0, 72.0, 6480.0, 1.0),
(85.0, 69.0, 5865.0, 1.0), (60.0, 75.0, 4500.0, 1.0),
(70.0, 57.0, 3990.0, 1.0), (58.0, 54.0, 3132.0, 1.0),
(40.0, 34.0, 1360.0, 1.0), (61.0, 62.0, 3782.0, 1.0),
(66.0, 50.0, 3300.0, 1.0), (37.0, 58.0, 2146.0, 1.0),
(54.0, 48.0, 2592.0, 1.0), (77.0, 63.0, 4851.0, 1.0),
(75.0, 74.0, 5550.0, 1.0), (57.0, 45.0, 2565.0, 1.0),
(85.0, 71.0, 6035.0, 1.0), (82.0, 59.0, 4838.0, 1.0)],
dtype=[('x1', '<f8'), ('x3', '<f8'), ('x1*x3', '<f8'), ('1', '<f8')])
Bases: sympy.core.symbol.Dummy
A symbol tied to a Term term
Methods
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Returns a tuple of arguments of ‘self’.
Example:
>>> from sympy import symbols, cot
>>> from sympy.abc import x, y
>>> cot(x).args
(x,)
>>> cot(x).args[0]
x
>>> (x*y).args
(x, y)
>>> (x*y).args[1]
y
Note for developers: Never use self._args, always use self.args. Only when you are creating your own new function, use _args in the __new__. Don’t override .args() from Basic (so that it’s easy to change the interface in the future if needed).
Converts polynomial to a valid sympy expression.
>>> from sympy import sin
>>> from sympy.abc import x, y
>>> p = (x**2 + x*y).as_poly(x, y)
>>> p.as_basic()
x*y + x**2
>>> f = sin(x)
>>> f.as_basic()
sin(x)
c*x**e -> c,e where x can be any symbolic expression.
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 E, pi, sin, I
>>> from sympy.abc import x, y
>>> 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 sin, cos
>>> from sympy.abc import x, y
>>> (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)
Returns the leading term.
Example:
>>> from sympy.abc import x
>>> (1+x+x**2).as_leading_term(x)
1
>>> (1/x**2+x+x**2).as_leading_term(x)
x**(-2)
Note:
self is assumed to be the result returned by Basic.series().
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 Poly, sin
>>> from sympy.abc import x, y
>>> 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 symbols, I
>>> x, y = symbols('xy', real=True)
>>> (x + y*I).as_real_imag()
(x, y)
>>> from sympy.abc import z, w
>>> (z + w*I).as_real_imag()
(-im(w) + re(z), im(z) + re(w))
Return object type assumptions.
For example:
Symbol(‘x’, real=True) Symbol(‘x’, integer=True)
are different objects. In other words, besides Python type (Symbol in this case), the initial assumptions are also forming their typeinfo.
Example:
>>> from sympy import Symbol
>>> from sympy.abc import x
>>> x.assumptions0
{}
>>> x = Symbol("x", positive=True)
>>> x.assumptions0
{'commutative': True, 'complex': True, 'imaginary': False,
'negative': False, 'nonnegative': True, 'nonpositive': False,
'nonzero': True, 'positive': True, 'real': True, 'zero': False}
Returns the atoms that form the current object.
By default, only objects that are truly atomic and can’t be divided into smaller pieces are returned: symbols, numbers, and number symbols like I and pi. It is possible to request atoms of any type, however, as demonstrated below.
Examples:
>>> from sympy import I, pi, sin
>>> from sympy.abc import x, y
>>> list((1+x+2*sin(y+I*pi)).atoms())
[y, I, 2, x, 1, pi]
If one or more types are given, the results will contain only those types of atoms:
Examples:
>>> from sympy import Number, NumberSymbol, Symbol
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Symbol))
[x, y]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number))
[1, 2]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol))
[1, 2, pi]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol, I))
[1, 2, pi, I]
Note that I (imaginary unit) and zoo (complex infinity) are special types of number symbols and are not part of the NumberSymbol class.
The type can be given implicitly, too:
>>> sorted((1+x+2*sin(y+I*pi)).atoms(x)) # x is a Symbol
[x, y]
Be careful to check your assumptions when using the implicit option since S(1).is_Integer = True but type(S(1)) is One, a special type of sympy atom, while type(S(2)) is type Integer and will find all integers in an expression:
>>> from sympy import S
>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(1)))
[1]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(2)))
[1, 2]
Finally, arguments to atoms() can select more than atomic atoms: any sympy type (loaded in core/__init__.py) can be listed as an argument and those types of “atoms” as found in scanning the arguments of the expression nonrecursively:
>>> from sympy import Function, Mul
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Function))
[sin(y + pi*I)]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Mul))
[2*sin(y + pi*I)]
Returns the coefficient of the term “x” or None if there is no “x”.
Optional expand keyword argument allows one to control whether the expression is expanded before terms are collected, which can be useful if the term “x” isn’t nested inside of terms and you don’t want the returned coefficient to be expanded.
Example:
>>> from sympy import symbols
>>> from sympy.abc import x, y, z
>>> (3+2*x+4*x**2).coeff(1)
>>> (3+2*x+4*x**2).coeff(x)
2
>>> (3+2*x+4*x**2).coeff(x**2)
4
>>> (3+2*x+4*x**2).coeff(x**3)
>>> (z*(x+y)**2).coeff(z)
2*x*y + x**2 + y**2
>>> (z*(x+y)**2).coeff(z, expand=False)
(x + y)**2
>>>
Return -1,0,1 if the object is smaller, equal, or greater than other.
Not in the mathematical sense. If the object is of a different type from the “other” then their classes are ordered according to the sorted_classes list.
Example:
>>> from sympy.abc import x, y
>>> x.compare(y)
-1
>>> x.compare(x)
0
>>> y.compare(x)
1
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.
Example:
>>> from sympy.abc import x
>>> from sympy import Basic
>>> Basic._compare_pretty(x, x**2)
-1
>>> Basic._compare_pretty(x**2, x**2)
0
>>> Basic._compare_pretty(x**3, x**2)
1
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.abc import x, y
>>> (x-y).could_extract_minus_sign() != (y-x).could_extract_minus_sign()
True
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.
See the docstring in function.expand for more information.
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 symbols
>>> 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 symbols, Rational
>>> 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
The top-level function in an expression.
The following should hold for all objects:
>> x == x.func(*x.args)
Example:
>>> from sympy.abc import x
>>> a = 2*x
>>> a.func
<class 'sympy.core.mul.Mul'>
>>> a.args
(2, x)
>>> a.func(*a.args)
2*x
>>> a == a.func(*a.args)
True
Returns the O(..) symbol, or None if there is none.
Return True if self has any of the patterns.
Example: >>> from sympy.abc import x >>> (2*x).has(x) True >>> (2*x/x).has(x) False
Return True if ‘self’ has all of the symbols.
>>> from sympy import sin
>>> from sympy.abc import x, y, z
>>> (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 sin
>>> from sympy.abc import x, y, z
>>> (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
Returns True if any args are Piecewise or has_piecewise
Test whether function is rational function - ratio of two polynomials. When no arguments are present, Basic.atoms(Symbol) is used instead.
Example:
>>> from sympy import symbols, sin
>>> from sympy.abc import x, y
>>> (x/y).is_rational_function()
True
>>> (x**2).is_rational_function()
True
>>> (x/sin(y)).is_rational_function(y)
False
Iterates arguments of ‘self’.
Example:
>>> from sympy.abc import x
>>> a = 2*x
>>> a.iter_basic_args()
<tupleiterator object at 0x...>
>>> list(a.iter_basic_args())
[2, x]
Returns the leading term a*x**b as a tuple (a, b).
Example:
>>> from sympy.abc import x
>>> (1+x+x**2).leadterm(x)
(1, 0)
>>> (1/x**2+x+x**2).leadterm(x)
(1, -2)
Note:
self is assumed to be the result returned by Basic.series().
Compute limit x->xlim.
lseries is a generator yielding terms in the series.
Example: if you do:
It will print all terms of the sin(x) series (i.e. it never terminates).
The advantage of lseries() over nseries() is that many times you are just interested in the next term in the series (i.e. the first term for example), but you don’t know how many you should ask for in nseries() using the “n” parameter.
See also nseries().
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
Example:
>>> from sympy import symbols, Wild
>>> from sympy.abc import x, y
>>> p = Wild("p")
>>> q = Wild("q")
>>> r = Wild("r")
>>> e = (x+y)**(x+y)
>>> e.match(p**p)
{p_: x + y}
>>> e.match(p**q)
{p_: x + y, q_: x + y}
>>> e = (2*x)**2
>>> e.match(p*q**r)
{p_: 4, q_: x, r_: 2}
>>> (p*q**r).subs(e.match(p*q**r))
4*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)
Create new ‘similar’ object.
this is conceptually equivalent to:
type(self) (*args)
but takes type assumptions into account. See also: assumptions0
Example:
>>> from sympy.abc import x
>>> x.new("x")
x
Calculates a generalized series expansion.
nseries calculates “n” terms in the innermost expressions and then builds up the final series just by “cross-multiplying” 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 end up with less terms than you may have expected, but the O(x**n) term appended will always be correct and so the result, though perhaps shorter, will also be correct.
See also lseries().
Removes the O(..) symbol if there is one
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 sin, exp, I
>>> from sympy.abc import x, y
>>> 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).
For dir=”+” (default) it calculates the series from the right and for dir=”-” the series from the left. For smooth functions this argument doesn’t matter.
This method is the most high level method and it returns the series including the O(x**n) term.
Internally, it executes a method nseries(), see nseries() docstring for more information.
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 pi
>>> from sympy.abc import x, y
>>> (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
Bases: nipy.modalities.fmri.formula.Formula
A qualitative variable in a regression model
A Factor is similar to R’s factor. The levels of the Factor can be either strings or ints.
Methods
design | |
fromcol | |
fromrec | |
get_term | |
stratify | |
subs |
Parameters : | name : str levels : [str or int]
char : str |
---|
Coefficients in the linear regression formula.
Construct the design matrix, and optional contrast matrices.
Parameters : | input : np.recarray
param : None or np.recarray
return_float : bool, optional
contrasts : None or dict, optional
|
---|
The dtype of the design matrix of the Formula.
Create a Factor from a column array.
Parameters : | col : ndarray
name : str
|
---|---|
Returns : | factor : Factor |
Examples
>>> data = np.array([(3,'a'),(4,'a'),(5,'b'),(3,'b')], np.dtype([('x', np.float), ('y', 'S1')]))
>>> f1 = Factor.fromcol(data['y'], 'y')
>>> f2 = Factor.fromcol(data['x'], 'x')
>>> d = f1.design(data)
>>> print d.dtype.descr
[('y_a', '<f8'), ('y_b', '<f8')]
>>> d = f2.design(data)
>>> print d.dtype.descr
[('x_3', '<f8'), ('x_4', '<f8'), ('x_5', '<f8')]
Construct Formula from recarray
For fields with a string-dtype, it is assumed that these are qualtiatitve regressors, i.e. Factors.
Parameters : | rec: recarray :
keep: [] :
drop: [] :
|
---|
Retrieve a term of the Factor...
Expression for the mean, expressed as a linear combination of terms, each with dummy variables in front.
The parameters in the Formula.
Create a new variable, stratified by the levels of a Factor.
Parameters : | variable : str or a simple sympy expression whose string representation
|
---|---|
Returns : | formula : Formula
|
Examples
>>> f = Factor('a', ['x','y'])
>>> sf = f.stratify('theta')
>>> sf.mean
_theta0*a_x + _theta1*a_y
Perform a sympy substitution on all terms in the Formula
Returns a new instance of the same class
Parameters : | old : sympy.Basic
new : sympy.Basic
|
---|---|
Returns : | newf : Formula |
Examples
>>> s, t = [Term(l) for l in 'st']
>>> f, g = [sympy.Function(l) for l in 'fg']
>>> form = Formula([f(t),g(s)])
>>> newform = form.subs(g, sympy.Function('h'))
>>> newform.terms
array([f(t), h(s)], dtype=object)
>>> form.terms
array([f(t), g(s)], dtype=object)
Terms in the linear regression formula.
Bases: nipy.modalities.fmri.formula.Term
Boolean Term derived from a Factor.
Its properties are the same as a Term except that its product with itself is itself.
Methods
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Returns a tuple of arguments of ‘self’.
Example:
>>> from sympy import symbols, cot
>>> from sympy.abc import x, y
>>> cot(x).args
(x,)
>>> cot(x).args[0]
x
>>> (x*y).args
(x, y)
>>> (x*y).args[1]
y
Note for developers: Never use self._args, always use self.args. Only when you are creating your own new function, use _args in the __new__. Don’t override .args() from Basic (so that it’s easy to change the interface in the future if needed).
Converts polynomial to a valid sympy expression.
>>> from sympy import sin
>>> from sympy.abc import x, y
>>> p = (x**2 + x*y).as_poly(x, y)
>>> p.as_basic()
x*y + x**2
>>> f = sin(x)
>>> f.as_basic()
sin(x)
c*x**e -> c,e where x can be any symbolic expression.
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 E, pi, sin, I
>>> from sympy.abc import x, y
>>> 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 sin, cos
>>> from sympy.abc import x, y
>>> (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)
Returns the leading term.
Example:
>>> from sympy.abc import x
>>> (1+x+x**2).as_leading_term(x)
1
>>> (1/x**2+x+x**2).as_leading_term(x)
x**(-2)
Note:
self is assumed to be the result returned by Basic.series().
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 Poly, sin
>>> from sympy.abc import x, y
>>> 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 symbols, I
>>> x, y = symbols('xy', real=True)
>>> (x + y*I).as_real_imag()
(x, y)
>>> from sympy.abc import z, w
>>> (z + w*I).as_real_imag()
(-im(w) + re(z), im(z) + re(w))
Return object type assumptions.
For example:
Symbol(‘x’, real=True) Symbol(‘x’, integer=True)
are different objects. In other words, besides Python type (Symbol in this case), the initial assumptions are also forming their typeinfo.
Example:
>>> from sympy import Symbol
>>> from sympy.abc import x
>>> x.assumptions0
{}
>>> x = Symbol("x", positive=True)
>>> x.assumptions0
{'commutative': True, 'complex': True, 'imaginary': False,
'negative': False, 'nonnegative': True, 'nonpositive': False,
'nonzero': True, 'positive': True, 'real': True, 'zero': False}
Returns the atoms that form the current object.
By default, only objects that are truly atomic and can’t be divided into smaller pieces are returned: symbols, numbers, and number symbols like I and pi. It is possible to request atoms of any type, however, as demonstrated below.
Examples:
>>> from sympy import I, pi, sin
>>> from sympy.abc import x, y
>>> list((1+x+2*sin(y+I*pi)).atoms())
[y, I, 2, x, 1, pi]
If one or more types are given, the results will contain only those types of atoms:
Examples:
>>> from sympy import Number, NumberSymbol, Symbol
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Symbol))
[x, y]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number))
[1, 2]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol))
[1, 2, pi]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol, I))
[1, 2, pi, I]
Note that I (imaginary unit) and zoo (complex infinity) are special types of number symbols and are not part of the NumberSymbol class.
The type can be given implicitly, too:
>>> sorted((1+x+2*sin(y+I*pi)).atoms(x)) # x is a Symbol
[x, y]
Be careful to check your assumptions when using the implicit option since S(1).is_Integer = True but type(S(1)) is One, a special type of sympy atom, while type(S(2)) is type Integer and will find all integers in an expression:
>>> from sympy import S
>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(1)))
[1]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(2)))
[1, 2]
Finally, arguments to atoms() can select more than atomic atoms: any sympy type (loaded in core/__init__.py) can be listed as an argument and those types of “atoms” as found in scanning the arguments of the expression nonrecursively:
>>> from sympy import Function, Mul
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Function))
[sin(y + pi*I)]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Mul))
[2*sin(y + pi*I)]
Returns the coefficient of the term “x” or None if there is no “x”.
Optional expand keyword argument allows one to control whether the expression is expanded before terms are collected, which can be useful if the term “x” isn’t nested inside of terms and you don’t want the returned coefficient to be expanded.
Example:
>>> from sympy import symbols
>>> from sympy.abc import x, y, z
>>> (3+2*x+4*x**2).coeff(1)
>>> (3+2*x+4*x**2).coeff(x)
2
>>> (3+2*x+4*x**2).coeff(x**2)
4
>>> (3+2*x+4*x**2).coeff(x**3)
>>> (z*(x+y)**2).coeff(z)
2*x*y + x**2 + y**2
>>> (z*(x+y)**2).coeff(z, expand=False)
(x + y)**2
>>>
Return -1,0,1 if the object is smaller, equal, or greater than other.
Not in the mathematical sense. If the object is of a different type from the “other” then their classes are ordered according to the sorted_classes list.
Example:
>>> from sympy.abc import x, y
>>> x.compare(y)
-1
>>> x.compare(x)
0
>>> y.compare(x)
1
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.
Example:
>>> from sympy.abc import x
>>> from sympy import Basic
>>> Basic._compare_pretty(x, x**2)
-1
>>> Basic._compare_pretty(x**2, x**2)
0
>>> Basic._compare_pretty(x**3, x**2)
1
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.abc import x, y
>>> (x-y).could_extract_minus_sign() != (y-x).could_extract_minus_sign()
True
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.
See the docstring in function.expand for more information.
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 symbols
>>> 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 symbols, Rational
>>> 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 a Formula with only terms=[self].
The top-level function in an expression.
The following should hold for all objects:
>> x == x.func(*x.args)
Example:
>>> from sympy.abc import x
>>> a = 2*x
>>> a.func
<class 'sympy.core.mul.Mul'>
>>> a.args
(2, x)
>>> a.func(*a.args)
2*x
>>> a == a.func(*a.args)
True
Returns the O(..) symbol, or None if there is none.
Return True if self has any of the patterns.
Example: >>> from sympy.abc import x >>> (2*x).has(x) True >>> (2*x/x).has(x) False
Return True if ‘self’ has all of the symbols.
>>> from sympy import sin
>>> from sympy.abc import x, y, z
>>> (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 sin
>>> from sympy.abc import x, y, z
>>> (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
Returns True if any args are Piecewise or has_piecewise
Test whether function is rational function - ratio of two polynomials. When no arguments are present, Basic.atoms(Symbol) is used instead.
Example:
>>> from sympy import symbols, sin
>>> from sympy.abc import x, y
>>> (x/y).is_rational_function()
True
>>> (x**2).is_rational_function()
True
>>> (x/sin(y)).is_rational_function(y)
False
Iterates arguments of ‘self’.
Example:
>>> from sympy.abc import x
>>> a = 2*x
>>> a.iter_basic_args()
<tupleiterator object at 0x...>
>>> list(a.iter_basic_args())
[2, x]
Returns the leading term a*x**b as a tuple (a, b).
Example:
>>> from sympy.abc import x
>>> (1+x+x**2).leadterm(x)
(1, 0)
>>> (1/x**2+x+x**2).leadterm(x)
(1, -2)
Note:
self is assumed to be the result returned by Basic.series().
Compute limit x->xlim.
lseries is a generator yielding terms in the series.
Example: if you do:
It will print all terms of the sin(x) series (i.e. it never terminates).
The advantage of lseries() over nseries() is that many times you are just interested in the next term in the series (i.e. the first term for example), but you don’t know how many you should ask for in nseries() using the “n” parameter.
See also nseries().
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
Example:
>>> from sympy import symbols, Wild
>>> from sympy.abc import x, y
>>> p = Wild("p")
>>> q = Wild("q")
>>> r = Wild("r")
>>> e = (x+y)**(x+y)
>>> e.match(p**p)
{p_: x + y}
>>> e.match(p**q)
{p_: x + y, q_: x + y}
>>> e = (2*x)**2
>>> e.match(p*q**r)
{p_: 4, q_: x, r_: 2}
>>> (p*q**r).subs(e.match(p*q**r))
4*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)
Create new ‘similar’ object.
this is conceptually equivalent to:
type(self) (*args)
but takes type assumptions into account. See also: assumptions0
Example:
>>> from sympy.abc import x
>>> x.new("x")
x
Calculates a generalized series expansion.
nseries calculates “n” terms in the innermost expressions and then builds up the final series just by “cross-multiplying” 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 end up with less terms than you may have expected, but the O(x**n) term appended will always be correct and so the result, though perhaps shorter, will also be correct.
See also lseries().
Removes the O(..) symbol if there is one
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 sin, exp, I
>>> from sympy.abc import x, y
>>> 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).
For dir=”+” (default) it calculates the series from the right and for dir=”-” the series from the left. For smooth functions this argument doesn’t matter.
This method is the most high level method and it returns the series including the O(x**n) term.
Internally, it executes a method nseries(), see nseries() docstring for more information.
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 pi
>>> from sympy.abc import x, y
>>> (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
Bases: object
A Formula is a model for a mean in a regression model.
It is often given by a sequence of sympy expressions, with the mean model being the sum of each term multiplied by a linear regression coefficient.
The expressions may depend on additional Symbol instances, giving a non-linear regression model.
Methods
design | |
fromrec | |
subs |
Coefficients in the linear regression formula.
Construct the design matrix, and optional contrast matrices.
Parameters : | input : np.recarray
param : None or np.recarray
return_float : bool, optional
contrasts : None or dict, optional
|
---|
The dtype of the design matrix of the Formula.
Construct Formula from recarray
For fields with a string-dtype, it is assumed that these are qualtiatitve regressors, i.e. Factors.
Parameters : | rec: recarray :
keep: [] :
drop: [] :
|
---|
Expression for the mean, expressed as a linear combination of terms, each with dummy variables in front.
The parameters in the Formula.
Perform a sympy substitution on all terms in the Formula
Returns a new instance of the same class
Parameters : | old : sympy.Basic
new : sympy.Basic
|
---|---|
Returns : | newf : Formula |
Examples
>>> s, t = [Term(l) for l in 'st']
>>> f, g = [sympy.Function(l) for l in 'fg']
>>> form = Formula([f(t),g(s)])
>>> newform = form.subs(g, sympy.Function('h'))
>>> newform.terms
array([f(t), h(s)], dtype=object)
>>> form.terms
array([f(t), g(s)], dtype=object)
Terms in the linear regression formula.
Bases: nipy.modalities.fmri.formula.Formula
Covariance matrices for common random effects analyses.
Examples
>>> subj = make_recarray([2,2,2,3,3], 's')
>>> subj_factor = Factor('s', [2,3])
>>> c = RandomEffects(subj_factor.terms)
>>> c.cov(subj)
array([[_s2_0, _s2_0, _s2_0, 0, 0],
[_s2_0, _s2_0, _s2_0, 0, 0],
[_s2_0, _s2_0, _s2_0, 0, 0],
[0, 0, 0, _s2_1, _s2_1],
[0, 0, 0, _s2_1, _s2_1]], dtype=object)
>>> c = RandomEffects(subj_factor.terms, sigma=np.array([[4,1],[1,6]]))
>>> c.cov(subj)
array([[ 4., 4., 4., 1., 1.],
[ 4., 4., 4., 1., 1.],
[ 4., 4., 4., 1., 1.],
[ 1., 1., 1., 6., 6.],
[ 1., 1., 1., 6., 6.]])
Methods
cov | |
design | |
fromrec | |
subs |
Parameters : | seq : [sympy.Basic] sigma : ndarray
char : character for regression coefficient |
---|
Coefficients in the linear regression formula.
Compute the covariance matrix for some given data.
Construct the design matrix, and optional contrast matrices.
Parameters : | input : np.recarray
param : None or np.recarray
return_float : bool, optional
contrasts : None or dict, optional
|
---|
The dtype of the design matrix of the Formula.
Construct Formula from recarray
For fields with a string-dtype, it is assumed that these are qualtiatitve regressors, i.e. Factors.
Parameters : | rec: recarray :
keep: [] :
drop: [] :
|
---|
Expression for the mean, expressed as a linear combination of terms, each with dummy variables in front.
The parameters in the Formula.
Perform a sympy substitution on all terms in the Formula
Returns a new instance of the same class
Parameters : | old : sympy.Basic
new : sympy.Basic
|
---|---|
Returns : | newf : Formula |
Examples
>>> s, t = [Term(l) for l in 'st']
>>> f, g = [sympy.Function(l) for l in 'fg']
>>> form = Formula([f(t),g(s)])
>>> newform = form.subs(g, sympy.Function('h'))
>>> newform.terms
array([f(t), h(s)], dtype=object)
>>> form.terms
array([f(t), g(s)], dtype=object)
Terms in the linear regression formula.
Bases: sympy.core.symbol.Symbol
A sympy.Symbol type to represent a term an a regression model
Terms can be added to other sympy expressions with the single convention that a term plus itself returns itself.
It is meant to emulate something on the right hand side of a formula in R. In particular, its name can be the name of a field in a recarray used to create a design matrix.
>>> t = Term('x')
>>> xval = np.array([(3,),(4,),(5,)], np.dtype([('x', np.float)]))
>>> f = t.formula
>>> d = f.design(xval)
>>> print d.dtype.descr
[('x', '<f8')]
>>> f.design(xval, return_float=True)
array([ 3., 4., 5.])
Methods
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Returns a tuple of arguments of ‘self’.
Example:
>>> from sympy import symbols, cot
>>> from sympy.abc import x, y
>>> cot(x).args
(x,)
>>> cot(x).args[0]
x
>>> (x*y).args
(x, y)
>>> (x*y).args[1]
y
Note for developers: Never use self._args, always use self.args. Only when you are creating your own new function, use _args in the __new__. Don’t override .args() from Basic (so that it’s easy to change the interface in the future if needed).
Converts polynomial to a valid sympy expression.
>>> from sympy import sin
>>> from sympy.abc import x, y
>>> p = (x**2 + x*y).as_poly(x, y)
>>> p.as_basic()
x*y + x**2
>>> f = sin(x)
>>> f.as_basic()
sin(x)
c*x**e -> c,e where x can be any symbolic expression.
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 E, pi, sin, I
>>> from sympy.abc import x, y
>>> 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 sin, cos
>>> from sympy.abc import x, y
>>> (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)
Returns the leading term.
Example:
>>> from sympy.abc import x
>>> (1+x+x**2).as_leading_term(x)
1
>>> (1/x**2+x+x**2).as_leading_term(x)
x**(-2)
Note:
self is assumed to be the result returned by Basic.series().
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 Poly, sin
>>> from sympy.abc import x, y
>>> 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 symbols, I
>>> x, y = symbols('xy', real=True)
>>> (x + y*I).as_real_imag()
(x, y)
>>> from sympy.abc import z, w
>>> (z + w*I).as_real_imag()
(-im(w) + re(z), im(z) + re(w))
Return object type assumptions.
For example:
Symbol(‘x’, real=True) Symbol(‘x’, integer=True)
are different objects. In other words, besides Python type (Symbol in this case), the initial assumptions are also forming their typeinfo.
Example:
>>> from sympy import Symbol
>>> from sympy.abc import x
>>> x.assumptions0
{}
>>> x = Symbol("x", positive=True)
>>> x.assumptions0
{'commutative': True, 'complex': True, 'imaginary': False,
'negative': False, 'nonnegative': True, 'nonpositive': False,
'nonzero': True, 'positive': True, 'real': True, 'zero': False}
Returns the atoms that form the current object.
By default, only objects that are truly atomic and can’t be divided into smaller pieces are returned: symbols, numbers, and number symbols like I and pi. It is possible to request atoms of any type, however, as demonstrated below.
Examples:
>>> from sympy import I, pi, sin
>>> from sympy.abc import x, y
>>> list((1+x+2*sin(y+I*pi)).atoms())
[y, I, 2, x, 1, pi]
If one or more types are given, the results will contain only those types of atoms:
Examples:
>>> from sympy import Number, NumberSymbol, Symbol
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Symbol))
[x, y]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number))
[1, 2]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol))
[1, 2, pi]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol, I))
[1, 2, pi, I]
Note that I (imaginary unit) and zoo (complex infinity) are special types of number symbols and are not part of the NumberSymbol class.
The type can be given implicitly, too:
>>> sorted((1+x+2*sin(y+I*pi)).atoms(x)) # x is a Symbol
[x, y]
Be careful to check your assumptions when using the implicit option since S(1).is_Integer = True but type(S(1)) is One, a special type of sympy atom, while type(S(2)) is type Integer and will find all integers in an expression:
>>> from sympy import S
>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(1)))
[1]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(2)))
[1, 2]
Finally, arguments to atoms() can select more than atomic atoms: any sympy type (loaded in core/__init__.py) can be listed as an argument and those types of “atoms” as found in scanning the arguments of the expression nonrecursively:
>>> from sympy import Function, Mul
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Function))
[sin(y + pi*I)]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Mul))
[2*sin(y + pi*I)]
Returns the coefficient of the term “x” or None if there is no “x”.
Optional expand keyword argument allows one to control whether the expression is expanded before terms are collected, which can be useful if the term “x” isn’t nested inside of terms and you don’t want the returned coefficient to be expanded.
Example:
>>> from sympy import symbols
>>> from sympy.abc import x, y, z
>>> (3+2*x+4*x**2).coeff(1)
>>> (3+2*x+4*x**2).coeff(x)
2
>>> (3+2*x+4*x**2).coeff(x**2)
4
>>> (3+2*x+4*x**2).coeff(x**3)
>>> (z*(x+y)**2).coeff(z)
2*x*y + x**2 + y**2
>>> (z*(x+y)**2).coeff(z, expand=False)
(x + y)**2
>>>
Return -1,0,1 if the object is smaller, equal, or greater than other.
Not in the mathematical sense. If the object is of a different type from the “other” then their classes are ordered according to the sorted_classes list.
Example:
>>> from sympy.abc import x, y
>>> x.compare(y)
-1
>>> x.compare(x)
0
>>> y.compare(x)
1
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.
Example:
>>> from sympy.abc import x
>>> from sympy import Basic
>>> Basic._compare_pretty(x, x**2)
-1
>>> Basic._compare_pretty(x**2, x**2)
0
>>> Basic._compare_pretty(x**3, x**2)
1
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.abc import x, y
>>> (x-y).could_extract_minus_sign() != (y-x).could_extract_minus_sign()
True
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.
See the docstring in function.expand for more information.
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 symbols
>>> 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 symbols, Rational
>>> 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 a Formula with only terms=[self].
The top-level function in an expression.
The following should hold for all objects:
>> x == x.func(*x.args)
Example:
>>> from sympy.abc import x
>>> a = 2*x
>>> a.func
<class 'sympy.core.mul.Mul'>
>>> a.args
(2, x)
>>> a.func(*a.args)
2*x
>>> a == a.func(*a.args)
True
Returns the O(..) symbol, or None if there is none.
Return True if self has any of the patterns.
Example: >>> from sympy.abc import x >>> (2*x).has(x) True >>> (2*x/x).has(x) False
Return True if ‘self’ has all of the symbols.
>>> from sympy import sin
>>> from sympy.abc import x, y, z
>>> (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 sin
>>> from sympy.abc import x, y, z
>>> (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
Returns True if any args are Piecewise or has_piecewise
Test whether function is rational function - ratio of two polynomials. When no arguments are present, Basic.atoms(Symbol) is used instead.
Example:
>>> from sympy import symbols, sin
>>> from sympy.abc import x, y
>>> (x/y).is_rational_function()
True
>>> (x**2).is_rational_function()
True
>>> (x/sin(y)).is_rational_function(y)
False
Iterates arguments of ‘self’.
Example:
>>> from sympy.abc import x
>>> a = 2*x
>>> a.iter_basic_args()
<tupleiterator object at 0x...>
>>> list(a.iter_basic_args())
[2, x]
Returns the leading term a*x**b as a tuple (a, b).
Example:
>>> from sympy.abc import x
>>> (1+x+x**2).leadterm(x)
(1, 0)
>>> (1/x**2+x+x**2).leadterm(x)
(1, -2)
Note:
self is assumed to be the result returned by Basic.series().
Compute limit x->xlim.
lseries is a generator yielding terms in the series.
Example: if you do:
It will print all terms of the sin(x) series (i.e. it never terminates).
The advantage of lseries() over nseries() is that many times you are just interested in the next term in the series (i.e. the first term for example), but you don’t know how many you should ask for in nseries() using the “n” parameter.
See also nseries().
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
Example:
>>> from sympy import symbols, Wild
>>> from sympy.abc import x, y
>>> p = Wild("p")
>>> q = Wild("q")
>>> r = Wild("r")
>>> e = (x+y)**(x+y)
>>> e.match(p**p)
{p_: x + y}
>>> e.match(p**q)
{p_: x + y, q_: x + y}
>>> e = (2*x)**2
>>> e.match(p*q**r)
{p_: 4, q_: x, r_: 2}
>>> (p*q**r).subs(e.match(p*q**r))
4*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)
Create new ‘similar’ object.
this is conceptually equivalent to:
type(self) (*args)
but takes type assumptions into account. See also: assumptions0
Example:
>>> from sympy.abc import x
>>> x.new("x")
x
Calculates a generalized series expansion.
nseries calculates “n” terms in the innermost expressions and then builds up the final series just by “cross-multiplying” 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 end up with less terms than you may have expected, but the O(x**n) term appended will always be correct and so the result, though perhaps shorter, will also be correct.
See also lseries().
Removes the O(..) symbol if there is one
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 sin, exp, I
>>> from sympy.abc import x, y
>>> 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).
For dir=”+” (default) it calculates the series from the right and for dir=”-” the series from the left. For smooth functions this argument doesn’t matter.
This method is the most high level method and it returns the series including the O(x**n) term.
Internally, it executes a method nseries(), see nseries() docstring for more information.
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 pi
>>> from sympy.abc import x, y
>>> (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
Construct a contrast matrix from a design matrix D
(possibly with its pseudo inverse already computed) and a matrix L that either specifies something in the column space of D or the row space of D.
Parameters : | L : ndarray
D : ndarray
pseudo : None or array-like, optional
|
---|---|
Returns : | C : ndarray
|
Notes
From an n x p design matrix D and a matrix L, tries to determine a p x q contrast matrix C which determines a contrast of full rank, i.e. the n x q matrix
dot(transpose(C), pinv(D))
is full rank.
L must satisfy either L.shape[0] == n or L.shape[1] == p.
If L.shape[0] == n, then L is thought of as representing columns in the column space of D.
If L.shape[1] == p, then L is thought of as what is known as a contrast matrix. In this case, this function returns an estimable contrast corresponding to the dot(D, L.T)
This always produces a meaningful contrast, not always with the intended properties because q is always non-zero unless L is identically 0. That is, it produces a contrast that spans the column space of L (after projection onto the column space of D).
Return a matrix whose column span is the same as X using an SVD decomposition.
If the rank of X is known it can be specified by r– no check is made to ensure that this really is the rank of X.
Return the parameters of an expression that are not Term instances but are instances of sympy.Symbol.
Examples
>>> x, y, z = [Term(l) for l in 'xyz']
>>> f = Formula([x,y,z])
>>> getparams(f)
[]
>>> f.mean
_b0*x + _b1*y + _b2*z
>>> getparams(f.mean)
[_b0, _b1, _b2]
>>>
>>> th = sympy.Symbol('theta')
>>> f.mean*sympy.exp(th)
(_b0*x + _b1*y + _b2*z)*exp(theta)
>>> getparams(f.mean*sympy.exp(th))
[theta, _b0, _b1, _b2]
Return the all instances of Term in an expression.
Examples
>>> x, y, z = [Term(l) for l in 'xyz']
>>> f = Formula([x,y,z])
>>> getterms(f)
[x, y, z]
>>> getterms(f.mean)
[x, y, z]
Is obj a Formula?
Is obj a FactorTerm?
Is obj a Formula?
Is obj a Term?
Create recarray from rows with field names names
Create a recarray with named columns from a list of rows and names for the columns. If dtype is None, the dtype is based on rows if it is an np.ndarray, else the data is cast as np.float. If dtypes are supplied, it uses the dtypes to create a np.dtype unless rows is an np.ndarray, in which case dtypes are ignored
Parameters : | rows: array-like :
names: sequence :
dtypes: [str or np.dtype] :
|
---|---|
Returns : | v : np.ndarray |
Examples
The following tests depend on machine byte order to pass
>>> arr = np.array([[3,4 ], [4, 6], [6, 8]])
>>> make_recarray(arr, ['x', 'y'])
array([[(3, 4)],
[(4, 6)],
[(6, 8)]],
dtype=[('x', '...'), ('y', '...')])
>>> r = make_recarray(arr, ['w', 'u'])
>>> make_recarray(r, ['x', 'y'])
array([[(3, 4)],
[(4, 6)],
[(6, 8)]],
dtype=[('x', '...'), ('y', '...')])
>>> make_recarray([[3, 4], [4, 6], [7, 9]], 'wv', [np.float, np.int])
array([(3.0, 4), (4.0, 6), (7.0, 9)],
dtype=[('w', '...'), ('v', '...')])
Return a Formula containing a natural spline
Spline for a Term with specified knots and order.
Parameters : | t : Term knots : None or sequence, optional
order : int, optional
intercept : bool, optional
|
---|---|
Returns : | formula : Formula
|
Examples
The following results depend on machine byte order
>>> x = Term('x')
>>> n = natural_spline(x, knots=[1,3,4], order=3)
>>> xval = np.array([3,5,7.]).view(np.dtype([('x', np.float)]))
>>> n.design(xval, return_float=True)
array([[ 3., 9., 27., 8., 0., -0.],
[ 5., 25., 125., 64., 8., 1.],
[ 7., 49., 343., 216., 64., 27.]])
>>> d = n.design(xval)
>>> print d.dtype.descr
[('ns_1(x)', '<f8'), ('ns_2(x)', '<f8'), ('ns_3(x)', '<f8'), ('ns_4(x)', '<f8'), ('ns_5(x)', '<f8'), ('ns_6(x)', '<f8')]
>>>
Return the rank of a matrix X
Rank based on its generalized inverse, not the SVD.
Return list of terms with names given by names
This is just a convenience in defining a set of terms, and is the equivalent of sympy.symbols for defining symbols in sympy.
Parameters : | *names : str or sequence of str
|
---|---|
Returns : | ts : list
|
Examples
>>> terms('a', 'b', 'c')
[a, b, c]
>>> terms('a, b, c')
[a, b, c]