I have an array A[:,:], and an operation Op acting on the rows x=A[:,J] of A. Therefore, for each row x of A I obtain Op(x). If Op(x) is not a row of A, I append it to A. I do this until A is closed under Op (I am assuming that Op does not give rise to a neverending loop, i.e. Op is closed under a certain number of iterations). At the end of this process, given the extended A closed under Op, I also want the permutation Pindex such that Op(A[:,J])=A[:,Pindex(J)].
I have been able to write a Python code to do that:
import numpy as np
A=np.array([[0,2,3],
[0,-3,-1],
[0,4,3]])
def Op(x):
return [0,-x[2],x[1]-x[2]]
A=A.tolist()
last=len(A)
Pindex=[]
for i,x in enumerate(A):
found=False
xOp=Op(x)
for j,y in enumerate(A):
if np.array_equal(y,xOp):
Pindex.append(j)
found=True
break
if not found:
A.append(xOp)
Pindex.append(last)
last+=1
A=np.asarray(A)
print A
print Pindex
print A[Pindex]
However, it does not seem to me very "pythonic". I guess it can be improved, to make it faster. Any suggestion?
P.S. This is part of a bigger code, where I need to use arrays. I needed to convert the array to a list because I needed to update the length of the object on which I am iterating. Maybe there is a smarter way to do that only with arrays.
P.P.S. I was not sure about the title of the question. I can change it if you have suggestions.