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.Tue, 21 Jul 2020 17:30:46 +0200Finding all complex nth rootshttps://ask.sagemath.org/question/52598/finding-all-complex-nth-roots/I would love it if there were a command, let's say all_complex_roots, that returned a list or tuple or whatever of the 2 square roots, 3 cube roots, etc of a number. For instance, an input like
all_complex_roots(1,4)
would have the expected output [1,-1,I,-I] or some such.
Does such a command exist? I've seen workarounds, like finding all complex roots of the polynomial $X^4-1$, but is there something direct?Tue, 21 Jul 2020 16:20:11 +0200https://ask.sagemath.org/question/52598/finding-all-complex-nth-roots/Answer by slelievre for <p>I would love it if there were a command, let's say all_complex_roots, that returned a list or tuple or whatever of the 2 square roots, 3 cube roots, etc of a number. For instance, an input like
all_complex_roots(1,4)
would have the expected output [1,-1,I,-I] or some such.</p>
<p>Does such a command exist? I've seen workarounds, like finding all complex roots of the polynomial $X^4-1$, but is there something direct?</p>
https://ask.sagemath.org/question/52598/finding-all-complex-nth-roots/?answer=52605#post-id-52605Defining the function you need only takes a few lines:
def all_complex_roots(a, n):
"""
Return all complex `n`-th roots of `a`.
"""
x = polygen(QQbar)
return (x^n - a).roots(multiplicities=False)
It is then ready to be used!
sage: all_complex_roots(1, 4)
[-1, 1, -1*I, 1*I]
Tue, 21 Jul 2020 17:30:46 +0200https://ask.sagemath.org/question/52598/finding-all-complex-nth-roots/?answer=52605#post-id-52605