python - Error in the results of a list comprehension -
i have issue list comprehensions.
import numpy import random diam=1.5 p=1 a=10 d=0.2 h=0.15 lx = list(numpy.arange(-diam/2,diam/2+0.05,0.05)) loop in range(50): f=random.uniform(0,p/2) l = [k k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)<-d/2-h/np.tan(np.pi/2-a))] + [k k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a))>(d/2)]
but when @ results, there seems errors. a=10
, last value of f
,
l = [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
then if type this
[k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a) k in list(np.arange(-diam/2,diam/2+0.05,0.05))]
i this
[-0.72926117723233752, -0.68565303028301938, -0.64160406588192997, -0.59711428402906952, -0.54781631527556185, -0.49318773203196459, -0.43899996624013848, -0.38525301790008354, -0.33194688701179981, -0.27908157357528718, -0.22665707759054576, -0.17467339905757551, -0.12313053797637638, -0.072028494346948441, -0.021367268169291655, 0.028853140556593968, 0.07863273183070843, 0.12797150565305171, 0.17686946202362386, 0.22532660094242482, 0.27334292240945463, 0.32091842642471324, 0.36805311298820076, 0.41474698209991706, 0.4610000337598622, 0.50681226796803625, 0.55218368472443902, 0.60288571597093177, 0.65839593411807129, 0.71434696971698208, 0.77073882276766403]
(i'll refer mylist
)
and know -d/2-h/np.tan(np.pi/2-a) = -0.12644904710626975
, d/2 = 0.1
then if @ values in mylist
, notice 12 first values inferior -0.12644904710626975
. however, in l
, there 11 consecutive values k
(-0.15
should in l
too).
i don't understand why -0.15
isn't in list l
.
edit : here's sample
l = [-0.75, -0.7 , -0.65, -0.6 , -0.55, -0.5 , -0.45, -0.4 , -0.35, -0.3 , -0.25, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 , 0.45, 0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75]
mylist = [-0.73693754824886837, -0.68054569519818642, -0.62459465959927574, -0.5690844414521361, -0.51401504075676774, -0.45938645751317042, -0.40519869172134437, -0.35145174338128943, -0.29814561249300564, -0.24528029905649307, -0.19285580307175165, -0.14087212453878137, -0.089329263457582256, -0.038227219828154309, 0.012434006349502473, 0.06265441507538809, 0.11243400634950255, 0.16177278017184585, 0.21067073654241797, 0.25912787546121896, 0.30714419692824874, 0.35471970094350741, 0.40185438750699487, 0.44854825661871117, 0.49480130827865632, 0.54061354248683036, 0.58598495924323313, 0.63091555854786485, 0.67540534040072531, 0.7194543048018146, 0.76306245175113285]
the 12 first values of mylist
inferior -0.12644904710626975
, in l
, there 11 consecutive values : -0.2
should in l
too.
(verification : k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)
k = -0.2
, , -0.1408721245387819
inferior -0.12644904710626975
)
well way checked - seems good. provide different sample, i'd happy it.
import numpy np import random diam = 1.5 p = 1 = 10 d = 0.2 h = 0.15 lx = list(np.arange(-diam/2, diam/2 + 0.05, 0.05)) loop in range(50): f = random.uniform(0, p/2) a1 = [k k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)<-d/2-h/np.tan(np.pi/2-a))] + [k k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a))>(d/2)] a2 = [k k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)<-d/2-h/np.tan(np.pi/2-a))] a3 = [k k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a))>(d/2)] a4 = [k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a) k in list(np.arange(-diam/2,diam/2+0.05,0.05))] print "lx", lx print "a1", a1 print "a2", a2 print "a3", a3 print "a4", a4 # however... print -d/2-h/np.tan(np.pi/2-a) print k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a) print -0.19999999999999951 < -d/2-h/np.tan(np.pi/2-a) print -0.14999999999999947 < -d/2-h/np.tan(np.pi/2-a) # edit2 => addition, see explanation below. print "edit 2 results step step equation comment below answer" print "===========================================" x1 = (1/(2*p)) print "x1 = ", x1 x2 = (0.05**2) print "x2 = ", x2 print "abs(f-x1*x2) = ", abs(f-x1*x2) print "np.tan(a) = ", np.tan(a) print "abs(f-x1*x2) * np.tan(a) = ", abs(f-x1*x2) * np.tan(a) print "abs(f-x1*x2) * np.tan(a) + 0.05 = ", 0.05 + abs(f-x1*x2) * np.tan(a) print "==========================================="
output
lx [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, -0.14999999999999947, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133] a1 [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133] a2 [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951] a3 [0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133] a4 [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, -0.14999999999999947, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133] -0.197254124119 0.75 true false edit 2 results step step equation comment below answer =========================================== x1 = 0 x2 = 0.0025 abs(f-x1*x2) = 0.0 np.tan(a) = 0.648360827459 abs(f-x1*x2) * np.tan(a) = 0.0 abs(f-x1*x2) * np.tan(a) + 0.05 = 0.05 ===========================================
edit, in response @jack's comment , post edit.
@jack - sample provided in edit still unclear. first: a4 , lx values same, feel free copy post new script , test yourself. sure values in post correct? refer 0.12644904710626975, while example states that:
-d/2-h/np.tan(np.pi/2-a) != -0.12644904710626975 -d/2-h/np.tan(np.pi/2-a) == -0.197254124119
perhaps changed d/h/a somewhere else in program.
how assign variables in place of such long constructs these, hard read , may cause errors.
don't know, parameters used generate values in edit.
perhaps write trying achieve, might easier fix problem way.
edit2:
in comments below wrote:
a4 , lx not same, tried , lx = [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, (...)
whereas a4 = [-0.67386015491279805, -0.64736323490818992, -0.61924541283493406, -0.58950668869303047, -0.54185293751752051, -0.47483346579671942, (...)
value seems -0.197254124119. sorry, mixed different values... anyway, d/2 = 0.1, can see last 15 values of mylist in edit example superior 0.1, there 14 consecutive values last values of l
, 0.05+(abs(f-(1/(2*p))*(0.05**2)))*np.tan(a) = 0.15540118660134225 > 0.1. why 0.05 isn't in list l ?
have copied above script , tried on computer, written? have added above few rows starting , ending "==========", demonstrate step step results i'm getting calculations. please note diam, p, a, d, h not changed anywhere. below i've written cause - why happening , can avoid such behavior.
summary
now see has happened. please observe value x1 has got. have x1 == 0 whereas (as human beings) see 1/(2*1) 0.5.
know why happening, in case don't, computers dont count - called integer division in python2 (it has changed in python3, 2/2 = 1.0
), in python2 "normal" division operator "/" dividies integers, whereas "//" divides float's. other ways possible, casting 1 of operands float before division float(1)/2 == 0.5
or, using statement from __future__ import division
. i'm pretty sure can write number straight float 1.0/2 == 0.5
means there @ least 4 ways of getting proper result.
in summary, need use 1 of these 4 ways in python2:
- 1//other_number
- 1.0/other_number
- float(1/other_number)
- use normal "/" from __future__ import division
statement in front of script.
edit3: turns out, op using python3
import numpy np import random diam = 1.5 p = 1 = 10 d = 0.2 h = 0.15 lx = list(np.arange(-diam/2, diam/2 + 0.05, 0.05)) # aware casting numpy array list loose precision? # compare: # print(np.arange(-diam/2, diam/2 + 0.05, 0.05)) # print(list(np.arange(-diam/2, diam/2 + 0.05, 0.05))) # constants x1 = 1/(2*p) tan = np.tan(a) cond1 = -d/2 - h/np.tan(np.pi/2 - a) cond2 = d/2 disp1 = "abs( f - x1 * k**2 ) * tan = {} \n(f {}, last k {})\n" print("cond1 = {}".format(cond1)) print("cond2 = {}".format(cond2)) print("") loop in range(1): f = random.uniform(0, p/2) a1 = [k k in lx if k + abs(f - x1 * k**2 ) * tan < cond1] a2 = [k k in lx if k + abs(f - x1 * k**2 ) * tan > cond2] = a1 + a2 b = [k + abs(f - x1 * k**2 ) * tan k in lx] last = abs(f - x1 * lx[-1]**2) * tan print(disp1.format(last, f, lx[-1])) print("a1=", a1, "\n") print("a2=", a2, "\n") print("lx=", lx, "\n") print("a=", a, "\n") print("b=", b, "\n") # have: # lx uniform distribution 0.75 0.75 step 0.05 # a1 have values lx condition # a2 have rest of lx's values # sum of a1 , a2 should equal lx # in rare cases condition equal cond1 or 2, should not. # , have b suppose testing condition # does. # looking @ lx[i] , a[i], while checking b[i] versus cond1 or cond2 # see intended.
output (python3):
cond1 = -0.197254124118863 cond2 = 0.1 abs( f - x1 * k**2 ) * tan = 0.0266300386512908 (f 0.32232286795171455, last k 0.7500000000000013) a1= [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969] a2= [-0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133] lx= [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, -0.14999999999999947, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133] a= [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133] b= [-0.72336996134870857, -0.64986688135331661, -0.57798470342657238, -0.50772342756847588, -0.43908305377902707, -0.37206358205822598, -0.30666501240607263, -0.24288734482256696, -0.18073057930770905, -0.12019471586149885, -0.061279754483936383, -0.0039856951750216196, 0.05168746206524541, 0.10573971723686473, 0.15817107033983635, 0.20898152137416023, 0.25817107033983638, 0.30573971723686488, 0.35168746206524559, 0.39601430482497857, 0.43872024551606381, 0.47980528413850143, 0.51926942069229121, 0.55711265517743336, 0.59333498759392778, 0.62793641794177446, 0.66091694622097341, 0.69227657243152474, 0.72201529657342822, 0.75013311864668408, 0.7766300386512921]
i assume aware f
gets random value each time in loop, , create arrays , b 50 times different f's, last accessible, since dont store them. perhaps wanted append element list in range?
ad rem, judging trying boils down selecting items lx fulfil cond1 < item < cond2, whereas there may multiple better solutions how that, let's check yours.
if observe above, b[i] i=0...n see in example 8 first values fulfil first condition, (being smaller -0.197...), list a1 populated first 8 numbers lx.
next, 5 consecutive numbers b[i] not match condition, , a2 starts index 14 (or 13 if counting 0), correct. populated first 8 items lx , next remaining items starting index 13.
in first example -0.12... numbers lx fulfiled condition in l.
Comments
Post a Comment