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


Groups > comp.lang.python > #6669

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

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 <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.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" <gagsl-py2@yahoo.com.ar>
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> <irvp0e$mc6$2@dough.gmane.org>
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 <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.2278.1306781932.9059.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

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