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


Groups > comp.lang.python > #89105

Re: Need opinions on P vs NP

Path csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.117
X-Spam-Level *
X-Spam-Evidence '*H*': 0.77; '*S*': 0.00; 'column': 0.07; 'rows': 0.09; 'def': 0.12; 'columns': 0.16; 'conditional': 0.16; 'reminded': 0.16; 'subscripting': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'version.': 0.19; 'seems': 0.21; 'mathematical': 0.24; 'versions': 0.24; 'equivalent': 0.26; 'first,': 0.26; 'second': 0.26; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'involving': 0.30; 'relative': 0.30; 'said,': 0.30; 'message- id:@mail.gmail.com': 0.30; 'gives': 0.31; 'code': 0.31; 'comments': 0.31; 'routine': 0.31; 'fri,': 0.33; 'third': 0.33; 'except': 0.35; 'beyond': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'false': 0.36; 'version,': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'expression': 0.60; "you're": 0.61; 'first': 0.61; 'here:': 0.62; "you've": 0.63; 'subject:Need': 0.64; 'more': 0.64; 'different': 0.65; 'url:blogspot': 0.65; 'url:co': 0.67; 'receive': 0.70; 'opinions': 0.70; 'url:v': 0.71; 'url:youtube': 0.71; 'url:watch': 0.77; '2015': 0.84
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 :content-type:content-transfer-encoding; bh=TGgWpBHbtOtRPKZu3VqZNgHR05Ii6w9Q4JmRv48Xctc=; b=glx1Psso+oQehp9fGs3d/EtKPvVcH+EV30Y11sjjpzUW9DOXVxXM+yvHNKrXBorO7o ntKxbttURbbCWeCOoII2/+Czy71ePwipojvYOr3S7qh8IkSTs+ajnUfCN2TzuESQ5Wl2 FoTv6uCctaQFG/zBdYV80U7XLnAGb/W5jzMsvvzkrGwQr03T6jIIzO8EezMEGTXYw/D8 ruA1qLW4sUhl4n2WO+zTLzlhhmv1rM57vuRflueacFQM4xQa632Rc8K7LDDN0vqQ4x/y pbZ9pqYWSAsSdmBBl4WVX0L708EAApL4XKJCU5b3PeRO5xQmUjiEBJ8z0rRUEAOA/LlG gFCg==
X-Received by 10.43.39.1 with SMTP id tk1mr6797721icb.26.1429324452740; Fri, 17 Apr 2015 19:34:12 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <f2e51cb7-dbcf-4679-baf3-9b006b82e08c@googlegroups.com>
References <f2e51cb7-dbcf-4679-baf3-9b006b82e08c@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 17 Apr 2015 20:33:32 -0600
Subject Re: Need opinions on P vs NP
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20
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>
Newsgroups comp.lang.python
Message-ID <mailman.389.1429324454.12925.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1429324454 news.xs4all.nl 2915 [2001:888:2000:d::a6]:38806
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89105

Show key headers only | View raw


On Fri, Apr 17, 2015 at 7:19 PM, Paddy <paddy3118@gmail.com> wrote:
> Having just seen Raymond's talk on Beyond PEP-8 here: https://www.youtube.com/watch?v=wf-BqAjZb8M, it reminded me of my own recent post where I am soliciting opinions from non-newbies on the relative Pythonicity of different versions of a routine that has non-simple array manipulations.
>
> The blog post: http://paddy3118.blogspot.co.uk/2015/04/pythonic-matrix-manipulation.html
>
> The first, (and original), code sample:
>
> def cholesky(A):
>     L = [[0.0] * len(A) for _ in range(len(A))]
>     for i in range(len(A)):
>         for j in range(i+1):
>             s = sum(L[i][k] * L[j][k] for k in range(j))
>             L[i][j] = sqrt(A[i][i] - s) if (i == j) else \
>                       (1.0 / L[j][j] * (A[i][j] - s))
>     return L
>
>
> The second equivalent code sample:
>
> def cholesky2(A):
>     L = [[0.0] * len(A) for _ in range(len(A))]
>     for i, (Ai, Li) in enumerate(zip(A, L)):
>         for j, Lj in enumerate(L[:i+1]):
>             s = sum(Li[k] * Lj[k] for k in range(j))
>             Li[j] = sqrt(Ai[i] - s) if (i == j) else \
>                       (1.0 / Lj[j] * (Ai[j] - s))
>     return L
>
>
> The third:
>
> def cholesky3(A):
>     L = [[0.0] * len(A) for _ in range(len(A))]
>     for i, (Ai, Li) in enumerate(zip(A, L)):
>         for j, Lj in enumerate(L[:i]):
>             #s = fsum(Li[k] * Lj[k] for k in range(j))
>             s = fsum(Lik * Ljk for Lik, Ljk in zip(Li, Lj[:j]))
>             Li[j] = (1.0 / Lj[j] * (Ai[j] - s))
>         s = fsum(Lik * Lik for Lik in Li[:i])
>         Li[i] = sqrt(Ai[i] - s)
>     return L
>
> My blog post gives a little more explanation, but I have yet to receive any comments on relative Pythonicity.

I prefer the first version. You're dealing with mathematical formulas
involving matrices here, so subscripting seems appropriate, and
enumerating out rows and columns just feels weird to me.

That said, I also prefer how the third version pulls the last column
of each row out of the inner loop instead of using a verbose
conditional expression that you already know will be false for every
column except the last one. Do that in the first version, and I think
you've got it.

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


Thread

Need opinions on P vs NP Paddy <paddy3118@gmail.com> - 2015-04-17 18:19 -0700
  Re: Need opinions on P vs NP Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-17 20:33 -0600
    Re: Need opinions on P vs NP Paddy <paddy3118@gmail.com> - 2015-04-17 22:11 -0700
  Re: Need opinions on P vs NP wxjmfauth@gmail.com - 2015-04-18 00:08 -0700
    Re: Need opinions on P vs NP Paddy <paddy3118@gmail.com> - 2015-04-18 01:08 -0700

csiph-web