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


Groups > comp.lang.python > #98168

Re: LU decomposition

From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Newsgroups comp.lang.python
Subject Re: LU decomposition
Date 2015-11-03 15:52 +0000
Message-ID <mailman.32.1446565975.8789.python-list@python.org> (permalink)
References <1e6c4ce1-885c-43ac-b0a5-054f46d4c96e@googlegroups.com>

Show all headers | View raw


On 1 November 2015 at 10:04, 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)

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

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