views:

1919

answers:

1

Hi,if i have a function A,which can apply a certain rule on a given matrix to generate a another matrix which i call it the next state of the origin matrix,also the function can determine the the final state of the matrix by given times N(apply the rule on origin,and apply the rule on the next state of the origin matrix again,and apply rule apply rule... for N times).

So suppose for a given matrix,apply the rule on it for 5 times and the final matrix become the same as the origin matrix,and we call that matrix's period is 5.

And I have another function B,how can i make the functionB can determine the period of a given function under the same rule of the functionA,and return the period?I just have no idea how to start to make it...Thanks in advance.

def functionA(origin_matrix,N_times):
   #apply rule on the origin_matrix to generate another matrix which is the next sate of it.
   #apply rule on origin_matrix for N_times
   return the_final_matrix

def functionB(origin_matrix):
   #determine the period of the the origin_matrix.
   return period
+4  A: 

Use a for loop, or a while loop with a temporary result and a counter. The latter method is most efficient (in general).

Simple version, in pseudocode:

iterations = 0;
tmp = origin_matrix;

do
    tmp = operation(tmp);
    iterations += 1;
while tmp != origin_matrix;

return iterations;

EDIT: You can also use a simple while construct:

while True:
    tmp = operation(tmp)
    iterations += 1

    if tmp == origin_matrix:
        break  # Or you could return here.

EDIT: That was for functionB. I didn't know they were separate questions. For that example, operation(x) = functionA(x, 1).

For functionA, you'd use a for loop, most likely. Pseudocode:

matrix = origin_matrix

for i in range(N_times):
    matrix = operation(matrix)

return matrix
strager
Note that this may be non-halting if the operation's period is infinite. You might want to build in a guard so that the loop executes some maximum number of times in that case. It may depend on how the language handles integer overflow.
tvanfosson
good point,thanks:)
NONEenglisher
if `operation` modifies `tmp` inplace then it will always return iterations==1. In that case: tmp = copy.copy(original) or copy.deepcopy(original) could be useful.
J.F. Sebastian
#strager,thanks for your answer,but is that your operation function is as smae as my functtionA(origin_matrix,N_times)?The point i dont know is in operation function seems do not have parameter N_times....i am quite confusing,sorry about my silly question..
NONEenglisher
@NONEenglisher, the operation function in the pseudocode is like your functionA with N_times=1 (it turns one cycle).
strager
@J.F. Sebastian, I posted pseudocode, not Python; I don't know if Python does deep copies or not with =. I meant for a deep copy to occur, yes (or at least an implicitly shared copy).
strager
@strager,whats the "do" does in your pseudocode?my python is 2.5.thanks
NONEenglisher
i am a geginner of python...please forgive my boring questions..
NONEenglisher
@NONEenglisher, It's fine that you are a beginner coder. I have edited my post to answer your question about do (rather, I provided an alternative which may be easier in Python).
strager
@strager,thanks for your patient and excellent answer.
NONEenglisher