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

Added the ability to evaluate linear recurrences without obtaining closed form expressions #14816

Merged
merged 5 commits into from Jun 25, 2018

Conversation

Projects
None yet
4 participants
@sidhantnagpal
Member

sidhantnagpal commented Jun 21, 2018

Brief description of what is fixed or changed

linrec(coeffs, init, n) takes the coefficients, initial values, and point of evaluation of the recurrence as input and produces the corresponding result.
Usage for a recurrence like f(n) = f(n - 7) + f(n - 13) + f(n - 17) (having order 17) would be:

In []: coeffs, init = [0]*17, [1]*17
In []: coeffs[7 - 1] = coeffs[13 - 1] = coeffs[17 - 1] = 1
In []: [linrec(coeffs, init, n) for n in range(40, 50)]
Out[]: [17, 21, 21, 23, 29, 31, 31, 35, 41, 41]

In []: def f(n):
  ...:     if n < 17:
  ...:         return 1
  ...:     return f(n - 7) + f(n - 13) + f(n - 17)
  ...: 

In []: [f(n) for n in range(40, 50)]
Out[]: [17, 21, 21, 23, 29, 31, 31, 35, 41, 41]

Other comments

@asmeurer

This comment has been minimized.

Member

asmeurer commented Jun 21, 2018

This seems like it should be an internal function (not in __init__.py) and the user-facing function should accept a symbolic input (like the same format as rsolve), and work even for nonlinear recurrences.

@sidhantnagpal

This comment has been minimized.

Member

sidhantnagpal commented Jun 21, 2018

Yes, that would be better. NotImplementedError could be raised for the cases not covered by this approach (I am not sure if it's possible to do that for nonlinear case).

if n < k:
return [S.Zero]*n + [S.One] + [S.Zero]*(k - n - 1)
else:
return _square_and_reduce(_final_coeffs(n // 2), offset=n%2)

This comment has been minimized.

@jksuom

jksuom Jun 21, 2018

Member

Is the parameter offset explicitly written for a particular reason?

This comment has been minimized.

@sidhantnagpal

sidhantnagpal Jun 21, 2018

Member

It can be dropped, there's no specific reason for writing it.

@asmeurer

This comment has been minimized.

Member

asmeurer commented Jun 21, 2018

Or it could just evaluate it directly (but NotImplementedError is also fine for now).

n : Integer
point of evaluation of recurrence
Explanation

This comment has been minimized.

@jksuom

jksuom Jun 24, 2018

Member

The name of this section should probably be Notes. See https://numpydoc.readthedocs.io/en/latest/format.html#sections.

`g(x) = a_0 x^0 + a_1 x^1 + \cdots + a_{k-1} x^{k-1}` modulo `p(x)`, then
`T(x^n) = x_n` is equal to
`T(g(x)) = a_0 x_0 + a_1 x_1 + \cdots + a_{k-1} x_{k-1}`.

This comment has been minimized.

@jksuom

jksuom Jun 24, 2018

Member

A note on the computation of x^n by squaring could go here.

@jksuom

This comment has been minimized.

Member

jksuom commented Jun 25, 2018

Thanks. We can return to the public recursion API later.

@jksuom jksuom merged commit ab14b02 into sympy:master Jun 25, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment