简体   繁体   中英

Converting a real number to imaginary

For the code below, to convert a number to imaginary:

def output_imag(a)
    return a * 1j

Is this the right way to do this? Because a * 1j is simple enough, but I wonder whether there is a more correct way to do this.

Edit 2014-02-06:

Thanks for all the answers! Actually the function I defined is a terrible way to say this... What I actually tried to do is this:

import numpy as np
my_array = np.r_[0:1:100j] 

This code will generate an array like np.linspace(0, 1, 100). Now I wanted to make the "100" a variable, like np.linspace(0, 1, number) but np.r_[0:1:numberj] couldn't work, only np.r_[0:1:number*1j] works.

So, that's how I've been wondering: a better way than number*1j? It seems not...

Thank you for all who answered!

Another way is complex(0, a) .

I would also argue that defining your own function just for this is counter-productive as it obscures the intent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM