Opened 12 years ago

Closed 12 years ago

# segfault when multiplying QQ constant and SR vector (with user basis)

Reported by: Owned by: jason jason, was critical sage-4.6.1 linear algebra burcin, rbeezer, craigcitro, robertwb, novoselt, vbraun sage-4.6.1.alpha2 Andrey Novoseltsev, Jason Grout Jason Grout, Andrey Novoseltsev N/A

### Description

This code worked in Sage 4.5.2. I just realized that it now gives a segfault in 4.6. Does anyone know what is going on?

```sage: A=matrix(QQ, [[2,1],[0,3]])
sage: eval, evecs,mult=A.eigenvectors_right()[0]
sage: evec=evecs[0]
sage: unitevec=evec/evec.norm()
sage: unitevec
(1/2*sqrt(2), 1/2*sqrt(2))
sage: parent(eval)
Rational Field
sage: eval
3
```

and then:

```sage: w=(SR2)(unitevec)
sage: parent(w) # works fine
Vector space of dimension 2 over Symbolic Ring
sage: eval*w
(3/2*sqrt(2), 3/2*sqrt(2))
```

but here's the problem:

```sage: parent(unitevec) # gives problem
Vector space of degree 2 and dimension 1 over Symbolic Ring
User basis matrix:
[1 1]
sage: eval*unitevec

------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occurred in Sage.
This probably occurred because a *compiled* component
of Sage has a bug in it (typically accessing invalid memory)
or is not properly wrapped with _sig_on, _sig_off.
You might want to run Sage under gdb with 'sage -gdb' to debug this.
Sage will now terminate (sorry).
------------------------------------------------------------
```

Here is a backtrace using sage -gdb, which indicates a problem in ginac or pynac.

