[Numpy-discussion] Re: New matvec and vecmat functions
On Tue, Jan 23, 2024 at 3:18 PM Marten van Kerkwijk wrote: > Hi All, > > I have a PR [1] that adds `np.matvec` and `np.vecmat` gufuncs for > matrix-vector and vector-matrix calculations, to add to plain > matrix-matrix multiplication with `np.matmul` and the inner vector > product with `np.vecdot`. They call BLAS where possible for speed. > I'd like to hear whether these are good additions. > > I also note that for complex numbers, `vecmat` is defined as `x†A`, > i.e., the complex conjugate of the vector is taken. This seems to be the > standard and is what we used for `vecdot` too (`x†x`). However, it is > *not* what `matmul` does for vector-matrix or indeed vector-vector > products (remember that those are possible only if the vector is > one-dimensional, i.e., not with a stack of vectors). I think this is a > bug in matmul, which I'm happy to fix. But I'm posting here in part to > get feedback on that. > > Thanks! > > Marten > I tend to agree with not using the complex conjugate for vecmat, but would prefer having separate functions for that that make it explicit in the name. I also note that mathematicians use sesquilinear forms, which have the vector conjugate on the other side, so there are different conventions. I prefer the Dirac convention myself, but many mathematical methods texts use the opposite. It is tricky for the teacher in introductory courses, right up there with vectors being called contravariant when they are actually covariant (the coefficients are contravariant). Anyway, I think having the convention explicit in the name will avoid confusion. Chuck ___ 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
[Numpy-discussion] Re: New matvec and vecmat functions
On Wed, Jan 24, 2024 at 11:02 PM Marten van Kerkwijk wrote: > Stack of matrices in this context is a an ndarray in which the last two > dimensions are interpreted as columns and rows of matrices (in that > order), stack of vectors as an ndarray in which the last dimension is > interpreted as column vectors. (Well, axes in all these functions can > be chosen arbitrarily, but those are the defaults.) > > So a simple example for matvec would be a rotation matrix that I'd like > to apply to a large number of vectors (say to points in space); this is > currently not easy. Complex vectors might be Fourier components. (Note > that I was rather sloppy in calling it multiplication rather than using > the term vector-matrix product, etc.; it is definitely not element-wise!). > > The vector-matrix product comes up a bit less, but as mentioned by > Evgeni in physics there is, e.g., the bra-ket stuff with Hamiltonians > (<\psi | \hat H | \psi>), and in linear least squares one often gets > terms which for real data are written as Xᵀ Z, but for complex would be > Xᴴ Z [1] > > Hope this clarifies things! > > Thanks, but I'm still confused about the need. Perhaps the following illustrates why. With respect to the example you offered, what am I missing? Alan Isaac import numpy as np rng = np.random.default_rng() rot = np.array([[0,-1],[1,0]])#a rotation matrix print("first type of stack of vectors:") vecs1 = rng.random((2,10)) print(vecs1.shape==(2,10)) result1 = rot.dot(vecs1) print("second type of stack of vectors:") rng = np.random.default_rng(314) vecs2 = vecs1.T result2 = rot.dot(vecs2.T) print(f"same result2? {(result1==result2).all()}") print("third type of stack of vectors:") vecs3 = np.reshape(vecs2,(10,2,1)) result3 = rot.dot(vecs3.squeeze().T) print(f"same result3? {(result1==result3).all()}") print("fourth type of stack of vectors:") vecs4 = np.reshape(vecs2,(10,1,2)) result4 = rot.dot(vecs4.squeeze().T) print(f"same result4? {(result1==result4).all()}") ___ 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
[Numpy-discussion] Re: New matvec and vecmat functions
Hi Alan, The problem with .dot is not that it is not possible, but more that it is not obvious exactly what will happen given the overloading of multiple use cases; indeed, this is why `np.matmul` was created. For the stacks of vectors case in particular, it is surprising that the vector dimension is the one but last rather than the last dimension (at least if the total dimension is more than 2). The idea here is to have a function that does just one obvious thing (sadly, for matmul we didn't quite stick to that...). All the best, Marten ___ 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