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

handling of rsolve coefficients #18751

Closed
j3parera opened this issue Mar 1, 2020 · 2 comments
Closed

handling of rsolve coefficients #18751

j3parera opened this issue Mar 1, 2020 · 2 comments

Comments

@j3parera
Copy link

j3parera commented Mar 1, 2020

The rsolve method does not always allow the use of coefficients with functions even if its result is constant or independent of the solution's function symbol. For instance, a classic second order system with two complex conjugate poles and expanded coefficients:

n = Symbol('n', integer=True)
y = Function('y')
r = Symbol('r', real=True, positive=True)
theta = Symbol('theta', real=True)
f = y(n) - 2*r*cos(theta)*y(n-1) + r**2*y(n-2)
sol = rsolve(f, y(n))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-44-fbee907275eb> in <module>
      3 theta = Symbol('theta', real=True)
      4 f = y(n) - 2*r*cos(theta)*y(n-1) + r**2*y(n-2)
----> 5 sol = rsolve(f, y(n))

~/.local/lib/python3.7/site-packages/sympy/solvers/recurr.py in rsolve(f, y, init)
    739                 else:
    740                     raise ValueError(
--> 741                         "'%s' expected, got '%s'" % (y.func, h.func))
    742             else:
    743                 coeff *= h

ValueError: 'y' expected, got 'cos'

In this case cos(theta) is independent of n.
With some specific values of theta it doesn't work:

f = y(n) - 2*r*cos(S.Pi/16)*y(n-1) + r**2*y(n-2)
sol = rsolve(f, y(n))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-45-0a4e9e18043f> in <module>
      1 f = y(n) - 2*r*cos(S.Pi/16)*y(n-1) + r**2*y(n-2)
----> 2 sol = rsolve(f, y(n))

~/.local/lib/python3.7/site-packages/sympy/solvers/recurr.py in rsolve(f, y, init)
    739                 else:
    740                     raise ValueError(
--> 741                         "'%s' expected, got '%s'" % (y.func, h.func))
    742             else:
    743                 coeff *= h

ValueError: 'y' expected, got 'cos'

But with some other values it does:

f = y(n) - 2*r*cos(S.Pi/4)*y(n-1) + r**2*y(n-2)
sol = rsolve(f, y(n))
print(sol)

with the independent symbol r in solution correctly handled:
C0*(sqrt(2)*r*(1 - I)/2)**n + C1*(sqrt(2)*r*(1 + I)/2)**n.

skirpichev added a commit to skirpichev/diofant that referenced this issue Mar 1, 2020
@j3parera
Copy link
Author

j3parera commented Mar 9, 2020

rsolve also fails for quite simple recurrences:

y = Function('y')
n = Symbol('n', integer=True)
f = y(n) - 0.9 * y(n - 1) + 0.81 * y(n - 2)
sol = rsolve(f, y(n))

results in sol = None.
But, surprisingly, just changing the last coefficient from 0.81 to 0.8 it works!

@smichr
Copy link
Member

smichr commented Mar 13, 2020

rsolve also fails for quite simple recurrences:

my guess is that this is a Float vs Rational issue:

>>> y = Function('y')
... n = Symbol('n', integer=True)
... f = y(n) - S(9)/10 * y(n - 1) + Rational('0.81') * y(n - 2)
... sol = rsolve(f, y(n))

>>> sol
C0*(9/20 - 9*sqrt(3)*I/20)**n + C1*(9/20 + 9*sqrt(3)*I/20)**n

@smichr smichr closed this as completed Mar 14, 2020
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