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: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98040 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.