Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110308
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Unexpected NANs in complex arithmetic |
| Date | Wed, 22 Jun 2016 10:18:36 +0100 |
| Lines | 68 |
| Message-ID | <mailman.32.1466587139.11516.python-list@python.org> (permalink) |
| References | <5769fc9b$0$1622$c3e8da3$5496439d@news.astraweb.com> <576A3ADA.5060000@rece.vub.ac.be> <CAHVvXxRJ8daxUnqXwL4e3y1bi79+GXciiETwsc9DPQsMh9uwVg@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de Xu/DtAX7XeU0JU+V7/GsBQMYhyDb/3P3QJ2VzaAYol+w== |
| Return-Path | <oscar.j.benjamin@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.019 |
| X-Spam-Evidence | '*H*': 0.96; '*S*': 0.00; 'anyway.': 0.04; 'error:': 0.05; 'cc:addr:python-list': 0.09; 'type)': 0.09; 'unexpected': 0.09; '2016': 0.16; 'cc:name:python list': 0.16; 'formula': 0.16; 'inf': 0.16; 'nan': 0.16; 'occurred.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'refers': 0.18; 'steve': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'implicit': 0.22; "python's": 0.23; 'implemented': 0.24; 'header:In-Reply- To:1': 0.24; "doesn't": 0.26; 'example': 0.26; 'distribute': 0.27; '(e.g.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'expansion': 0.27; 'mathematical': 0.27; 'correct': 0.28; 'idea': 0.28; 'values': 0.28; 'arithmetic': 0.29; 'equivalent.': 0.29; 'far,': 0.29; 'robust': 0.29; "i'm": 0.30; 'probably': 0.31; "can't": 0.32; 'getting': 0.33; 'point': 0.33; 'problem': 0.33; 'usually': 0.33; 'rule': 0.33; 'steven': 0.33; 'surprised': 0.33; 'received:google.com': 0.35; 'i.e.': 0.35; 'involving': 0.35; 'should': 0.36; 'received:209.85': 0.36; '(3)': 0.36; '(i.e.': 0.36; 'cases': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; '(2)': 0.37; 'doing': 0.38; 'received:209': 0.38; '(1)': 0.38; 'sure': 0.39; 'some': 0.40; 'your': 0.60; 'real': 0.62; 'results': 0.66; 'applying': 0.70; 'special': 0.73; 'gain': 0.82; '(once': 0.84; "d'aprano:": 0.84; 'oscar': 0.84; 'pardon': 0.84; 'presumably': 0.84; 'schreef': 0.84; 'essence': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=i1zvFPJq3v2KyA7XcikG9ZH5z+CYLuNsGMl787KYslM=; b=mm/wtGZoERT4VJFXkHbHJ/JfAsiFk3ykGZDgOSHRPIWGM5L0CVjy+0ZLk9JZyGdKDs XiiDPd2acApAwQFfMvQ/yCNqA7f7M0SqWLoaOc7nNYUCISQkai241KKpCWpJgxiswgWU vGA1UaP7CQk0YNkzBqaBpnt7ZQ7MBCCgD3CtSCFnstw0z12JDXEKYSOliRdokRjqmWU8 9frjx3Xq/srxDx25n0Md+0PY0YQqrHof3KIhnYV5WXLfhC7xQ7wE8+a9HuGU9FXrtGjO yU7D2K4Y5FoMhV0XFULtHYcFu1dpVBArzJinbKeCqGX5etfeP+5FaU8hnmieAb4wibRl 2Mgg== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=i1zvFPJq3v2KyA7XcikG9ZH5z+CYLuNsGMl787KYslM=; b=EIH5ih6bZURm/aAry4YnQIeaL8n9RnyUP2Cw22+peie8KARk3nJPPpuVnMJeN3+pSM L4+yCTlM+dUsvlH45xNr6Xd1/0kxJ/629I+yg7Yqlz/ZrtIhfkoRvcMMbjhL0DW3V71A ulkY/Itdf1TRkbg7dl9zxuY2JxogBt/6UMJhM/um9gL5S+UlDw8HQKZEl1bxp1iboJE6 apvirzaw3ByAI2wKfi3iViDpKGq4h4XVdnb577nnLfs68wT7hYHdjnT6IZO2g8WTGwL0 5UtH6bbj6gBjB2AhymQp3lJXyqRcHlLz7+9ntznafJalp8j7muznHEI4uRczxFIJXrYx BscQ== |
| X-Gm-Message-State | ALyK8tIe1lXVexm4k6lPjSs6pgUlOYpRs1xlv4ooK966HKmKK3KP104m165E75IHYUFMtAYt3ifXl5GB0mghfA== |
| X-Received | by 10.25.168.202 with SMTP id r193mr10108658lfe.85.1466587136306; Wed, 22 Jun 2016 02:18:56 -0700 (PDT) |
| In-Reply-To | <576A3ADA.5060000@rece.vub.ac.be> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <CAHVvXxRJ8daxUnqXwL4e3y1bi79+GXciiETwsc9DPQsMh9uwVg@mail.gmail.com> |
| X-Mailman-Original-References | <5769fc9b$0$1622$c3e8da3$5496439d@news.astraweb.com> <576A3ADA.5060000@rece.vub.ac.be> |
| Xref | csiph.com comp.lang.python:110308 |
Show key headers only | View raw
On 22 June 2016 at 08:14, Antoon Pardon <antoon.pardon@rece.vub.ac.be> wrote:
> Op 22-06-16 om 04:48 schreef Steven D'Aprano:
>> I'm doing some arithmetic on complex numbers involving INFs, and getting
>> unexpected NANs.
>>
>> py> INF = float('inf')
>> py> z = INF + 3j
>> py> z
>> (inf+3j)
>> py> -z
>> (-inf-3j)
>>
>> So far, nothing unexpected has occurred. But:
>>
>> py> -1*z # should be the same as -z
>> (-inf+nanj)
>
> Also the multiplication of a+bj with c+dj is (ac-bd)+(ad+bc)j
> With your "numbers" this gives.
>
> (inf*(-1) - 3*0) + (inf*0 + 3*(-1))j
>
> Again the nan part makes perfect sense.
This explains presumably how complex multiplication is implemented
although it's not really a mathematical justification.
The problem is that mathematically complex numbers distribute over
addition. Complex floats (i.e. Python's complex type) for the most
part distribute approximately over addition so that the expansion
Antoon refers to will usually be correct up to floating point error:
(1) (x1 + j*y1) * (x2 + j*y2) ~= (x1*x2 - y1*y2) + j*(x1*y2 + x2*y1)
We can think of this as applying the distribution rule twice twice
(once for each pair of brackets). If the first complex number is real
it should have zero real part i.e. y1 = 0. This leads to
(2) (x1 + j*y1) * (x2 + j*y2) = x1 * (x2 + j*y2) = x1*x2 + j*x1*y2
which is I think the kind of formula Steve is expecting when being
surprised above. However we also have using the full distributed
formula (1):
(3) (x1 + j*y1)*(x2 + j*y2) = x1*x2 - 0*y2 + j*(x1*y2 + x2*0)
If y2 and x2 are real then the RHSs of (3) and (2) are equivalent.
However in your example x2 is infinity.
In essence the problem is that the set of numbers represented by
Python's complex type is the set of complex numbers plus a few
infinities/nans. The special values break the distributivity of
addition so that the algebra implicit in the implementation of the
complex type doesn't hold.
To see that infinities/nans break distribution over addition consider:
inf = inf*1 = inf*(3-2) = inf*3-inf*2 = inf-inf = nan
If you did want to change these cases to match some particular idea of
the infinities as limits then special casing for pure real or
imaginary complex numbers (e.g. using (2) for complex numbers with
zero imaginary part) would probably lead to the results Steve expects.
I'm not really sure what the gain is though since we can't really do
robust algebra with infinities anyway.
--
Oscar
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Unexpected NANs in complex arithmetic Steven D'Aprano <steve@pearwood.info> - 2016-06-22 12:48 +1000
Re: Unexpected NANs in complex arithmetic Chris Angelico <rosuav@gmail.com> - 2016-06-22 12:57 +1000
Re: Unexpected NANs in complex arithmetic Dan Sommers <dan@tombstonezero.net> - 2016-06-22 03:54 +0000
Re: Unexpected NANs in complex arithmetic Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-22 16:17 +1000
Re: Unexpected NANs in complex arithmetic Paul Rubin <no.email@nospam.invalid> - 2016-06-21 23:25 -0700
Re: Unexpected NANs in complex arithmetic Chris Angelico <rosuav@gmail.com> - 2016-06-22 16:30 +1000
Re: Unexpected NANs in complex arithmetic Dan Sommers <dan@tombstonezero.net> - 2016-06-22 22:54 +0000
Re: Unexpected NANs in complex arithmetic Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-22 16:58 +1000
Re: Unexpected NANs in complex arithmetic Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-22 17:05 +1000
Re: Unexpected NANs in complex arithmetic Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-06-22 09:14 +0200
Re: Unexpected NANs in complex arithmetic Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-06-22 10:18 +0100
csiph-web