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


Groups > comp.lang.python > #6644

Re: Best way to compute length of arbitrary dimension vector?

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 <chris@rebertia.com>
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 <clp2@rebertia.com>
To Gabriel <snoopy.67.z@googlemail.com>
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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2256.1306747466.9059.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Mon, May 30, 2011 at 2:11 AM, Gabriel <snoopy.67.z@googlemail.com> 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.
>
> Any ideas?

def length(self):
    return sqrt(sum(coord*coord for coord in self._coords))

Cheers,
Chris
--
http://rebertia.com

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


Thread

Best way to compute length of arbitrary dimension vector? Gabriel <snoopy.67.z@googlemail.com> - 2011-05-30 02:11 -0700
  Re: Best way to compute length of arbitrary dimension vector? Chris Rebert <clp2@rebertia.com> - 2011-05-30 02:24 -0700
  Re: Best way to compute length of arbitrary dimension vector? Peter Otten <__peter__@web.de> - 2011-05-30 11:46 +0200
    Re: Best way to compute length of arbitrary dimension vector? Gabriel <snoopy.67.z@googlemail.com> - 2011-05-30 06:38 -0700
      Re: Best way to compute length of arbitrary dimension vector? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-02 17:19 -0600
        Re: Best way to compute length of arbitrary dimension vector? Gabriel <snoopy.67.z@googlemail.com> - 2011-06-03 14:53 -0700
          Re: Best way to compute length of arbitrary dimension vector? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-03 16:17 -0600
          Re: Best way to compute length of arbitrary dimension vector? Robert Kern <robert.kern@gmail.com> - 2011-06-03 18:12 -0500
  Re: Best way to compute length of arbitrary dimension vector? "Gabriel Genellina" <gagsl-py2@yahoo.com.ar> - 2011-05-30 16:01 -0300
    Re: Best way to compute length of arbitrary dimension vector? Gabriel <snoopy.67.z@googlemail.com> - 2011-06-01 12:35 -0700

csiph-web