Skip to content

Conversation

Zeroto521
Copy link
Contributor

fix #1058

Implements the __matmul__ method for MatrixExpr, enabling matrix multiplication using the @ operator and ensuring the result is returned as a MatrixExpr instance.
Adds tests to verify that matrix multiplication returns MatrixExpr instead of MatrixVariable for various input shapes.
Replaces isinstance checks with type() comparisons for MatrixExpr in matrix matmul return type tests to ensure exact type matching.
Comment on lines +401 to +403
# test 1D @ 1D → 0D
x = m.addMatrixVar(3)
assert type(x @ x) is MatrixExpr
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we can't use isinstance. Because MatrixVariable is a subclass of MatrixExpr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result of x@x is a size 1 matrix. What type should it be, MatrixExpr or Expr?
Related to #1057

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should isinstance(x @ x) not be True for both MatrixVariable and MatrixExpr? I personally don't mind using type, but is there actually a need?

A size 1 matrix should still be a MatrixExpr. I don't think we should be removing the matrix language for the user, as that's just going to cause confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MatrixVariable has the .vtype() method. But the x@x doesn't have this method.
So the result of x@x should be MatrixExpr, not MatrixVariable.
To test this, I use type to check x@x is a MatrixExpr.

Corrects the shapes of matrix variables in test_matrix_matmul_return_type to ensure proper 2D matrix multiplication and type assertion.
Copy link
Collaborator

@Opt-Mucca Opt-Mucca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy with this change! (Would like an answer to type vs isinstance first though)

Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: the result of @ can't access .vtype method
2 participants