I believe your fix is correct.

The problem is that cse() is falsely assuming that it can replace a
Tuple with a Symbol.  But this almost never works.  You get similar
problems with other expressions that use Tuples in their .args.  For
example:

In [612]: cse(meijerg((1, 2), (a, 4), (5,), [], x) + meijerg((1, 3),
(a, 4), (5,), [], x))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/aaronmeurer/Documents/Python/sympy/sympy/<ipython-input-612-504251608d3e>
in <module>()
----> 1 cse(meijerg((1, 2), (a, 4), (5,), [], x) + meijerg((1, 3), (a,
4), (5,), [], x))

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/simplify/cse_main.py
in cse(exprs, symbols, optimizations)
    119
    120     # Preprocess the expressions to give us better
optimization opportunities.
--> 121     reduced_exprs = [preprocess_for_cse(e, optimizations) for
e in exprs]
    122     # Find all of the repeated subexpressions.
    123     def insert(subtree):

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/simplify/cse_main.py
in preprocess_for_cse(expr, optimizations)
     41     for pre, post in optimizations:
     42         if pre is not None:
---> 43             expr = pre(expr)
     44     return expr
     45

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/core/exprtools.py
in factor_terms(expr, radical, clear, fraction)
    566     list_args = [gcd_terms(a,
    567         isprimitive=True,
--> 568         clear=clear,
    569         fraction=fraction) for a in Add.make_args(p)]
    570     p = Add._from_args(list_args) # gcd_terms will fix up ordering

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/core/exprtools.py
in gcd_terms(terms, isprimitive, clear, fraction)
    489                 return a.func(*[gcd_terms(i, isprimitive,
clear) for i in a.args])
    490             return type(a)([gcd_terms(i, isprimitive, clear,
fraction) for i in a])
--> 491         return gcd_terms(a, isprimitive, clear, fraction)
    492     return terms.func(*[handle(i) for i in terms.args])
    493

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/functions/special/hyper.pyc
in __new__(cls, *args)
    418
    419         # TODO should we check convergence conditions?
--> 420         return Function.__new__(cls, tr(args[0]), tr(args[1]), args[2])
    421
    422     def fdiff(self, argindex=3):

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/functions/special/hyper.pyc
in tr(p)
    415             if len(p) != 2:
    416                 raise TypeError("wrong argument")
--> 417             return Tuple(_prep_tuple(p[0]), _prep_tuple(p[1]))
    418
    419         # TODO should we check convergence conditions?

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/functions/special/hyper.pyc
in _prep_tuple(v)
     26     """
     27     from sympy.simplify.simplify import unpolarify
---> 28     return Tuple(*[unpolarify(x) for x in v])
     29
     30 class TupleParametersBase(Function):

TypeError: 'Integer' object is not iterable

Can you submit your patch as a pull request?

Aaron Meurer

On Sun, Apr 15, 2012 at 12:07 PM, Andreas Kloeckner
<[email protected]> wrote:
> Hi all,
>
> it looks like CSE and Subs don't interact very well. Here's what
> happens:
>
> - CSE finds a few Subs instances that share the entirety of their
>  'point' values.
>
> - CSE creates a symbol for the entire 'point' value--i.e. the whole
>  Tuple.
>
> - Upon construction of the new Subs with the symbol for 'point', Subs
>  wraps that symbol in a Tuple.
>
> - When the CSE'd value of the symbol is substituted back into the
>  expression, we end up with Tuple(Tuple(expr)).
>
> There are multiple ways of fixing this, none strikes me as 'obviously
> right':
>
> - Special-case Subs in CSE, to only look at the elements of Subs.point,
>  not the whole thing.
>
> - Special-case Tuple in CSE--in effect, don't collect entire Tuples in
>  CSE. (*)
>
> - Denest in the Subs constructor, i.e. turn Tuple(Tuple(x)) into
>  Tuple(x).
>
> - Add a flag to prevent the Tuple wrapping in Subs when called from CSE.
>
> (*) is what I've implemented as a stopgap, patch attached.
>
> I'd much appreciate any insight/fixes for this.
>
> Thanks,
> Andreas
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to