# 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
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]
``````

### sidhantnagpal added some commits Jun 16, 2018

``` add reval module, reval_lhcc method ```
``` 3fd9b75 ```
``` Merge branch master, clean up code, add cc exception ```
``` 72abce7 ```
``` Add unit tests, doctests, documentation ```
``` 38655da ```
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.
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).

### jksuom reviewed Jun 21, 2018

 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)

#### jksuom Jun 21, 2018

Member

Is the parameter `offset` explicitly written for a particular reason?

#### sidhantnagpal Jun 21, 2018

Member

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

Member

### asmeurer commented Jun 21, 2018

 Or it could just evaluate it directly (but NotImplementedError is also fine for now).
``` improve discrete.recurrence docs, solvers.recurr ```
``` a791a95 ```

### jksuom reviewed Jun 24, 2018

 n : Integer point of evaluation of recurrence Explanation

#### jksuom Jun 24, 2018

Member

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

### jksuom reviewed Jun 24, 2018

 `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}`.

#### jksuom Jun 24, 2018

Member

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

### Abdullahjavednesar added the PR: author's turn label Jun 24, 2018

``` documentation, code improvements ```
``` 4fe09c1 ```
Member