Skip to content

Conversation

ks147
Copy link
Contributor

@ks147 ks147 commented Mar 25, 2021

References to other Issues or PRs

Brief description of what is fixed or changed

Added Documentation for SDM class

Other comments

Release Notes

  • polys
    • Added Documentation for SDM class

@sympy-bot
Copy link

sympy-bot commented Mar 25, 2021

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

  • polys

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

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

#### Brief description of what is fixed or changed
Added Documentation for SDM class

#### Other comments


#### Release Notes

<!-- BEGIN RELEASE NOTES -->
* polys
  * Added Documentation for SDM class
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@ks147 ks147 changed the title Sdm docs Added Documentation for SDM class Mar 25, 2021
Comment on lines 43 to 46
>>> elemsdict = {0:{1:QQ(2)}, 1:{}}
>>> A = SDM(elemsdict, (2, 2), QQ)
>>> A
{0: {1: 2}, 1: {}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

The elemsdict should not have any empty dicts in there. This docstring should explain that it is the responsibility of whoever creates elemsdict to remove zero rows and zero elements.

Also this should show how to construct a DomainMatrix from the SDM.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be better to move this docstring to be the main class docstring and also to show examples of using the SDM e.g. for adding and multiplying

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll do that


>>> from sympy.polys.matrices.sdm import SDM
>>> from sympy import QQ
>>> ddm = [[QQ(1, 2), 0], [0, QQ(3, 4)]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

The 0 should be QQ(0).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll change it everywhere


>>> from sympy import QQ
>>> from sympy.polys.matrices.sdm import SDM
>>> A = SDM({0:{0:QQ(1), 1:QQ(2)}, 1:{0: QQ(3), 1: QQ(4)}}, (2, 2), QQ)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The spacing around colons is inconsistent.

>>> from sympy.polys.matrices.sdm import SDM
>>> A = SDM({0:{0:QQ(1), 1:QQ(2)}, 1:{0: QQ(3), 1: QQ(4)}}, (2, 2), QQ)
>>> A.charpoly()
[1, -5, -2]
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be good to show here how to construct a Poly from these e.g.:

x = Symbol('x')
p = Poly(A.charpoly(), x, domain=A.domain)
p

>>> A = SDM({0:{1: ZZ(2)}, 1:{0:ZZ(1)}}, (2, 2), ZZ)
>>> B = SDM({0:{0:ZZ(2), 1:ZZ(3)}, 1:{0:ZZ(4)}}, (2, 2), ZZ)
>>> A.matmul(B)
{0: {0: 8}, 1: {0: 2, 1: 3}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

For each of these methods it would be good to point out that they are also used by the operators so e.g. this could show that you get the same from A*B.

Copy link
Collaborator

Choose a reason for hiding this comment

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

These docstrings should also explain when the domains are are required to match or what constraints there are on the shapes of the matrices for the operation to be defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You actually don't get the same from A * B.

A * B
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for *: 'SDM' and 'SDM'

This is what you get with A*B

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think __add__(A, B) needs to be implemented for SDM class

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh yeah, I think it's A @ B or __matmul__. Is that not implemented for SDM?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps it is better to use * anyway. Either way SDM and DDM should be made consistent so can you compare how DDM works?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consistent in what ways? You want both to have the same functionality right, is that what you're saying

Copy link
Contributor Author

@ks147 ks147 Mar 27, 2021

Choose a reason for hiding this comment

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

By the way * does not work in DDM either

A = DDM([[ZZ(1), ZZ(2)], [ZZ(2), ZZ(3)]], (2, 2), ZZ)
B = DDM([[ZZ(3), ZZ(4)], [ZZ(1), ZZ(2)]], (2, 2), ZZ)
A * B
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'DDM' and 'DDM

I'll have to make __mul__ in DDM consistent with definition in DomainMatrix

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean that when using methods like A * B or A.matmul(B) etc then all behaviour should be exactly the same whether A and B are both DDM or both SDM. These two classes are supposed to be completely interchangeable in that sense. So if you are changing any part of the behaviour of SDM you should check that DDM behaves in exactly the same way.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll have to make __mul__ in DDM consistent with definition in DomainMatrix

Note that DDM and DomainMatrix do not need to be consistent as they are not intended to be interchangeable. It is DDM and SDM that need to be consistent. For now I think that SDM should just use @ for matrix multiplication just like DDM does. We can consider changing that in future but these two classes need to remain equivalent.

>>> elemsdict = {0:{1:QQ(2)}}
>>> A = SDM(elemsdict, (2, 2), QQ)
>>> A
{0: {1: 2}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

The most useful thing to show here would be what this looks like as a Matrix. I don't think that the meaning of the dict of dicts structure is obvious without being explained.

Comment on lines 39 to 55
We can manipulate :py:class:`~.SDM` the same way
as a Matrix class

>>> from sympy import ZZ
>>> A = SDM({0:{1: ZZ(2)}, 1:{0:ZZ(1)}}, (2, 2), ZZ)
>>> B = SDM({0:{0: ZZ(3)}, 1:{1:ZZ(4)}}, (2, 2), ZZ)
>>> A + B
{0: {0: 3, 1: 2}, 1: {0: 1, 1: 4}}

Multiplication

>>> A = SDM({0:{1: ZZ(2)}, 1:{0:ZZ(1)}}, (2, 2), ZZ)
>>> B = SDM({0:{0: ZZ(3)}, 1:{1:ZZ(4)}}, (2, 2), ZZ)
>>> A*B
{0: {1: 8}, 1: {0: 3}}
>>> A*ZZ(2)
{0: {1: 4}, 1: {0: 2}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

These examples can be condensed. We don't need to create new matrices for each demonstration.


>>> from sympy.polys.matrices.sdm import SDM
>>> from sympy import QQ
>>> elemsdict = {0:{1:2}, 1:{}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

The empty dict shouldn't be there. The 2 should be QQ(2)

>>> from sympy import QQ
>>> A = SDM.zeros((2, 3), QQ)
>>> A
{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would be easier to understand if you also show what the corresponding matrix would be.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You could show what A.to_Matrix() gives here

@@ -308,7 +751,7 @@ def sdm_irref(A):
>>> pivots
[0, 1]

The analogous calculation with :py:class:`~.Matrix` would be
The analogous calculation with Matrix class would be
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this changed?

@ks147
Copy link
Contributor Author

ks147 commented May 1, 2021

@oscarbenjamin please review

>>> elemsdict = {0:{1:QQ(1, 2)}}
>>> A = SDM(elemsdict, (2, 2), QQ)
>>> A
{0: {1: 1/2}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be good so short the result of A.to_Matrix() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think SDM class has to_Matrix(), DomainMatrix on the other hand does have that functionality

MT = sdm_transpose(M)
return M.new(MT, M.shape[::-1], M.domain)
return MT
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this changed? Does this return a dict or SDM?

@oscarbenjamin
Copy link
Collaborator

Okay, looks good.

@oscarbenjamin oscarbenjamin merged commit bf1f842 into sympy:master May 1, 2021
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.

3 participants