# unique elements in complex numbers list

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 for r in r1])
s2 = set([r 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 close merge delete

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

Sort by » oldest newest most voted

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

more

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