First time here? Check out the FAQ!

Ask Your Question
2

unique elements in complex numbers list

asked 3 years ago

alvaro gravatar image

updated 3 years ago

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?

Preview: (hide)

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 ( 3 years ago )

agreed, added example and results

alvaro gravatar imagealvaro ( 3 years ago )

1 Answer

Sort by » oldest newest most voted
2

answered 3 years ago

tmonteil gravatar image

updated 3 years ago

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

Preview: (hide)
link

Comments

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

alvaro gravatar imagealvaro ( 3 years ago )

in fact that is a great solution! Thanks

alvaro gravatar imagealvaro ( 3 years ago )

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: 3 years ago

Seen: 595 times

Last updated: May 28 '21