```sage: eval*unitevec

0x00007fff8294a7a2 in __dynamic_cast ()
(gdb) bt
#0  0x00007fff8294a7a2 in __dynamic_cast ()
#1  0x000000010798ffcc in is_a<GiNaC::relational> [inlined] () at /Users/grout/sage-trees/sage-4.6/local/include/pynac/basic.h:939
#2  0x000000010798ffcc in is_a<GiNaC::relational> [inlined] () at /Users/grout/sage-trees/sage-4.6/local/include/pynac/ex.h:939
#3  0x000000010798ffcc in __pyx_f_4sage_8symbolic_10expression_10Expression__mul_ (__pyx_v_left=0x10c825a70, __pyx_v_right=0x10c8281d0, __pyx_skip_dispatch=<value temporarily unavailable, due to optimizations>) at ex.h:11284
#4  0x0000000104be0632 in __pyx_f_4sage_7modules_19free_module_element_31FreeModuleElement_generic_dense__rmul_ (__pyx_v_self=0x10c81e628, __pyx_v_left=0x10c825a70, __pyx_skip_dispatch=<value temporarily unavailable, due to optimizations>) at sage/modules/free_module_element.c:15600
#5  0x00000001022b5b7f in __pyx_f_4sage_9structure_14coerce_actions_16LeftModuleAction__call_ (__pyx_v_self=0x1006be758, __pyx_v_g=0x10c825a70, __pyx_v_a=0x10c81e628, __pyx_skip_dispatch=<value temporarily unavailable, due to optimizations>) at sage/structure/coerce_actions.c:5437
#6  0x000000010223fe62 in __pyx_pf_4sage_10categories_6action_6Action_act (__pyx_v_self=0x1006be758, __pyx_args=0x10bd2e638, __pyx_kwds=0x0) at sage/categories/action.c:2563
#7  0x000000010000bcd2 in PyObject_Call ()
#8  0x00000001022b68eb in __pyx_pf_4sage_9structure_14coerce_actions_12ModuleAction___init__ (__pyx_v_self=0x1006be758, __pyx_args=<value temporarily unavailable, due to optimizations>, __pyx_kwds=<value temporarily unavailable, due to optimizations>) at sage/structure/coerce_actions.c:5133
#9  0x000000010006d8c5 in type_call ()
#10 0x000000010000bcd2 in PyObject_Call ()
#11 0x00000001022b8673 in __pyx_pf_4sage_9structure_14coerce_actions_detect_element_action (__pyx_self=<value temporarily unavailable, due to optimizations>, __pyx_args=<value temporarily unavailable, due to optimizations>, __pyx_kwds=<value temporarily unavailable, due to optimizations>) at sage/structure/coerce_actions.c:4184
#12 0x000000010000bcd2 in PyObject_Call ()
#13 0x0000000101f9988c in __pyx_f_4sage_9structure_6parent_6Parent_discover_action (__pyx_v_self=0x10bed9b40, __pyx_v_S=0x1021654d0, __pyx_v_op=0x1006418c0, __pyx_v_self_on_left=0) at sage/structure/parent.c:14710
#14 0x0000000101f5b825 in __pyx_f_4sage_9structure_10parent_old_6Parent_get_action_c_impl (__pyx_v_self=0x10bed9b40, __pyx_v_S=0x1021654d0, __pyx_v_op=0x1006418c0, __pyx_v_self_on_left=0) at sage/structure/parent_old.c:3254
#15 0x0000000101f5ba6f in __pyx_pf_4sage_9structure_10parent_old_6Parent_get_action_impl (__pyx_v_self=0x10bed9b40, __pyx_args=<value temporarily unavailable, due to optimizations>, __pyx_kwds=<value temporarily unavailable, due to optimizations>) at sage/structure/parent_old.c:3204
#16 0x000000010000bcd2 in PyObject_Call ()
#17 0x0000000101f5c424 in __pyx_f_4sage_9structure_10parent_old_6Parent_get_action_c (__pyx_v_self=0x10bed9b40, __pyx_v_S=0x1021654d0, __pyx_v_op=0x1006418c0, __pyx_v_self_on_left=0, __pyx_skip_dispatch=<value temporarily unavailable, due to optimizations>) at sage/structure/parent_old.c:2892
#18 0x0000000101f4f359 in __pyx_f_4sage_9structure_10parent_old_6Parent__get_action_ (__pyx_v_self=0x10bed9b40, __pyx_v_other=0x1021654d0, __pyx_v_op=0x1006418c0, __pyx_v_self_on_left=0, __pyx_skip_dispatch=<value temporarily unavailable, due to optimizations>) at sage/structure/parent_old.c:6069
#19 0x0000000101f9b9a4 in __pyx_f_4sage_9structure_6parent_6Parent_get_action (__pyx_v_self=0x10bed9b40, __pyx_v_S=0x1021654d0, __pyx_skip_dispatch=<value temporarily unavailable, due to optimizations>, __pyx_optional_args=<value temporarily unavailable, due to optimizations>) at sage/structure/parent.c:13731
#20 0x00000001020aaa87 in __pyx_f_4sage_9structure_6coerce_24CoercionModel_cache_maps_discover_action (__pyx_v_self=0x101f38710, __pyx_v_R=0x1021654d0, __pyx_v_S=0x10bed9b40, __pyx_v_op=0x1006418c0, __pyx_skip_dispatch=<value temporarily unavailable, due to optimizations>) at sage/structure/coerce.c:11909
#21 0x00000001020afcf1 in __pyx_f_4sage_9structure_6coerce_24CoercionModel_cache_maps_get_action (__pyx_v_self=0x101f38710, __pyx_v_R=0x1021654d0, __pyx_v_S=0x10bed9b40, __pyx_v_op=0x1006418c0, __pyx_skip_dispatch=<value temporarily unavailable, due to optimizations>) at sage/structure/coerce.c:10837
#22 0x00000001020bbe36 in __pyx_f_4sage_9structure_6coerce_24CoercionModel_cache_maps_bin_op (__pyx_v_self=0x101f38710, __pyx_v_x=0x1006b56b0, __pyx_v_y=0x10c8292b8, __pyx_v_op=0x1006418c0, __pyx_skip_dispatch=<value temporarily unavailable, due to optimizations>) at sage/structure/coerce.c:6080
#23 0x000000010203165d in __pyx_pf_4sage_9structure_7element_11RingElement___mul__ (__pyx_v_left=0x1006b56b0, __pyx_v_right=0x10c8292b8) at sage/structure/element.c:11399
#24 0x000000010000bfbc in binary_op1 ()
#25 0x000000010000e5a8 in PyNumber_Multiply ()
#26 0x00000001000b35df in PyEval_EvalFrameEx ()
#27 0x00000001000b9010 in PyEval_EvalCodeEx ()
#28 0x00000001000b7799 in PyEval_EvalFrameEx ()
#29 0x00000001000b9010 in PyEval_EvalCodeEx ()
#30 0x00000001000b6ffd in PyEval_EvalFrameEx ()
#31 0x00000001000b9010 in PyEval_EvalCodeEx ()
#32 0x00000001000b6ffd in PyEval_EvalFrameEx ()
#33 0x00000001000b745a in PyEval_EvalFrameEx ()
#34 0x00000001000b9010 in PyEval_EvalCodeEx ()
#35 0x00000001000b6ffd in PyEval_EvalFrameEx ()
#36 0x00000001000b9010 in PyEval_EvalCodeEx ()
#37 0x00000001000b6ffd in PyEval_EvalFrameEx ()
#38 0x00000001000b9010 in PyEval_EvalCodeEx ()
#39 0x00000001000b6ffd in PyEval_EvalFrameEx ()
#40 0x00000001000b9010 in PyEval_EvalCodeEx ()
#41 0x00000001000b90f6 in PyEval_EvalCode ()
#42 0x00000001000dde0e in PyRun_FileExFlags ()
#43 0x00000001000de0c9 in PyRun_SimpleFileExFlags ()
#44 0x00000001000eb5eb in Py_Main ()
#45 0x0000000100000f14 in start ()
(gdb)
```

