Ask Your Question
1

Apply Functions Iteratively

asked 2016-12-12 20:29:49 +0100

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

2 Answers

Sort by ยป oldest newest most voted
2

answered 2016-12-12 22:11:53 +0100

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

Comments

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 23:16:11 +0100 )edit
1

answered 2017-03-18 01:41:41 +0100

dan_fulea gravatar image

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]
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: 2016-12-12 20:29:49 +0100

Seen: 883 times

Last updated: Mar 18 '17