primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
Documentation
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Tridiagonal matrix solver (Thomas algorithm)
//!
//! Solves tridiagonal linear systems A·x = d in O(n) time using
//! the Thomas algorithm (modified Gaussian elimination).
//!
//! # System Structure
//!
//! ```text
//! | b₀  c₀  0   0   ...  0  | | x₀ |   | d₀ |
//! | a₁  b₁  c₁  0   ...  0  | | x₁ |   | d₁ |
//! | 0   a₂  b₂  c₂  ...  0  | | x₂ | = | d₂ |
//! | ⋮                    ⋮  | | ⋮  |   | ⋮  |
//! | 0   ...  0  aₙ₋₁ bₙ₋₁ | | xₙ₋₁|   | dₙ₋₁|
//! ```
//!
//! # Applications
//!
//! - **Crank-Nicolson PDE** (TTM heat equation)
//! - **Cubic spline interpolation**
//! - **Implicit finite difference schemes**
//!
//! # References
//!
//! - Numerical Recipes, §2.4
//! - DLMF: <https://dlmf.nist.gov>

use crate::error::{BarracudaError, Result};

/// Solve a tridiagonal system A·x = d using the Thomas algorithm.
///
/// # Arguments
///
/// * `a` - Sub-diagonal (length n-1): a[i] is A[i+1, i]
/// * `b` - Main diagonal (length n): b[i] is A[i, i]
/// * `c` - Super-diagonal (length n-1): c[i] is A[i, i+1]
/// * `d` - Right-hand side (length n)
///
/// # Returns
///
/// Solution vector x of length n.
///
/// # Complexity
///
/// O(n) time, O(n) space for temporary arrays.
///
/// # Examples
///
/// ```
/// use barracuda::ops::linalg::tridiagonal::tridiagonal_solve;
///
/// // Simple 3x3 system
/// let a = vec![1.0, 1.0];           // sub-diagonal
/// let b = vec![4.0, 4.0, 4.0];      // main diagonal
/// let c = vec![1.0, 1.0];           // super-diagonal
/// let d = vec![1.0, 2.0, 1.0];      // RHS
///
/// let x = tridiagonal_solve(&a, &b, &c, &d).unwrap();
/// // Verify: A·x ≈ d
/// ```
///
/// # Errors
///
/// Returns [`Err`] if system size is zero, diagonal lengths are mismatched (a and c must have
/// length n-1, d must have length n), or if the matrix is singular (division by zero).
pub fn tridiagonal_solve(a: &[f64], b: &[f64], c: &[f64], d: &[f64]) -> Result<Vec<f64>> {
    let n = b.len();

    if n == 0 {
        return Err(BarracudaError::InvalidInput {
            message: "System size must be > 0".to_string(),
        });
    }

    if a.len() != n - 1 || c.len() != n - 1 || d.len() != n {
        return Err(BarracudaError::InvalidInput {
            message: format!(
                "Dimension mismatch: a.len()={}, b.len()={}, c.len()={}, d.len()={}",
                a.len(),
                b.len(),
                c.len(),
                d.len()
            ),
        });
    }

    // Special case: n=1
    if n == 1 {
        if b[0].abs() < 1e-15 {
            return Err(BarracudaError::Numerical {
                message: "Division by zero: b[0] is near zero".to_string(),
            });
        }
        return Ok(vec![d[0] / b[0]]);
    }

    // Forward sweep: eliminate sub-diagonal
    let mut c_prime = vec![0.0; n - 1];
    let mut d_prime = vec![0.0; n];

    // First row
    if b[0].abs() < 1e-15 {
        return Err(BarracudaError::Numerical {
            message: "Division by zero: b[0] is near zero".to_string(),
        });
    }
    c_prime[0] = c[0] / b[0];
    d_prime[0] = d[0] / b[0];

    // Forward sweep for i = 1..n-1
    for i in 1..n {
        let denom = a[i - 1].mul_add(-c_prime[i - 1], b[i]);
        if denom.abs() < 1e-15 {
            return Err(BarracudaError::Numerical {
                message: format!("Division by zero at row {i}: matrix may be singular"),
            });
        }
        if i < n - 1 {
            c_prime[i] = c[i] / denom;
        }
        d_prime[i] = a[i - 1].mul_add(-d_prime[i - 1], d[i]) / denom;
    }

    // Back substitution
    let mut x = vec![0.0; n];
    x[n - 1] = d_prime[n - 1];

    for i in (0..n - 1).rev() {
        x[i] = c_prime[i].mul_add(-x[i + 1], d_prime[i]);
    }

    Ok(x)
}

