X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.16.109 Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!nospam.fr.eu.org!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.03; 'function,': 0.07; 'python': 0.08; 'compute': 0.09; 'googled': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; '>>>': 0.12; 'def': 0.12; ';-)': 0.14; 'wrote:': 0.14; 'jumping': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'math': 0.16; 'seems': 0.21; 'all:': 0.23; 'elegant': 0.23; 'least,': 0.23; 'says': 0.27; 'wondering': 0.28; 'subject:?': 0.29; 'import': 0.29; 'class': 0.29; 'bit': 0.30; 'least': 0.30; 'from:addr:web.de': 0.30; 'seem': 0.32; "can't": 0.32; 'apply': 0.32; 'header:X-Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; 'too': 0.33; "i've": 0.33; 'however,': 0.34; 'there': 0.35; 'case': 0.37; 'received:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; "i'd": 0.39; 'header:Mime-Version:1': 0.39; 'got': 0.39; 'to:addr:python.org': 0.39; 'really': 0.40; 'almost': 0.60; 'total': 0.61; 'subject': 0.62; 'skip:1 10': 0.63; 'here': 0.66; '5.0': 0.84; '0.0': 0.91; 'gabriel': 0.91; 'subject:Best': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Best way to compute length of arbitrary dimension vector? Date: Mon, 30 May 2011 11:46:01 +0200 Organization: None References: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a968.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 38 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306749013 news.xs4all.nl 49177 [::ffff:82.94.164.166]:44722 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6647 Gabriel wrote: > Well, the subject says it almost all: I'd like to write a small Vector > class for arbitrary-dimensional vectors. > > I am wondering what would be the most efficient and/or most elegant > way to compute the length of such a Vector? > > Right now, I've got > > def length(self): # x.length() = || x || > total = 0.0 > for k in range(len(self._coords)): > d = self._coords[k] > total += d*d > return sqrt(total) > > However, that seems a bit awkward to me (at least in Python ;-) ). > > I know there is the reduce() function, but I can't seem to find a way > to apply that to the case here (at least, not without jumping through > too many hoops). > > I have also googled a bit, but found nothing really elegant. >>> class Vector(object): ... def __init__(self, *coords): ... self._coords = coords ... def __abs__(self): ... return math.sqrt(sum(x*x for x in self._coords)) ... >>> import math >>> abs(Vector(1,1)) 1.4142135623730951 >>> abs(Vector(3,4)) 5.0