ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 29 May 2021 00:14:40 +0200unique elements in complex numbers listhttps://ask.sagemath.org/question/57315/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[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?Fri, 28 May 2021 09:55:04 +0200https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/Comment by alvaro for <p>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)</p>
<pre><code>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
</code></pre>
<p>which results in</p>
<pre><code>{-1,
0.500000000000000? - 0.866025403784439?*I,
0.500000000000000? - 0.866025403784439?*I,
0.500000000000000? + 0.866025403784439?*I,
0.500000000000000? + 0.866025403784439?*I}
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/?comment=57326#post-id-57326agreed, added example and resultsFri, 28 May 2021 19:43:17 +0200https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/?comment=57326#post-id-57326Comment by et_l for <p>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)</p>
<pre><code>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
</code></pre>
<p>which results in</p>
<pre><code>{-1,
0.500000000000000? - 0.866025403784439?*I,
0.500000000000000? - 0.866025403784439?*I,
0.500000000000000? + 0.866025403784439?*I,
0.500000000000000? + 0.866025403784439?*I}
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/?comment=57319#post-id-57319Can you please add an MRE (Minimal Reproducible Example) so people can reproduce it for themselves and try helping you?Fri, 28 May 2021 10:39:35 +0200https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/?comment=57319#post-id-57319Answer by tmonteil for <p>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)</p>
<pre><code>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
</code></pre>
<p>which results in</p>
<pre><code>{-1,
0.500000000000000? - 0.866025403784439?*I,
0.500000000000000? - 0.866025403784439?*I,
0.500000000000000? + 0.866025403784439?*I,
0.500000000000000? + 0.866025403784439?*I}
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/?answer=57322#post-id-57322If 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](https://mathexp2018.metelu.net/worksheets/real_and_complex_numbers/real_and_complex_numbers.ipynb)Fri, 28 May 2021 12:58:25 +0200https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/?answer=57322#post-id-57322Comment by alvaro for <p>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 :</p>
<pre><code>sage: {0.1 + 0.2, 0.3}
{0.300000000000000, 0.300000000000000}
</code></pre>
<p>There is nothing wrong here since:</p>
<pre><code>sage: (0.1 + 0.2).sign_mantissa_exponent()
(1, 5404319552844596, -54)
sage: (0.3).sign_mantissa_exponent()
(1, 5404319552844595, -54)
</code></pre>
<p>It is important to understand that there is no such thing like "complex numbers" in Sage but various representations, with advantages and drawbacks.</p>
<p>If you want more help, you should provide details of what you want to achieve, with the code you proudced so far.</p>
<p><strong>EDIT</strong></p>
<p>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:</p>
<pre><code>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}
</code></pre>
<p>You can learn about the different representations of real and complex numbers in Sage in <a href="https://mathexp2018.metelu.net/worksheets/real_and_complex_numbers/real_and_complex_numbers.ipynb">this worksheet</a></p>
https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/?comment=57334#post-id-57334in fact that is a great solution!
ThanksSat, 29 May 2021 00:14:40 +0200https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/?comment=57334#post-id-57334Comment by alvaro for <p>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 :</p>
<pre><code>sage: {0.1 + 0.2, 0.3}
{0.300000000000000, 0.300000000000000}
</code></pre>
<p>There is nothing wrong here since:</p>
<pre><code>sage: (0.1 + 0.2).sign_mantissa_exponent()
(1, 5404319552844596, -54)
sage: (0.3).sign_mantissa_exponent()
(1, 5404319552844595, -54)
</code></pre>
<p>It is important to understand that there is no such thing like "complex numbers" in Sage but various representations, with advantages and drawbacks.</p>
<p>If you want more help, you should provide details of what you want to achieve, with the code you proudced so far.</p>
<p><strong>EDIT</strong></p>
<p>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:</p>
<pre><code>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}
</code></pre>
<p>You can learn about the different representations of real and complex numbers in Sage in <a href="https://mathexp2018.metelu.net/worksheets/real_and_complex_numbers/real_and_complex_numbers.ipynb">this worksheet</a></p>
https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/?comment=57327#post-id-57327exactly! i wonder if there is an instruction to reduce a set taking that into accountFri, 28 May 2021 19:44:09 +0200https://ask.sagemath.org/question/57315/unique-elements-in-complex-numbers-list/?comment=57327#post-id-57327