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


Groups > comp.lang.python > #98040

Re: LU decomposition

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: LU decomposition
Date Sun, 01 Nov 2015 12:55:50 +0100
Organization None
Lines 44
Message-ID <mailman.19.1446378964.4463.python-list@python.org> (permalink)
References <1e6c4ce1-885c-43ac-b0a5-054f46d4c96e@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de pDaAN/j4cSqLSw7POsH3gw5l0VnFU+4XSBMSLORts+rA==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '"__main__":': 0.07; '__name__': 0.07; 'unittest': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'better?': 0.16; 'loops': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'pointed': 0.18; 'tests': 0.18; 'changes': 0.20; 'import': 0.24; 'implemented': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'header:X -Complaints-To:1': 0.26; 'chris': 0.26; 'subject:skip:d 10': 0.27; 'interface': 0.29; 'factor': 0.29; 'way?': 0.29; 'array': 0.29; 'code': 0.30; 'operations': 0.31; 'point': 0.33; 'class': 0.33; 'rule': 0.33; 'similar': 0.33; 'quickly': 0.34; 'worked': 0.34; 'could': 0.35; 'replace': 0.35; 'unit': 0.35; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'possible.': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'google': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'skip:u 10': 0.61; 'real': 0.62; 'skip:n 10': 0.62; 'making': 0.62; 'url:0': 0.63; 'levels': 0.70; 'url:scipy': 0.84; 'url:reference': 0.91; 'url:14': 0.95
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8ef4.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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:98040

Show key headers only | View raw


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 want to become better? A good way to find the problems Chris pointed out 
quickly is to write unit tests similar to

import unittest
...
from mymodule import LU

class LUTest(unittest.TestCase):
    def test_LU(self):
        matrix = ...
        expected_result = ...
        self.assertEqual(LU(matrix), expected_result)

if __name__ == "__main__":
   unittest.main()

There's no point making stylistic changes or tweaks to improve performance 
on code that doesn't work and doesn't have a well-defined interface yet,
but as a rule of thumb for efficient number-crunching you should try to 
replace for loops with numpy's array operations if at all possible.
Google found me 

http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.linalg.lu.html

but it's hard to learn from that code as the real meat is a few levels down 
and implemented in C.

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