### comment:1 Changed 12 years ago by jason

This is puzzling that this works, though:

```sage: V=(SR^2).subspace_with_basis([[1,1]])
sage: v=V([sqrt(2)/2,sqrt(2)/2])
sage: parent(v)
Vector space of degree 2 and dimension 1 over Symbolic Ring
User basis matrix:
[1 1]
sage: QQ(3)*v
(3/2*sqrt(2), 3/2*sqrt(2))
```

So apparently creating the vector manually (rather than getting it as an eigenvector of a matrix) works okay.

### comment:2 Changed 12 years ago by jason

#4834 would probably take care of this effect of the problem, but probably wouldn't fix the actual problem.

### comment:3 Changed 12 years ago by jason

Tracking this down even more:

```sage: V=(QQ^2).span_of_basis([[1,1]])
sage: v=V([1,1])
sage: w=sqrt(2)*v
sage: 3*w

------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occurred in Sage.
This probably occurred because a *compiled* component
of Sage has a bug in it (typically accessing invalid memory)
or is not properly wrapped with _sig_on, _sig_off.
You might want to run Sage under gdb with 'sage -gdb' to debug this.
Sage will now terminate (sorry).
------------------------------------------------------------

```

### comment:4 Changed 12 years ago by jason

Ccing Robert B and Craig Citro, since it looks like it might be a problem in the coercion system, and maybe in how the coercion system builds a module with a user basis:

```sage: V=(QQ^2).span_of_basis([[1,1]])
sage: v=V([1,1])
sage: w=v.change_ring(SR)
sage: w.parent()
Vector space of degree 2 and dimension 1 over Symbolic Ring
User basis matrix:
[1 1]
sage: 3*w
(3, 3)
sage: x=sqrt(2)*v # here the coercion system builds the parent of x
sage: x.parent() is w.parent()
False
sage: x.parent()
Vector space of degree 2 and dimension 1 over Symbolic Ring
User basis matrix:
[1 1]
sage: x.parent() == w.parent() # equivalent parents between x and w
True
sage: 3*x # but we get a segfault when multiplying x by something

------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occurred in Sage.
This probably occurred because a *compiled* component
of Sage has a bug in it (typically accessing invalid memory)
or is not properly wrapped with _sig_on, _sig_off.
You might want to run Sage under gdb with 'sage -gdb' to debug this.
Sage will now terminate (sorry).
------------------------------------------------------------

```

Also, notice that trying to get the coercion system to explain itself also segfaults:

```sage: cm = sage.structure.element.get_coercion_model(); cm
<sage.structure.coerce.CoercionModel_cache_maps object at 0x101f3b6b0>
sage: sage: V=(QQ^2).span_of_basis([[1,1]])
sage: v=V([1,1])
sage: x=sqrt(2)*v
sage: cm.explain(3.parent(), x.parent())

------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occurred in Sage.
This probably occurred because a *compiled* component
of Sage has a bug in it (typically accessing invalid memory)
or is not properly wrapped with _sig_on, _sig_off.
You might want to run Sage under gdb with 'sage -gdb' to debug this.
Sage will now terminate (sorry).
------------------------------------------------------------

```

