[Tutor] numerical problem

Danny Yoo dyoo at hashcollision.org
Tue Mar 4 03:01:28 CET 2014


> I'm doing a sum in a for loop:
>
> www is the quantity I add.
>
> MYMAP[i, j, k] = MYMAP[i, j, k] + www
>
> MYMAP is a numpy array
>
> I have strong reasons to think that in this operation happens some numerical
> error...Have you suggestions to discover where it is?


Hi Gabriele,


Can you explain those reasons why you suspect that this operation is
responsible?  Otherwise, we can not give you as good suggestions as
we'd like.

If you have literal error messages, that would also be helpful to
present those to us.

Basically, if you can provide a code snippet of what you're doing,
then we can independently try to reproduce the error you're seeing.
The question above omits too much details because it prevents folks
from knowing that they are answering the right question.


Here is an example of a program that adds a delta to every element in
a 3d numpy array.

######################################
>>> import numpy as np
>>> x = np.array([[[ 0,  1,  2],
...         [ 3,  4,  5],
...         [ 6,  7,  8]],
...        [[ 9, 10, 11],
...         [12, 13, 14],
...         [15, 16, 17]],
...        [[18, 19, 20],
...         [21, 22, 23],
...         [24, 25, 26]]])
>>> x + 42
array([[[42, 43, 44],
        [45, 46, 47],
        [48, 49, 50]],

       [[51, 52, 53],
        [54, 55, 56],
        [57, 58, 59]],

       [[60, 61, 62],
        [63, 64, 65],
        [66, 67, 68]]])
######################################

Note: adding a scalar to the numpy array has the addition apply
pointwise to every component of the array.  This is the direct and
natural approach.  When you mention "loop" in your question above,
this makes me pause.


If you are doing an explicit loop, as discussed in:

     http://docs.scipy.org/doc/numpy/reference/arrays.nditer.html

then it would be very helpful to see the code you are doing.


More information about the Tutor mailing list