NULAPACK
NUmerical Linear Algebra PACKage
Loading...
Searching...
No Matches
doolittle.py
Go to the documentation of this file.
1# ====================================================================
2# N U L A P A C K
3# U U L A P A C K
4# L L L A P A C K
5# A A A A P A C K
6# P P P P P A C K
7# A A A A A A C K
8# C C C C C C C K
9# K K K K K K K K
10#
11# This file is part of NULAPACK - NUmerical Linear Algebra PACKage
12#
13# Copyright (C) 2025 Saud Zahir
14#
15# NULAPACK is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# NULAPACK is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with NULAPACK. If not, see <https://www.gnu.org/licenses/>.
27# ====================================================================
28
29import _nulapack
30import numpy as np
31
32
33def doolittle(a: np.ndarray):
34 """
35 Compute the LU Doolittle decomposition of a general matrix A.
36
37 Parameters
38 ----------
39 a : ndarray
40 Coefficient matrix (n x n) stored as a full matrix.
41
42 Returns
43 -------
44 L : ndarray
45 Lower triangular matrix from the factorization.
46 U : ndarray
47 Upper triangular matrix from the factorization.
48 info : int
49 0 if success, <0 if a zero diagonal in U was detected.
50 """
51 a = np.ascontiguousarray(a)
52 n = a.shape[0]
53
54 a_flat = a.ravel(order="C")
55 l_flat = np.zeros_like(a_flat)
56 u_flat = np.zeros_like(a_flat)
57 info = np.zeros(1, dtype=np.int32)
58
59 if np.issubdtype(a.dtype, np.floating):
60 if a.dtype == np.float32:
61 _nulapack.sgedtrf(n, a_flat, l_flat, u_flat, info)
62 else: # float64
63 _nulapack.dgedtrf(n, a_flat, l_flat, u_flat, info)
64 elif np.issubdtype(a.dtype, np.complexfloating):
65 if a.dtype == np.complex64:
66 _nulapack.cgedtrf(n, a_flat, l_flat, u_flat, info)
67 else: # complex128
68 _nulapack.zgedtrf(n, a_flat, l_flat, u_flat, info)
69 else:
70 raise TypeError(f"Unsupported array dtype: {a.dtype}")
71
72 return l_flat.reshape(n, n, order="C"), u_flat.reshape(n, n, order="C"), int(info[0])