Ask Your Question
0

Matrix recursion

asked 2021-03-09 18:49:38 +0200

Cyrille gravatar image

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 flag offensive close merge delete

Comments

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?

Max Alekseyev gravatar imageMax Alekseyev ( 2021-03-09 19:49:24 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-03-09 21:48:09 +0200

slelievre gravatar image

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]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-03-09 18:49:38 +0200

Seen: 57 times

Last updated: Mar 09