Matrix exponentiation

Why do we need Matrix exponentiation?

Let us consider the case for fibonacci series..!!

f(n) = f(n-1) + f(n-2)

1, 1, 2, 3, 5, 8, 13..........

But what if we need to find f(n) in which n

can be up to 

10^{18}

Why do we need Matrix exponentiation?

f(n) = f(n-1) + f(n-2)

1, 1, 2, 3, 5, 8, 13..........

But what if we need to find f(n) in which n

can be up to 

10^{18}

O(n)  solution will not work here we need to find a better approach.

Thats where matrix exponentiation comes in!!!!

Concept

Before entering the core algorithm : Let us understand some basics for matrix

Concept

Before entering the core algorithm : Let us understand some basics for matrix

Matrix multiplication : 

Concept

Before entering the core algorithm : Let us understand some basics for matrix

Matrix multiplication : 

(n,t)   X    (t,m)         

(n,m)

Concept

Before entering the core algorithm : Let us understand some basics for matrix

Matrix multiplication : 

(n,t)   X    (t,m)         

(n,m)

n

t

t

m

n

m

X

=

Concept

Before entering the core algorithm : Let us understand some basics for matrix

Concept of exponentiation : 

Concept

Before entering the core algorithm : Let us understand some basics for matrix

Concept of exponentiation : 

Suppose we have a matrix A.

Then

=   A * A * A..........(t- times)

A^t

Steps for Matrix exponentiation

Consider a function

f(n)  = f(n-1) + f(n-2)

Steps for Matrix exponentiation

Consider a function

f(n)  = f(n-1) + f(n-2)

Recursively

Dynamic programming

Exponential

O(n)

Steps for Matrix exponentiation

States of function

f(n-1)

f(n-2)

f(n)  =  f(n-1) + f(n-2)

Kth state

(K+1)th state

f(n)

f(n-1)

Steps for Matrix exponentiation

States of function

f(n)  =  f(n-1) + f(n-2)

Kth state

(K+1)th state

\begin{bmatrix} f(n-1) \\ f(n-2) \end{bmatrix}
\begin{bmatrix} f(n) \\ f(n-1) \end{bmatrix}

M *

Steps for Matrix exponentiation

States of function

f(n)  =  f(n-1) + f(n-2)

Kth state

(K+1)th state

\begin{bmatrix} f(n-1) \\ f(n-2) \end{bmatrix}
\begin{bmatrix} f(n) \\ f(n-1) \end{bmatrix}

M *

The main game is to find this matrix 

Steps for Matrix exponentiation

States of function

f(2)  =  f(1) + f(0)

Kth state

(K+1)th state

\begin{bmatrix} f(1) \\ f(0) \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}

M *

Steps for Matrix exponentiation

States of function

f(2)  =  f(1) + f(0)

2X1

2X1

\begin{bmatrix} f(1) \\ f(0) \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}

M *

Size of matrix

2X2

Steps for Matrix exponentiation

States of function

f(2)  =  f(1) + f(0)

\begin{bmatrix} f(1) \\ f(0) \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} a & b \\ c & d \end{bmatrix}

*

Steps for Matrix exponentiation

States of function

f(2)  =  f(1) + f(0)

\begin{bmatrix} f(1) \\ f(0) \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} a & b \\ c & d \end{bmatrix}

*

a*f(1) + b*f(0) = f(2)

c*f(1) + d*f(0) = f(1)

Steps for Matrix exponentiation

States of function

f(2)  =  f(1) + f(0)

\begin{bmatrix} f(1) \\ f(0) \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} a & b \\ c & d \end{bmatrix}

*

a*f(1) + b*f(0) = f(2)

c*f(1) + d*f(0) = f(1)

We get

a = 1 and b = 1

c = 1 and d = 0

Steps for Matrix exponentiation

States of function

f(2)  =  f(1) + f(0)

\begin{bmatrix} f(1) \\ f(0) \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

a*f(1) + b*f(0) = f(2)

c*f(1) + d*f(0) = f(1)

Steps for Matrix exponentiation

States of function

f(2)  =  f(1) + f(0)

\begin{bmatrix} f(1) \\ f(0) \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

Ultimately we need to get f(2)

f(0) = 1

f(1) = 1

Steps for Matrix exponentiation

States of function

f(2)  =  f(1) + f(0)

\begin{bmatrix} f(1) \\ f(0) \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

Ultimately we need to get f(2)

f(0) = 1

f(1) = 1

1*f(1) + 1*f(0) = f(2)

Steps for Matrix exponentiation

States of function

f(2)  =  f(1) + f(0)

\begin{bmatrix} 1 \\ 1 \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

Ultimately we need to get f(2)

f(0) = 1

f(1) = 1

1*1+ 1*1 = f(2)

Steps for Matrix exponentiation

States of function

f(2)  =  f(1) + f(0)

\begin{bmatrix} 1 \\ 1 \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

