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
Canonicalization of Hadamard Product #16685
Conversation
✅ 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:
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.
Update The release notes on the wiki have been updated. |
Codecov Report
@@ Coverage Diff @@
## master #16685 +/- ##
============================================
- Coverage 73.847% 73.827% -0.02%
============================================
Files 619 619
Lines 159329 159406 +77
Branches 37388 37414 +26
============================================
+ Hits 117660 117685 +25
- Misses 36241 36263 +22
- Partials 5428 5458 +30 |
I've added some tests and made some minor fix. It looks good, I need this PR to go on with the matrix derivatives algorithm. |
…s the product order to depend on the Python hashing values.
…57/sympy into hadamard_canonicalization
I would put the examples in the |
For diagonal matrix checks: from sympy.assumptions import ask, Q
ask(Q.diagonal(mat)) # <== this returns True if the matrix is diagonal Though I would use it with care, as slowdowns might be expected. |
===== | ||
|
||
As Hadamard product is associative, all the nested products can be | ||
flattened to the level 1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the Hadamard product is associative nested products can be flattened.
flattened to the level 1. | ||
|
||
As Hadamard product is commutative, all the arguments can be sorted to | ||
the canonical form. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Hadamard product is commutative so factors can be sorted for canonical form.
the canonical form. | ||
|
||
Matrix of only ones is an identity for Hadamard product, | ||
so every ``OneMatrix`` can be removed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A matrix of only ones...
Matrix of only ones is an identity for Hadamard product, | ||
so every ``OneMatrix`` can be removed. | ||
|
||
Matrix of only zeros will make Hadamard product zero. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any zero matrix will make the whole product a zero matrix.
|
||
# Rewriting with HadamardPower | ||
if isinstance(x, HadamardProduct): | ||
tally = dict() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use collections.Counter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or multiset
from iterables
:
>>> multiset('mississippi')
{'i': 4, 'm': 1, 'p': 2, 's': 4}
so
tally = multiset(x.args)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done some experiment in cpython 3.6
from sympy.utilities.iterables import multiset
from collections import Counter
l = 'abc'+'c'*999999
%timeit Counter(l)
349 ms ± 53.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit multiset(l)
578 ms ± 36.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
I guess the one from the official library can use C function to speed up
https://github.com/python/cpython/blob/f2f55e7f03d332fd43bc665a86d585a79c3b3ed4/Modules/_collectionsmodule.c#L2237
x = HadamardProduct(*new_arg) | ||
|
||
# Commutativity | ||
fun = condition( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what aspect of this relates to commutativity? After using the tally, things are going to be reordered, aren't they?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the fact that things are getting reordered will make a reason to sort lastly.
Is there more you wanted to do with this? |
Things like boilerplating can better be cleaned up later after some big progress in the module is made, regarding the comments. I had some other things to do, but I think that there is no problem for merging this now. |
Should it use AssocOp? I don't know if there are issues using it in the matrix expressions. |
I guess MatAdd and MatMul use it via subclassing from Add and Mul. Although I added an |
I considered |
References to other Issues or PRs
Brief description of what is fixed or changed
With matrix of ones implemented in #16676, we may use the identity to make hadamard product more canonical.
IdentityMatrix
(Or any rectangulareye
) After the decision is made for Rename DiagonalizeVector? #16682Other comments
This is still work in progress, and I will add some tests if the design is set
I also think that
HadamardProduct
can automatically unpack its argument, if given a single argument, like in SymPy'sAdd
,Mul
.As it is confusing that
HadamardProduct(A) == A
is givingFalse
because it does not unpacks single arguentbut
hadamard_product(A) == A
is givingTrue
.And the printing is not suggestive that
HadamardProduct
is wrapped on this.Though changing this would make some tests fail, and there may be some discussion for design change?
Release Notes
HadamardProduct
rules
frommatrices.expressions.hadamard