### comment:5 follow-up:  6 Changed 12 years ago by kcrisman

This works in Sage 4.4.4, and doesn't have anything to do with Pynac, as you now suspect.

```sage: var('y')
y
sage: get_systems('diff(3*y,y)')
['ginac']
```

but in your example I get

```sage: get_systems('3*x')
[]
```

### comment:6 in reply to:  5 Changed 12 years ago by jason

This works in Sage 4.4.4, and doesn't have anything to do with Pynac

Well, yes it does. The backtrace posted above points to the error happening in Ginac.

```(gdb) bt
#0  0x00007fff8294a7a2 in __dynamic_cast ()
#1  0x000000010798ffcc in is_a<GiNaC::relational> [inlined] () at /Users/grout/sage-trees/sage-4.6/local/include/pynac/basic.h:939
```

### comment:7 Changed 12 years ago by kcrisman

Yes, I saw that. So why don't I get that system as being used? Maybe we are now using Ginac, after the 4.6, and shouldn't be, due to coercion? But I have no idea - sorry.

### comment:8 Changed 12 years ago by jason

Okay, thanks to John Palmieri testing this out on various versions of Sage, I found that reverting #9502 fixes the issue. I'm CCing the author and reviewer of #9502. In short, #9502 causes a segfault with the following code:

```sage: V=(QQ^2).span_of_basis([[1,1]])
sage: v=V([1,1])
sage: x=sqrt(2)*v
sage: 3*x # but we get a segfault when multiplying x by something

------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occurred in Sage.
This probably occurred because a *compiled* component
of Sage has a bug in it (typically accessing invalid memory)
or is not properly wrapped with _sig_on, _sig_off.
You might want to run Sage under gdb with 'sage -gdb' to debug this.
Sage will now terminate (sorry).
------------------------------------------------------------
```

Do you guys that did #9502 know what could have caused this?

### comment:9 Changed 12 years ago by jason

Now I'm CCing the author/reviewer of #9502. #9502 is causing a segfault now. Please #10250 (this ticket) for details. Do you have any idea of what is happening to cause the segfault?

### comment:10 Changed 12 years ago by novoselt

I don't, never had issues with segfaults before... I'll look into it, but I am probably not the best person to solve this problem.

### comment:11 Changed 12 years ago by vbraun

