Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.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.068 X-Spam-Evidence: '*H*': 0.88; '*S*': 0.02; '>>>>': 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; 'def': 0.12; 'wrote:': 0.14; 'accuracy,': 0.16; 'escribi\xc3\xb3:': 0.16; 'from:addr:yahoo.com.ar': 0.16; 'math': 0.16; 'mon,': 0.17; 'all:': 0.23; 'says': 0.27; 'subject:?': 0.29; 'import': 0.29; 'class': 0.29; 'instead': 0.29; 'header:X-Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; '...': 0.34; 'header:User-Agent:1': 0.35; 'using': 0.35; 'specially': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; "i'd": 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'almost': 0.60; 'subject': 0.62; 'skip:1 10': 0.63; 'received:190': 0.74; '8bit%:20': 0.80; '-0300,': 0.84; '5.0': 0.84; 'genellina': 0.84; 'math.fsum': 0.84; 'sum': 0.88; '0.0': 0.91; 'gabriel': 0.91; 'subject:Best': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Gabriel Genellina" Subject: Re: Best way to compute length of arbitrary dimension vector? Date: Mon, 30 May 2011 16:01:08 -0300 References: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 190.2.4.25 User-Agent: Opera Mail/11.11 (Win32) 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: 43 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306781933 news.xs4all.nl 49179 [::ffff:82.94.164.166]:38954 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6669 En Mon, 30 May 2011 06:46:01 -0300, Peter Otten <__peter__@web.de> escribió: > Gabriel wrote: > >> Well, the subject says it almost all: I'd like to write a small Vector >> class for arbitrary-dimensional vectors. >> > >>>> 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 Using math.fsum instead of sum may improve accuracy, specially when len(coords)≫2 py> import math py> py> def f1(*args): ... return math.sqrt(sum(x*x for x in args)) ... py> def f2(*args): ... return math.sqrt(math.fsum(x*x for x in args)) ... py> pi=math.pi py> args=[pi]*16 py> abs(f1(*args)/4 - pi) 4.4408920985006262e-16 py> abs(f2(*args)/4 - pi) 0.0 -- Gabriel Genellina