converting between lists and vectors?

1,883 views
Skip to first unread message

Stan Schymanski

unread,
Aug 28, 2008, 6:03:01 AM8/28/08
to sage-support
Dear all,

I use pylab for plotting, which requires creating lists of x and y
values. Now, in order to manipulate the lists, I would like to perform
simple operations on them, like for example multiply by 2. However,
lists seem to behave very differently to vectors when performing
mathematical operations on them (see below). Is there an easy way of
converting between lists and vectors to use them in mathematical
computations without having to loop through all elements? Intuitively,
I thought that vector(list) would work to convert a list to a vector,
but it does not (see below).

Thanks already for your help, and I'm sorry if I missed this issue in
the documentation.

Stan

----------------------------------------------------------------------
| SAGE Version 3.1.1, Release Date: 2008-08-17 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------

sage: v = vector([1,2,3]); v
(1, 2, 3)
sage: 2*v
(2, 4, 6)
sage: w=range(1,4); w
[1, 2, 3]
sage: 2*w
[1, 2, 3, 1, 2, 3]
sage: w1=vector(w)
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)

/Users/sschym/modelling/VegPat/<ipython console> in <module>()

/Users/sschym/modelling/VegPat/free_module_element.pyx in
sage.modules.free_module_element.vector (sage/modules/
free_module_element.c:2376)()

/Users/sschym/modelling/VegPat/free_module_element.pyx in
sage.modules.free_module_element.prepare (sage/modules/
free_module_element.c:2622)()

TypeError: unable to find a common ring for all elements

John Cremona

unread,
Aug 28, 2008, 6:35:43 AM8/28/08
to sage-s...@googlegroups.com, sage-...@googlegroups.com
There are two separate things going on here.

I think this is a bug:

sage: vector(range(3))


TypeError: unable to find a common ring for all elements

since this does work:
sage: vector(srange(3))
(0, 1, 2)

The difference is that the elements of range(3) are python ints while
the elements of srange(3) are Sage Integers.

It also works if you specify the coordinate ring:

sage: vector(RR,range(3))
(0.000000000000000, 1.00000000000000, 2.00000000000000)
sage: vector(ZZ,range(3))
(0, 1, 2)

The problem lies in the function prepare() in
sage/modules/free_module_element.pyx, since Sequence(range(3)) has
universe <type 'int'> and is_Ring() fails on that.

The fix would therefore appear to be to test for the case where the
universe is <type 'int'>
and change it to ZZ.

The second issue is just that after w=range(3), w is a python list,
and for lists, multiplying by 2 just repeats the list. Similarly
sage: range(3) + range(3)
[0, 1, 2, 0, 1, 2]

If you want to do mathematical operations such as scalar
multiplcation, convert to a vector.
(You could also do [2*i for i in range(3)], but I don't think you like
that construction.)

I have CC's this to sage-devel so that the bug will be fixed (if
people agree with me that i is a bug).

John Cremona

2008/8/28 Stan Schymanski <schy...@gmail.com>:

AlexGhitza

unread,
Sep 8, 2008, 6:50:35 AM9/8/08
to sage-support
This is related to http://trac.sagemath.org/sage_trac/ticket/3847

I have just put up a patch that fixes this issue.

Best,
Alex
Reply all
Reply to author
Forward
0 new messages