GraphBLAS / graphblas-api-c

Other
7 stars 3 forks source link

Allow broadcasting in ewise operations #72

Open rayegun opened 1 year ago

rayegun commented 1 year ago

Forgive me if this is already an open issue.

It would be very nice to add broadcasting of vectors into matrices to the spec. See Python/Julia and more languages for prior art. For instance if you ewise_mul(A::GrB_Matrix, v::GrB_Vector) v would expand across columns. Transpose operator could be used to expand across rows.

Note that this treats GrB_Vector explicitly as a column vector, which may not be in line with the spec.

This can already be achieved with a Diagonal matrix multiplication, but this has a few issues:

  1. At least SS:GrB doesn't implement a Diagonal type internally, so it has storage/construction overhead as I understand it.
  2. To expand over columns the diagonal must be on the left, vice versa for expanding over rows. This can be a problem for operations without commutativity.
eriknw commented 1 year ago

This may be related to (or part of) the rank promotion issue: https://github.com/GraphBLAS/graphblas-api-c/issues/24

We have found this functionality to be useful for many algorithms--we use it all the time. We added syntax in python-graphblas that performs recipes for ewise-multiply, ewise-add, and ewise-union (Tim's GxB extension with left and right defaults) between Matrix and Vector objects. We apply masks as appropriate for the latter two operations.

So, +1 from me.

Yes, one can use GrB_Matrix_diag to first expand the vector to matrix. One then needs to do mxm with a semiring where only the binaryop is used. If we added methods to do this instead, it could take a BinaryOp (and/or Monoid--or even IndexUnaryOp if you're twisted) instead of a Semiring.

rayegun commented 1 year ago

How do you implement the add and union rank promotion? The emul is clear to me.

eriknw commented 1 year ago

How do you implement the add and union rank promotion? The emul is clear to me.

See the important bits here: https://github.com/python-graphblas/python-graphblas/blob/856356bd02e28936059e5b8af22c6cb327495d98/graphblas/core/matrix.py#L50-L65