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

dense + sparse matrix should be dense #29359

Open
mwageringel opened this issue Mar 18, 2020 · 4 comments
Open

dense + sparse matrix should be dense #29359

mwageringel opened this issue Mar 18, 2020 · 4 comments

Comments

@mwageringel
Copy link

When adding or subtracting a dense matrix and a sparse matrix, the resulting matrix is sparse in some cases.

sage: a = matrix.random(ZZ, 3, sparse=False)
sage: b = matrix.random(ZZ, 3, sparse=True)
sage: (a + b).is_sparse()  # correct
False
sage: (a.change_ring(QQ) + b).is_sparse()  # correct
False
sage: (a.change_ring(QQ) + b.change_ring(QQ)).is_sparse()  # correct
False
sage: (a + b.change_ring(QQ)).is_sparse()  # should also be False
True

This seems to happen when the rings are different and the common coercion parent of the rings is the ring of the sparse matrix.

Component: coercion

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

@mwageringel mwageringel added this to the sage-9.1 milestone Mar 18, 2020
@mkoeppe
Copy link
Contributor

mkoeppe commented May 1, 2020

comment:1

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
@mwageringel
Copy link
Author

comment:2

The problem here seems to be that there is a coercion from the dense matrix space to the sparse one, so the sparse matrix space is discovered as a common parent:

sage: a = matrix.random(ZZ, 3, sparse=False)
sage: b = matrix.random(QQ, 3, sparse=True)
sage: b.parent().has_coerce_map_from(a.parent())
True
sage: coercion_model.common_parent(a, b)
Full MatrixSpace of 3 by 3 sparse matrices over Rational Field

It would be better if the pushout were used here, as it correctly results in a dense matrix space:

sage: sage.categories.pushout.pushout(a.parent(), b.parent())
Full MatrixSpace of 3 by 3 dense matrices over Rational Field

@mkoeppe mkoeppe modified the milestones: sage-9.2, sage-9.3 Oct 24, 2020
@fchapoton
Copy link
Contributor

comment:4

more information:

sage: cm.explain(A,B,operator.mul)                                              
Action discovered.
    Left action by Full MatrixSpace of 3 by 3 dense matrices over Integer Ring on Full MatrixSpace of 3 by 3 sparse matrices over Rational Field
Result lives in Full MatrixSpace of 3 by 3 dense matrices over Rational Field
Full MatrixSpace of 3 by 3 dense matrices over Rational Field

sage: cm.explain(A,B,operator.add)                                              
Coercion on left operand via
    Coercion map:
      From: Full MatrixSpace of 3 by 3 dense matrices over Integer Ring
      To:   Full MatrixSpace of 3 by 3 sparse matrices over Rational Field
Arithmetic performed after coercions.
Result lives in Full MatrixSpace of 3 by 3 sparse matrices over Rational Field
Full MatrixSpace of 3 by 3 sparse matrices over Rational Field

@mkoeppe
Copy link
Contributor

mkoeppe commented Feb 13, 2021

comment:5

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 Feb 13, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Jul 19, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.5, sage-9.6 Dec 18, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.6, sage-9.7 Apr 1, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Aug 31, 2022
@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