# Matrix recursion

I need to construct a non linear matricial recursive system for the series of matrix $M_k$ like the following one

$M_k[i][j] = F(M_{k-1}[i][j],M_{k-1}[i][j]) \text{ for } M_0 \text{ given}$

I have read that for one dimension one can use Sympy rec. But is there a way to do it simply in Sagemath?

edit retag close merge delete

The problem as stated has nothing to do with matrices, since for any fixed $i$, $j$, it represents just a recurrence sequence of numbers. Why do you want to deal with matrices rather than with their individual elements?

( 2021-03-09 19:49:24 +0200 )edit

Sort by ยป oldest newest most voted

Define a function, either recursive or not, to do that.

Recursive version:

def Mk_rec(M0, k, F):
import numbers
if not isinstance(k, numbers.Integral) or k < 0:
raise ValueError(f'Expected non-negative integer k, got: {k}')
MS = M0.parent()  # the matrix space
if k == 0:
return MS(M0)
M = Mk_rec(M0, k - 1, F)
FF = lambda a: F(a, a)
return MS(lambda i, j: FF(M[i, j]))


Non-recursive version:

def Mk_nrec(M0, k, F):
import numbers
if not isinstance(k, numbers.Integral) or k < 0:
raise ValueError(f'Expected non-negative integer k, got: {k}')
MS = M0.parent()  # the matrix space
M = MS(M0)
FF = lambda a: F(a, a)
for j in range(k):
M = MS(lambda i, j: FF(M[i, j]))
return M


Examples:

sage: M0 = matrix([[0, 2], [1, -1]])
sage: F = lambda a, b: a + b
sage: Mk_nrec(M0, 2, F)
[ 0  8]
[ 4 -4]
sage: Mk_rec(M0, 2, F)
[ 0  8]
[ 4 -4]

more