NULAPACK
NUmerical Linear Algebra PACKage
Loading...
Searching...
No Matches
cholesky.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 cholesky(a: np.ndarray):
34 """
35 Compute the Cholesky factorization of a symmetric/Hermitian
36 positive-definite matrix A using NULAPACK.
37
38 Parameters
39 ----------
40 a : ndarray
41 Coefficient matrix (n x n) stored as a full matrix. Real matrices
42 should be symmetric, complex matrices should be Hermitian and
43 positive-definite.
44
45 Returns
46 -------
47 L : ndarray
48 Lower-triangular matrix from the factorization (A = L * L^T or
49 A = L * L^H).
50 info : int
51 0 if success, >0 if the matrix is not positive-definite.
52 """
53 a = np.ascontiguousarray(a)
54 n = a.shape[0]
55 lda = n
56
57 a_flat = a.ravel(order="C")
58 l_flat = np.zeros_like(a_flat)
59 info = np.zeros(1, dtype=np.int32)
60
61 if np.issubdtype(a.dtype, np.floating):
62 if a.dtype == np.float32:
63 _nulapack.spoctrf(n, a_flat, l_flat, lda, info)
64 else: # float64
65 _nulapack.dpoctrf(n, a_flat, l_flat, lda, info)
66 elif np.issubdtype(a.dtype, np.complexfloating):
67 if a.dtype == np.complex64:
68 _nulapack.cpoctrf(n, a_flat, l_flat, lda, info)
69 else: # complex128
70 _nulapack.zpoctrf(n, a_flat, l_flat, lda, info)
71 else:
72 raise TypeError(f"Unsupported array dtype: {a.dtype}")
73
74 return np.tril(l_flat.reshape(n, n, order="C")), int(info[0])