Ask Your Question

Apply Functions Iteratively

asked 2016-12-12 13:29:49 -0600

Daniel L gravatar image

I have a few functions R1,R2,R3,R4 which act on vectors. If X = matrix(3,1,(x0,y0,r)) then I have R2 defined as

def R2(X): return matrix(3,1,(-X[0,0],X[1,0],X[2,0]))

For instance, if v = matrix(3,1,(1,1,1)), then applying R2 to v gives the matrix $(-1,1,1)$.

I'd like to apply about 10 "levels" of iteration of these functions and have the output as a list of vectors. So, the list should be $R1(v), R2(v),R3(v),R4(v),R1^2(v),R2(R1(v)),R3(R1(v)),R4(R1(v)),R1^3(v),...,R1^{10}(v)\,...,R4^{10}(v)$ (as vectors) where $R_j^n = R_j \circ \cdots \circ R_j$ $n$ times.

How can I do this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-12-12 15:11:53 -0600

ndomes gravatar image

You provided the code of R2. So I use R2 to show how to define the n-th power of R2 recursively.

def R2(X): 
    return matrix(3,1,(-X[0,0],X[1,0],X[2,0]))

def R2_pow(X,n):
    if n <= 1:
        return R2(X)
        return R2(R2_pow(X,n-1))

M = matrix(3,1,(1,1,1))

L = [R2_pow(M,k) for k in [3..10]]
D = {k:R2_pow(M,k) for k in [3..10]}

L; D
edit flag offensive delete link more


Thanks. Ok, and if I have def R3(X): return matrix(3,1,(-X[0,0]+2,X[1,0],X[2,0])) def R4(X): return matrix(3,1,(X[0,0],-X[1,0]+2,X[2,0])) how could I combine the three together as I asked above?

Daniel L gravatar imageDaniel L ( 2016-12-12 16:16:11 -0600 )edit

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


Asked: 2016-12-12 13:29:49 -0600

Seen: 56 times

Last updated: Dec 12 '16