ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 18 Mar 2017 01:41:41 +0100Apply Functions Iterativelyhttps://ask.sagemath.org/question/35989/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?Mon, 12 Dec 2016 20:29:49 +0100https://ask.sagemath.org/question/35989/apply-functions-iteratively/Answer by dan_fulea for <p>I have a few functions <code>R1,R2,R3,R4</code> which act on vectors. If <code>X = matrix(3,1,(x0,y0,r))</code> then I have <code>R2</code> defined as</p>
<p><code>def R2(X):
return matrix(3,1,(-X[0,0],X[1,0],X[2,0]))</code></p>
<p>For instance, if <code>v = matrix(3,1,(1,1,1))</code>, then applying R2 to v gives the matrix $(-1,1,1)$.</p>
<p>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.</p>
<p>How can I do this?</p>
https://ask.sagemath.org/question/35989/apply-functions-iteratively/?answer=36972#post-id-36972In 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]
Sat, 18 Mar 2017 01:41:41 +0100https://ask.sagemath.org/question/35989/apply-functions-iteratively/?answer=36972#post-id-36972Answer by ndomes for <p>I have a few functions <code>R1,R2,R3,R4</code> which act on vectors. If <code>X = matrix(3,1,(x0,y0,r))</code> then I have <code>R2</code> defined as</p>
<p><code>def R2(X):
return matrix(3,1,(-X[0,0],X[1,0],X[2,0]))</code></p>
<p>For instance, if <code>v = matrix(3,1,(1,1,1))</code>, then applying R2 to v gives the matrix $(-1,1,1)$.</p>
<p>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.</p>
<p>How can I do this?</p>
https://ask.sagemath.org/question/35989/apply-functions-iteratively/?answer=35991#post-id-35991You 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 Mon, 12 Dec 2016 22:11:53 +0100https://ask.sagemath.org/question/35989/apply-functions-iteratively/?answer=35991#post-id-35991Comment by Daniel L for <p>You provided the code of R2. So I use R2 to show how to define the n-th power of R2 recursively.</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/35989/apply-functions-iteratively/?comment=35992#post-id-35992Thanks. 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?Mon, 12 Dec 2016 23:16:11 +0100https://ask.sagemath.org/question/35989/apply-functions-iteratively/?comment=35992#post-id-35992