Need opinions on P vs NP

Paddy paddy3118 at gmail.com
Sat Apr 18 10:08:43 CEST 2015


On Saturday, 18 April 2015 08:09:06 UTC+1, wxjm... at gmail.com  wrote:
> Le samedi 18 avril 2015 03:19:40 UTC+2, Paddy a écrit :
> > 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.
> 
> ========
> 
> def cholesky999(A):
>     n = len(A)
>     L = [[0.0] * n for i in range(n)]
>     for i in range(n):
>         for j in range(i+1):
>             s = 0.0
>             for k in range(j):
>                 s += L[i][k] * L[j][k]
>             if i == j:
>                 L[i][j] = sqrt(A[i][i] - s)
>             else:
>                 L[i][j] = (1.0 / L[j][j] * (A[i][j] - s))
>     return L
> 
> Simple, clear, logical, consistant.

And so most Pythonic? Maybe so.



More information about the Python-list mailing list