Convenience functions for working with formulae / terms
Create function of t expression from arbitrary expression expr
Take an arbitrarily complicated expression expr of ‘t’ and make it an expression that is a simple function of t, of form '%s(t)' % name such that when it evaluates (via lambdify) it has the right values.
Parameters : | expr : sympy expression
name : str |
---|---|
Returns : | nexpr: sympy expression : |
Examples
>>> from sympy import lambdify
>>> t = Term('t')
>>> expr = t**2 + 3*t
>>> print expr
t**2 + 3*t
>>> newexpr = define('f', expr)
>>> print newexpr
f(t)
>>> f = lambdify(t, newexpr)
>>> f(4)
28
>>> 3*4+4**2
28
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
We’ve used the ``#doctest:+ELLIPSIS option here to allow us not to care either about the default integer size (i4 or i8), or the machine byte order (<,>), in the record array dtype.
>>> 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 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.
We enforce the sympy 0.7.0 behavior of returning symbol “abc” from input “abc”, rthan than 3 symbols “a”, “b”, “c”.
Parameters : | names : str or sequence of str
**kwargs : keyword arguments
|
---|---|
Returns : | ts : Term or tuple
|
Examples
>>> terms(('a', 'b', 'c'))
(a, b, c)
>>> terms('a, b, c')
(a, b, c)
>>> terms('abc')
abc
Construct terms from recarray
For fields with a string-dtype, it is assumed that these are qualtiatitve regressors, i.e. Factors.
Parameters : | rec: recarray :
keep: [] :
drop: [] :
|
---|