On Tue, 2022-07-05 at 23:36 +0000, rmccampbe...@gmail.com wrote: > Maybe I wasn't clear, I'm talking about the 1-dimensional vector > product, but applied to N-D arrays of vectors. Certainly dot products > can be realized as matrix products, and often are in mathematics for > convenience, but matrices and vectors are not the same thing, > theoretically or coding wise. If I have two (M, N, k) arrays a and b > where k is the vector dimension, to dot product them using matrix > notation I have to do: > > (a[:, :, np.newaxis, :] @ b[:, :, :, np.newaxis])[:, :, 0, 0] >
You can make it more readable for example with: res = a[..., np.newaxis, :] @ b[..., :, np.newaxis] res = res[..., 0, 0] (could remove the `:`). Maybe even more tricks like: rowmat = np.s_[..., np.newaxis, :] colmat = np.s_[..., :, np.newaxis] res = a[rowmat] @ a[colmat] > Which I certainly don't find readable (I always have to scratch my > head a little bit to figure out whether the newaxis's are in the > right places). If this is a common operation in larger expressions, > then it basically has to be written as a separate function, which > then someone reading the code may have to look at for the semantics. > It also breaks down if you want to write generic vector functions > that may be applied along different axes; then you need to do > something like > I would suggest using `np.moveaxis` to implement a helper. Now of course there may be a point to put that helper into NumPy as `np.vecdot` (or similar), even if it is probably a 3 line function if implemented in terms of `matmul`. Cheers, Sebastian > np.squeeze(np.expand_dims(a, axis=axis) @ np.expand_dims(b, > axis=axis+1), (axis, axis+1)) > > (after normalizing the axis; if it's negative you'd need to do axis-1 > and axis instead). > > Compare this to the simplicity, composability and consistency of: > > a.dot(b, axis=-1) * np.cross(c, d, axis=-1).dot(e, axis=-1) / > np.linalg.norm(f, axis=-1) > > (the cross and norm operators already support an axis parameter) > _______________________________________________ > NumPy-Discussion mailing list -- numpy-discussion@python.org > To unsubscribe send an email to numpy-discussion-le...@python.org > https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ > Member address: sebast...@sipsolutions.net >
signature.asc
Description: This is a digitally signed message part
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-le...@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: arch...@mail-archive.com