Home > other >  Regarding direct multiplication of scalar with list
Regarding direct multiplication of scalar with list

Time:12-06

The snippet was supposed to store/print instantaneous voltage/current values generated by the sin and the arrange functions in numpy. I first had tolist() after those functions, but the multiplication of the magnitude (230 in the case of voltage, 5 in the case of current) had no effect on the result unless I removed the tolist(). Why does that occur?

    V_magnitude = 230
    I_magnitude = 5
    voltage = V_magnitude*np.sin(np.arange(0,10,0.01)).tolist()
    current = I_magnitude*np.sin(np.arange(-0.3,9.7,0.01))

What I've tried

-> making both the magnitudes as the second operand for multiplication

-> with and without tolist()

CodePudding user response:

When you multiply a list by X you extend the list to contain X times the values in it.

When you multiply numpy array by X you multiply the values inside the the array by X.

Try it with a simple example

lst = [1, 2, 3]
print(lst * 3)           # [1, 2, 3, 1, 2, 3, 1, 2, 3]
print(np.array(lst) * 3) # [3 6 9]
  • Related