Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: LU decomposition Date: Tue, 3 Nov 2015 15:52:27 +0000 Lines: 27 Message-ID: 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 cmuPJ292jryU0Fka5OQESgMqUq27atOVFo7LUTYXfFFA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'context': 0.05; 'cc:addr :python-list': 0.09; 'spelled': 0.09; 'python': 0.10; 'python.': 0.11; 'def': 0.13; 'bit.': 0.16; 'cc:name:python list': 0.16; 'matlab': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'algorithm': 0.20; 'header:In-Reply- To:1': 0.24; 'sort': 0.25; 'checking': 0.27; 'message- id:@mail.gmail.com': 0.27; 'subject:skip:d 10': 0.27; 'factor': 0.29; 'way?': 0.29; 'code': 0.30; 'e.g.': 0.30; 'another': 0.32; 'common': 0.33; 'worked': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'problem.': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'sure': 0.39; 'where': 0.40; "you'll": 0.61; 'more': 0.63; 'natural': 0.67; '10:04,': 0.84; 'divide': 0.84; 'oscar': 0.84; 'pivoting': 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 :cc:content-type; bh=RyS4wF5tskKIZIwI2sZ5XpeJnUPWKBA9higljhxuUUY=; b=r7pq1U8XEdQoyeAJEq/xPp3OKl1vwf62vaJYdau/6AaSDMir04ATRa+eNJAV0Dh7lC io1A7DVZ5oHEhAzEd6QImgY20//P0E0tUTTSFGGBzpfynt6abCKESa3IgiI3JSFWediE ov/saS7/+iniSvwJKRJMW553c1gND0FpQnQFIzicr9+jKok8g9th3usV5jilSDSmuJIa IKZXV0dj1tybp71jBgSUL5vPzyjcmyONxF8QGqnqoJt2GjS3h6b3k66HtAY/qaUqyush MnOf8jV0jIgGPXJwUNUvgXBf5pe2WD7jxxTBWtm10OM0w54Df5kfMu8UP2ev9+s/7dux +M4Q== X-Received: by 10.112.157.36 with SMTP id wj4mr4800392lbb.100.1446565967226; Tue, 03 Nov 2015 07:52:47 -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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98168 On 1 November 2015 at 10:04, gers antifx 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) You're using ii and ll, kk etc. This is common practise in Matlab code because Matlab uses i as sqrt(-1) but this is spelled 1j in Python so we don't have that problem. In this sort of context it would be more natural to use simply i, j, k in Python. Another is that you're not checking for divide by zero in float(x[ll,ii])/x[ii,ii]. Are you sure that it will not be zero? What happens if e.g. x[0, 0] is zero? This is where you'll need to use pivoting which complicates the algorithm a bit. -- Oscar