[Numpy-discussion] How-to: Uniform vector scale

Robert Kern robert.kern@gmail....
Wed Sep 5 14:18:24 CDT 2007


Robert Dailey wrote:
> Hi,
> 
> I have a scalar value S. I want to perform the following math on vectors
> A and B (both of type array):
> 
> A + B * S
> 
> By order of operations, B * S should be done first. This is a vector
> multiplied by a real number and should be valid. However, the
> interpreter outputs:
>     "ValueError: shape mismatch: objects cannot be broadcast to a single
> shape"

Most likely, the + is failing, not the multiplication. However, I can't tell
from your description. It is much more helpful for us to help you if you give us
a self-contained, small example that demonstrates the problem along with the
output (copy-and-paste, no summaries) and the output that you expected.

> I am not sure how I am supposed to multiply a vector with a scalar
> value. For example:
> 
> array([5,2]) * 2 = [10,4]
> 
> The above should happen. However, I get the error message instead. Any
> ideas? Thanks.

In [3]: from numpy import *

In [4]: array([5,2]) * 2
Out[4]: array([10,  4])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list