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]
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?