We assume that the numpy module of Python has been imported using the instruction \texttt{import numpy as np}. This thus gives access to the following functions:
\texttt{np.identity( n )} creates $I _ { n }$, the identity matrix of order n;
\texttt{A.shape} gives, in the form of a tuple, the size of array A, for example \texttt{np.identity(5).shape} $\rightarrow ( 5,5 )$;
\texttt{np.dot(A, B)} computes the matrix product of A and B if A and B are two-dimensional arrays compatible with matrix multiplication.
The fast exponentiation algorithm is based on the following principle: $$\begin{cases} B ^ { 0 } = I _ { n } & \\ B ^ { k } = \left( B ^ { 2 } \right) ^ { \frac { k } { 2 } } & \text { if } k \text { is even } \\ B ^ { k } = B \left( B ^ { 2 } \right) ^ { \frac { k - 1 } { 2 } } & \text { if } k \text { is odd.} \end{cases}$$ Write a Python function \texttt{puissance2(B, k)} that takes as arguments a square matrix B and a natural number $k$ and returns the matrix $B ^ { k }$ computed using the fast exponentiation algorithm.
We assume that the numpy module of Python has been imported using the instruction \texttt{import numpy as np}. This thus gives access to the following functions:
\begin{itemize}
\item \texttt{np.identity( n )} creates $I _ { n }$, the identity matrix of order n;
\item \texttt{A.shape} gives, in the form of a tuple, the size of array A, for example \texttt{np.identity(5).shape} $\rightarrow ( 5,5 )$;
\item \texttt{np.dot(A, B)} computes the matrix product of A and B if A and B are two-dimensional arrays compatible with matrix multiplication.
\end{itemize}
The fast exponentiation algorithm is based on the following principle:
$$\begin{cases} B ^ { 0 } = I _ { n } & \\ B ^ { k } = \left( B ^ { 2 } \right) ^ { \frac { k } { 2 } } & \text { if } k \text { is even } \\ B ^ { k } = B \left( B ^ { 2 } \right) ^ { \frac { k - 1 } { 2 } } & \text { if } k \text { is odd.} \end{cases}$$
Write a Python function \texttt{puissance2(B, k)} that takes as arguments a square matrix B and a natural number $k$ and returns the matrix $B ^ { k }$ computed using the fast exponentiation algorithm.