/// Solve a tridiagonal system with f32 precision.
///
/// Same as [`tridiagonal_solve`] but uses f32 for GPU compatibility.
///
/// # Errors
///
/// Returns [`Err`] if system size is zero, diagonal lengths are mismatched, or if the matrix
/// is singular (division by zero).
pub fn tridiagonal_solve_f32(a: &[f32], b: &[f32], c: &[f32], d: &[f32]) -> Result<Vec<f32>> {
    let n = b.len();

    if n == 0 {
        return Err(BarracudaError::InvalidInput {
            message: "System size must be > 0".to_string(),
        });
    }

    if a.len() != n - 1 || c.len() != n - 1 || d.len() != n {
        return Err(BarracudaError::InvalidInput {
            message: format!(
                "Dimension mismatch: a.len()={}, b.len()={}, c.len()={}, d.len()={}",
                a.len(),
                b.len(),
                c.len(),
                d.len()
            ),
        });
    }

    if n == 1 {
        if b[0].abs() < 1e-7 {
            return Err(BarracudaError::Numerical {
                message: "Division by zero: b[0] is near zero".to_string(),
            });
        }
        return Ok(vec![d[0] / b[0]]);
    }

    let mut c_prime = vec![0.0f32; n - 1];
    let mut d_prime = vec![0.0f32; n];

    if b[0].abs() < 1e-7 {
        return Err(BarracudaError::Numerical {
            message: "Division by zero: b[0] is near zero".to_string(),
        });
    }
    c_prime[0] = c[0] / b[0];
    d_prime[0] = d[0] / b[0];

    for i in 1..n {
        let denom = a[i - 1].mul_add(-c_prime[i - 1], b[i]);
        if denom.abs() < 1e-7 {
            return Err(BarracudaError::Numerical {
                message: format!("Division by zero at row {i}: matrix may be singular"),
            });
        }
        if i < n - 1 {
            c_prime[i] = c[i] / denom;
        }
        d_prime[i] = a[i - 1].mul_add(-d_prime[i - 1], d[i]) / denom;
    }

    let mut x = vec![0.0f32; n];
    x[n - 1] = d_prime[n - 1];

    for i in (0..n - 1).rev() {
        x[i] = c_prime[i].mul_add(-x[i + 1], d_prime[i]);
    }

    Ok(x)
}

/// Solve multiple tridiagonal systems with the same structure.
///
/// Efficient when solving the same matrix A with multiple right-hand sides.
///
/// # Arguments
///
/// * `a`, `b`, `c` - Matrix diagonals (same for all systems)
/// * `rhs_batch` - Vector of right-hand sides
///
/// # Returns
///
/// Vector of solution vectors.
///
/// # Errors
///
/// Returns [`Err`] if any RHS in the batch fails [`tridiagonal_solve`].
pub fn tridiagonal_solve_batch(
    a: &[f64],
    b: &[f64],
    c: &[f64],
    rhs_batch: &[Vec<f64>],
) -> Result<Vec<Vec<f64>>> {
    rhs_batch
        .iter()
        .map(|d| tridiagonal_solve(a, b, c, d))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tridiagonal_simple() {
        // Simple diagonally dominant 3x3 system
        let a = vec![1.0, 1.0];
        let b = vec![4.0, 4.0, 4.0];
        let c = vec![1.0, 1.0];
        let d = vec![5.0, 6.0, 5.0];

        let x = tridiagonal_solve(&a, &b, &c, &d).unwrap();
        assert_eq!(x.len(), 3);

        // Verify solution by computing A·x
        let ax0 = b[0].mul_add(x[0], c[0] * x[1]);
        let ax1 = c[1].mul_add(x[2], a[0].mul_add(x[0], b[1] * x[1]));
        let ax2 = a[1].mul_add(x[1], b[2] * x[2]);

        assert!((ax0 - d[0]).abs() < 1e-12);
        assert!((ax1 - d[1]).abs() < 1e-12);
        assert!((ax2 - d[2]).abs() < 1e-12);
    }

    #[test]
    fn test_tridiagonal_identity() {
        // Identity matrix: b=1, a=c=0
        let a = vec![0.0, 0.0];
        let b = vec![1.0, 1.0, 1.0];
        let c = vec![0.0, 0.0];
        let d = vec![1.0, 2.0, 3.0];

        let x = tridiagonal_solve(&a, &b, &c, &d).unwrap();
        assert!((x[0] - 1.0).abs() < 1e-14);
        assert!((x[1] - 2.0).abs() < 1e-14);
        assert!((x[2] - 3.0).abs() < 1e-14);
    }

    #[test]
    fn test_tridiagonal_n1() {
        // Single equation
        let a: Vec<f64> = vec![];
        let b = vec![2.0];
        let c: Vec<f64> = vec![];
        let d = vec![4.0];

        let x = tridiagonal_solve(&a, &b, &c, &d).unwrap();
        assert_eq!(x.len(), 1);
        assert!((x[0] - 2.0).abs() < 1e-14);
    }

    #[test]
    fn test_tridiagonal_n2() {
        // 2x2 system
        let a = vec![1.0];
        let b = vec![2.0, 2.0];
        let c = vec![1.0];
        let d = vec![3.0, 3.0];

        let x = tridiagonal_solve(&a, &b, &c, &d).unwrap();
        assert_eq!(x.len(), 2);

        // Verify
        let ax0 = b[0].mul_add(x[0], c[0] * x[1]);
        let ax1 = a[0].mul_add(x[0], b[1] * x[1]);
        assert!((ax0 - d[0]).abs() < 1e-12);
        assert!((ax1 - d[1]).abs() < 1e-12);
    }

    #[test]
    fn test_tridiagonal_heat_equation() {
        // Heat equation discretization matrix
        // -u_{i-1} + 2u_i - u_{i+1} = f_i
        let n = 10;
        let a = vec![-1.0; n - 1];
        let b = vec![2.0; n];
        let c = vec![-1.0; n - 1];
        let d = vec![1.0; n]; // uniform source

        let x = tridiagonal_solve(&a, &b, &c, &d).unwrap();
        assert_eq!(x.len(), n);

        // Verify residual is small
        for i in 0..n {
            let ax_i = if i == 0 {
                b[0].mul_add(x[0], c[0] * x[1])
            } else if i == n - 1 {
                a[n - 2].mul_add(x[n - 2], b[n - 1] * x[n - 1])
            } else {
                c[i].mul_add(x[i + 1], a[i - 1].mul_add(x[i - 1], b[i] * x[i]))
            };
            assert!(
                (ax_i - d[i]).abs() < 1e-10,
                "Residual at {}: {}",
                i,
                ax_i - d[i]
            );
        }
    }

    #[test]
    fn test_tridiagonal_f32() {
        let a = vec![1.0f32, 1.0];
        let b = vec![4.0f32, 4.0, 4.0];
        let c = vec![1.0f32, 1.0];
        let d = vec![5.0f32, 6.0, 5.0];

        let x = tridiagonal_solve_f32(&a, &b, &c, &d).unwrap();
        assert_eq!(x.len(), 3);

        let ax0 = b[0].mul_add(x[0], c[0] * x[1]);
        assert!((ax0 - d[0]).abs() < 1e-5);
    }

    #[test]
    fn test_tridiagonal_batch() {
        let a = vec![1.0, 1.0];
        let b = vec![4.0, 4.0, 4.0];
        let c = vec![1.0, 1.0];

        let rhs = vec![
            vec![1.0, 2.0, 1.0],
            vec![2.0, 4.0, 2.0],
            vec![0.0, 1.0, 0.0],
        ];

        let solutions = tridiagonal_solve_batch(&a, &b, &c, &rhs).unwrap();
        assert_eq!(solutions.len(), 3);

        // Each solution should satisfy A·x = d
        for (sol, d) in solutions.iter().zip(rhs.iter()) {
            let ax0 = b[0].mul_add(sol[0], c[0] * sol[1]);
            assert!((ax0 - d[0]).abs() < 1e-12);
        }
    }

    #[test]
    fn test_tridiagonal_error_empty() {
        let a: Vec<f64> = vec![];
        let b: Vec<f64> = vec![];
        let c: Vec<f64> = vec![];
        let d: Vec<f64> = vec![];

        assert!(tridiagonal_solve(&a, &b, &c, &d).is_err());
    }

    #[test]
    fn test_tridiagonal_error_mismatch() {
        let a = vec![1.0];
        let b = vec![2.0, 2.0, 2.0]; // n=3
        let c = vec![1.0]; // should be length 2
        let d = vec![1.0, 1.0, 1.0];

        assert!(tridiagonal_solve(&a, &b, &c, &d).is_err());
    }

    #[test]
    fn test_tridiagonal_large() {
        // Larger system
        let n = 100;
        let a = vec![1.0; n - 1];
        let b = vec![4.0; n];
        let c = vec![1.0; n - 1];
        let d: Vec<f64> = (0..n).map(|i| (i + 1) as f64).collect();

        let x = tridiagonal_solve(&a, &b, &c, &d).unwrap();
        assert_eq!(x.len(), n);

        // Check a few residuals
        let ax0 = b[0].mul_add(x[0], c[0] * x[1]);
        assert!((ax0 - d[0]).abs() < 1e-10);
    }
}