Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98040
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: LU decomposition |
| Date | 2015-11-01 12:55 +0100 |
| Organization | None |
| Message-ID | <mailman.19.1446378964.4463.python-list@python.org> (permalink) |
| References | <1e6c4ce1-885c-43ac-b0a5-054f46d4c96e@googlegroups.com> |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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