2

Before I ask my question, I provide you with the code:

from scipy import *
x = randn(10)
cum_x = cumsum(x)
# The objective is to recover x using cum_x and the diff function.
y = append(cum_x[0], diff(cum_x))
# Now, y should be equal to x but this is not confirmed by the function in1d
test = in1d(x,y)

The variable test does not return an array of "True" boolean values even if y and x are clearly the same. What is the problem here?

4
  • 3
    I imagine there are floating point issues, allclose might be what you want Commented Jan 3, 2016 at 15:26
  • It is. But why in1d does not return an array of True values? Why rounding issues? Do you think the cumulative sum and the difference produce slightly different floats? Commented Jan 3, 2016 at 15:28
  • I added an answer showing increased precision, you can see there are slight differences Commented Jan 3, 2016 at 15:31
  • 2
    Aside: even if x and y weren't clearly different, in1d isn't a good way to check to see if they're equal: -- in1d([1,2],[2,1]) gives all true, after all. Commented Jan 3, 2016 at 15:31

1 Answer 1

3

if you use set_printoptions to increase precision you will see some differences:

import numpy as np
from scipy import *
np.set_printoptions(30)
x = randn(10)
cum_x = cumsum(x)

#The objective is to recover x using cum_x and the diff function.
y = append(cum_x[0], diff(cum_x))
print(x)
print("\n")
print(y)
#Now, y should be equal to x but this is not confirmed by the function in1d
test = in1d(x, y)
print(test)

Output:

[ 0.54816314147543721002620031868   0.14319052613251953554041051575
  0.489110961092741158839913850898 -0.093011827554544138085823590245
 -0.58370623188476589149331630324  -0.40395493550429123486011917521
  0.387387395892057895263604905267  1.001637373359834937147638811439
 -1.486778459872974744726548124163  1.446772274227251076084144187917]


[ 0.54816314147543721002620031868   0.143190526132519591051561747008
  0.48911096109274110332876261964  -0.093011827554544179719187013688
 -0.58370623188476589149331630324  -0.40395493550429123486011917521
  0.387387395892057895263604905267  1.001637373359834937147638811439
 -1.486778459872974744726548124163  1.446772274227251076084144187917]
[ True False False False  True  True  True  True  True  True]

What you probably want is allclose but interestingly setting the dtype to np.float128 or np.longdouble on my ubuntu system does not lose precision and in1d returns True.

 cum_x = cumsum(x,dtype=np.longdouble)
Sign up to request clarification or add additional context in comments.

3 Comments

So do you think the cumulative sum and the difference functions produce these slight differences?
@Charlie, if you look at the output they definitely do, what you want to be using is allclose.
set_printoptions() is in numpy package, not scipy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.