Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #94540
| From | Seb <spluque@gmail.com> |
|---|---|
| Subject | scalar vs array and program control |
| Date | 2015-07-24 23:18 -0500 |
| Organization | Church of Emacs |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.971.1437797935.3674.python-list@python.org> (permalink) |
Hello,
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.
Cheers,
--
Seb
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
scalar vs array and program control Seb <spluque@gmail.com> - 2015-07-24 23:18 -0500 Re: scalar vs array and program control Steven D'Aprano <steve@pearwood.info> - 2015-07-26 14:03 +1000
csiph-web