Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(at': 0.03; 'function,': 0.07; 'python': 0.08; 'compute': 0.09; 'googled': 0.09; 'received:209.85.160.174': 0.09; 'received:mail- gy0-f174.google.com': 0.09; 'def': 0.12; ';-)': 0.14; 'am,': 0.14; 'wrote:': 0.14; 'ideas?': 0.16; 'jumping': 0.16; 'cc:addr:python- list': 0.17; 'mon,': 0.17; 'cheers,': 0.19; 'header:In-Reply- To:1': 0.21; 'seems': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'all:': 0.23; 'elegant': 0.23; 'least,': 0.23; 'says': 0.27; 'wondering': 0.28; 'message-id:@mail.gmail.com': 0.28; 'subject:?': 0.29; 'class': 0.29; 'received:209.85.160': 0.29; 'bit': 0.30; 'least': 0.30; 'cc:addr:python.org': 0.30; 'seem': 0.32; "can't": 0.32; 'apply': 0.32; 'too': 0.33; "i've": 0.33; 'chris': 0.34; 'however,': 0.34; 'there': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'case': 0.37; 'but': 0.38; 'subject:: ': 0.38; 'skip:s 20': 0.39; "i'd": 0.39; 'received:209': 0.39; 'got': 0.39; 'really': 0.40; 'almost': 0.60; 'subject': 0.62; 'here': 0.66; '30,': 0.84; 'sender:addr:chris': 0.84; '0.0': 0.91; 'gabriel': 0.91; 'subject:Best': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=sBIRVyjqAfTloQ22R9cqNZ4shwNxoWPE89X8fpvlwHo=; b=LwxH61HfC+ynsrsoKPGAVxRElQvSvBPvwmAK+WywIONTpLKlwziDwrrgCLbxFpfBU5 gzDSkdQX38MCIzRf5jT4FvhL5OL1DjxK7XGTm1oBkqdypGpPDWu4JldIV8a2XWTuya3M Pre+csC0lwWPGN5IN0BnhaEdPIEe5ECbMIMO4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=TyjGSDkFn64LtS/tCnXGf8A7uN3sVhAxg3aWy93/cMx2CUXKBlY4+xkNKbAAQ5b8b9 VsRbwMqBoXRZHi1/6PgiM/vk5uicrH2TZeOssV2D2pTGGsxs0tcezNmp4vuxcOONeXrC Kvk9D35B4AyH+qtLm5tnLBdgNvN5J1IFKoca8= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> References: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> Date: Mon, 30 May 2011 02:24:17 -0700 X-Google-Sender-Auth: xjd9KiKzijRIf56qv4dFpTmqqk4 Subject: Re: Best way to compute length of arbitrary dimension vector? From: Chris Rebert To: Gabriel Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 1306747466 news.xs4all.nl 49174 [::ffff:82.94.164.166]:60007 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6644 On Mon, May 30, 2011 at 2:11 AM, 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 > > =C2=A0def length(self): =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 # x.length() =3D || x || > =C2=A0 =C2=A0total =3D 0.0 > =C2=A0 =C2=A0for k in range(len(self._coords)): > =C2=A0 =C2=A0 =C2=A0d =3D self._coords[k] > =C2=A0 =C2=A0 =C2=A0total +=3D d*d > =C2=A0 =C2=A0return 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. > > Any ideas? def length(self): return sqrt(sum(coord*coord for coord in self._coords)) Cheers, Chris -- http://rebertia.com