Opened 15 months ago

Last modified 2 weeks ago

#29619 new enhancement

tensors should have a sparse iterator

Reported by: gh-mwageringel Owned by:
Priority: major Milestone: sage-9.5
Component: linear algebra Keywords:
Cc: egourgoulhon, gh-LBrunswic, gh-honglizhaobob Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: u/gh-honglizhaobob/tensors_should_have_a_sparse_iterator (Commits, GitHub, GitLab) Commit: bbed88ad016ef4b2c944e28f011ad0d3516deaf7
Dependencies: Stopgaps:

Status badges


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.

Change History (13)

comment:1 Changed 15 months ago by mkoeppe

  • Priority changed from minor to major

comment:2 Changed 15 months ago by egourgoulhon

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

comment:3 Changed 12 months ago by mkoeppe

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

comment:4 Changed 11 months ago by mkoeppe

  • Milestone changed from sage-9.2 to sage-9.3

comment:5 Changed 6 months ago by mkoeppe

  • Milestone changed from sage-9.3 to sage-9.4

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

comment:6 Changed 7 weeks ago by mkoeppe

  • Cc gh-honglizhaobob added

comment:7 follow-up: Changed 7 weeks ago by mkoeppe

@gh-honglizhaobob This is probably the best ticket for starting

comment:8 in reply to: ↑ 7 Changed 7 weeks ago by gh-honglizhaobob

Replying to mkoeppe:

@gh-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?

comment:9 Changed 7 weeks ago by mkoeppe

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

Sage mainly uses doctests - see but there are also unit tests, which are invoked within the doctests by TestSuite(...).run().

comment:10 Changed 6 weeks ago by gh-honglizhaobob

  • Branch set to u/gh-honglizhaobob/tensors_should_have_a_sparse_iterator

comment:11 Changed 4 weeks ago by git

  • Commit set to bbed88ad016ef4b2c944e28f011ad0d3516deaf7

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

bbed88aadded doc for iter

comment:12 Changed 4 weeks ago by mkoeppe

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

You can use .items() here - see

comment:13 Changed 2 weeks ago by mkoeppe

  • Milestone changed from sage-9.4 to sage-9.5
Note: See TracTickets for help on using tickets.