Opened 7 years ago

Closed 6 years ago

# Matrix constructor broken for symmetric function rings and 1-column matrices

Reported by: Owned by: darij major sage-6.4 linear algebra matrix, symmetric functions, nthiery, tscrim, sage-combinat, zabrocki Darij Grinberg Travis Scrimshaw N/A d1a5094 d1a509441ff8d1c33324e180cffaca0301b3d073

### Description

```sage: Sym = SymmetricFunctions(QQ); Sym.inject_shorthands()
/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/combinat/sf/sf.py:1198: RuntimeWarning: redefining global value `e`
inject_variable(shorthand, getattr(self, shorthand)())
sage: Matrix(h, [[h[1]], [h[2]]])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-e73595c500f3> in <module>()
----> 1 Matrix(h, [[h[Integer(1)]], [h[Integer(2)]]])

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/constructor.pyc in _matrix_constructor(*args, **kwds)
727             ring = rings.ZZ
728
--> 729     return matrix_space.MatrixSpace(ring, nrows, ncols, sparse=sparse)(entries)
730
731

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/matrix_space.pyc in __call__(self, entries, coerce, copy, sparse)
468             Full MatrixSpace of 3 by 5 dense matrices over Integer Ring!
469         """
--> 470         return self.matrix(entries, coerce, copy)
471
472     def change_ring(self, R):

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/matrix_space.pyc in matrix(self, x, coerce, copy)
1358                                       copy=False, coerce=coerce)
1359                         else:
-> 1360                             return MC(self, new_x, copy=False, coerce=coerce)
1361                     except TypeError:
1362                         pass

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/matrix_generic_dense.so in sage.matrix.matrix_generic_dense.Matrix_generic_dense.__init__ (build/cythonized/sage/matrix/matrix_generic_dense.c:2850)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9603)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4256)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4163)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/combinat/sf/classical.pyc in _element_constructor_(self, x)
299         # Elements of the base ring #
300         #############################
--> 301         elif x.parent() is R:
302             return eclass(self, {sage.combinat.partition.Partition([]):x})
303

AttributeError: 'tuple' object has no attribute 'parent'
```

Using the generic parent doesn't help either:

```sage: Matrix(Sym, [[h[1]], [h[2]]])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-9515a5c3ba14> in <module>()
----> 1 Matrix(Sym, [[h[Integer(1)]], [h[Integer(2)]]])

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/constructor.pyc in _matrix_constructor(*args, **kwds)
727             ring = rings.ZZ
728
--> 729     return matrix_space.MatrixSpace(ring, nrows, ncols, sparse=sparse)(entries)
730
731

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/matrix_space.pyc in __call__(self, entries, coerce, copy, sparse)
468             Full MatrixSpace of 3 by 5 dense matrices over Integer Ring!
469         """
--> 470         return self.matrix(entries, coerce, copy)
471
472     def change_ring(self, R):

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/matrix_space.pyc in matrix(self, x, coerce, copy)
1358                                       copy=False, coerce=coerce)
1359                         else:
-> 1360                             return MC(self, new_x, copy=False, coerce=coerce)
1361                     except TypeError:
1362                         pass

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/matrix_generic_dense.so in sage.matrix.matrix_generic_dense.Matrix_generic_dense.__init__ (build/cythonized/sage/matrix/matrix_generic_dense.c:2850)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9603)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4256)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4163)()

99                     except Exception:
100                         pass
--> 101             raise ValueError("Can't coerce `%s` in any parent `%s` is a facade for"%(element, self))
102

ValueError: Can't coerce `([1], 1)` in any parent `Symmetric Functions over Rational Field` is a facade for
```

### comment:1 Changed 7 years ago by darij

It's not in the `matrix` constructor, but in the less frontendish `MatrixSpace` init:

```sage: MatrixSpace(h, 1, 1)
Full MatrixSpace of 1 by 1 dense matrices over Symmetric Functions over Rational Field in the homogeneous basis
sage: MatrixSpace(h, 1, 1)([h[1]])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-d7504c152076> in <module>()
----> 1 MatrixSpace(h, Integer(1), Integer(1))([h[Integer(1)]])

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/matrix_space.pyc in __call__(self, entries, coerce, copy, sparse)
468             Full MatrixSpace of 3 by 5 dense matrices over Integer Ring!
469         """
--> 470         return self.matrix(entries, coerce, copy)
471
472     def change_ring(self, R):

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/matrix_space.pyc in matrix(self, x, coerce, copy)
1358                                       copy=False, coerce=coerce)
1359                         else:
-> 1360                             return MC(self, new_x, copy=False, coerce=coerce)
1361                     except TypeError:
1362                         pass

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/matrix/matrix_generic_dense.so in sage.matrix.matrix_generic_dense.Matrix_generic_dense.__init__ (build/cythonized/sage/matrix/matrix_generic_dense.c:2850)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9603)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4256)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4163)()

/home/darij/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/combinat/sf/classical.pyc in _element_constructor_(self, x)
299         # Elements of the base ring #
300         #############################
--> 301         elif x.parent() is R:
302             return eclass(self, {sage.combinat.partition.Partition([]):x})
303

AttributeError: 'tuple' object has no attribute 'parent'
```

### comment:2 Changed 7 years ago by darij

• Status changed from new to needs_review

### comment:3 Changed 7 years ago by darij

Fixed in branch public/linalg/uglyduck. I am not happy with the way it went, but at least I can't reproduce the bug anymore. There is now some precision issues:

```sage -t src/sage/matrix/matrix2.pyx
**********************************************************************
File "src/sage/matrix/matrix2.pyx", line 5209, in sage.matrix.matrix2.Matrix.diagonal.eigenspaces_right
Failed example:
eigenvalues = em[0]; eigenvalues.dense_matrix().zero_at(1e-15)
Expected:
[ 13.348469228349...                0.0                 0.0]
[                0.0 -1.348469228349...                 0.0]
[                0.0                0.0                 0.0]
Got:
[13.348469228349522                0.0                0.0]
[               0.0 -1.348469228349534                0.0]
[               0.0                0.0                0.0]
**********************************************************************
File "src/sage/matrix/matrix2.pyx", line 5689, in sage.matrix.matrix2.Matrix.diagonal.eigenmatrix_right
Failed example:
evalues = em[0]; evalues.dense_matrix().zero_at(2e-15)
Expected:
[ 13.348469228349...                0.0                 0.0]
[                0.0 -1.348469228349...                 0.0]
[                0.0                0.0                 0.0]
Got:
[13.348469228349522                0.0                0.0]
[               0.0 -1.348469228349534                0.0]
[               0.0                0.0                0.0]
**********************************************************************
File "src/sage/matrix/matrix2.pyx", line 12207, in sage.matrix.matrix2.Matrix.diagonal.norm
Failed example:
A.norm()
Expected:
15.0
Got:
14.999999999999998
**********************************************************************
```

### comment:4 Changed 7 years ago by darij

• Branch set to public/linalg/uglyduck
• Commit set to d1a509441ff8d1c33324e180cffaca0301b3d073

New commits:

 ​d1a5094 `godawful fix for MatrixSpace matrix method; breaks some floating-point stuff but that's for you guys`

### comment:5 Changed 7 years ago by darij

Fuck yeah.

These precision failures appear on the develop branch without my patch. It is now #17126.

Last edited 7 years ago by darij (previous) (diff)

### comment:6 Changed 6 years ago by tscrim

• Authors set to Darij Grinberg
• Reviewers set to Travis Scrimshaw
• Status changed from needs_review to positive_review

LGTM.

Thank you!

### comment:8 Changed 6 years ago by vbraun

• Branch changed from public/linalg/uglyduck to d1a509441ff8d1c33324e180cffaca0301b3d073
• Resolution set to fixed
• Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.