1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use cmp;
use crate::;
use crate profiling;
const NB: usize = 32;
// #[allow(unsafe_op_in_unsafe_fn, clippy::missing_safety_doc)]
// pub unsafe fn potrf(uplo: char, n: usize, a: *mut f64, lda: usize) -> Result<(), String> {
// #[cfg(feature = "profiling")]
// let _timer = profiling::ScopedTimer::new("POTRF");
// if uplo != 'U' && uplo != 'L' { return Err("Argument 1 to potrf had an illegal value".to_string()); }
// if lda < n.max(1) { return Err("Argument 4 to potrf had an illegal value".to_string()); }
// // --- SIMPLIFIED IMPLEMENTATION ---
// // The blocked algorithm is removed to bypass bugs in the underlying BLAS calls.
// // This directly calls the correct, iterative kernel.
// potrf2(uplo, n, a, lda)
// }
// #[allow(unsafe_op_in_unsafe_fn, clippy::missing_safety_doc)]
// pub unsafe fn potrf2(uplo: char, n: usize, a: *mut f64, lda: usize) -> Result<(), String> {
// #[cfg(feature = "profiling")]
// let _timer = profiling::ScopedTimer::new("POTRF2");
// // Corrected, robust, iterative Cholesky factorization.
// if uplo == 'L' {
// for j in 0..n {
// // Update the diagonal element A(j, j)
// let mut s = 0.0;
// for k in 0..j {
// let v = *a.add(j + k * lda);
// s += v * v;
// }
// s = *a.add(j + j * lda) - s;
// if s <= 0.0 {
// return Err(format!("Matrix is not positive-definite. Failure at column {}.", j + 1));
// }
// let l_jj = s.sqrt();
// *a.add(j + j * lda) = l_jj;
// // Update the rest of column j
// for i in (j + 1)..n {
// let mut s = 0.0;
// for k in 0..j {
// s += *a.add(i + k * lda) * *a.add(j + k * lda);
// }
// *a.add(i + j * lda) = (*a.add(i + j * lda) - s) / l_jj;
// }
// }
// } else { // uplo == 'U'
// for j in 0..n {
// let mut s = 0.0;
// for k in 0..j {
// let v = *a.add(k + j * lda);
// s += v * v;
// }
// s = *a.add(j + j * lda) - s;
// if s <= 0.0 {
// return Err(format!("Matrix is not positive-definite. Failure at column {}.", j + 1));
// }
// let u_jj = s.sqrt();
// *a.add(j + j * lda) = u_jj;
// // Update the rest of row j
// for i in (j + 1)..n {
// let mut s = 0.0;
// for k in 0..j {
// s += *a.add(k + i * lda) * *a.add(k + j * lda);
// }
// *a.add(j + i * lda) = (*a.add(j + i * lda) - s) / u_jj;
// }
// }
// }
// Ok(())
// }
/// Computes the Cholesky factorisation of a symmetric, positive-definite matrix.
/// - If `uplo` = 'U', then $A = U^T U$, where `U` is an upper triangular matrix.
/// - If `uplo` = 'L', then $A = L L^T$, where `L` is a lower triangular matrix.
///
/// # Arguments
/// * `uplo` - A character specifying which triangular part of `A` is stored:
/// - 'U' or 'u': Upper triangle of `A` is stored.
/// - 'L' or 'l': Lower triangle of `A` is stored.
/// * `n` - The order of the matrix `A`. `n` must be non-negative.
/// * `a` - A raw mutable pointer to the first element of the `N`-by-`N` matrix `A`
/// (in column-major order). On exit, the specified `uplo` part of `A`
/// is overwritten with the corresponding factor `U` or `L`.
/// * `lda` - The leading dimension of the matrix `A`. `lda` must be at least `max(1, n)`.
///
/// # Returns
/// * `Ok(())` - If the factorization completed successfully.
/// * `Err(String)`
pub unsafe