scalar vs array and program control

Seb spluque at
Sat Jul 25 06:18:21 CEST 2015


I'm fairly new to Python, struggling to write in a more object-oriented,
functional style.  I just wrote a function that takes two arrays
representing sine (y) and cosine (x) angle coordinates, and returns the
angle in degrees.  I had initially written the function to take
array-like arguments x/y, but I'd like to generalize and take scalars as
well.  However, the function has a subsetting operations, which don't
work with scalars:

    vmag = np.sqrt((x ** 2) + (y ** 2))
    ang = np.arctan2(y, x)
    ang[ang < 0] = ang[ang < 0] + (2 * np.pi) # output range 0 - 2*pi
    ang[vmag == 0] = 0          # when magnitude is 0 the angle is also 0
    ang[ang == 0] = 2 * np.pi   # convention

If I want to take scalars x/y, I naively thought about implementing an
if/else statement right before the subsetting operations.  However, my
intuition tells me there must be a proper object-oriented solution to
this.  Any tips appreciated.



More information about the Python-list mailing list