```python
import numpy as np
import sympy as sy
sy.init_printing()
```


```python
n = sy.Symbol('n')
```

$$
  \left[
    \begin{array}{rrr}
      a_{n+2} \\
      a_{n+1}
    \end{array}
  \right]
  = \left[
    \begin{array}{rrr}
      2 & 8 \\
      1 & 0 \\
    \end{array}
  \right]
  \left[
    \begin{array}{rrr}
      a_{n+1} \\
      a_{n}
    \end{array}
  \right]
$$
を変形すると
$$
  \left[
    \begin{array}{}
      a_{n+1} \\
      a_{n}
    \end{array}
  \right]
  = \left[
    \begin{array}{}
      2 & 8 \\
      1 & 0 \\
    \end{array}
  \right] ^ {n-1}
  \left[
    \begin{array}{}
      a_{2} \\
      a_{1}
    \end{array}
  \right]
$$


```python
A = sy.Matrix([[2, 8], [1, 0]])
A
```




$$\left[\begin{matrix}2 & 8\\1 & 0\end{matrix}\right]$$



$$
  \left[
    \begin{array}{rrr}
      a_{n+1} \\
      a_{n}
    \end{array}
  \right]
= A^{n-1} \left[
    \begin{array}{}
      a_{2} \\
      a_{1}
    \end{array}
  \right]
$$


```python
A ** (n-1) * sy.Matrix([3, 1])
```




$$\left[\begin{matrix}- \frac{\left(-2\right)^{n - 1}}{3} + \frac{10 \cdot 4^{n - 1}}{3}\\\frac{\left(-2\right)^{n - 1}}{6} + \frac{5 \cdot 4^{n - 1}}{6}\end{matrix}\right]$$



このように求めることができた(2行目が `n` の一般項)

でも、これでは面白くないし、人力では厳しいので対角化して解いてみることにする


```python
# スペクトル分解して、固有値と固有ベクトルを求める
# 固有値, 重複度, 固有ベクトル(重複度分)
evs = A.eigenvects()
evs
```




$$\left [ \left ( -2, \quad 1, \quad \left [ \left[\begin{matrix}-2\\1\end{matrix}\right]\right ]\right ), \quad \left ( 4, \quad 1, \quad \left [ \left[\begin{matrix}4\\1\end{matrix}\right]\right ]\right )\right ]$$




```python
P = sy.Matrix([ev[-1][0].values() for ev in evs]).T
P
```




$$\left[\begin{matrix}-2 & 4\\1 & 1\end{matrix}\right]$$




```python
P ** -1
```




$$\left[\begin{matrix}- \frac{1}{6} & \frac{2}{3}\\\frac{1}{6} & \frac{1}{3}\end{matrix}\right]$$




```python
# 6倍したもの
P ** -1 * 6
```




$$\left[\begin{matrix}-1 & 4\\1 & 2\end{matrix}\right]$$



$$ D = P^{-1} A P $$


```python
D = P ** (-1) * A * P
D
```




$$\left[\begin{matrix}-2 & 0\\0 & 4\end{matrix}\right]$$



D を用いて

$$ A^{n-1} = P D^{n-1} P^{-1} $$

のように表現できる


```python
# これを計算すると同様の結果を得られる
(P) * (D ** (n-1)) * (P ** -1) * sy.Matrix([3, 1])
```




$$\left[\begin{matrix}- \frac{\left(-2\right)^{n - 1}}{3} + \frac{10 \cdot 4^{n - 1}}{3}\\\frac{\left(-2\right)^{n - 1}}{6} + \frac{5 \cdot 4^{n - 1}}{6}\end{matrix}\right]$$



ちなみに
$$
  \left[
    \begin{array}{rrr}
      a_{n+2} \\
      a_{n+1}
    \end{array}
  \right]= A
  \left[
    \begin{array}{rrr}
      a_{n+1} \\
      a_{n}
    \end{array}
  \right]  
$$

を変形して

$$
  \left[
    \begin{array}{rrr}
      a_{n+2} \\
      a_{n+1}
    \end{array}
  \right] = P D P^{-1}
  \left[
    \begin{array}{rrr}
      a_{n+1} \\
      a_{n}
    \end{array}
  \right]
$$

$$ P^{-1}
  \left[
    \begin{array}{rrr}
      a_{n+2} \\
      a_{n+1}
    \end{array}
  \right]
= D P^{-1}
  \left[
    \begin{array}{rrr}
      a_{n+1} \\
      a_{n}
    \end{array}
  \right]
$$


$$
  \left[
    \begin{array}{}
      -1 & 4 \\
      1 & 2 \\
    \end{array}
  \right]
  \left[
    \begin{array}{rrr}
      a_{n+2} \\
      a_{n+1}
    \end{array}
  \right]
  = \left[
    \begin{array}{}
      -2 & 0 \\
      0 & 4 \\
    \end{array}
  \right]
  \left[
    \begin{array}{}
      -1 & 4 \\
      1 & 2 \\
    \end{array}
  \right]
  \left[
    \begin{array}{rrr}
      a_{n+1} \\
      a_{n}
    \end{array}
  \right]
$$
(両辺を6倍してある)

漸化式の途中式と一致しているのがわかる
$$
\begin{eqnarray}
\left\{
\begin{array}{l}
  a_{n+2} - 4a_{n+1} = -2(a_{n+1} - 4a_n) \\
  a_{n+2} + 2a_{n+1} =  4(a_{n+1} + 2a_n)
\end{array}
\right.
\end{eqnarray}
$$


```python

```

adf03fb8a4f56c83602761fbbb05088022892874
ipynb
Jupyter Notebook
.examples/matrix-3.ipynb
righ/react-ipynb-renderer
e827ed6f83a14c30ea614ecc6250ebe38aaeb1de
Apache-2.0
2021-06-08T13:39:58.000Z
2022-02-19T13:02:13.000Z
.examples/matrix-3.ipynb
righ/react-ipynb-renderer
e827ed6f83a14c30ea614ecc6250ebe38aaeb1de
Apache-2.0
.examples/matrix-3.ipynb
righ/react-ipynb-renderer
e827ed6f83a14c30ea614ecc6250ebe38aaeb1de
Apache-2.0
2021-08-09T13:27:28.000Z
2022-02-19T13:02:14.000Z
Qwen/Qwen-72B
1. YES
2. YES