cetz-package / cetz

CeTZ: ein Typst Zeichenpaket - A library for drawing stuff with Typst.
https://cetz-package.github.io
GNU Lesser General Public License v3.0
752 stars 34 forks source link

`matrix.mul-vec` expects dimensions to match in the wrong axis #644

Open kwshi opened 2 weeks ago

kwshi commented 2 weeks ago

Currently the code for matrix.mul-vec requires "matrix m must be equal to vector dim", where by inspection of the source code m appears to be the number of rows in the matrix:

https://github.com/cetz-package/cetz/blob/86509124affe0b8d3640abd1549ba0b944949d98/src/matrix.typ#L229-L242

But this isn't the correct requirement, at least mathematically speaking: it is the number of columns in the matrix that should match the vector dimension. One way of checking this is to observe, from the source code itself,

#{
  v += vec.at(n) * mat.at(m).at(n)
}

that the matrix entries getting matched with the vector entries come from the nested n axis, which are the columns (not rows) of the matrix.

So the assertion that mat.len() == vector.dim(vec) is (1) not actually necessary for valid matrix multiplication on vectors, (2) doesn't catch actual dimension mismatches with the columns, and (3) prevents certain useful things, such as multiplying by a non-square matrix in order to project vectors into 2D coordinates.

johannes-wolf commented 2 weeks ago

Thank you. The function is currently not in use, but I've opened a PR that fixes the implementation.