Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dsolve(diff(D(t), t) - k1*(B + C2*exp(-k*t) - D(t)), D(t), 'nth_linear_constant_coeff_variation_of_parameters') gives AttributeError: 'tuple' object has no attribute 'match' #5173

Closed
asmeurer opened this issue Oct 11, 2010 · 8 comments

Comments

@asmeurer
Copy link
Member

In [36]: var('t C2 k k1 B')
Out[36]: (t, C₂, k, k₁, B)

In [37]: D, R = map(Function, 'DR')

In [38]: dsolve(diff(D(t), t) -  k1*(B + C2*exp(-k*t) - D(t)), D(t), 'nth_linear_constant_coeff_variation_of_parameters')
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (327, 0))

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

/Users/aaronmeurer/Documents/Python/sympy/sympy/<ipython console> in <module>()

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/ode.pyc in dsolve(eq, func, hint, simplify, **kwargs)
    455     if simplify:
    456         return odesimp(solvefunc(eq, func, order=hints['order'],
--> 457             match=hints[hint]), func, hints['order'], hint)
    458     else:
    459         # We still want to integrate (you can disable it separately with the hint)


/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/core/multidimensional.pyc in wrapper(*args, **kwargs)
    133                     result = apply_on_element(wrapper, args, kwargs, n)
    134                     return result
--> 135             return f(*args, **kwargs)
    136         wrapper.__doc__ = f.__doc__
    137         wrapper.__name__ = f.__name__

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/ode.pyc in odesimp(eq, func, order, hint)
    894     # a simpler expression, but the solved expression could have introduced

    895     # things like -C1, so rerun constantsimp() one last time before returning.

--> 896     eq = constant_renumber(constantsimp(eq, x, 2*order), 'C', 1, 2*order)
    897 
    898     return eq

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/core/multidimensional.pyc in wrapper(*args, **kwargs)
    133                     result = apply_on_element(wrapper, args, kwargs, n)
    134                     return result
--> 135             return f(*args, **kwargs)
    136         wrapper.__doc__ = f.__doc__
    137         wrapper.__name__ = f.__name__

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/ode.pyc in constant_renumber(expr, symbolname, startnumber, endnumber)
   1416 
   1417 
-> 1418     return _constant_renumber(expr, symbolname, startnumber, endnumber)
   1419 
   1420 

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/ode.pyc in _constant_renumber(expr, symbolname, startnumber, endnumber)
   1393         if isinstance(expr, Equality):
   1394             return Eq(_constant_renumber(expr.lhs, symbolname, startnumber, endnumber),
-> 1395             _constant_renumber(expr.rhs, symbolname, startnumber, endnumber))
   1396 
   1397         if type(expr) not in (Mul, Add, Pow) and not expr.is_Function and\

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/ode.pyc in _constant_renumber(expr, symbolname, startnumber, endnumber)
   1413                 sortedargs.sort(Basic._compare_pretty)
   1414                 return expr.new(*[_constant_renumber(x, symbolname, startnumber,
-> 1415                 endnumber) for x in sortedargs])
   1416 
   1417 

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/ode.pyc in _constant_renumber(expr, symbolname, startnumber, endnumber)
   1413                 sortedargs.sort(Basic._compare_pretty)
   1414                 return expr.new(*[_constant_renumber(x, symbolname, startnumber,
-> 1415                 endnumber) for x in sortedargs])
   1416 
   1417 

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/ode.pyc in _constant_renumber(expr, symbolname, startnumber, endnumber)
   1413                 sortedargs.sort(Basic._compare_pretty)
   1414                 return expr.new(*[_constant_renumber(x, symbolname, startnumber,
-> 1415                 endnumber) for x in sortedargs])
   1416 
   1417 

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/ode.pyc in _constant_renumber(expr, symbolname, startnumber, endnumber)
   1413                 sortedargs.sort(Basic._compare_pretty)
   1414                 return expr.new(*[_constant_renumber(x, symbolname, startnumber,
-> 1415                 endnumber) for x in sortedargs])
   1416 
   1417 

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/ode.pyc in _constant_renumber(expr, symbolname, startnumber, endnumber)
   1413                 sortedargs.sort(Basic._compare_pretty)
   1414                 return expr.new(*[_constant_renumber(x, symbolname, startnumber,
-> 1415                 endnumber) for x in sortedargs])
   1416 
   1417 

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/ode.pyc in _constant_renumber(expr, symbolname, startnumber, endnumber)
   1411             else:
   1412                 sortedargs = list(expr.args)
-> 1413                 sortedargs.sort(Basic._compare_pretty)
   1414                 return expr.new(*[_constant_renumber(x, symbolname, startnumber,
   1415                 endnumber) for x in sortedargs])

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/core/basic.pyc in _compare_pretty(a, b)
    273         from sympy.core.symbol import Wild
    274         p1, p2, p3 = Wild("p1"), Wild("p2"), Wild("p3")