The problem seems to be that the coercion ends up calling `__rmul__` (which is inherited) and not `_rmul_` (which is directly defined in `FreeModuleElement_generic_dense`:

```sage: x
(sqrt(2), sqrt(2))
sage: x._rmul_(3)
(3*sqrt(2), 3*sqrt(2))
sage: x.__rmul__(3)
------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occurred in Sage.
```

For the record, stack backtrace:

```#0  __cxxabiv1::__dynamic_cast (src_ptr=0x100000001, src_type=0x7fffd3a02f40,
dst_type=0x7fffd3a090c0, src2dst=0)
at ../../../../libstdc++-v3/libsupc++/dyncast.cc:54
#1  0x00007fffd3224803 in is_a<GiNaC::relational> (__pyx_v_left=0x428c9e0,
__pyx_v_right=0x41b9d70, __pyx_skip_dispatch=<value optimized out>)
at /home/vbraun/Sage/sage/local/include/pynac/basic.h:305
#2  is_a<GiNaC::relational> (__pyx_v_left=0x428c9e0, __pyx_v_right=0x41b9d70,
__pyx_skip_dispatch=<value optimized out>)
at /home/vbraun/Sage/sage/local/include/pynac/ex.h:939
#3  __pyx_f_4sage_8symbolic_10expression_10Expression__mul_ (
__pyx_v_left=0x428c9e0, __pyx_v_right=0x41b9d70,
__pyx_skip_dispatch=<value optimized out>)
at sage/symbolic/expression.cpp:11284
#4  0x00007fffe046a122 in __pyx_f_4sage_7modules_19free_module_element_31FreeModuleElement_generic_dense__rmul_ (__pyx_v_self=0x4289940,
__pyx_v_left=0x428c9e0, __pyx_skip_dispatch=<value optimized out>)
at sage/modules/free_module_element.c:15600
#5  0x00007fffe992212c in __pyx_f_4sage_9structure_14coerce_actions_16LeftModuleAction__call_ (__pyx_v_self=0x72c410, __pyx_v_g=0x428c9e0,
__pyx_v_a=0x4289940, __pyx_skip_dispatch=<value optimized out>)
at sage/structure/coerce_actions.c:5437
#6  0x00007fffe9d4e9a1 in __pyx_pf_4sage_10categories_6action_6Action_act (
__pyx_v_self=0x72c410, __pyx_args=0x412ce18, __pyx_kwds=0x0)
at sage/categories/action.c:2563
#7  0x00007ffff7c7e3b3 in PyObject_Call (func=0x737098,
arg=<value optimized out>, kw=<value optimized out>)
at Objects/abstract.c:2492
#8  0x00007fffe9927764 in __pyx_pf_4sage_9structure_14coerce_actions_12ModuleAction___init__ (__pyx_v_self=0x72c410, __pyx_args=<value optimized out>,
__pyx_kwds=<value optimized out>) at sage/structure/coerce_actions.c:5133
#9  0x00007ffff7cd9f18 in type_call (type=<value optimized out>,
args=0x412d5f0, kwds=0x0) at Objects/typeobject.c:747
#10 0x00007ffff7c7e3b3 in PyObject_Call (func=0x7fffe9b33de0,
arg=<value optimized out>, kw=<value optimized out>)
at Objects/abstract.c:2492
#11 0x00007fffe992a52c in __pyx_pf_4sage_9structure_14coerce_actions_detect_element_action (__pyx_self=<value optimized out>,
__pyx_args=<value optimized out>, __pyx_kwds=<value optimized out>)
at sage/structure/coerce_actions.c:4184
#12 0x00007ffff7c7e3b3 in PyObject_Call (func=0x1042d40,
arg=<value optimized out>, kw=<value optimized out>)
at Objects/abstract.c:2492
#13 0x00007fffeae9d34f in __pyx_f_4sage_9structure_6parent_6Parent_discover_action (__pyx_v_self=0x42d27f0, __pyx_v_S=0x121e8e0, __pyx_v_op=0x1042d40,
__pyx_v_self_on_left=0) at sage/structure/parent.c:14710
#14 0x00007fffeb0c5ecd in __pyx_f_4sage_9structure_10parent_old_6Parent_get_acti---Type <return> to continue, or q <return> to quit---
on_c_impl (__pyx_v_self=0x42d27f0, __pyx_v_S=<value optimized out>,
__pyx_v_op=<value optimized out>,
__pyx_v_self_on_left=<value optimized out>)
at sage/structure/parent_old.c:3254
#15 0x00007fffeb0c7441 in __pyx_pf_4sage_9structure_10parent_old_6Parent_get_action_impl (__pyx_v_self=0x42d27f0, __pyx_args=<value optimized out>,
__pyx_kwds=<value optimized out>) at sage/structure/parent_old.c:3204
#16 0x00007ffff7c7e3b3 in PyObject_Call (func=0x41abfc8,
arg=<value optimized out>, kw=<value optimized out>)
at Objects/abstract.c:2492
#17 0x00007fffeb0cdeb2 in __pyx_f_4sage_9structure_10parent_old_6Parent_get_action_c (__pyx_v_self=0x42d27f0, __pyx_v_S=0x121e8e0, __pyx_v_op=0x78a1b8,
__pyx_v_self_on_left=0, __pyx_skip_dispatch=<value optimized out>)
at sage/structure/parent_old.c:2892
#18 0x00007fffeb0c16c3 in __pyx_f_4sage_9structure_10parent_old_6Parent__get_action_ (__pyx_v_self=0x42d27f0, __pyx_v_other=0x121e8e0,
__pyx_v_op=<value optimized out>,
__pyx_v_self_on_left=<value optimized out>,
__pyx_skip_dispatch=<value optimized out>)
at sage/structure/parent_old.c:6069
#19 0x00007fffeae9ab71 in __pyx_f_4sage_9structure_6parent_6Parent_get_action (
__pyx_v_self=0x42d27f0, __pyx_v_S=0x121e8e0,
__pyx_skip_dispatch=<value optimized out>,
__pyx_optional_args=<value optimized out>) at sage/structure/parent.c:13731
#20 0x00007fffea5bf810 in __pyx_f_4sage_9structure_6coerce_24CoercionModel_cache_maps_discover_action (__pyx_v_self=0x1043350, __pyx_v_R=0x121e8e0,
__pyx_v_S=0x42d27f0, __pyx_v_op=0x78a1b8,
__pyx_skip_dispatch=<value optimized out>) at sage/structure/coerce.c:11909
#21 0x00007fffea5b2aca in __pyx_f_4sage_9structure_6coerce_24CoercionModel_cache_maps_get_action (__pyx_v_self=0x1043350, __pyx_v_R=0x121e8e0,
__pyx_v_S=0x42d27f0, __pyx_v_op=0x78a1b8,
__pyx_skip_dispatch=<value optimized out>) at sage/structure/coerce.c:10837
#22 0x00007fffea5b5417 in __pyx_f_4sage_9structure_6coerce_24CoercionModel_cache_maps_bin_op (__pyx_v_self=0x1043350, __pyx_v_x=0x3296bd0,
__pyx_v_y=0x4289c00, __pyx_v_op=0x78a1b8,
__pyx_skip_dispatch=<value optimized out>) at sage/structure/coerce.c:6080
#23 0x00007fffea7f8c8d in __pyx_pf_4sage_9structure_7element_11RingElement___mul__ (__pyx_v_left=0x3296bd0, __pyx_v_right=0x4289c00)
at sage/structure/element.c:11399
#24 0x00007ffff7c7998a in binary_op1 (v=0x3296bd0, w=0x4289c00, op_slot=16)
at Objects/abstract.c:917
#25 0x00007ffff7c7c788 in PyNumber_Multiply (v=0x3296bd0, w=0x4289c00)
at Objects/abstract.c:1188
#26 0x00007ffff7
throwflag=<value optimized out>) at Python/ceval.c:1118
#27 0x00007ffff7d2072c in PyEval_EvalCodeEx (co=0x6fabe8,
globals=<value optimized out>, locals=<value optimized out>,
args=<value optimized out>, argcount=0, kws=<value optimized out>,
kwcount=0, defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:2968
#28 0x00007ffff7d20802 in PyEval_EvalCode (co=<value optimized out>,
globals=<value optimized out>, locals=<value optimized out>)
at Python/ceval.c:522
#29 0x00007ffff7d1f162 in exec_statement (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:4401
#30 PyEval_EvalFrameEx (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:1717
#31 0x00007ffff7d2072c in PyEval_EvalCodeEx (co=0xb13c60,
globals=<value optimized out>, locals=<value optimized out>,
args=<value optimized out>, argcount=2, kws=<value optimized out>,
kwcount=0, defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:2968
#32 0x00007ffff7d1e8de in fast_function (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:3802
#33 call_function (f=<value optimized out>, throwflag=<value optimized out>)
at Python/ceval.c:3727
#34 PyEval_EvalFrameEx (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:2389
#35 0x00007ffff7d2072c in PyEval_EvalCodeEx (co=0xb13af8,
globals=<value optimized out>, locals=<value optimized out>,
args=<value optimized out>, argcount=3, kws=<value optimized out>,
kwcount=0, defs=0xb730f8, defcount=2, closure=0x0) at Python/ceval.c:2968
#36 0x00007ffff7d1e8de in fast_function (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:3802
#37 call_function (f=<value optimized out>, throwflag=<value optimized out>)
at Python/ceval.c:3727
#38 PyEval_EvalFrameEx (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:2389
#39 0x00007ffff7d1fa7f in fast_function (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:3792
#40 call_function (f=<value optimized out>, throwflag=<value optimized out>)
at Python/ceval.c:3727
#41 PyEval_EvalFrameEx (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:2389
#42 0x00007ffff7d2072c in PyEval_EvalCodeEx (co=0xb13300,
globals=<value optimized out>, locals=<value optimized out>,
args=<value optimized out>, argcount=2, kws=<value optimized out>,
kwcount=0, defs=0xb6c868, defcount=1, closure=0x0) at Python/ceval.c:2968
#43 0x00007ffff7d1e8de in fast_function (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:3802
#44 call_function (f=<value optimized out>, throwflag=<value optimized out>)
at Python/ceval.c:3727
#45 PyEval_EvalFrameEx (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:2389
#46 0x00007ffff7d2072c in PyEval_EvalCodeEx (co=0xb10cd8,
globals=<value optimized out>, locals=<value optimized out>,
args=<value optimized out>, argcount=2, kws=<value optimized out>,
kwcount=0, defs=0xb6c828, defcount=1, closure=0x0) at Python/ceval.c:2968
#47 0x00007ffff7d1e8de in fast_function (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:3802
#48 call_function (f=<value optimized out>, throwflag=<value optimized out>)
at Python/ceval.c:3727
#49 PyEval_EvalFrameEx (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:2389
#50 0x00007ffff7d2072c in PyEval_EvalCodeEx (co=0x931f30,
globals=<value optimized out>, locals=<value optimized out>,
args=<value optimized out>, argcount=1, kws=<value optimized out>,
kwcount=2, defs=0x93d8d8, defcount=2, closure=0x0) at Python/ceval.c:2968
#51 0x00007ffff7d1e8de in fast_function (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:3802
#52 call_function (f=<value optimized out>, throwflag=<value optimized out>)
at Python/ceval.c:3727
#53 PyEval_EvalFrameEx (f=<value optimized out>,
throwflag=<value optimized out>) at Python/ceval.c:2389
#54 0x00007ffff7d2072c in PyEval_EvalCodeEx (co=0x6da378,
globals=<value optimized out>, locals=<value optimized out>,
args=<value optimized out>, argcount=0, kws=<value optimized out>,
kwcount=0, defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:2968
#55 0x00007ffff7d20802 in PyEval_EvalCode (co=<value optimized out>,
globals=<value optimized out>, locals=<value optimized out>)
at Python/ceval.c:522
#56 0x00007ffff7d42600 in run_mod (fp=0x625830,
filename=<value optimized out>, start=<value optimized out>,
globals=0x63c080, locals=0x63c080, closeit=0, flags=0x7fffffffd8b0)
at Python/pythonrun.c:1335
#57 PyRun_FileExFlags (fp=0x625830, filename=<value optimized out>,
start=<value optimized out>, globals=0x63c080, locals=0x63c080, closeit=0,
flags=0x7fffffffd8b0) at Python/pythonrun.c:1321
#58 0x00007ffff7d430cf in PyRun_SimpleFileExFlags (fp=0x625830,
filename=0x7fffffffeac3 "/home/vbraun/Sage/sage/local/bin/sage-ipython",
closeit=0, flags=0x7fffffffd8b0) at Python/pythonrun.c:931
#59 0x00007ffff7d5205b in RunStartupFile (argc=<value optimized out>,
argv=0x7fffffffd9d8) at Modules/main.c:142
#60 Py_Main (argc=<value optimized out>, argv=0x7fffffffd9d8)
at Modules/main.c:558
#61 0x000000381a81ee7d in __libc_start_main (main=0x400710 <main>, argc=2,
ubp_av
fini=<value optimized out>, rtld_fini=<value optimized out>,
stack_end=0x7fffffffd9c8) at libc-start.c:226
#62 0x0000000000400649 in _start ()
```

### comment:12 Changed 12 years ago by jason

No, it's deeper than that. rmul should call the right coercion stuff so that _rmul_ is eventually called. So far, I've traced it down to this:

```sage: p=x.parent()
sage: p.get_action(3.parent(), operator.mul,False)

------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occurred in Sage.
This probably occurred because a *compiled* component
of Sage has a bug in it (typically accessing invalid memory)
or is not properly wrapped with _sig_on, _sig_off.
You might want to run Sage under gdb with 'sage -gdb' to debug this.
Sage will now terminate (sorry).
------------------------------------------------------------

```

### comment:13 Changed 12 years ago by vbraun

Actually, I think something is wrong with the parent:

```sage: x.parent().an_element()
(1, 1)
sage: x.parent().an_element().__mul__(3)
(0/0, 0/0)
```

### comment:14 Changed 12 years ago by jason

I think that the appropriate action is not getting registered (which is a problem with the parent), leading to all sorts of weirdness as the coercion system tries to do things. It can't register the action because when it asks for the action (using the get_action method of the parent), it segfaults.

```sage: v.parent().get_action(3.parent(), operator.mul, False)
Left scalar multiplication by Integer Ring on Vector space of degree 2 and dimension 1 over Rational Field
User basis matrix:
[1 1]
sage: x.parent().get_action(3.parent(), operator.mul, False)

------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occurred in Sage.
This probably occurred because a *compiled* component
of Sage has a bug in it (typically accessing invalid memory)
or is not properly wrapped with _sig_on, _sig_off.
You might want to run Sage under gdb with 'sage -gdb' to debug this.
Sage will now terminate (sorry).
------------------------------------------------------------

```

### comment:15 Changed 12 years ago by novoselt

Carefully looking at my patch at #9502, I found that I skipped one conversion. I posted a patch that restores it and gives

```sage: sage: V=(QQ^2).span_of_basis([[1,1]])
sage: sage: v=V([1,1])
sage: sage: w=sqrt(2)*v
sage: sage: 3*w
(3*sqrt(2), 3*sqrt(2))
```

But I don't yet understand why the single conversion is not good enough.

### comment:16 Changed 12 years ago by jason

Yeah, I just found that if you add

```        if check:
V = ambient.vector_space()
basis = [V(x) for x in basis]
```

right before the if check block, things work. Notice that V is ambient.vector_space() in this case, rather than ambient.ambient_vector_space(). I bet that's the difference.

### comment:17 follow-up:  18 Changed 12 years ago by jason

Status: new → needs_work

We should also add a doctest to the patch.

### comment:18 in reply to:  17 Changed 12 years ago by novoselt

We should also add a doctest to the patch.

I am not sure that this is a satisfactory solution - why converting x into the ambient vector space is not the same as converting it into the spanned vector space and then into the ambient vector space?

### comment:19 Changed 12 years ago by jason

I think I see the problem. Here is the code in question:

```        if check:
V = ambient.ambient_vector_space()
for i in range(len(basis)):
x = basis[i]
if x not in V:
try:
basis[i] = V(x)
except TypeError:
raise TypeError("each element of basis must be in "
"the ambient vector space")
```

Notice that a basis element is only converted to V if it's not already in V. The problem is that the test "x in V" may succeed, even if x is not a vector in V. In this case, when you try to construct a basis vector for SR2, you're handed the vector (1,1) as a rational vector. The test "x not in V" fails, since the rational vector (1,1) *is* in V. So it is put in as a basis vector. You need the initial conversion to first convert vectors to the same type of vectors as live in V, because you need those basis elements to actually be vectors with parent SR2.

I.e., we are handed a basis vector in QQ2, and we fail to convert it to a vector in SR2 because the if statement fails:

```sage: (QQ^2)([1,1]) not in (SR^2)
False
```

The initial conversion using ambient.ambient_vector_space is to ensure that the vectors are actually vectors in SR2 (in this case).

### comment:20 Changed 12 years ago by novoselt

Ah, now I see. Thank you for the clarification, I'll write a proper patch and post it shortly.

### Changed 12 years ago by novoselt

Apply this patch only.

### comment:21 Changed 12 years ago by novoselt

Authors: → Andrey Novoseltsev major → critical needs_work → needs_review

Instead of adding previously removed code I removed some more, now it works and is easier to understand ;-) Didn't run full library tests yet but will do it now and report if there are any issues.

### comment:22 Changed 12 years ago by jason

Reviewers: → Jason Grout

I think the new patch looks correct. Thanks for your work on this!

### comment:23 Changed 12 years ago by jason

Milestone: → sage-4.6.1

Tests pass in the modules/ directory. So if tests pass on the full test suite, please set this to positive review.

Thanks again!

### comment:24 Changed 12 years ago by novoselt

There will be some delay, as sage.math gets out of memory (not because of this patch ;-))

### comment:25 Changed 12 years ago by novoselt

Status: needs_review → positive_review

OK, all long tests pass on mod.math, so I am switching it to "positive review."

### comment:26 Changed 12 years ago by jason

Authors: Andrey Novoseltsev → Andrey Novoseltsev, Jason Grout Jason Grout → Jason Grout, Andrey Novoseltsev

I think we both came up with the solution independently and worked with each other on authoring and checking each other on this, so I'm putting both of us as authors and reviewers (with Andrey as the primary author and me as the primary reviewer).

### comment:27 Changed 12 years ago by jdemeyer

Merged in: → sage-4.6.1.alpha2 → fixed positive_review → closed
Note: See TracTickets for help on using tickets.