ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 14 Dec 2011 04:30:11 +0100How to multiply vector by numberhttps://ask.sagemath.org/question/8539/how-to-multiply-vector-by-number/I have vector
W = [1, 8, 4, 7, 10, 1, 6, 3]
I need to multiply it by number, but command 2*W gives just concatenated vector by itself
[1, 8, 4, 7, 10, 1, 6, 3, 1, 8, 4, 7, 10, 1, 6, 3]
I dont need it, I need multiply all elements of vector to numberWed, 14 Dec 2011 03:25:23 +0100https://ask.sagemath.org/question/8539/how-to-multiply-vector-by-number/Answer by Laurent Claessens for <p>I have vector</p>
<pre><code>W = [1, 8, 4, 7, 10, 1, 6, 3]
</code></pre>
<p>I need to multiply it by number, but command 2*W gives just concatenated vector by itself</p>
<pre><code>[1, 8, 4, 7, 10, 1, 6, 3, 1, 8, 4, 7, 10, 1, 6, 3]
</code></pre>
<p>I dont need it, I need multiply all elements of vector to number</p>
https://ask.sagemath.org/question/8539/how-to-multiply-vector-by-number/?answer=13005#post-id-13005Your W is not a vector but a list. If you want a vector you have to do the following :
sage: v=vector([1,2])
sage: v
(1, 2)
sage: 3*v
(3, 6)
sage:
sage: v.column()
[1]
[2]
LaurentWed, 14 Dec 2011 04:18:24 +0100https://ask.sagemath.org/question/8539/how-to-multiply-vector-by-number/?answer=13005#post-id-13005Answer by Simon for <p>I have vector</p>
<pre><code>W = [1, 8, 4, 7, 10, 1, 6, 3]
</code></pre>
<p>I need to multiply it by number, but command 2*W gives just concatenated vector by itself</p>
<pre><code>[1, 8, 4, 7, 10, 1, 6, 3, 1, 8, 4, 7, 10, 1, 6, 3]
</code></pre>
<p>I dont need it, I need multiply all elements of vector to number</p>
https://ask.sagemath.org/question/8539/how-to-multiply-vector-by-number/?answer=13006#post-id-13006This behaviour is part of Python (the following code is evaluated in the standard python shell, although it should also work in sage or a sage notebook):
>>> W = [1, 8, 4, 7, 10, 1, 6, 3]
>>> 2*W
[1, 8, 4, 7, 10, 1, 6, 3, 1, 8, 4, 7, 10, 1, 6, 3]
The standard thing to do is to either use list comprehension
>>> [3*i for i in W]
[3, 24, 12, 21, 30, 3, 18, 9]
or map (for a comparison, see http://stackoverflow.com/q/1247486/421225)
>>> map(lambda i: 3*i, W)
[3, 24, 12, 21, 30, 3, 18, 9]
----
Although, if you're using just numerical lists and are used to matlab, then maybe you should use [numpy](http://numpy.scipy.org/):
>>> import numpy
>>> npW = numpy.array(W)
>>> npW*3
array([ 3, 24, 12, 21, 30, 3, 18, 9])
For large arrays, NumPy will be faster at this type of operation than the pythonic methods above. For a discussion of NumPy in sage see http://www.sagemath.org/doc/numerical_sage/numpy.html
---
Of course, the standard sage approach is to use a vector object (see [Laurent's answer](http://ask.sagemath.org/question/982/how-to-multiply-vector-by-number?answer=1583#1583))
Wed, 14 Dec 2011 04:30:11 +0100https://ask.sagemath.org/question/8539/how-to-multiply-vector-by-number/?answer=13006#post-id-13006