Ask Your Question
1

Select list element around a value

asked 2017-08-24 19:14:20 +0100

ortollj gravatar image

HI

I would like to show all elements of allRootsAPow2 which are around 2 + 3*I this code below does not work ;-(

allRootsAPow2 =[2.00000000000000 + 3.00000000000000*I,
 2.00000000000000 - 3.00000000000000*I,
 -1.05087254532372e-32,
 11.2111025509280 - 6.86482042695334e-16*I,
 2.00000000000000 - 3.00000000000000*I,
 2.00000000000000 + 3.00000000000000*I,
 11.2111025509280 - 6.86482042695334e-16*I,
 -1.05087254532372e-32,
 2.00000000000000 + 3.00000000000000*I,
 2.00000000000000 - 3.00000000000000*I,
 -1.05087254532372e-32,
 11.2111025509280 - 6.86482042695334e-16*I,
 2.00000000000000 - 3.00000000000000*I,
 2.00000000000000 + 3.00000000000000*I,
 11.2111025509280 - 6.86482042695334e-16*I,
 -1.05087254532372e-32,
 -3.21110255092798 + 1.96623323036392e-16*I,
 3.00992653992793e-33,
 2.00000000000000 - 3.00000000000000*I,
 2.00000000000000 + 3.00000000000000*I,
 3.00992653992793e-33,
 -3.21110255092798 + 1.96623323036392e-16*I,
 2.00000000000000 + 3.00000000000000*I,
 2.00000000000000 - 3.00000000000000*I,
 -3.21110255092798 + 1.96623323036392e-16*I,
 3.00992653992793e-33,
 2.00000000000000 - 3.00000000000000*I,
 2.00000000000000 + 3.00000000000000*I,
 3.00992653992793e-33,
 -3.21110255092798 + 1.96623323036392e-16*I,
 2.00000000000000 + 3.00000000000000*I,
 2.00000000000000 - 3.00000000000000*I]

 for el in allRootsAPow2 :
        if (math.ceil(el.real_part()*10)/10)== 2 and (math.ceil(el.imag_part()*10)/10)== 3  :
        #if (math.ceil(el.real_part()*10)/10)== 2  :
        #if (math.ceil(el.imag_part()*10)/10)== 3  :
            show(el)
edit retag flag offensive close merge delete

Comments

Sometimes it is better to show how the question arises, since a small change of the path to the problem is no longer showing the problem. Here explicitly: How (and why) do we compute the above redundant list allRootsAPow2 ?

dan_fulea gravatar imagedan_fulea ( 2017-08-24 20:43:43 +0100 )edit

yes this code is just arranged to show the pb I get, but the title of my post is resuming what I would like ?

ortollj gravatar imageortollj ( 2017-08-25 05:09:56 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-08-24 20:21:56 +0100

fidbc gravatar image

updated 2017-08-24 20:23:07 +0100

This seems to output an AttributeError, namely 'sage.rings.real_mpfr.RealLiteral' object has no attribute 'real_part'. Also note that there are some real numbers in the list.

A way to work around this is to convert each element x to a complex number via CC(x), see updated for loop below.

for el in [CC(x) for x in allRootsAPow2]:
    if (math.ceil(el.real_part()*10)/10)== 2 and (math.ceil(el.imag_part()*10)/10)== 3  :
        show(el)

One other option is to define a number to which you want to compare to ($2+3i$ in your example) and a tolerance (0 from above) and use the modulus of the difference as a metric (here the conversion to CC seems to be implicit through the subtraction from other).

other = CC(2+3*i)
tolerance = 0.001
print [x for x in allRootsAPow2 if abs(other-x)<=tolerance]
edit flag offensive delete link more

Comments

Thank you fidbc, the first code you write above is ok. and the second 'th is ok if other = CC(2+3*I) not

 other = CC(2+3*i)

there is also:

compVal = CC(2+3*I)
for el in  allRootsAPow2 :
        if round(el.real_part())== round(compVal.real_part(),1) and round(el.imag_part())== round(compVal.imag_part(),1)   :
            print str(el)
ortollj gravatar imageortollj ( 2017-08-25 05:22:35 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-08-24 19:14:20 +0100

Seen: 696 times

Last updated: Aug 24 '17