--> 275         r_a = a.match(p1 * p2**p3)
    276         r_b = b.match(p1 * p2**p3)
    277         if r_a is not None and r_b is not None:

AttributeError: 'tuple' object has no attribute 'match'


The error also occurs if you use the nth_linear_constant_coeff_variation_of_parameters_Integral hint.  This is probably related to issue 5169 .

Original issue for #5173: http://code.google.com/p/sympy/issues/detail?id=2074
Original author: https://code.google.com/u/asmeurer@gmail.com/
Referenced issues: #5169
Original owner: https://code.google.com/u/asmeurer@gmail.com/

@asmeurer
Copy link
Member Author

Now, the Integral hint gives AttributeError: 'Tuple' object has no attribute 'as_base_exp' and the regular hint just hangs (in heurisch()).  

The problem is in constant_renumber, which pulls apart an expression and renumbers the constants using Basic._compare_pretty() (so they appear in order in the final expression).  This method is not defined for Tuple, so it works its way to the Basic version, which assumes that you can call as_base_exp().  This routine should be moved to Expr, and a more generic routine implemented for Basic.

Original comment: http://code.google.com/p/sympy/issues/detail?id=2074#c1
Original author: https://code.google.com/u/asmeurer@gmail.com/

@asmeurer
Copy link
Member Author

Actually, _compare_pretty is not even used by the printer by default any more.  compare_pretty now calls sort_key().  Does the old printing order still use it?  If not, it should be removed (see also issue 4590 ).

It appears that in a lot of cases, compare_pretty is being used with cmp_to_key.  This is wrong.  It should just use default_sort_key (or some variation of sort_key()).

**Cc:** vlada.pe...@gmail.com  

Referenced issues: #4590
Original comment: http://code.google.com/p/sympy/issues/detail?id=2074#c2
Original author: https://code.google.com/u/asmeurer@gmail.com/

@vperic
Copy link
Contributor

vperic commented Sep 4, 2011

@#2: Aaron, you're right about cmp_to_key. It was never intended to be a permanent solution anyway. I've opened an issue about that, see: http://code.google.com/p/sympy/issues/detail?id=267

Original comment: http://code.google.com/p/sympy/issues/detail?id=2074#c3
Original author: https://code.google.com/u/108713607268198052411/

@asmeurer
Copy link
Member Author

asmeurer commented Sep 4, 2011

I just don't understand why you used it on compare_pretty, when the key equivalent, sort_key, already existed.  Perhaps you didn't know that at the time?

Original comment: http://code.google.com/p/sympy/issues/detail?id=2074#c4
Original author: https://code.google.com/u/asmeurer@gmail.com/

@vperic
Copy link
Contributor

vperic commented Sep 5, 2011

Yeah, I didn't know about it (someone did point it out later, though, perhaps it was you or Ronan), but I also just wanted to move on ASAP.

Original comment: http://code.google.com/p/sympy/issues/detail?id=2074#c5
Original author: https://code.google.com/u/108713607268198052411/

@asmeurer
Copy link
Member Author

**Status:** Valid  

Original comment: http://code.google.com/p/sympy/issues/detail?id=2074#c6
Original author: https://code.google.com/u/asmeurer@gmail.com/

@smichr
Copy link
Member

smichr commented Apr 1, 2013

>>> var('t C2 k k1 B')
(t, C2, k, k1, B)
>>> D, R = map(Function, 'DR')
>>> dsolve(diff(D(t), t) -  k1*(B + C2*exp(-k*t) - D(t)), D(t), 'nth_linear_cons
tant_coeff_variation_of_parameters')
D(t) == (C3*cos(t*im(k1)) + (C1 - Integral(k1*(-B + C2*exp(-k*t))*exp(t*re(k1))/
sin(t*Abs(im(k1))), t))*sin(t*Abs(im(k1))))*exp(-t*re(k1))

Is the underlying cmp_to_key problem fixed?

Original comment: http://code.google.com/p/sympy/issues/detail?id=2074#c7
Original author: https://code.google.com/u/117933771799683895267/

@asmeurer
Copy link
Member Author

asmeurer commented Apr 7, 2013

I bisected to 

commit aa94e72e5d53f0ae774cf114fae107ec0687bb21
Author: Mateusz Paprocki <mattpap@gmail.com>
Date:   Mon Oct 3 10:20:00 2011 -0700

    Don't use and deprecate Basic.compare_pretty()

    The only place where Basic.compare_pretty() is used in SymPy
    is in Printer where old term ordering is used. The default way
    to sort expressions is to use .sort_key() or default_sort_key.


(note you need to use the _Integral hint when bisecting)

**Status:** Fixed  

Original comment: http://code.google.com/p/sympy/issues/detail?id=2074#c8
Original author: https://code.google.com/u/asmeurer@gmail.com/

@asmeurer asmeurer self-assigned this Mar 7, 2014
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants