# Apply Functions Iteratively

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

Sort by ยป oldest newest most voted

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)
else:
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

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?

( 2016-12-12 16:16:11 -0500 )edit

In the strict sense of the question i've tried (without having R1):

# def R1(X):   a,b,c = X; return ???
def R2(X):   a,b,c = X; return ( -a, b, c )
def R3(X):   a,b,c = X; return ( -a+2, b, c )
def R4(X):   a,b,c = X; return ( a, -b+2, c )

myFunctions = ( R2, R3, R4, )
myTuples    = [ (0,0,0), ]

depth = 0
while depth < 10:
depth += 1
newTuples = []
for R in myFunctions:
for X in myTuples:
RX = R(X)
if RX not in myTuples and RX not in newTuples:
newTuples.append( RX )
myTuples.extend( newTuples )
print "There are %s tuples after a generation of depth %s" % ( len(myTuples), depth )


And we get:

There are 3 tuples after a generation of depth 1
There are 5 tuples after a generation of depth 2
There are 7 tuples after a generation of depth 3
There are 9 tuples after a generation of depth 4
There are 11 tuples after a generation of depth 5
There are 13 tuples after a generation of depth 6
There are 15 tuples after a generation of depth 7
There are 17 tuples after a generation of depth 8
There are 19 tuples after a generation of depth 9
There are 21 tuples after a generation of depth 10


and the list (and the mark points 3,5,7,...) show which tuples were added at each step. (It was simpler for me to use tuples.)

A better idea would be (for my taste) to introduce a fourth component equal to one and to use matrix multiplication for the above operations using the matrices from the following code:

v  = matrix( QQ, 4, 1, [0,0,0,1] )
R2 = matrix.diagonal( QQ, [-1,1,1,1] )
R3 = matrix.elementary( QQ, 4, row1=0, row2=3, scale=2 ) * R2
R4 = matrix.elementary( QQ, 4, row1=1, row2=3, scale=2 ) * matrix.diagonal( QQ, [1,-1,1,1] )

print "R2 is\n%s\n" % R2
print "R3 is\n%s\n" % R3
print "R4 is\n%s\n" % R4

print "R2 * v transposed is %s" % ( R2*v ).transpose()
print "R3 * v transposed is %s" % ( R3*v ).transpose()
print "R4 * v transposed is %s" % ( R4*v ).transpose()


where i had more to print the following matrices than to define them.

R2 is
[-1  0  0  0]
[ 0  1  0  0]
[ 0  0  1  0]
[ 0  0  0  1]

R3 is
[-1  0  0  2]
[ 0  1  0  0]
[ 0  0  1  0]
[ 0  0  0  1]

R4 is
[ 1  0  0  0]
[ 0 -1  0  2]
[ 0  0  1  0]
[ 0  0  0  1]

R2 * v transposed is [0 0 0 1]
R3 * v transposed is [2 0 0 1]
R4 * v transposed is [0 2 0 1]

more

## Stats

Seen: 248 times

Last updated: Mar 17 '17