Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
1

Apply Functions Iteratively

asked 8 years ago

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),R12(v),R2(R1(v)),R3(R1(v)),R4(R1(v)),R13(v),...,R110(v)...,R410(v) (as vectors) where Rnj=RjRj n times.

How can I do this?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 8 years ago

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
Preview: (hide)
link

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 ( 8 years ago )
1

answered 8 years ago

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]
Preview: (hide)
link

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: 8 years ago

Seen: 1,032 times

Last updated: Mar 18 '17