简体   繁体   中英

Condition not satisfying in python

I am having the following problem. Thought the differences are less than 0.0001 the condition is not getting satisfying. I am facing a problem with abs(beta - beta1).any() condition.

    alpha = -29.18835001947976
    prev_alpha = -29.188337321421681
    beta = np.matrix([[-0.26220145],[ 8.37991712]])
    beta1 = np.matrix([[-0.26220149],[ 8.37991514]])
    print(alpha - prev_alpha)
    print (beta - beta1)
    epsilon = 0.0001
    if ((abs(alpha - prev_alpha) <= epsilon) & (abs(beta - beta1).any() <= epsilon)):
        print 'x'


    print (-1.26980580788e-05 <= 0.001)
True
    print (abs(beta - beta1).all() <0.001)
False

Since the condition is not satisfying, the condition is not working. I want to know what can be done to make the 'x' print. I want to get into the condition if all the values in the matrix are almost the same as previous one. If it same, 'x' needs to be printed. I have taken 0.0001 as condition to determine it is almost same. Can anybody help me with this.

Update :

    print (any(beta - beta1) <0.001)
False

print(alpha - prev_alpha)
print (beta - beta1)

-1.26980580788e-05
[[  4.00000000e-08]
 [  1.98000000e-06]]

abs(beta - beta1).any() is a boolean. If it is True, it is the same as 1, which is always bigger than epsilon.

What you want is probably more like any(difference < epsilon)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM