Search code examples
pythonloopsmatrixnested-loopspython-itertools

Subtraction equivalent of itertools.product()


I am a college student working on a project analysing some large datasets.

Simplifying my problem, I have a 2 sets of points, In Matrices "A" and "B" Such that:

A = [[x1, y1], [x2, y2],...] and B = [[x'1, y'1], [x'2, y'2],...] 

I would like to create a function which outputs a Matrix, C, with elements:

Cij  = atan((y'i - yj)/(x'i - xj))

Essentially, the angle (wrt x.axis) subtended by the line connecting any two points, one from each list. The dataset is sufficiently large such that nested FOR Loops are not an option.

Current attempts have led me to itertools product function. If there was an equivalent which provided a subtraction between the elements (i.e y'i-yj ) then I would be able to go from there quite simply.

Is anyone aware of something which would provide this functionality? Or perhaps any other way of achieving the angle between all of these points without a slow iterative process?

Thanks in advance,

Alex


Solution

  • Use numpy for these computations

    import numpy as np
    
    A = np.array(A)
    B = np.array(B)
    
    C = np.arctan((B[:, None,  1] - A[:, 1]) / (B[:, None, 0] - A[:, 0]))