sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.41k stars 475 forks source link

Mutability of tableaux part I: lists of tuples instead of lists of lists #15862

Closed darijgr closed 9 years ago

darijgr commented 10 years ago

Tableaux in Sage used to be implemented as lists of lists. The outer list was wrapped in a CombinatorialObject, which made it immutable (at least without accessing underscored attributes). The inner lists, however, could be easily mutated; for example:

sage: T = Tableau([[1,2],[2]])
sage: t0 = T[0]
sage: t0[1] = 3 
sage: T
[[1, 3], [2]]

This kind of mutability was likely not intended. I, personally, have only ever triggered it by accident.

The present branch replaces the inner lists in the implementation of tableaux and skew tableaux by tuples. As a consequence, tableaux become completely (rather than just shallowly) immutable (unless their entries themselves are mutable, which can be blamed on the user). They are still printed as lists of lists, but this is just a _repr_ issue.

The branch also makes some optimizations and corrections.


Old description:

Tableaux in Sage are mutable objects, at least indirectly:

sage: T = Tableau([[1,2],[2]])
sage: t0 = T[0]
sage: t0[1] = 3 
sage: T
[[1, 3], [2]]

This in itself is probably not a bug, although not the kind of behavior I like either (what exactly is sped up by mutability of tableaux?). But there are things which probably are bugs given this behavior:

  1. Tableaux are hashed by reference:
sage: T = Tableau([[1,2],[2]])
sage: hash(T)
-7723024261707595164
sage: T[0][1] = 4
sage: hash(T)
-7723024261707595164
  1. Line 311 of sage/combinat/tableau.py says:
            # Since we are (suppose to be) immutable, we can share the underlying data

But we are not immutable. This comment line is supposed to provide justification for initializing the tableau as a CombinatorialObject, but the docstring of CombinatorialObject says that "CombinatorialObjects are shallowly immutable, and the intention is that they are semantically immutable". The latter is not satisfied for tableaux.

If we want tableaux to be mutable, why wrap them inside such a class? If we want them to be immutable, wouldn't it be right to encode them as CombinatorialObjects of CombinatorialObjects? Or is the speed cost for this too steep? And, finally, what is it that CombinatorialObject does that tuple does not?

And, on a related note, does Sage provide a class for immutable dictionaries? (I'm still hell-bent on implementing arbitrary-shaped tableaux.)

  1. Mutating tableaux poisons type judgments (or what passes for type judgments in Sage):
sage: T = StandardTableau([[1,2],[3]])
sage: T[0][1] = 5
sage: isinstance(T, StandardTableau)
True
  1. There is some action at a distance (although fortunately rare):
sage: T = SkewTableau([[1,2],[3]])
sage: S = T.to_tableau()  # Tableau(S) doesn't work, wondering if it should?
sage: S
[[1, 2], [3]]
sage: T
[[1, 2], [3]]
sage: T[0][1] = 5
sage: S
[[1, 5], [3]]
sage: T
[[1, 5], [3]]
  1. How would I define Loday's Hopf algebra of tableaux if tableaux are mutable?

CC: @anneschilling @tscrim @nthiery @stumpc5 @AndrewAtLarge @zabrocki @sagetrac-sage-combinat @hivert @sagetrac-jpswanson

Component: combinatorics

Keywords: tableaux, sage-combinat, mutability, days64

Stopgaps: #17997

Author: Josh Swanson, Jan Keitel, Darij Grinberg

Branch/Commit: 430003d

Reviewer: Travis Scrimshaw

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

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

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

9ce201eone less to_word
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from b68a79b to 9ce201e

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from 9ce201e to 81d3af3

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

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

93c6448an even more hands-off approach to testing iterability
81d3af3Merge branch 'public/15862' of git://trac.sagemath.org/sage into tableau_verynew
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

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

fcf411clast edits
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from 81d3af3 to fcf411c

darijgr commented 9 years ago
comment:40

This is ready for review.

There are minor slowdowns in skew_tableau.py left, relating to the tuple-cast in its initialization. I don't know if they are avoidable.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from fcf411c to 4520047

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

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

4520047put checking into `__init__` and normalization into __classcall_private__
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

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

8c19ad3so normalizaton is necessary in the init too
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from 4520047 to 8c19ad3

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from 8c19ad3 to 430003d

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

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

430003dactually, probably no need for normalization in __classcall_private__
darijgr commented 9 years ago

Author: Josh Swanson, Jan Keitel, Darij Grinberg

darijgr commented 9 years ago

Changed work issues from see if skew tableaux have gotten slower to none

darijgr commented 9 years ago

Description changed:

--- 
+++ 
@@ -1,3 +1,22 @@
+Tableaux in Sage used to be implemented as lists of lists. The outer list was wrapped in a `CombinatorialObject`, which made it immutable (at least without accessing underscored attributes). The inner lists, however, could be easily mutated; for example:
+
+```
+sage: T = Tableau([[1,2],[2]])
+sage: t0 = T[0]
+sage: t0[1] = 3 
+sage: T
+[[1, 3], [2]]
+```
+This kind of mutability was likely not intended. I, personally, have only ever triggered it by accident.
+
+The present branch replaces the inner lists in the implementation of tableaux and skew tableaux by tuples. As a consequence, tableaux become completely (rather than just shallowly) immutable (unless their entries themselves are mutable, which can be blamed on the user). They are still printed as lists of lists, but this is just a `_repr_` issue.
+
+The branch also makes some optimizations and corrections.
+
+---
+
+Old description:
+
 Tableaux in Sage are mutable objects, at least indirectly:
tscrim commented 9 years ago

Reviewer: Travis Scrimshaw

tscrim commented 9 years ago
comment:47

LGTM.

darijgr commented 9 years ago
comment:48

Thank you, Travis!

vbraun commented 9 years ago

Changed branch from public/15862 to 430003d