-
-
Notifications
You must be signed in to change notification settings - Fork 661
Return a vector of monomial instead of a matrix in PolynomialSequence.coefficient_matrix() #37035
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
Return a vector of monomial instead of a matrix in PolynomialSequence.coefficient_matrix() #37035
Conversation
The I am not sure about the deprecation. I think that it is required that old code works for about one year, and then the deprecated functionality is removed. I don't know how to best do it, but one way is to introduce an optional keyword like The downside is that both seeing the deprecation warning but also always having to provide the keyword is extremely annoying to users. See #36073. Thus, if we follow this model, we have to think hard about a good name for the keyword argument. |
How about below implementation, I think we should not show deprecation message when user is using vector_form(I don't whether this name works or not but for now I am using this for reference) attribute have some value then we should not show deprecation message, so what if we make vector_form by default None, and do below changes ? if vector_form == True:
return A, vector(m)
else:
if vector_form == None:
deprecation(37027,'the function PolynomialSequence.coeffient_matrix() will'
' return a vector of monomilas instead of a matrix as second element in tuple in the future,'
' please use vector_form argument to get desired type.')
return A, Matrix(Matrix(R,nm,1,m)) But still I am not sure whether this is a good idea or not? And also don't know wether the name for the attribute is good or not. |
To make the linter happy, you also need to remove trailing whitespace. After that, I think we should ask on sage-devel whether this is the way to go. One problem I see (and do not know how to solve) is that, eventually, we want to remove the |
Ok, I'll ask. And thank you for your help ! |
3cae654
to
82aa940
Compare
Hi @RuchitJagodara ! I am now a bit confused. Could you let me now what the state of the PR is now? Was the problem with You squashed the commits, right? |
No, there was no problem with that.
Yes. I am really sorry for the confusion caused by this change. I reset the branch because there were some conflicts in my branches due to which my one PR got merged without any positive review, so I was just sorting out that issue. Therefore, I had to reset this branch, but now I have included all commits that were missing in this branch. I am really sorry for the inconvenience caused because of this. 😞 |
Oh, that's good.
Absolutely no problem at all! Could you just do the minor modifications? I would then wait for a green bot and set it to "positive review". I am so glad that you did this, by the way! |
6ebc0f1
to
230f7eb
Compare
Does this PR need any further changes becuase I have done all necessary changes. And should I add some tests for new function becuase codecov/patch check is failing ? |
@RuchitJagodara, I think there is a misunderstanding: there are still comments which have not been adressed, most importantly the question about line 771 we discussed before. The last answer I saw I responded to in comment #37035 (comment), but then the code changed back. |
I have addreseed that in comment. Actually, there is a problem with When you asked |
Could you please give me a simple example for that? If I do
it works. |
Hmm... It is working fine now. I think something should have changed because the last time I tried this method, it was not working; it was giving me a ValueError, saying there were not enough values to unpack. Because of that, I changed the code at that time after leaving a comment. However, I think now it is safe to go this way because I have checked the code in different test cases, and it is working fine. |
@mantepse, I don't know why, but the checks are not running; they are queued. Do you know what the problem is? |
That's indeed very confusing. Anyhow, could you please also do the 2 whitespace comments (which you can see on "Files changed" tab)? |
I think that's just because too many people are testing right now. (So it really would be good to do the final changes now, in order not to waste resources) |
Are you talking about these lines ?
|
No, I'm talking about Can you see these comments? github is quite tricky at times... |
I am not able to see those whitespace comments but I have removed the R from the code. |
Oh, I see. I am not able to see this comments. Could you please submit the review, I think after submitting the review I will be able to see those comments. Because it is showing pending tag on the comments. |
Although, I have updated the code with suggested changes, please let me know if there is anything else. |
No, the pending tag means that the code was not yet changed. It would be really important that you can see these comments for developing sage |
Hmmm.... It's very strange because I am not able to see those comments. And the resolved conversation that you can see in the above screenshot is review |
Dear @RuchitJagodara, I now see the problem: the BooleanPolynomialRing is a polynomial ring which does not adhere to the polynomial protocol 👎 Here is what I would suggest to do: diff --git a/src/sage/rings/polynomial/multi_polynomial_sequence.py b/src/sage/rings/polynomial/multi_polynomial_sequence.py
index 72ce72f748..9519b2d0c9 100644
--- a/src/sage/rings/polynomial/multi_polynomial_sequence.py
+++ b/src/sage/rings/polynomial/multi_polynomial_sequence.py
@@ -750,7 +750,6 @@ class PolynomialSequence_generic(Sequence_generic):
from sage.modules.free_module_element import vector
from sage.matrix.constructor import Matrix
- f = tuple(self)
if order is None:
v = sorted(self.monomials(), reverse=True)
else:
@@ -760,8 +759,8 @@ class PolynomialSequence_generic(Sequence_generic):
raise ValueError("order argument can only accept list or tuple")
y = dict(zip(v, range(len(v)))) # construct dictionary for fast lookups
- A = Matrix(self.ring().base_ring(), len(f), len(v), sparse=sparse)
- for x, poly in enumerate(f):
+ A = Matrix(self.ring().base_ring(), len(self), len(v), sparse=sparse)
+ for x, poly in enumerate(self):
for c, m in poly:
try:
A[x, y[m]] = c
@@ -1695,6 +1694,60 @@ class PolynomialSequence_gf2(PolynomialSequence_generic):
else:
return PolynomialSequence_generic.reduced(self)
+ def coefficients_monomials(self, order=None, sparse=True):
+ """
+ Return the matrix of coefficients ``A`` and
+ the matching vector of monomials ``v``, such that ``A*v == vector(self)``.
+
+ Thus value of ``A[i,j]`` corresponds the coefficient of the
+ monomial ``v[j]`` in the ``i``-th polynomial in this system.
+
+ Monomials are ordered w.r.t. the term ordering of ``order``
+ if given; otherwise, they are ordered w.r.t. ``self.ring()``
+ in reverse order, i.e., such that the smallest entry comes last.
+
+ INPUT:
+
+ - ``sparse`` - construct a sparse matrix (default: ``True``)
+ - ``order`` - a list or tuple specifying the order of monomials (default: ``None``)
+
+ EXAMPLES::
+
+ sage: # needs sage.rings.polynomial.pbori
+ sage: B.<x,y,z> = BooleanPolynomialRing()
+ sage: F = Sequence([x*y + y + 1, z + 1])
+ sage: A, v = F.coefficients_monomials()
+ sage: A
+ [1 1 0 1]
+ [0 0 1 1]
+ sage: v
+ (x*y, y, z, 1)
+ sage: A*v
+ (x*y + y + 1, z + 1)
+ """
+ from sage.modules.free_module_element import vector
+ from sage.matrix.constructor import Matrix
+
+ if order is None:
+ v = sorted(self.monomials(), reverse=True)
+ else:
+ if isinstance(order, (list, tuple)):
+ v = order
+ else:
+ raise ValueError("order argument can only accept list or tuple")
+
+ R = self.ring().base_ring()
+ one = R.one()
+ y = dict(zip(v, range(len(v)))) # construct dictionary for fast lookups
+ A = Matrix(R, len(self), len(v), sparse=sparse)
+ for x, poly in enumerate(self):
+ for m in poly:
+ try:
+ A[x, y[m]] = one
+ except KeyError:
+ raise ValueError("order argument does not contain all monomials")
+ return A, vector(v)
+
class PolynomialSequence_gf2e(PolynomialSequence_generic):
r""" |
I have updated the code according to your suggestions. |
Documentation preview for this PR (built with commit 77b4f7c; changes) is ready! 🎉 |
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.
Thank you so much for your patience!
@@ -108,7 +108,7 @@ def coefficient_matrix(polys): | |||
.. NOTE:: | |||
|
|||
This function may be merged with | |||
:meth:`sage.rings.polynomial.multi_polynomial_sequence.PolynomialSequence_generic.coefficient_matrix()` in the future. | |||
:meth:`sage.rings.polynomial.multi_polynomial_sequence.PolynomialSequence_generic.coefficients_monomials()` in the future. |
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.
If you have time, this would be an excellent next project, together with #37075.
Now, I am able to see those comments to which you were referring earlier.
I think that issue has been resolved, but it seems that there is a function coeffcient_matrix in toy_variety.py. In which I found a note...
So, should I merge this function with this coefficients_monomials function because this note was added two years ago, so I think it is okay if we merge this. What do you say, @mantepse? |
This patch fixes
PolynomialSequence.coefficient_matrix()
returns a matrix instead of a vector as second element #37027 by returning a vector of monomials instead of a matrix,which follows the documentation.
I have also added a deprecation message.
📝 Checklist
⌛ Dependencies
Does not matter.