Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled commutative property for MatrixElement #11084

Merged
merged 9 commits into from May 12, 2016

Conversation

arihantparsoya
Copy link
Contributor

@arihantparsoya arihantparsoya commented May 7, 2016

This is fix for #11077

Previously the is_commutative property was None and hence Sympy wasnt able to simplify the determinant expression.

  • After setting the is_commutative property to True, the determinant expression is being simplified.
  • None of the tests fail after changing the is_commutative property

@arihantparsoya
Copy link
Contributor Author

@asmeurer , can you review this?

@asmeurer
Copy link
Member

asmeurer commented May 7, 2016

Looks like some tests failed. Those should be investigated.

@@ -338,7 +338,7 @@ class MatrixElement(Expr):
i = property(lambda self: self.args[1])
j = property(lambda self: self.args[2])
_diff_wrt = True

is_commutative = True
Copy link
Member

Choose a reason for hiding this comment

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

Keep a blank line before function definitions.

@asmeurer
Copy link
Member

asmeurer commented May 7, 2016

You should add a test for the original issue.

Printing tests which give the correct output but different in string
values are modified:

example:
M[6] = 2*q[4]*1/q[1]; is equivalent to
M[6] = 2*q[4]/q[1];
@arihantparsoya
Copy link
Contributor Author

There are in total 4 test failures, one is related to the PR #9364 and the remaining 3 are related to printing which I fixed in my last commit. I will look into #9364 and fix the last remining test case.

@jksuom
Copy link
Member

jksuom commented May 8, 2016

I wonder if there could be a way of making matrix elements commutative on demand. Matrices with elements in non-commutative rings are generally quite acceptable. Commutativity is needed only in some special cases such as for determinants.

As the MatrixElement is_commutative property is set to true, the
expression in line 2283 wasn’t being interpreted correctly and the
cancel function was returning unsimplified output previously
@arihantparsoya
Copy link
Contributor Author

@asmeurer , should I add the tests for the determinant of the matrix (i.e the issue for determinant which u originally posted) or just the commutability of the MatrixElement?

Added test for commutative property of MatrixElement
@arihantparsoya
Copy link
Contributor Author

@asmeurer , can you review this?

@@ -2881,7 +2882,7 @@ def test_cancel():
M = MatrixSymbol('M', 5, 5)
assert cancel(M[0,0] + 7) == M[0,0] + 7
expr = sin(M[1, 4] + M[2, 1] * 5 * M[4, 0]) - 5 * M[1, 2] / z
assert cancel(expr) == expr
assert simplify(cancel(expr)) == expr
Copy link
Member

Choose a reason for hiding this comment

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

This test seems wrong. The point of the function is to test cancel. What does this do without simplify here?

Copy link
Member

Choose a reason for hiding this comment

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

I guess it should return (z*sin(M[1, 4] + M[2, 1] * 5 * M[4, 0]) - 5 * M[1, 2]) / z. If that's what it's giving, we should test that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah it returns cancel(expr) = (z*sin(M[1, 4] + M[2, 1] * 5 * M[4, 0]) - 5 * M[1, 2]) / z and previously it was returning the expression sin(M[1, 4] + 5*M[2, 1]*M[4, 0]) - 5*M[1, 2]/z, please note that the whole numerator is encloses in a bracket after is_commutative is set to True hence I used simplify

Copy link
Member

Choose a reason for hiding this comment

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

(z*sin(M[1, 4] + M[2, 1] * 5 * M[4, 0]) - 5 * M[1, 2]) / z is the right answer for cancel to return. The test should be

assert cancel(expr) == (z*sin(M[1, 4] + M[2, 1] * 5 * M[4, 0]) - 5 * M[1, 2]) / z

Calling simplify in the function that tests the output of cancel defeats the purpose.

@asmeurer
Copy link
Member

asmeurer commented May 9, 2016

I would add a test for the commutativity (which you already have), and a test for the determinant, to make sure that that works.

@arihantparsoya
Copy link
Contributor Author

@asmeurer , I added the test for determinant of the matrix.

@asmeurer
Copy link
Member

Just the comment on the cancel test. The rest looks good.

@arihantparsoya
Copy link
Contributor Author

arihantparsoya commented May 11, 2016

@asmeurer , I fixed the test

@arihantparsoya
Copy link
Contributor Author

@asmeurer , can you review this again?

@asmeurer asmeurer merged commit d15efa2 into sympy:master May 12, 2016
@asmeurer
Copy link
Member

Looks good, thanks.

@asmeurer
Copy link
Member

Oh, one thing. In the future, if you fix an issue, put "Fixes #11077" in one of your commit messages. That way, when the PR is merged, the issue will be automatically closed.

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.

None yet

3 participants