def cartesian_to_barycentric_2D(tri, xy):
xy = np.asarray(xy)<EOL>tri = np.asarray(tri)<EOL>if len(xy.shape) == <NUM_LIT:1>:<EOL><INDENT>return cartesian_to_barycentric_2D(np.transpose(np.asarray([tri]), (<NUM_LIT:1>,<NUM_LIT:2>,<NUM_LIT:0>)),<EOL>np.asarray([xy]).T)[:,<NUM_LIT:0>]<EOL><DEDENT>xy = xy if xy.shape[<NUM_LIT:0>] == <NUM_LIT:2> else xy.T<EOL>if tri.shape[<NUM_LIT:0>] == <NUM_LIT:3>:<EOL><INDENT>tri = tri if tri.shape[<NUM_LIT:1>] == <NUM_LIT:2> else np.transpose(tri, (<NUM_LIT:0>,<NUM_LIT:2>,<NUM_LIT:1>))<EOL><DEDENT>elif tri.shape[<NUM_LIT:1>] == <NUM_LIT:3>:<EOL><INDENT>tri = tri.T if tri.shape[<NUM_LIT:0>] == <NUM_LIT:2> else np.transpose(tri, (<NUM_LIT:1>,<NUM_LIT:2>,<NUM_LIT:0>))<EOL><DEDENT>elif tri.shape[<NUM_LIT:2>] == <NUM_LIT:3>:<EOL><INDENT>tri = np.transpose(tri, (<NUM_LIT:2>,<NUM_LIT:1>,<NUM_LIT:0>) if tri.shape[<NUM_LIT:1>] == <NUM_LIT:2> else (<NUM_LIT:2>,<NUM_LIT:0>,<NUM_LIT:1>))<EOL><DEDENT>if tri.shape[<NUM_LIT:0>] != <NUM_LIT:3> or tri.shape[<NUM_LIT:1>] != <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if xy.shape[<NUM_LIT:0>] != <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if tri.shape[<NUM_LIT:2>] != xy.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>(x,y) = xy<EOL>((x1,y1), (x2,y2), (x3,y3)) = tri<EOL>x_x3  = x  - x3<EOL>x1_x3 = x1 - x3<EOL>x3_x2 = x3 - x2<EOL>y_y3  = y  - y3<EOL>y1_y3 = y1 - y3<EOL>y2_y3 = y2 - y3<EOL>num1 = (y2_y3*x_x3  + x3_x2*y_y3)<EOL>num2 = (-y1_y3*x_x3 + x1_x3*y_y3)<EOL>den  = (y2_y3*x1_x3 + x3_x2*y1_y3)<EOL>zero = np.isclose(den, <NUM_LIT:0>)<EOL>den += zero<EOL>unit = <NUM_LIT:1> - zero<EOL>l1 = unit * num1 / den<EOL>l2 = unit * num2 / den<EOL>return np.asarray((l1, l2))<EOL>
cartesian_to_barycentric_2D(tri, xy) yields a (2 x n) barycentric coordinate matrix 
(or just a tuple if xy is a single (x, y) coordinate) of the first two barycentric coordinates
for the triangle coordinate array tri. The array tri should be 3 (vertices) x 2 (coordinates) x
n (triangles) unless xy is a tuple, in which case it should be a (3 x 2) matrix.