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

Matrix and Components should have a sparse iterator #29619

Closed
mwageringel opened this issue Apr 29, 2020 · 25 comments
Closed

Matrix and Components should have a sparse iterator #29619

mwageringel opened this issue Apr 29, 2020 · 25 comments

Comments

@mwageringel
Copy link

As observed on this Ask SageMath question, it does not seem to be possible to iterate over the non-zero entries of a tensor. This is unfortunate because the entries are stored in a sparse format, in a dictionary.

Since a tensor might have symmetries, this is more involved than just iterating over the dictionary, but such an iterator would immediately be useful for the implementation of the display() method, for instance.

Here we implement a method Components.items, returning an iterable of (indices, value). This is compatible with the sparse and dense vectors from sage.modules.

(Because a Components does not currently have a parent, it does not make sense to define the method monomial_coefficients - as defined by ModulesWithBasis.)

We also define Matrix.items with the same interface.

CC: @egourgoulhon @LBrunswic @honglizhaobob @tscrim

Component: linear algebra

Author: Matthias Koeppe

Branch/Commit: 74d9493

Reviewer: Eric Gourgoulhon

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

@egourgoulhon
Copy link
Member

comment:2

Thanks for opening this ticket. I've added a link to it in the "Algebraic part" section of the meta-ticket #18528.

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 13, 2020

comment:3

See also: #30309 - Unify free module elements API: methods dict, monomial_coefficients, etc.

@mkoeppe mkoeppe modified the milestones: sage-9.2, sage-9.3 Aug 29, 2020
@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
Copy link
Contributor

mkoeppe commented Jun 15, 2021

comment:7

@honglizhaobob This is probably the best ticket for starting

@honglizhaobob
Copy link
Mannequin

honglizhaobob mannequin commented Jun 18, 2021

comment:8

Replying to @mkoeppe:

@honglizhaobob This is probably the best ticket for starting

Is there a set of unit test cases one needs to pass to ensure a justified change of data structure?

@mkoeppe
Copy link
Contributor

mkoeppe commented Jun 18, 2021

comment:9

To clarify - here on this ticket, the data structure shouldn't be changed but instead methods added.

Sage mainly uses doctests - see https://doc.sagemath.org/html/en/developer/doctesting.html
but there are also unit tests, which are invoked within the doctests by TestSuite(...).run().

@honglizhaobob
Copy link
Mannequin

honglizhaobob mannequin commented Jun 22, 2021

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 5, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

bbed88aadded doc for iter

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 5, 2021

Commit: bbed88a

@mkoeppe
Copy link
Contributor

mkoeppe commented Jul 5, 2021

comment:12
+        for multi_idx in self._comp.keys():
+            # access value in dictionary
+            coef = self._comp[multi_idx]

You can use .items() here - see https://docs.python.org/3/tutorial/datastructures.html#looping-techniques

@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 14, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.6, sage-9.7 Apr 1, 2022
@mkoeppe

This comment has been minimized.

@mkoeppe mkoeppe changed the title tensors should have a sparse iterator Components should have a sparse iterator Aug 30, 2022
@mkoeppe

This comment has been minimized.

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 30, 2022

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 30, 2022

New commits:

3862383src/sage/tensor/modules/comp.py: WIP
882c5eaComponents.items: New

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 30, 2022

Changed commit from bbed88a to 882c5ea

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 30, 2022

Author: Matthias Koeppe

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 30, 2022

Changed commit from 882c5ea to 4958211

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 30, 2022

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

4958211Components.items: New

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 30, 2022

Changed commit from 4958211 to 74d9493

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 30, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

74d9493Matrix.items: New

@mkoeppe

This comment has been minimized.

@mkoeppe mkoeppe changed the title Components should have a sparse iterator Matrix and Components should have a sparse iterator Aug 30, 2022
@egourgoulhon
Copy link
Member

comment:23

Thanks for implementing this. LGTM.

@egourgoulhon
Copy link
Member

Reviewer: Eric Gourgoulhon

@mkoeppe
Copy link
Contributor

mkoeppe commented Sep 1, 2022

comment:24

Thanks!

@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Sep 19, 2022
@vbraun
Copy link
Member

vbraun commented Sep 20, 2022

Changed branch from u/mkoeppe/tensors_should_have_a_sparse_iterator to 74d9493

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

4 participants