First time here? Check out the FAQ!

Ask Your Question
3

check if the result is a real number

asked 12 years ago

disi gravatar image

I get several results and want to check, which one is a real number. From trial and error, I can see that maxSol[2][x] is the only plausable solution.

Is there something like isReal()?

my attempt (don't laugh) ;)

for i in maxSol: print RealField(50)(i)

where I obviously get: TypeError: Unable to convert x (='.....

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 12 years ago

niles gravatar image

updated 12 years ago

I think "a in RR" is what you want to use. Your first attempt could work too, using a try/except block.

For testing integrality, or rationality, "in ZZ" and "in QQ" work similarly.

And if you know the numbers you're checking are complex, you could also test to see whether their imaginary parts are zero.

Preview: (hide)
link

Comments

Thanks so much :)

disi gravatar imagedisi ( 12 years ago )

for self reference, this is what I got:

disi gravatar imagedisi ( 12 years ago )

It is not so easy: arccos(2) in RR is evaluated as true, while arccos(2).n() is NaN. The best we found with my student is a.n() != NaN and a.n().is_real() -- is this too complicated? See also http://bit.ly/LndAoI

Robert Samal gravatar imageRobert Samal ( 12 years ago )
0

answered 12 years ago

disi gravatar image

again, this works perfect :)

for a in maxSol:
    if a[x] in RR:
        print RealField(50)(a[x])
Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 12 years ago

Seen: 3,359 times

Last updated: May 04 '12