Ultimately we need to get f(2)

f(0) = 1

f(1) = 1

1*1+ 1*1 = f(2)

f(2) = 2

Steps for Matrix exponentiation

States of function

f(3)  =  f(2) + f(1)

\begin{bmatrix} 1 \\ 1 \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

f(0) = 1

f(1) = 1

\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} f(3) \\ f(2) \end{bmatrix}

*

M

Steps for Matrix exponentiation

States of function

f(3)  =  f(2) + f(1)

\begin{bmatrix} 1 \\ 1 \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

f(0) = 1

f(1) = 1

\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} f(3) \\ f(2) \end{bmatrix}

*

\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

Steps for Matrix exponentiation

States of function

f(3)  =  f(2) + f(1)

\begin{bmatrix} 1 \\ 1 \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

f(0) = 1

f(1) = 1

\begin{bmatrix} f(3) \\ f(2) \end{bmatrix}

*

\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}
\begin{bmatrix} 1 \\ 1 \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

Steps for Matrix exponentiation

States of function

f(3)  =  f(2) + f(1)

\begin{bmatrix} 1 \\ 1 \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

f(0) = 1

f(1) = 1

\begin{bmatrix} f(3) \\ f(2) \end{bmatrix}

*

\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}
\begin{bmatrix} 1 \\ 1 \end{bmatrix}

3-1 = 2

Steps for Matrix exponentiation

States of function

f(3)  =  f(2) + f(1)

\begin{bmatrix} 1 \\ 1 \end{bmatrix}
\begin{bmatrix} f(2) \\ f(1) \end{bmatrix}
\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

*

f(0) = 1

f(1) = 1

\begin{bmatrix} f(3) \\ f(2) \end{bmatrix}

*

\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}
\begin{bmatrix} 1 \\ 1 \end{bmatrix}

2

Steps for Matrix exponentiation

States of function

f(n)  =  f(n-1) + f(n-2)

\begin{bmatrix} f(n-1) \\ f(n-2) \end{bmatrix}
\begin{bmatrix} f(n) \\ f(n-1) \end{bmatrix}

*

M

n-1

Steps for Matrix exponentiation

States of function

f(n)  =  f(n-1) + f(n-2)

\begin{bmatrix} f(n-1) \\ f(n-2) \end{bmatrix}
\begin{bmatrix} f(n) \\ f(n-1) \end{bmatrix}

*

M

n-1

M[0][0] * 1 + M[0][1] * 1

Some examples

States of function

f(n)  =  A*f(n-1) + B*f(n-2)

Some examples

States of function

f(n)  =  A*f(n-1) + B*f(n-2)

\begin{bmatrix} f(n-1) \\ f(n-2) \end{bmatrix}
\begin{bmatrix} f(n) \\ f(n-1) \end{bmatrix}

*

M

n-1

Some examples

States of function

f(n)  =  A*f(n-1) + B*f(n-2)

\begin{bmatrix} f(n-1) \\ f(n-2) \end{bmatrix}
\begin{bmatrix} f(n) \\ f(n-1) \end{bmatrix}

*

M

n-1

\begin{bmatrix} A & B \\ 1 & 0 \end{bmatrix}

Some examples

States of function

f(n)  =  A*f(n-1) + B*f(n-2) + C

Some examples

States of function

f(n)  =  A*f(n-1) + B*f(n-2) + C

\begin{bmatrix} f(n-1) \\ f(n-2) \\ c \end{bmatrix}
\begin{bmatrix} f(n) \\ f(n-1) \\ c \end{bmatrix}

*

M

n-1

Some examples

States of function

f(n)  =  A*f(n-1) + B*f(n-2) + C

\begin{bmatrix} f(n-1) \\ f(n-2) \\ c \end{bmatrix}
\begin{bmatrix} f(n) \\ f(n-1) \\ c \end{bmatrix}

*

M

n-1

\begin{bmatrix} A & B & C\\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{bmatrix}

Some examples

States of function

f(n)  = f(n-1) + f(n-4)

Some examples

States of function

f(n)  = f(n-1) + f(n-4)

\begin{bmatrix} f(n-1) \\ f(n-2) \\ f(n-3) \\ f(n-4) \end{bmatrix}
\begin{bmatrix} f(n) \\ f(n-1) \\ f(n-2) \\ f(n-3) \end{bmatrix}

*

M

n-1

Some examples

States of function

f(n)  = f(n-1) + f(n-4)

\begin{bmatrix} f(n-1) \\ f(n-2) \\ f(n-3) \\ f(n-4) \end{bmatrix}
\begin{bmatrix} f(n) \\ f(n-1) \\ f(n-2) \\ f(n-3) \end{bmatrix}

*

M

n-1

\begin{bmatrix} 1 & 0 & 0 & 1\\ 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ \end{bmatrix}

Matrix exponentiation

By Chirayu Jain

Matrix exponentiation

  • 70