Skip to content
Browse files

RF - removed our copy of aliasing code

The ``aliased.py`` module is in sympy.utiltities.lambdify as of 0.7.0.
In order to use it, we have to specify that we want the numpy namespace,
for lambdify; otherwise we get python math by default, which fail for
numpy arrays.
  • Loading branch information...
1 parent ba424bb commit 9469837cbcf546cc006fca398e09eaf5d1d688f6 @matthew-brett matthew-brett committed Jul 25, 2011
Showing with 4 additions and 132 deletions.
  1. +0 −122 formula/aliased.py
  2. +4 −10 formula/formulae.py
View
122 formula/aliased.py
@@ -1,122 +0,0 @@
-"""
-An implementation of Functions in sympy that allow 'anonymous'
-functions that can be evaluated when 'lambdified'.
-
-"""
-import new
-import sympy
-import warnings
-
-class lambdify(object):
- """
- A modified version of sympy's lambdify that
- will find 'aliased' Functions and substitute
- them appropriately at evaluation time.
-
- """
-
- def __init__(self, args, expr):
- if isinstance(expr, sympy.FunctionClass):
- expr = expr(t)
- n = {}
- _add_aliases_to_namespace(n, expr)
- self.n = n.copy()
-
- from sympy.utilities.lambdify import _get_namespace
- for k, v in _get_namespace('numpy').items():
- self.n[k] = v
-
- self._f = sympy.lambdify(args, expr, self.n)
- self.expr = expr
-
- def __call__(self, _t):
- return self._f(_t)
-
-
-class vectorize(lambdify):
- """
- This class can be used to take a (single-valued) sympy
- expression with only 't' as a Symbol and return a
- callable that can be evaluated at an array of floats.
-
- Parameters
- ----------
-
- expr : sympy expr
- Expression with 't' the only Symbol. If it is
- an instance of sympy.FunctionClass,
- then vectorize expr(t) instead.
-
- Returns
- -------
-
- f : callable
- A function that can be evaluated at an array of time points.
-
- """
-
- def __init__(self, expr):
- deft = sympy.DeferredVector('t')
- t = sympy.Symbol('t')
- if isinstance(expr, sympy.FunctionClass):
- expr = expr(t)
- lambdify.__init__(self, deft, expr.subs(t, deft))
-
-
-class AliasedFunctionClass(sympy.FunctionClass):
-
- """
-
- This class allows 'anonymous' sympy Functions
- that can be replaed with an appropriate callable
- function when lambdifying.
-
- No checking is done on the signature of the alias.
-
- This is not meant to be called by users, rather
- use 'aliased_function'.
-
- """
- def __new__(cls, arg1, arg2, arg3=None, alias=None):
- r = sympy.FunctionClass.__new__(cls, arg1, arg2, arg3)
- if alias is not None:
- r.alias = new.instancemethod(lambda self, x: alias(x), r, cls)
- return r
-
-
-def _add_aliases_to_namespace(namespace, *exprs):
- """
- Given a sequence of sympy expressions,
- find all aliases in each expression and add them to the namespace.
- """
-
- for expr in exprs:
- if hasattr(expr, 'alias') and isinstance(expr, sympy.FunctionClass):
- if namespace.has_key(str(expr)):
- if namespace[str(expr)] != expr.alias:
- warnings.warn('two aliases with the same name were found')
- namespace[str(expr)] = expr.alias
-
- if hasattr(expr, 'func'):
- if isinstance(expr.func, sympy.FunctionClass) and hasattr(expr.func, 'alias'):
- if namespace.has_key(expr.func.__name__):
- if namespace[expr.func.__name__] != expr.func.alias:
- warnings.warn('two aliases with the same name were found')
- namespace[expr.func.__name__] = expr.func.alias
- if hasattr(expr, 'args'):
- try:
- _add_aliases_to_namespace(namespace, *expr.args)
- except TypeError:
- pass
- return namespace
-
-def aliased_function(symbol, alias):
- """
- Create an aliased function with a given symbol
- and alias.
-
- """
-
- y = AliasedFunctionClass(sympy.Function, symbol, alias=alias)
- return y
-
View
14 formula/formulae.py
@@ -106,8 +106,6 @@
import numpy as np
from utils import contrast_from_cols_or_rows
-from aliased import _add_aliases_to_namespace
-
class Beta(sympy.symbol.Dummy):
''' A dummy symbol tied to a Term `term` '''
@@ -380,17 +378,13 @@ def _setup_design(self):
d[j] = d[j].subs(p, newp)
newparams.append(newp)
- # If there are any aliased functions, these need to be added
- # to the name space before sympy lambdifies the expression
-
- # These "aliased" functions are used for things like
+ # These "implemented" functions are used for things like
# the natural splines, etc. You can represent natural splines
# with sympy but the expression is pretty awful.
- _namespace = {};
- _add_aliases_to_namespace(_namespace, *d)
-
- self._f = sympy.lambdify(newparams + newterms, d, (_namespace, "numpy"))
+ # Note that ``d`` here is list giving the differentiation of the
+ # expression for the mean. self._f(...) therefore also returns a list
+ self._f = sympy.lambdify(newparams + newterms, d, ("numpy"))
# The input to self.design will be a recarray of that must
# have field names that the Formula will expect to see.

0 comments on commit 9469837

Please sign in to comment.
Something went wrong with that request. Please try again.