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

Fix matrix rank with complicated elements #10650

Closed
wants to merge 5 commits into from
Closed

Conversation

rpisarev
Copy link
Contributor

Fix matrix rank with complicated elements.

If some expresion does not contain symbols we can do _simplify() for that,
before comparing with zero.

   modified:   sympy/matrices/matrices.py
   modified:   sympy/matrices/tests/test_matrices.py

fixes #9480

If some expresion does not contain symbols we can do _simplify() for that,
before comparing with zero.

       modified:   sympy/matrices/matrices.py
       modified:   sympy/matrices/tests/test_matrices.py
@rpisarev rpisarev changed the title Fix #9480 Fix 9480 Feb 22, 2016
@rpisarev rpisarev changed the title Fix 9480 Fix matrix rank with complicated elements Feb 25, 2016
@@ -26,6 +26,9 @@

def _iszero(x):
"""Returns True if x is zero."""
if len(x.free_symbols) == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

The len function call and comparison with zero are extraneous here. This is because x.free_symbols returns a python set data type. Python allows direct testing of container data types where an empty container returns false and is otherwise true.

In this case you could use
if not x.free_symbols:
(the "not" is included so that it will return True when the set is empty)

This reduces the complexity of the statement.

@jbm950
Copy link
Contributor

jbm950 commented May 25, 2016

I like the simplicity of the fix and the addition of the test case going over the code mentioned in the issue. Please add some commenting to the change in the _iszero() function explaining the presence of the if block to make maintenance easier for the next person who looks over the code. Also please address the inline comments I left in the code.

@rpisarev
Copy link
Contributor Author

I've fixed that @jbm950

@jbm950
Copy link
Contributor

jbm950 commented May 26, 2016

@debugger22
@jksuom

# if we do not have free symbols, we will call _simplify()
# without risks to lose some confines for domain of an expression,
# because it will be some number
x = _simplify(x)
Copy link
Member

Choose a reason for hiding this comment

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

In general, I always give a -1 to having simplification calls in our code base. If x is an arbitrary matrix, simplify can take inordinate amounts of computation time to complete. An iszero check should be fast. You probably want to read the conversation around issues and pull requests like #10280.

If you are going to add this simplify call, then I request that you add a benchmark to https://github.com/sympy/sympy_benchmarks so that we track how slow things start to get.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An iszero check should be fast.

I think, iszero check should be right too.

@moorepants
Copy link
Member

Relevant issue: #10279

@rpisarev
Copy link
Contributor Author

rpisarev commented May 29, 2016

I don't understand, it is bad fix and it won't be merged, or I should somesing to do. I wanted to know, if some expression (e.g. determinant) equals zero. In case, when expression is complicated, _iszero() works badly. I wanted to simplify expression, if expression is number (without free symbols) and only if. So, there is check to having free symbols and, if it does not have, we call _simplify().

If this behavior is so awful, let's think of a better.

skirpichev added a commit to skirpichev/diofant that referenced this pull request Jun 15, 2016
Fix is similar to sympy/sympy#10650, thanks to @rpisarev.  In case of
zero-decision problem - we raise NotImplementedError.

Closes diofant#288
Closes sympy/sympy#9480

(Maybe it fixes also sympy/sympy#11238)

See sympy/sympy#10280 for alternative.
skirpichev added a commit to skirpichev/diofant that referenced this pull request Jun 15, 2016
Fix is similar to sympy/sympy#10650, thanks to @rpisarev.  In case of
zero-decision problem - we raise NotImplementedError.

Closes diofant#288
Closes sympy/sympy#9480

(Maybe it fixes also sympy/sympy#11238)

See sympy/sympy#10280 for alternative.
@rpisarev
Copy link
Contributor Author

rpisarev commented Aug 6, 2016

@moorepants Can you read #11479?
I think I did some benchmarks.

@siefkenj
Copy link
Contributor

This has been fixed in mainline. Time to close?

@moorepants moorepants closed this Dec 28, 2016
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.

Matrix.rank() incorrect results
4 participants