Use multiple functions act on each other without repeating in succession

Viewed 29

I am using Python3.9.

There are six functions and I want to process 7 times while preventing the same function from repeating twice.

I want to find the coordinates of the mirror image when an arbitrary target coordinate point is reflected 7 times by 6 walls (including ceiling/floor) in a rectangular 3D space. The functions to be processed are def mirror 1 to 6 in the sample code below.

Could anyone tell me How can I code this?

#initial conditions
#room dimensions
xwide = 20
ywide = 15
zwide = 5
#arbitrary coordinates
target = np.array([2, 2, 2])

#Reflection in the y direction on the wall through the origin
xzmirror = np.array([[1, 0, 0], [0, -1, 0], [0, 0, 1]])
#Reflection in the z direction on the wall through the origin
xymirror = np.array([[1, 0, 0], [0, 1, 0], [0, 0, -1]])
#Reflection in the x direction on the wall through the origin
yzmirror = np.array([[-1, 0, 0], [0, 1, 0], [0, 0, 1]])
#prepare normal vector
xn = np.array([-1, 0, 0])
yn = np.array([0, -1, 0])
zn = np.array([0, 0, -1])

#functions to find the mirrored coordinates of a reflection
def mirror1(a):
    x0_mir1 = np.dot(a, yzmirror)
    return x0_mir1

def mirror2(a):
    y0_mir1 = np.dot(a, xzmirror)
    return y0_mir1

def mirror3(a):
    z0_mir1 = np.dot(a, xymirror)
    return z0_mir1

def mirror4(a):
    x1_mir1 = a - (2 * (np.dot(a, xn) + xwide)) * xn
    return x1_mir1

def mirror5(a):
    y1_mir1 = a - (2 * (np.dot(a, yn) + ywide)) * yn
    return y1_mir1

def mirror6(a):
    z1_mir1 = a - (2 * (np.dot(a, zn) + zwide)) * zn
    return z1_mir1
1 Answers

You want to process seven times with six functions, and you don't want any function called twice. That's impossible. Perhaps you've misstated what you want or I'm mis-understanding.

But let's say instead that you wanted to handle four operations without any repetitions:

operations = (mirror1, mirror2, mirror3, mirror4, mirror5, mirror6)
coordinate = .....

for op1, op2, op3, op4 in itertools.permutations(operations, 4):
    result = op1(op2(op3(op4(coordinate))))

Now that you've explained better what you want:

The absolutely simplest method to get what you what is

operations = (mirror1, mirror2, mirror3, mirror4, mirror5, mirror6)
coordinate = .....

for op1, op2, op3, op4, op5, op6, op7 = itertools.product(operations, repeat=7):
    if op1 != op2 != op3 != op4 != op5 != op6 != op7:
        result = op1(op2(op3(op4(op5(op6(op7(coordinates)))))))

Yeah, there are more efficient wants to get the result you want, but they'll be a lot more code, and not really faster with only six mirror operations and seven reflections.

Note that in most cases a != b != c ... is the wrong thing to write. People thing it means that a, b, and c are all different. But you want that they are pairwise different, which is precisely what this is saying.

Related