Ask Your Question
2

unique elements in complex numbers list

asked 2021-05-28 09:55:04 +0200

alvaro gravatar image

updated 2021-05-28 20:07:45 +0200

I have a list of complex numbers, but even after converting it to a set, there are a lot of repetitions. here is a minimal example (in my code i have 10^6 polynomials, here only 2)

from sage.rings.polynomial.complex_roots import complex_roots
x = polygen(ZZ)

r1 = complex_roots( x^3+1)
r2 = complex_roots( x^6+2*x^3+1)

s1 = set([r[0] for r in r1])
s2 = set([r[0] for r in r2])

s1 | s2

which results in

{-1,
 0.500000000000000? - 0.866025403784439?*I,
 0.500000000000000? - 0.866025403784439?*I,
 0.500000000000000? + 0.866025403784439?*I,
 0.500000000000000? + 0.866025403784439?*I}

as you can see, the roots must be only 3. Is there an instruction to reduce a set of complex numbers differing less than a delta?

edit retag flag offensive close merge delete

Comments

Can you please add an MRE (Minimal Reproducible Example) so people can reproduce it for themselves and try helping you?

et_l gravatar imageet_l ( 2021-05-28 10:39:35 +0200 )edit

agreed, added example and results

alvaro gravatar imagealvaro ( 2021-05-28 19:43:17 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-05-28 12:58:25 +0200

tmonteil gravatar image

updated 2021-05-28 23:17:00 +0200

If your complex numbers are floating-point numbers, then it is likely that they differ by a very small amount due to numerical rounding, so that they are actually different, see for example :

sage: {0.1 + 0.2, 0.3}
{0.300000000000000, 0.300000000000000}

There is nothing wrong here since:

sage: (0.1 + 0.2).sign_mantissa_exponent()
(1, 5404319552844596, -54)
sage: (0.3).sign_mantissa_exponent()
(1, 5404319552844595, -54)

It is important to understand that there is no such thing like "complex numbers" in Sage but various representations, with advantages and drawbacks.

If you want more help, you should provide details of what you want to achieve, with the code you proudced so far.

EDIT

Given the added details, here is a possible solution : since your polynomials have integer entries, their roots are algebraic, hence you could work with thes field of algebraic numbers. If i try to follow your way, here is a proposal:

sage: x = polygen(ZZ)
sage: P1, P2 = (x^3+1, x^6+2*x^3+1)
sage: r1 = P1.roots(QQbar, multiplicities=False)
sage: r2 = P2.roots(QQbar, multiplicities=False)
sage: r1
[-1,
 0.500000000000000? - 0.866025403784439?*I,
 0.500000000000000? + 0.866025403784439?*I]
sage: r2
[-1,
 0.500000000000000? - 0.866025403784439?*I,
 0.500000000000000? + 0.866025403784439?*I]
sage: s1 = set(r1)
sage: s2 = set(r2)
sage: s1 | s2
{-1,
 0.500000000000000? - 0.866025403784439?*I,
 0.500000000000000? + 0.866025403784439?*I}

You can learn about the different representations of real and complex numbers in Sage in this worksheet

edit flag offensive delete link more

Comments

exactly! i wonder if there is an instruction to reduce a set taking that into account

alvaro gravatar imagealvaro ( 2021-05-28 19:44:09 +0200 )edit

in fact that is a great solution! Thanks

alvaro gravatar imagealvaro ( 2021-05-29 00:14:40 +0200 )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: 2021-05-28 09:55:04 +0200

Seen: 432 times

Last updated: May 28 '21