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

vector.length() should be number of nonzero entries #29218

Open
mwageringel opened this issue Feb 18, 2020 · 15 comments
Open

vector.length() should be number of nonzero entries #29218

mwageringel opened this issue Feb 18, 2020 · 15 comments

Comments

@mwageringel
Copy link

sage: vector([-1, 0, 2, 0]).length()  # should be 2
4

According to the documentation of length from modules_with_basis.py:

Return the number of basis elements whose coefficients in "self" are nonzero.

This is solved by overwriting length in FreeModuleElement.

Depends on #34509

Component: linear algebra

Author: Markus Wageringel

Branch/Commit: u/mkoeppe/29218 @ 324478d

Reviewer: Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/29218

@mwageringel
Copy link
Author

New commits:

324478d29218: overwrite .length() of FreeModuleElement

@mwageringel
Copy link
Author

Author: Markus Wageringel

@mwageringel
Copy link
Author

Branch: u/gh-mwageringel/29218

@mwageringel
Copy link
Author

Commit: 324478d

@tscrim
Copy link
Collaborator

tscrim commented Feb 20, 2020

Reviewer: Travis Scrimshaw

@tscrim
Copy link
Collaborator

tscrim commented Feb 20, 2020

comment:2

Hmmm...my initial thought was this is a good change to match the behavior as documented. However, there is an issue that does arise. The iterator for a vector is different than for a more generic element of a ModuleWithBasis. In the former, it goes over all values (in part because the basis is finite and has a fixed order) whereas in the latter it just goes over the supported basis elements. So for the latter, x.length() == len(x.support()), but I could see for a vector v someone doing something like

for i in range(v.length()):
    v[i] += 1

So this could break code and it is somewhat unnatural given the name of the method. I would propose instead overriding the documentation to reflect the more natural behavior because of the implementation and how people are expecting to use this class. I further believe this will be a very special case to allow the break in inherited behavior.

@mwageringel
Copy link
Author

comment:3

The method is also broken for matrices:

sage: matrix(QQ, 4).length()
...
TypeError: object of type 'sage.matrix.matrix_rational_dense.Matrix_rational_dense' has no len()

This would be useful for sparse matrices in particular.

What would you suggest to do with matrices then? Should it return the number of rows?

Given that the method does not currently work for matrices, it would not break anything if we make it return the length of the support, except that it is inconsistent with vectors.

@tscrim
Copy link
Collaborator

tscrim commented Feb 21, 2020

comment:4

There is much less of a case for matrices as it is only because of the internal representation as rows that length has a distinct meaning. There is nothing really special about the number of rows versus columns, and it makes equal sense to have it be the size of the support. So I would instead fix matrices to be the size of the support and keep vectors as something special (and inconsistent).

@mkoeppe
Copy link
Contributor

mkoeppe commented May 1, 2020

comment:5

Moving tickets to milestone sage-9.2 based on a review of last modification date, branch status, and severity.

@mkoeppe mkoeppe modified the milestones: sage-9.1, sage-9.2 May 1, 2020
@mkoeppe mkoeppe modified the milestones: sage-9.2, sage-9.3 Sep 5, 2020
@mkoeppe
Copy link
Contributor

mkoeppe commented Mar 15, 2021

comment:7

Setting new milestone based on a cursory review of ticket status, priority, and last modification date.

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 Mar 15, 2021
@mkoeppe
Copy link
Contributor

mkoeppe commented Jul 19, 2021

comment:8

Setting a new milestone for this ticket based on a cursory review.

@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Jul 19, 2021
@mkoeppe
Copy link
Contributor

mkoeppe commented Dec 18, 2021

comment:9

Stalled in needs_review or needs_info; likely won't make it into Sage 9.5.

@mkoeppe mkoeppe modified the milestones: sage-9.5, sage-9.6 Dec 18, 2021
@mkoeppe mkoeppe removed this from the sage-9.6 milestone Apr 2, 2022
@mkoeppe mkoeppe added this to the sage-9.7 milestone Apr 2, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Aug 31, 2022
@mkoeppe
Copy link
Contributor

mkoeppe commented Sep 11, 2022

comment:12

Replying to Markus Wageringel:

The method is also broken for matrices:

sage: matrix(QQ, 4).length()
...
TypeError: object of type 'sage.matrix.matrix_rational_dense.Matrix_rational_dense' has no len()

Still broken in 9.7.rc1. This must be some strange Cython-related effect:

sage: matrix(QQ, 4).length()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 matrix(QQ, Integer(4)).length()

File ~/s/sage/sage-rebasing/local/var/lib/sage/venv-python3.9/lib/python3.9/site-packages/sage/categories/modules_with_basis.py:1531, in ModulesWithBasis.ElementMethods.length(self)
   1511 def length(self):
   1512     """
   1513     Return the number of basis elements whose coefficients in
   1514     ``self`` are nonzero.
   (...)
   1529         4
   1530     """
-> 1531     return len(self)

TypeError: object of type 'sage.matrix.matrix_rational_dense.Matrix_rational_dense' has no len()
sage: matrix(QQ, 4).__len__
<bound method ModulesWithBasis.ElementMethods.__len__ of [0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]>

@mkoeppe
Copy link
Contributor

mkoeppe commented Sep 11, 2022

Dependencies: #34509

@mkoeppe
Copy link
Contributor

mkoeppe commented Sep 11, 2022

Changed branch from u/gh-mwageringel/29218 to u/mkoeppe/29218

@mkoeppe mkoeppe removed this from the sage-9.8 milestone Jan 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants