Ticket #6904 (closed defect: fixed)

Opened 4 years ago

Last modified 3 years ago

change ring broken over QQ and GF(2)

Reported by: was Owned by: cpernet
Priority: major Milestone: sage-4.5.2
Component: linear algebra Keywords:
Cc: Work issues:
Report Upstream: N/A Reviewers: William Stein
Authors: Clément Pernet Merged in: sage-4.5.2.alpha0
Dependencies: Stopgaps:

Description (last modified by cpernet) (diff)

I've uploaded a new patch (sage-6906-v2.patch) taking into account the remarks by was.

Attachments

sage-6904.patch Download (3.8 KB) - added by cpernet 3 years ago.
Proposed fix for the bug #6904
sage-6904-v2.patch Download (4.2 KB) - added by cpernet 3 years ago.
corrected version following the remarks by was
sage-6904-v3.patch Download (4.2 KB) - added by cpernet 3 years ago.
fix a typo in the docstring

Change History

comment:1 Changed 3 years ago by was

  • Report Upstream set to N/A

Here's another bug that bit me while doing research today and might be caused by the same problem:

sage: a = matrix(QQ,22,[0, 0, 0, -1, 1, -1, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 0, 0, -1, 1, 0, -1, 0, 1, 0, 0, -1, 0, -2, 1, -1, -1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0,
0, -1, 1, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, 0, -1, 0, 0,
-1, 1, 0, 1, -1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, -1, 0, 0, 0, -1, 0,
0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, -1, 0, 0,
0, 1, -1, 0, 1, 0, -1, 0, 1, 1, 0, 2, 0, 0, 1, -1, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 1, -1, 1, 0, 0, 1, 0, 0, 0, 0, 1, -2, 1, 1, 0,
0, 0, 0, 0, 1, -1, -1, 0, -1, 0, -1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1,
0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, -1, 1, 0, 0, 0, 0,
0, 0, 1, -1, -1, 0, 0, -1, -1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, -1, 1, 0,
-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 1, -1, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0,
0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 1, -1, 0, -1, 1, 0, -1, 0, 0, 0, 0, 0,
0, -2, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, -1, 1, 0, -1, -1, 1, 0, 0, 0, 0,
-2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 0, -1, 0, 0, 0, 0, 0, -1,
0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, -1, 1, 1, -1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, -1, 0, 0,
0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0,
1]).change_ring(GF(2))

sage: a^21
Traceback (click to the left of this block for traceback)
...
TypeError: Cannot convert
sage.matrix.matrix_modn_dense.Matrix_modn_dense to
sage.matrix.matrix_mod2_dense.Matrix_mod2_dense

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_79.py", line 9, in <module>
    open("___code___.py","w").write("# -*- coding: utf-8 -*-\n" + _support_.preparse_worksheet_cell(base64.b64decode("diA9IFsoYV5pKS5saXN0KCkgZm9yIGkgaW4gWzEuLjIxXV0="),globals())+"\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpVclvdc/___code___.py", line 3, in <module>
    v = [(a**i).list() for i in (ellipsis_range(_sage_const_1 ,Ellipsis,_sage_const_21 ))]
  File "", line 1, in <module>
    
  File "matrix0.pyx", line 3893, in sage.matrix.matrix0.Matrix.__pow__ (sage/matrix/matrix0.c:21527)
  File "element.pyx", line 1409, in sage.structure.element.RingElement.__pow__ (sage/structure/element.c:11201)
  File "element.pyx", line 3264, in sage.structure.element.generic_power_c (sage/structure/element.c:23714)
  File "element.pyx", line 2134, in sage.structure.element.Matrix.__mul__ (sage/structure/element.c:14292)
  File "matrix_mod2_dense.pyx", line 645, in sage.matrix.matrix_mod2_dense.Matrix_mod2_dense._matrix_times_matrix_ (sage/matrix/matrix_mod2_dense.c:5144)
TypeError: Cannot convert sage.matrix.matrix_modn_dense.Matrix_modn_dense to sage.matrix.matrix_mod2_dense.Matrix_mod2_dense

comment:2 Changed 3 years ago by was

Amazingly, this is still broken in Sage-4.5.alpha4!

wstein@sage:~/build/sage-4.5.alpha4$ ./sage
----------------------------------------------------------------------
| Sage Version 4.5.alpha4, Release Date: 2010-07-06                  |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
**********************************************************************
*                                                                    *
* Warning: this is a prerelease version, and it may be unstable.     *
*                                                                    *
**********************************************************************
sage: matrix(QQ,2,[1,0,0,1]).change_ring(GF(2)) - 1
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)

/mnt/usb1/scratch/wstein/build/sage-4.5.alpha4/<ipython console> in <module>()

/mnt/usb1/scratch/wstein/build/sage-4.5.alpha4/local/lib/python2.6/site-packages/sage/structure/element.so in sage.structure.element.RingElement.__sub__ (sage/structure/element.c:11073)()

/mnt/usb1/scratch/wstein/build/sage-4.5.alpha4/local/lib/python2.6/site-packages/sage/structure/coerce.so in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/coerce.c:6123)()

/mnt/usb1/scratch/wstein/build/sage-4.5.alpha4/local/lib/python2.6/site-packages/sage/structure/element.so in sage.structure.element.RingElement.__sub__ (sage/structure/element.c:11005)()

RuntimeError: 
sage: 

comment:3 Changed 3 years ago by wjp

This is occurring in Matrix_modn_dense._sub_(). It gets called with 'right' of type Matrix_mod2_dense, but casts it to Matrix_modn_dense internally. (I haven't looked at all at why this is happening and if it should be happening.)

Changed 3 years ago by cpernet

Proposed fix for the bug #6904

comment:4 Changed 3 years ago by cpernet

  • Status changed from new to needs_review
  • Summary changed from change ring broken over QQ and GF(2) to [with patch, need review] change ring broken over QQ and GF(2)

The problem is due to the fact that with p=2, the change_ring() method  should not create a matrix_modn_dense but a matrix_mod2_dense (wrapping m4ri).

I added this special case.

Meanwhile, it revealed a conflict in the declaration of ONE (in libs/pari/gens.pyx, and in m4ri). So I renammed the macros in pari/gens.*.

comment:5 Changed 3 years ago by cpernet

  • Owner changed from was to cpernet

comment:6 Changed 3 years ago by was

  • Status changed from needs_review to needs_work

Two changes needed:

  • Add the example as a doctest.
  • Change _mod_int_c(self, mod_int p) so it calls _mod_two if p=2.

Changed 3 years ago by cpernet

corrected version following the remarks by was

comment:7 Changed 3 years ago by cpernet

  • Description modified (diff)

Changed 3 years ago by cpernet

fix a typo in the docstring

comment:8 Changed 3 years ago by was

  • Status changed from needs_work to positive_review
  • Summary changed from [with patch, need review] change ring broken over QQ and GF(2) to [with patch, positive review] change ring broken over QQ and GF(2)

comment:9 Changed 3 years ago by mpatel

  • Status changed from positive_review to closed
  • Reviewers set to William Stein
  • Resolution set to fixed
  • Merged in set to sage-4.5.2.alpha0
  • Authors set to Clément Pernet

I've filled in the Author(s) and Reviewer(s) fields. If I'm wrong, please correct them.

comment:10 Changed 3 years ago by mvngu

  • Summary changed from [with patch, positive review] change ring broken over QQ and GF(2) to change ring broken over QQ and GF(2)
Note: See TracTickets for help on using tickets.