The function uses numpy for solving the algebra equations and I want the function to accept both standard tuples/lists and numpy's array type.

I came up with two solutions:

Number 1:

p1 = (1, 2) v1 = (1, 2) p2 = (5, 5) v2 = (1, 3) def intersection_1(p1, v1, p2, v2): """ Calculates and returns the intersection between two vectors, v1 and v2, starting from points p1 and p2. Arguments must be numpy arrays. Returns the intersection as type 'numpy.ndarray'. """ const = p2 - p1 coeff = ((v1[0], -v2[0]), (v1[1], -v2[1])) n1, n2 = np.linalg.solve(coeff, const) return p1 + n1 * v1 print(intersection_1(*list(map(np.array,(p1, v1, p2, v2)))))

This function is short and (relatively) straightforward. The function requires the arguments to be numpy arrays.

This is ensured by a quite 'bumpy' function call.

I didn't like this clumsy function call so I made version 2 including a test for argument type:

p1 = (1, 2) v1 = (1, 2) p2 = (5, 5) v2 = (1, 3) def intersection_2(*args): """ Calculates and returns the intersection between two vectors, v1 and v2, starting from points p1 and p2 Arguments must be in the order: p1, v1, p2, v2 Returns the intersection as type 'numpy.ndarray' """ if any(not isinstance(arg, (np.ndarray)) for arg in args): return intersection_2(*list(map(np.array, args))) p1, v1, p2, v2 = args const = p2 - p1 coeff = ((v1[0], -v2[0]), (v1[1], -v2[1])) n1, n2 = np.linalg.solve(coeff, const) return p1 + n1 * v1 print(intersection_2(p1, v1, p2, v2))

Now, the function call is nice and tidy; I've moved all the 'mechanics' handling tuple/list input inside the function - but the function itself is less appealing.

My question: Which is most pythonic? Or do you have an even better suggestion?

In case you wonder about the math:

We need to solve this equation: p1 + n1 * v1 = p2 + n2 * v2; the unknown are n1 and n2. Rearranging to n1 * v1 - n2 * v2 = p2 - p1 which is formatted to fit directly into numpy's linalg.solve() method.

Maybe someone can find a better way to write the coeff; admittedly, ((v1[0], -v2[0]), (v1[1], -v2[1])) is bulky but I didn't find anything better.