Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'output': 0.13; '2))': 0.16; 'angle': 0.16; 'intuition': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:array': 0.16; 'subject:program': 0.16; 'implementing': 0.18; 'tells': 0.18; 'fairly': 0.22; 'arguments': 0.22; 'arrays': 0.22; 'cheers,': 0.22; 'wrote': 0.23; 'written': 0.24; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'appreciated.': 0.27; 'operations,': 0.27; 'received:24': 0.28; 'function': 0.28; 'this.': 0.28; 'style.': 0.29; "i'm": 0.30; 'convention': 0.30; 'initially': 0.30; "i'd": 0.31; 'functional': 0.32; 'statement': 0.32; 'operations.': 0.33; 'message- id:@gmail.com': 0.34; 'but': 0.36; 'there': 0.36; 'to:addr:python- list': 0.36; 'two': 0.37; 'received:org': 0.37; 'thought': 0.37; 'takes': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'hello,': 0.40; 'skip:n 10': 0.62; 'more': 0.63; 'ang': 0.84; 'received:shawcable.net': 0.84; 'received:wp.shawcable.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Seb Subject: scalar vs array and program control Date: Fri, 24 Jul 2015 23:18:21 -0500 Organization: Church of Emacs Mime-Version: 1.0 Content-Type: text/plain X-Gmane-NNTP-Posting-Host: s0106503955564c1f.wp.shawcable.net User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:GxgUHSQPb39toD0TAJyf9oG4Yxw= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437797935 news.xs4all.nl 2897 [2001:888:2000:d::a6]:54012 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94540 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