# HP Forums

Full Version: (complex) root of unity
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi,
first of try by myself to write a formule, I wonder if there is already for the Prime a program or app to get "all the complex root of unity (or any complex number) (see https://en.wikipedia.org/wiki/Root_of_unity), in other words, I need a program or routine to calculate all real and complex roots of any real or complex number, returned in a list or matrix...

Thanks a lot,
Salvo
(01-16-2021 02:47 PM)salvomic Wrote: [ -> ]...I need a program or routine to calculate all real and complex roots of any real or complex number, returned in a list or matrix...

Roots of a number?

Perhaps if you provide an example it would be more clear?
Is the example I report correct?

See the attachment.
Assumed n is positive integer.

Cas> rootsOfOne(n) := e^(2*pi*i*range(n)/n)
Cas> rootsOfOne(3)

[1, 1/2*√3*i-1/2, -1/2*√3*i-1/2]

Cas> rootsOfz(z,n) := z^(1/n) * rootsOfOne(n)
Cas> approx(rootsOfz(3+4i, 3))

[ 1.62893714592 +0.520174502305*i,
−1.26495290636 +1.15061369838*i,
−0.363984239564-1.67078820069*i]

Cas> Ans .^ 3

[3.+4.*i, 3.+4.*i, 3.+4.*i]
(01-16-2021 02:57 PM)rprosperi Wrote: [ -> ]Roots of a number?

Perhaps if you provide an example it would be more clear?

e.g. the three roots of 3√1 or the four of 4√-3 or the two of √(1+i) ...

Like the function in Math1 pac for HP 41CX; input img and real part of the complex number, then the nRth exponent, to get the n roots...
See here the three roots of unity, but I need something to get the roots of any complex number.

Code:
``` Z "img" Y "real" X nRth XEQ "Z↑1/N"```

The solution of robmio below could be ok, with some semplications, however...

Salvo
(01-16-2021 03:24 PM)robmio Wrote: [ -> ]Is the example I report correct?

See the attachment.

yes, at least this works, however better to simplify...
(01-16-2021 03:40 PM)Albert Chan Wrote: [ -> ]Assumed n is positive integer.

Cas> rootsOfOne(n) := e^(2*pi*i*range(n)/n)
Cas> rootsOfOne(3)

[1, 1/2*√3*i-1/2, -1/2*√3*i-1/2]

Cas> rootsOfz(z,n) := z^(1/n) * rootsOfOne(n)
Cas> approx(rootsOfz(3+4i, 3))

[ 1.62893714592 +0.520174502305*i,
−1.26495290636 +1.15061369838*i,
−0.363984239564-1.67078820069*i]

Cas> Ans .^ 3

[3.+4.*i, 3.+4.*i, 3.+4.*i]

well, thanks,
I'll try this.
Salvo
(01-16-2021 03:40 PM)Albert Chan Wrote: [ -> ]Assumed n is positive integer.
...

something like this, then
Code:
``` EXPORT rootsOfOne(n) BEGIN RETURN e^(2*PI*i*range(n)/n); END; EXPORT rootsOfZ(z, n) BEGIN RETURN z^(1/n) * rootsOfOne(n); END;```

but: rootOFOne(3) I get "Error: Bad argument type"
it seems that within a program the HP PRIME does not recognize the "range" command
if you rewrite the program as CAS, it works.
(01-16-2021 05:47 PM)robmio Wrote: [ -> ]if you rewrite the program as CAS, it works.

yes, actually.
Now:
Code:
``` #cas rootsOfOne(n):= BEGIN RETURN e^(2*PI**range(n)/n); END; rootsOfZ(z, n):= BEGIN RETURN z^(1/n) * rootsOfOne(n); END; #end```

it works, but I get first a warning "Recursive" (see attached images).
Then, trying with "3+4i" I get a little square in the matrix (second item): I don't know what's the reason...

Salvo
The small square in the HP PRIME G2 with firmware 20200121 replaces the three dots that appear in the HP Prime Virtual Calculator lists or vectors

Code:
``` #cas rootsOFOne(n):= BEGIN RETURN e^(2**π*range(n)/n); END; rootsOfZ(z, n):= BEGIN RETURN z^(1/n)*rootsOFOne(n); END; #end```
(01-16-2021 06:12 PM)robmio Wrote: [ -> ]The small square in the HP PRIME G2 with firmware 20200121 replaces the three dots that appear in the HP Prime Virtual Calculator lists or vectors

yes, ok, I had this problem in another program of mine and I solved there shorting the decimal places.
Is there a way to avoid that?
Using Approx the small square is still there, without showing the second value of the root...

Salvo
see what happens if I use HP Prime Virtual Calculator on PC: instead of the square there are three dots. The square is a feature of HP PRIME G2 with filrmware 20200121
(01-16-2021 06:28 PM)robmio Wrote: [ -> ]see what happens if I use HP Prime Virtual Calculator on PC: instead of the square there are three dots. The square is a feature of HP PRIME G2 with filrmware 20200121

yes, I'm seeing.
I wonder if it is possible to show also the second item, without the dots (or the square)...
To see the second value, the third, and so on, use the "Show" option
(01-16-2021 06:40 PM)robmio Wrote: [ -> ]To see the second value, the third, and so on, use the "Show" option

oh, well, I've forgot that
thanks
Salvo
Hello salvomic,

Re, your post: "I need a program or routine to calculate all real and complex roots of any real or complex number, returned in a list or matrix..."

You might be interested in this solution for your problem that I stumbled across:

In CAS, For the fourth root of unity, use POLYROOT like this and enter unity as the complex number 1+0*i:

POLYROOT(X^4-(1+0*i))sto L1 .......(1)

Press “Enter”:

The results are in L1: (-1, -i, i, 1); scroll up and down to see them on the "contents" line.

In fact POLYROOT(X^n-1)sto L1 works for the case of n.th roots of unity.

For n.th roots of any complex number stored in a List, use the format above in (1).