Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Computing an array with a given shape containing powers of x = complex(0, 1)

I am trying to create a 9-lines array (shape = (9,)) containing

powers of x = complex(0, 1): 1, x, x**2, …, x**8.

I do not know if there is someone who knows what is meant by the statement powers of x = complex(0, 1) and how in your opinion this series might be computed.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks

>Solution :

If I understand correctly, you can use:

out = complex(0,1)**np.arange(9)

output:

array([ 1.+0.j,  0.+1.j, -1.+0.j, -0.-1.j,  1.+0.j,  0.+1.j, -1.+0.j,
       -0.-1.j,  1.+0.j])

If by 9 lines you rather mean a shape of (9,1), use instead:

out = complex(0,1)**np.arange(9)[:,None]

output:

array([[ 1.+0.j],
       [ 0.+1.j],
       [-1.+0.j],
       [-0.-1.j],
       [ 1.+0.j],
       [ 0.+1.j],
       [-1.+0.j],
       [-0.-1.j],
       [ 1.+0.j]])

explanation:

np.arange(9) is creating the array array([0, 1, 2, 3, 4, 5, 6, 7, 8]), and vectorization of x**powers is computing x**power for each power of powers

x = complex(0, 1)
# 0 + 1j

powers = np.arange(9)
# array([0, 1, 2, 3, 4, 5, 6, 7, 8])

x**powers
# array([ 1.+0.j,   1
#         0.+1.j,   x
#        -1.+0.j,   x**2
#        -0.-1.j,   x**3
#         1.+0.j,
#         0.+1.j,
#        -1.+0.j,
#        -0.-1.j,
#         1.+0.j,   x**8
#         ])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading