Skip to content

Conversation

sylee957
Copy link
Member

@sylee957 sylee957 commented Apr 4, 2022

References to other Issues or PRs

Fixes #23313

Brief description of what is fixed or changed

I'm attempting to fix the issue by changing the fuzzy logic to infer is_zero == None as nonzero.

Other comments

Release Notes

  • matrices
    • Fixed Matrix.QRdecomposition giving wrong results for matrices containing symbols (for most cases where zero test can automatically infer the correct results)

@sympy-bot
Copy link

sympy-bot commented Apr 4, 2022

Hi, I am the SymPy bot (v163). I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

  • matrices
    • Fixed Matrix.QRdecomposition giving wrong results for matrices containing symbols (for most cases where zero test can automatically infer the correct results) (#23316 by @sylee957)

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.11.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->
Fixes https://github.com/sympy/sympy/issues/23313

#### Brief description of what is fixed or changed

I'm attempting to fix the issue by changing the fuzzy logic to infer `is_zero == None` as nonzero.

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->

- matrices
  - Fixed `Matrix.QRdecomposition` giving wrong results for matrices containing symbols (for most cases where zero test can automatically infer the correct results)

<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@sylee957 sylee957 force-pushed the fix_QRdecomposition branch from d38ba55 to e9aeeba Compare April 4, 2022 02:38
@github-actions
Copy link

github-actions bot commented Apr 4, 2022

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@@ -153,6 +155,18 @@ def test_QR():
assert R.is_upper
assert A == Q*R

x = Symbol('x')
Copy link
Contributor

Choose a reason for hiding this comment

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

probably should be x = Symbol('x', nonzero=True)? Otherwise, the Q matrix is not well-defined.

Copy link
Contributor

Choose a reason for hiding this comment

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

...and would the original code have worked in that case? That suggests that a better fix may be possible.

Copy link
Contributor

Choose a reason for hiding this comment

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

It does seem suspicious to be able to take a path through this GS/MGS and get empty (degenerate) matrices.

Here's another case to worry about:

Matrix([[0]]).QRdecomposition()
Out[42]: (Matrix(1, 0, []), Matrix(0, 1, []))

(That should give Q=[[1]] and R=[[0]].)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see, this is a "thin" rank-revealing QR factorization.

>>> q, r = Matrix([[0]]).QRdecomposition()
>>>: q
Matrix(2, 0, [])
>>> r
Matrix(0, 2, [])

>>> q*r
Matrix([[0]])

I also tested with 2x2 zero matrix and it also works! That is pretty neat! At least some of this seems to be unit-tested too.

Comment on lines +158 to +167
x = Symbol('x')
A = Matrix([x])
Q, R = A.QRdecomposition()
assert Q == Matrix([x / Abs(x)])
assert R == Matrix([Abs(x)])

A = Matrix([[x, 0], [0, x]])
Q, R = A.QRdecomposition()
assert Q == x / Abs(x) * Matrix([[1, 0], [0, 1]])
assert R == Abs(x) * Matrix([[1, 0], [0, 1]])
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd probably use a new def test_QR_with_symbols(): but don't feel strongly.

@@ -1363,7 +1363,7 @@ def dot(u, v):
Q[:, j] -= Q[:, i] * R[i, j]

Q[:, j] = dps(Q[:, j])
if Q[:, j].is_zero_matrix is False:
if Q[:, j].is_zero_matrix is not True:
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, having experimented with the below, I agree with this change.

But perhaps it could use a comment:

  # if we are sure the column is zero, we do not need to include it
  if Q[:, j].is_zero_matrix in (False, None):

Or how about this without a comment?

  if Q[:, j].is_zero_matrix:
       continue
  ranked.append(j)
  R[j, j] = M.one

@oscarbenjamin
Copy link
Collaborator

Looks good to me.

@sylee957 sylee957 merged commit 471a0cf into sympy:master Apr 5, 2022
@sylee957 sylee957 deleted the fix_QRdecomposition branch April 5, 2022 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QR factorization fails if matrix has Symbols
4 participants