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

Matrix summation improvements #17241

Merged
merged 3 commits into from Jul 23, 2019
Merged

Matrix summation improvements #17241

merged 3 commits into from Jul 23, 2019

Conversation

sylee957
Copy link
Member

References to other Issues or PRs

Brief description of what is fixed or changed

There are some issues with sum involving matrices, which are fixed here.

  1. Matrix was casted into MutableDenseMatrix after summation, but now, it will be casted into ImmutableDenseMatrix and ImmutableSparseMatrix respecting the original matrix class.
    I made the behavior not to retain the mutability though, because sympify would already cast mutable matrix into a immutable one.

  2. There was inefficient casting of sparse matrices into dense matrices in the original code.

from sympy import *

x = Dummy('x', integer=True)
m = SparseMatrix(200, 200, {(0, 0): 1}) * x

from pyinstrument import Profiler

profiler = Profiler()
profiler.start()

Sum(m, (x, 1, 100)).doit()

profiler.stop()

print(profiler.output_text(unicode=True, color=True))

And the speed had improved as

  _     ._   __/__   _ _  _  _ _/_   Recorded: 18:56:58  Samples:  3315
 /_//_/// /_\ / //_// / //_'/ //     Duration: 3.343     CPU time: 3.344
/   _/                      v3.0.3
  _     ._   __/__   _ _  _  _ _/_   Recorded: 18:56:23  Samples:  284
 /_//_/// /_\ / //_// / //_'/ //     Duration: 0.284     CPU time: 0.266
/   _/                      v3.0.3
  1. Matrix expressions were throwing errors in summation because they don't have _mat attribute
from sympy import *

x = Symbol('x')
Sum(Identity(3), (x, 0, 3)).doit()

AttributeError: 'Identity' object has no attribute '_mat'
But after now, it may not be computed because of lack of some formulas, but at least it will return unevaluated.
I've also changed the check like

            if self != expanded:
                return expanded.doit()
            return self

because of the recursion error, if it does not succeed rewriting.

Other comments

Release Notes

  • concrete
    • Fixed Sum casting dense matrix into sparse matrix.
    • Sum will cast mutable matrices into immutable variant after computation

@sympy-bot
Copy link

sympy-bot commented Jul 22, 2019

Hi, I am the SymPy bot (v147). 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:

  • concrete
    • Fixed Sum casting dense matrix into sparse matrix. (#17241 by @sylee957)

    • Sum will cast mutable matrices into immutable variant after computation (#17241 by @sylee957)

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

Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it.

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://github.com/blog/1506-closing-issues-via-pull-requests . Please also
write a comment on that issue linking back to this pull request once it is
open. -->


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

There are some issues with `sum` involving matrices, which are fixed here.

1. Matrix was casted into MutableDenseMatrix after summation, but now, it will be casted into `ImmutableDenseMatrix` and `ImmutableSparseMatrix` respecting the original matrix class.
I made the behavior not to retain the mutability though, because `sympify` would already cast mutable matrix into a immutable one.

2. There was inefficient casting of sparse matrices into dense matrices in the original code.
```python
from sympy import *

x = Dummy('x', integer=True)
m = SparseMatrix(200, 200, {(0, 0): 1}) * x

from pyinstrument import Profiler

profiler = Profiler()
profiler.start()

Sum(m, (x, 1, 100)).doit()

profiler.stop()

print(profiler.output_text(unicode=True, color=True))
```
And the speed had improved as
```
  _     ._   __/__   _ _  _  _ _/_   Recorded: 18:56:58  Samples:  3315
 /_//_/// /_\ / //_// / //_'/ //     Duration: 3.343     CPU time: 3.344
/   _/                      v3.0.3
```
```
  _     ._   __/__   _ _  _  _ _/_   Recorded: 18:56:23  Samples:  284
 /_//_/// /_\ / //_// / //_'/ //     Duration: 0.284     CPU time: 0.266
/   _/                      v3.0.3
```

3. Matrix expressions were throwing errors in summation because they don't have `_mat` attribute
```python
from sympy import *

x = Symbol('x')
Sum(Identity(3), (x, 0, 3)).doit()
```
`AttributeError: 'Identity' object has no attribute '_mat'`
But after now, it may not be computed because of lack of some formulas, but at least it will return unevaluated.
I've also changed the check like
```
            if self != expanded:
                return expanded.doit()
            return self
```
because of the recursion error, if it does not succeed rewriting.

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below. 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 -->

- concrete
  - Fixed `Sum` casting dense matrix into sparse matrix.
  - `Sum` will cast mutable matrices into immutable variant after computation

<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@oscarbenjamin
Copy link
Contributor

This looks good to me.

@oscargus
Copy link
Contributor

Looks good! Can you try #16989 and add those tests that work? (And if both works, add a fixes to this?)

@sylee957
Copy link
Member Author

I've only added the first case because the second one hangs in jordan form computation.
Some other things may be improved in later, with base on this

@sylee957 sylee957 merged commit 8ca4a68 into sympy:master Jul 23, 2019
@codecov
Copy link

codecov bot commented Jul 23, 2019

Codecov Report

Merging #17241 into master will increase coverage by 0.014%.
The diff coverage is 71.428%.

@@              Coverage Diff              @@
##            master    #17241       +/-   ##
=============================================
+ Coverage   74.549%   74.564%   +0.014%     
=============================================
  Files          623       623               
  Lines       161739    161784       +45     
  Branches     37965     37966        +1     
=============================================
+ Hits        120576    120633       +57     
+ Misses       35817     35800       -17     
- Partials      5346      5351        +5

@sylee957 sylee957 deleted the fix_matrix_sum branch July 23, 2019 11:35
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.

None yet

4 participants