Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix scalar multiplication

Tags:

python

Is there a more 'mathematical' way to do the following:

1.2738 * (list_of_items)

So for what I'm doing is:

[1.2738 * item for item in list_of_items]
like image 537
David542 Avatar asked Mar 02 '15 23:03

David542


People also ask

What happens when a matrix is multiplied by scalar?

When performing a multiplication of a matrix by a scalar, the resulting matrix will always have the same dimensions as the original matrix in the multiplication. For example, if we multiply c ⋅X the matrix that results from it has the dimensions of X.

What are the properties of scalar multiplication?

The scalar product is commutative. The two manually perpendicular vectors of a scalar product are zero. The two parallel and vectors of a scalar product are equal to the product of their magnitudes. The square of its magnitude is equal to the Self-product of a vector.

What is meant by scalar multiplication?

Scalar multiplication is the multiplication of a vector by a scalar (where the product is a vector), and is to be distinguished from inner product of two vectors (where the product is a scalar).


2 Answers

The mathematical equivalent of what you're describing is the operation of multiplication by a scalar for a vector. Thus, my suggestion would be to convert your list of elements into a "vector" and then multiply that by the scalar.

A standard way of doing that would be using numpy.

Instead of

1.2738 * (list_of_items)

You can use

import numpy
1.2738 * numpy.array(list_of_items)

Sample Output:

In [8]: list_of_items
Out[8]: [1, 2, 4, 5]

In [9]: import numpy

In [10]: 1.2738 * numpy.array(list_of_items)
Out[10]: array([ 1.2738,  2.5476,  5.0952,  6.369 ])
like image 138
Nitish Avatar answered Oct 16 '22 20:10

Nitish


Another approach

map(lambda x:x*1.2738,list_of_items)
like image 2
levi Avatar answered Oct 16 '22 22:10

levi