ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 09 Mar 2021 21:48:09 +0100Matrix recursionhttps://ask.sagemath.org/question/56075/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**?Tue, 09 Mar 2021 18:49:38 +0100https://ask.sagemath.org/question/56075/matrix-recursion/Comment by Max Alekseyev for <p>I need to construct a non linear matricial recursive system for the series of matrix $M_k$ like the following one</p>
<p>$M_k[i][j] = F(M_{k-1}[i][j],M_{k-1}[i][j]) \text{ for } M_0 \text{ given}$</p>
<p>I have read that for one dimension one can use <strong>Sympy</strong> <code>rec</code>. But is there a way to do it simply in <strong>Sagemath</strong>?</p>
https://ask.sagemath.org/question/56075/matrix-recursion/?comment=56078#post-id-56078The 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?Tue, 09 Mar 2021 19:49:24 +0100https://ask.sagemath.org/question/56075/matrix-recursion/?comment=56078#post-id-56078Answer by slelievre for <p>I need to construct a non linear matricial recursive system for the series of matrix $M_k$ like the following one</p>
<p>$M_k[i][j] = F(M_{k-1}[i][j],M_{k-1}[i][j]) \text{ for } M_0 \text{ given}$</p>
<p>I have read that for one dimension one can use <strong>Sympy</strong> <code>rec</code>. But is there a way to do it simply in <strong>Sagemath</strong>?</p>
https://ask.sagemath.org/question/56075/matrix-recursion/?answer=56087#post-id-56087Define 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]
Tue, 09 Mar 2021 21:48:09 +0100https://ask.sagemath.org/question/56075/matrix-recursion/?answer=56087#post-id-56087