Opened 2 years ago
Last modified 2 months ago
#29619 new enhancement
tensors should have a sparse iterator
Reported by:  ghmwageringel  Owned by:  

Priority:  major  Milestone:  sage9.7 
Component:  linear algebra  Keywords:  
Cc:  egourgoulhon, ghLBrunswic, ghhonglizhaobob  Merged in:  
Authors:  Reviewers:  
Report Upstream:  N/A  Work issues:  
Branch:  u/ghhonglizhaobob/tensors_should_have_a_sparse_iterator (Commits, GitHub, GitLab)  Commit:  bbed88ad016ef4b2c944e28f011ad0d3516deaf7 
Dependencies:  Stopgaps: 
Description
As observed on this Ask SageMath question, it does not seem to be possible to iterate over the nonzero 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 (15)
comment:1 Changed 2 years ago by
 Priority changed from minor to major
comment:2 Changed 2 years ago by
comment:3 Changed 22 months ago by
See also: #30309  Unify free module elements API: methods dict
, monomial_coefficients
, etc.
comment:4 Changed 21 months ago by
 Milestone changed from sage9.2 to sage9.3
comment:5 Changed 16 months ago by
 Milestone changed from sage9.3 to sage9.4
Setting new milestone based on a cursory review of ticket status, priority, and last modification date.
comment:6 Changed 12 months ago by
 Cc ghhonglizhaobob added
comment:7 followup: ↓ 8 Changed 12 months ago by
@ghhonglizhaobob This is probably the best ticket for starting
comment:8 in reply to: ↑ 7 Changed 11 months ago by
Replying to mkoeppe:
@ghhonglizhaobob 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 11 months ago by
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()
.
comment:10 Changed 11 months ago by
 Branch set to u/ghhonglizhaobob/tensors_should_have_a_sparse_iterator
comment:11 Changed 11 months ago by
 Commit set to bbed88ad016ef4b2c944e28f011ad0d3516deaf7
Branch pushed to git repo; I updated commit sha1. New commits:
bbed88a  added doc for iter

comment:12 Changed 11 months ago by
+ 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#loopingtechniques
comment:13 Changed 10 months ago by
 Milestone changed from sage9.4 to sage9.5
comment:14 Changed 6 months ago by
 Milestone changed from sage9.5 to sage9.6
comment:15 Changed 2 months ago by
 Milestone changed from sage9.6 to sage9.7
Thanks for opening this ticket. I've added a link to it in the "Algebraic part" section of the metaticket #18528.