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


Groups > comp.lang.python > #98035

Re: LU decomposition

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: LU decomposition
Date Sun, 1 Nov 2015 21:18:37 +1100
Lines 42
Message-ID <mailman.14.1446373125.4463.python-list@python.org> (permalink)
References <1e6c4ce1-885c-43ac-b0a5-054f46d4c96e@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de le26RVIyV8FlZ8h496oSJwTLAsjfO5Uhh3XLVOc45HaQ==
Return-Path <rosuav@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; 'line:': 0.07; 'cc:addr :python-list': 0.09; 'assigning': 0.09; 'iterate': 0.09; 'def': 0.13; 'mathematics': 0.15; 'things.': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration': 0.16; 'names?': 0.16; 'numpy': 0.16; 'pythonic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subscripting': 0.16; 'wrote:': 0.16; 'variable': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fairly': 0.22; 'names.': 0.22; 'seems': 0.23; 'header:In-Reply-To:1': 0.24; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'start,': 0.27; 'subject:skip:d 10': 0.27; 'function': 0.28; 'actual': 0.28; 'factor': 0.29; 'way?': 0.29; 'array': 0.29; "i'm": 0.30; 'that.': 0.30; 'code': 0.30; "i'd": 0.31; 'generally': 0.32; "i'll": 0.33; 'except': 0.34; 'worked': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'behind': 0.35; 'could': 0.35; 'nov': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'beyond': 0.37; 'one,': 0.37; 'received:209.85.213': 0.37; 'itself': 0.38; 'received:209': 0.38; 'names': 0.38; 'end': 0.39; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'skip:n 10': 0.62; 'more': 0.63; 'here': 0.66; 'experts': 0.70; 'evaluate': 0.72; 'saw': 0.77; 'chrisa': 0.84; 'cuts': 0.84; 'difference.': 0.84; 'duplication,': 0.84; 'to:none': 0.91; 'careful': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=8T9i9/gAgetgiRcfBuZDN6QRvV7yxAjgYHn6GCZboqU=; b=YWJa4ko2urXA4I3gKFSWhi8J8DPDnAC6hvM3PMN4tnxsQFMOutJo+ATKTZ/XryfMMp VNrSaTSkoXYCQe5suLiPC7843Ul+XlV/mLY8CmGPJePNv5bfj3+kU0My+PpiLspKyo9O cULwePtZnTqMEtve3zhoBWZ8YEXl+9IddVbCt2EFv2ZJeUXSsVJh7GfFmigrjAyKHDnV KkcFbz+oBXgcoJGt1nAdRWVtAdl7YotG53o/UxVo7tCuI+3afAm+ct2wUEcpWmgo+ddW /+V93+o86cDjiRhBaFnFciuvqbnX/TB9ZJwlbvbRtLG/Ph3C7vD/zfxeuTp4CEcVAD8G /uhQ==
X-Received by 10.50.83.104 with SMTP id p8mr6265687igy.13.1446373117572; Sun, 01 Nov 2015 02:18:37 -0800 (PST)
In-Reply-To <1e6c4ce1-885c-43ac-b0a5-054f46d4c96e@googlegroups.com>
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>
Xref csiph.com comp.lang.python:98035

Show key headers only | View raw


On Sun, Nov 1, 2015 at 9:04 PM, gers antifx <schweiger.gerald@gmail.com> wrote:
> I have to write a LU-decomposition. My Code worked so far but (I want to become better:) ) I want to ask you, if I could write this LU-decomposition in a better way?
>
> def LU(x):
>     L = np.eye((x.shape[0]))
>     n = x.shape[0]
>     for ii in range(n-1):
>         for ll in range(1+ii,n):
>             factor = float(x[ll,ii])/x[ii,ii]
>             L[ll,ii] = factor
>             for kk in range(0+ii,n):
>                     x[ll,kk] = x[ll,kk] - faktor*x[ii,kk]
>     LU = np.dot(L,x)

For a start, I would recommend being careful with your variable names.
You have 'factor' all except one, where you have 'faktor' -
transcription error, or nearly-identical global and local names? And
all your other names are fairly opaque; can they be better named? I'm
particularly looking at this line:

x[ll,kk] = x[ll,kk] - faktor*x[ii,kk]

It is *extremely not obvious* that the first two are using 'll' and
the last one is using 'ii'. (Though I would write this as "x[ll,kk] -=
faktor*x[ii,kk]", which at least cuts down the duplication, so it's
less glitchy to have one out of three that's different.) I was going
to ask if you had some reason for not inverting the factor and simply
using "x[ll,kk] *= factor", till I read it again and saw the
difference.

I'm seeing here a lot of iteration over ranges, then subscripting with
that. Is it possible to iterate over the numpy array itself instead?
That's generally a more Pythonic way to do things.

Assigning to the local name LU at the end of the function seems odd.
Did you intend to return the dot-product?

Beyond that, I'd need a better comprehension of the mathematics behind
this, to evaluate what it's doing. So I'll let the actual experts dive
in :)

ChrisA

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


Thread

LU decomposition gers antifx <schweiger.gerald@gmail.com> - 2015-11-01 02:04 -0800
  Re: LU decomposition Chris Angelico <rosuav@gmail.com> - 2015-11-01 21:18 +1100
  Re: LU decomposition Peter Otten <__peter__@web.de> - 2015-11-01 12:55 +0100
  Re: LU decomposition Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-11-03 15:52 +0000
  Re: LU decomposition Nagy László Zsolt <gandalf@shopzeus.com> - 2015-11-03 19:21 +0100

csiph-web