Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94552

Re: scalar vs array and program control

Path csiph.com!usenet.pasdenom.info!bete-des-vosges.org!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'versions,': 0.05; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'output': 0.13; 'def': 0.13; '2))': 0.16; 'angle': 0.16; 'indexerror:': 0.16; 'intuition': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:array': 0.16; 'subject:program': 0.16; 'wrote:': 0.16; 'implementing': 0.18; 'tells': 0.18; 'try:': 0.18; 'fairly': 0.22; 'arguments': 0.22; 'arrays': 0.22; 'wrote': 0.23; 'import': 0.24; 'written': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'appreciated.': 0.27; 'operations,': 0.27; 'function': 0.28; 'this.': 0.28; 'style.': 0.29; "i'm": 0.30; 'code': 0.30; 'convention': 0.30; 'initially': 0.30; "i'd": 0.31; 'skip:_ 10': 0.32; 'implement': 0.32; 'functional': 0.32; 'statement': 0.32; 'operations.': 0.33; 'except': 0.34; 'quite': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'client': 0.37; 'received:org': 0.37; 'thought': 0.37; 'takes': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'hello,': 0.40; 'personally': 0.61; 'skip:n 10': 0.62; 'more': 0.63; 'ang': 0.84; 'inefficient': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: scalar vs array and program control
Date Sat, 25 Jul 2015 15:46:03 +0200
Organization None
References <87vbd850qa.fsf@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd9b4d.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.979.1437831975.3674.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1437831975 news.xs4all.nl 2917 [2001:888:2000:d::a6]:54009
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:94552

Show key headers only | View raw


Seb wrote:

> 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.

Here's a "duck-typed" solution:

import numpy as np

def _f(x, y):
    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
    return ang

def f(x, y):
    try:
        return _f(x, y)
    except IndexError:
        return _f(np.array([x]), y)[0]

However, this is quite inefficient for scalars. Personally I'd implement two 
versions, one for scalars and one for vectors. This may be a little less 
convenient, but most of the time the client code has to deal either with 
scalars or vectors, not both.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: scalar vs array and program control Peter Otten <__peter__@web.de> - 2015-07-25 15:46 +0200

csiph-web