1

How to find the $n$-th term in a sequence with following recurrence relation for a given $n$?

$$F(n)=2b F(n-1)-F(n-2), \ F(0)=a,\ F(1)=b,$$ where $a$ and $b$ are constants.

The value of $n$ is quite large $(1 \leq n\leq 10^{12})$ and so matrix exponentiation is required.

However I am facing difficulties deducing its matrix from the relation which would give the required value.

Could anyone help me with this question and show how to convert as well so I can tackle these kind of problems myself afterward?

Ѕᴀᴀᴅ
  • 35,369
jah
  • 35
  • The value of n is quite large ... and so matrix exponentiation is required Not clear on how matrix exponentiation helps there. One way or another, you'll get the same $,F_n=c_1 u^n + c_2 v^n,$ where $,u, v,$ are the roots of $,x^2 - 2bx + 1 = 0,$. It's easy to see that both roots are real, and they multiply to $,1,$, so one of them will be larger than $,1,$ in absolute value. That term will grow pretty quickly for large $,n,$, no matter how you calculate it. – dxiv Feb 09 '18 at 23:53
  • This is the final step of a programming problem . Obviously , it could be done in linear time ( albeit it times out for some cases ) but matrix exponentiation has logarithmic complexity and doesn't time out. – jah Feb 10 '18 at 11:40
  • I wasn't suggesting doing it in linear time. Integer powers can be calculated in logarithmic time (see here for example), and this applies to both real numbers and matrices. – dxiv Feb 10 '18 at 18:03
  • Yes i am aware of that and the roots will be real but not necessarily integral . I will have to use float (or double ) which can lose accuracy with higher power and yield incorrect results . – jah Feb 10 '18 at 19:23
  • The exponents are integers, which is what matters for log time exponentaition. The roots are real, and are in fact the eigenvalues of the associated matrix, which you have to raise to the same $n^{th}$ power anyway. – dxiv Feb 10 '18 at 19:27
  • You are right but think of it from a programming viewpoint . Take b = 2 the roots will be 2+sqrt(3) and 2-sqrt(3) . Now sqrt(3) will be evaluated to 1.73 and further digits will improve accuracy . But even there is a limit to the no. of digits after decimal point the computer can store . So (1.73)^20 maybe won't give the same result as (sqrt(3))^20 which is 3^10 . – jah Feb 10 '18 at 19:58
  • Right, that's what I hinted at in my first comment. But you still have to calculate the same $,\left(2 \pm \sqrt{3}\right)^{20},$ when you calculate the matrix power $,A^n,$, so I still don't see how that's any better, or in fact any different. – dxiv Feb 10 '18 at 20:02
  • But i am not calculating the (eigenvalues)^n . After calculating $A^{n}$ the result is A[0][0] . Take a look at method 5 link – jah Feb 10 '18 at 20:10
  • That method is precisely the same as the one in the link I posted earlier (Fast exponentiation algorithm - How to arrive at it?), only used for matrices instead of real numbers. It doesn't buy you any advantage at all vs. the direct evaluation of $,c_1u^n+c_2v^n,$, neither complexity-wise nor accuracy-wise. – dxiv Feb 10 '18 at 23:07
  • Well i have used matrix method for solving fibonacci recurrence so naturally i thought of it first . Maybe your method is also a viable option for implementation . Will look into it :) – jah Feb 11 '18 at 09:50

2 Answers2

5

$$\begin{bmatrix} F_{n+1}\\F_n\end{bmatrix} = \begin{bmatrix} 2b&-1\\1\end{bmatrix}\begin{bmatrix} F_n\\F_{n-1}\end{bmatrix}$$

$$A = PDP^{-1}\\ A^n = PD^nP^{-1}$$

$$\lambda^2 - 2b +1 = 0\\ \lambda = b\pm\sqrt {b^2 - 1}$$

$$A^n = \frac{1}{\lambda_1 - \lambda_2}\begin{bmatrix} 1&1\\\lambda_2&\lambda_1\end{bmatrix}\begin{bmatrix}\lambda_1^n\\&\lambda_2^n\end{bmatrix}\begin{bmatrix} \lambda_1&-1\\-\lambda_2&1\end{bmatrix}$$

$$A^n = \frac{1}{\lambda_1 - \lambda_2} \begin{bmatrix}\lambda_1^{n+1}-\lambda_2^{n+1}&-(\lambda_1^n-\lambda_2)\\(\lambda_1\lambda_2)(\lambda_1^{n}-\lambda_2^n)&-(\lambda_1\lambda_2)(\lambda_1^{n-1}-\lambda_2^{n-1})\end{bmatrix}$$

But $\lambda_1\lambda_2 = 1$,

$$A^n = \frac{1}{\lambda_1 - \lambda_2} \begin{bmatrix}\lambda_1^{n+1}-\lambda_2^{n+1}&-(\lambda_1^n-\lambda_2)\\(\lambda_1^{n}-\lambda_2^n)&-(\lambda_1^{n-1}-\lambda_2^{n-1})\end{bmatrix}$$

$$F_{n+1} = \frac {(\lambda_1^{n+1}-\lambda_2^{n+1})b-(\lambda_1^{n}-\lambda_2^{n})a}{\lambda_1-\lambda_2}$$

Ѕᴀᴀᴅ
  • 35,369
Doug M
  • 58,694
  • This is exactly what i applied but i did not get the answers right. :( – jah Feb 09 '18 at 22:54
  • What does your diagonlization look like? – Doug M Feb 09 '18 at 22:55
  • I don't remember much about diagonalization and eigenvalues now but I don't think it would change the required value . Unfortunately your formula gives square roots which can lose accuracy with higher powers . Still thank you for your help on the matrix , maybe their is a problem with my code . – jah Feb 10 '18 at 11:56
  • Yes there are square roots, but that doesn't mean that you must loose accuracy. – Doug M Feb 12 '18 at 17:10
1

You might note that the generating function is $$g(x) = \frac{a + (1-2a)b x}{1 - 2 b x + x^2} = \frac{r(1-2a)b - a}{2(b - r) (x-r)} + \frac{s(1-2a)b - a}{(2(b-s)(x-s)}$$ where $x=r$ and $x=s$ are the roots of $1-2bx+x^2$ (assuming these are distinct), so that $$F(n) = \frac{(2a-1) b r - a}{2(r-b)} r^{-n-1} + \frac{(2a-1) b s - a}{2(s-b)} s^{-n-1}$$

Robert Israel
  • 470,583
  • I would like to know how you arrived at this but since i wouldn't understand it i won't ask :) . Unfortunately it involves square roots as well . An elegant solution , nonetheless . Upvoted . – jah Feb 10 '18 at 12:02