trigivs 0.2.3

Rust library for solving tridiagonal systems of linear equations using Givens rotations
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use crate::solver_error::SolverErrors;
use crate::solver_parts::*;

use num_traits::Float;

/// Holds tridiagonal system precomputed for working with multiple righ hand sides without heap allocation
///
/// # Example
///
/// ```
/// let sup = [-4.];
/// let diag = [3., 2.];
/// let sub = [5.];
///
/// let rhs1 = [-3., 21.];
/// let rhs2 = [-23., 5.];
///
/// let precomputed = trigivs::prelude::precompute_givens(&sup, &diag, &sub).unwrap();
///
/// let x1 = precomputed.solve_givens_rhs(&rhs1).unwrap();
/// let x2 = precomputed.solve_givens_rhs(&rhs2).unwrap();
/// ```
#[derive(Clone, Debug)]
pub struct TridiagSysPrecomp<T: Float, const D: usize, const S: usize> {
    diag: [T; D],
    sup1: Option<[T; S]>,
    sup2: Option<[T; S]>,

    sins_cosins: Option<[(T, T); S]>,
}

/// Holds precomputed tridiagonal system with Ruiz preconditioning for working with multiple righ hand sides without heap allocation
///
/// # Example
///
/// ```
/// let sup = [-4.];
/// let diag = [3., 2.];
/// let sub = [5.];
///
/// let rhs1 = [-3., 21.];
/// let rhs2 = [-23., 5.];
///
/// let precomputed = trigivs::prelude::precompute_givens_ruiz(
///     &sup,
///     &diag,
///     &sub,
///     5,
///     0.001
/// ).unwrap();
///
/// let x1 = precomputed.solve_givens_rhs(&rhs1).unwrap();
/// let x2 = precomputed.solve_givens_rhs(&rhs2).unwrap();
/// ```
#[derive(Clone, Debug)]
pub struct TridiagSysRuizPrecomp<T: Float, const D: usize, const S: usize> {
    sys: TridiagSysPrecomp<T, D, S>,

    row_mul: [T; D],
    col_mul: [T; D],
}

/// Solves a trigiagonal system of linear equations without heap allocation
///
/// # Arguments
///
/// * `sub` - subdiagonal elements, length n-1
/// * `diag` - main diagonal elements, length n
/// * `sup` - superdiagonal elements, length n-1
/// * `rhs` - right-hand side vector, length n
///
/// # Example
///
/// ```
/// let sup = [-4.];
/// let diag = [3., 2.];
/// let sub = [5.];
/// let rhs = [-3., 21.];
///
/// let x = trigivs::prelude::solve_givens(&sup, &diag, &sub, &rhs).unwrap();
/// ```
pub fn solve_givens<T: Float, const D: usize, const S: usize>(
    sup: &[T; S],
    diag: &[T; D],
    sub: &[T; S],
    rhs: &[T; D],
) -> Result<[T; D], SolverErrors> {
    const {
        assert!(
            D == S + 1,
            "Sub and sup diagonals must be exctly 1 element smaller than main diagonal"
        )
    };

    let mut a = sup.clone();
    let mut d = diag.clone();
    let mut rhs = rhs.clone();

    let mut ur = [T::zero(); S];
    let u = if S > 1 { &mut ur[..S - 1] } else { &mut [] };

    let mut x = [T::zero(); D];

    solve_givens_body(sub, &mut d, &mut a, u, &mut rhs, &mut x)?;

    Ok(x)
}

/// Precomputes a system for multiple differenr RHS without heap allocation
///
/// # Arguments
///
/// * `sup` - superdiagonal elements, length n-1
/// * `diag` - main diagonal elements, length n
/// * `sub` - subdiagonal elements, length n-1
///
/// # Example
///
/// ```
/// let sup = [-4.];
/// let diag = [3., 2.];
/// let sub = [5.];
///
/// let rhs = [-3., 21.];
///
/// let precomputed = trigivs::prelude::precompute_givens(&sup, &diag, &sub).unwrap();
///
/// ```
pub fn precompute_givens<T: Float, const D: usize, const S: usize>(
    sup: &[T; S],
    diag: &[T; D],
    sub: &[T; S],
) -> Result<TridiagSysPrecomp<T, D, S>, SolverErrors> {
    const {
        assert!(
            D == S + 1,
            "Sub and sup diagonals must be exctly 1 element smaller than main diagonal"
        )
    };

    let mut a = sup.clone();
    let mut d = diag.clone();

    let mut ur = [T::zero(); S];
    let u = if S > 1 { &mut ur[..S - 1] } else { &mut [] };

    let mut sins_cosins = [(T::zero(), T::zero()); S];

    precompute_givens_body(sub, &mut d, &mut a, u, &mut sins_cosins)?;

    Ok(TridiagSysPrecomp {
        diag: d,
        sup1: if a.len() > 0 { Some(a) } else { None },
        sup2: if u.len() > 0 { Some(ur) } else { None },
        sins_cosins: if sins_cosins.len() > 0 {
            Some(sins_cosins)
        } else {
            None
        },
    })
}

/// Precomputes a system for multiple differenr RHS with Ruiz preconditioning without heap allocation
///
/// # Arguments
///
/// * `sup` - superdiagonal elements, length n-1
/// * `diag` - main diagonal elements, length n
/// * `sub` - subdiagonal elements, length n-1
/// * `r_iter` - maximum amount of iterations for Ruiz preconditioning (impractical over 10)
/// * `r_eps` - finishing criteria for Ruiz preconditioning through maximum column/row inf-norm deviation from 1
///
/// # Example
///
/// ```
/// let sup = [-4.];
/// let diag = [3., 2.];
/// let sub = [5.];
///
/// let rhs = [-3., 21.];
///
/// let precomputed = trigivs::prelude::precompute_givens_ruiz(&sup, &diag, &sub, 5, 0.01).unwrap();
/// ```
pub fn precompute_givens_ruiz<T: Float, const D: usize, const S: usize>(
    sup: &[T; S],
    diag: &[T; D],
    sub: &[T; S],
    r_iter: usize,
    r_eps: T,
) -> Result<TridiagSysRuizPrecomp<T, D, S>, SolverErrors> {
    const {
        assert!(
            D == S + 1,
            "Sub and sup diagonals must be exctly 1 element smaller than main diagonal"
        )
    };

    let mut sub_b = sub.clone();
    let mut sup_b = sup.clone();
    let mut diag_b = diag.clone();

    let mut col_b = [T::one(); D];
    let mut row_b = [T::one(); D];

    get_ruiz_equilibrium_mul(
        &diag_b, &sup_b, &sub_b, &mut row_b, &mut col_b, r_iter, r_eps,
    )?;
    apply_ruiz(&mut diag_b, &mut sup_b, &mut sub_b, &row_b, &col_b);

    let mut sins_cosins = [(T::zero(), T::zero()); S];

    let mut ur = [T::zero(); S];
    let u = if S > 1 { &mut ur[..S - 1] } else { &mut [] };

    precompute_givens_body(&sub_b, &mut diag_b, &mut sup_b, u, &mut sins_cosins)?;

    Ok(TridiagSysRuizPrecomp {
        sys: TridiagSysPrecomp {
            diag: diag_b,
            sup1: if sup_b.len() > 0 { Some(sup_b) } else { None },
            sup2: if u.len() > 0 { Some(ur) } else { None },
            sins_cosins: if sins_cosins.len() > 0 {
                Some(sins_cosins)
            } else {
                None
            },
        },
        row_mul: row_b,
        col_mul: col_b,
    })
}

impl<T: Float, const D: usize, const S: usize> TridiagSysPrecomp<T, D, S> {
    /// Solves precomputed system with a provided right hand side without heap allocation
    ///
    /// # Arguments
    ///
    /// * `rhs` - right-hand side vector, length n
    ///
    /// # Example
    ///
    /// ```
    /// let sup = [-4.];
    /// let diag = [3., 2.];
    /// let sub = [5.];
    ///
    /// let rhs = [-3., 21.];
    ///
    /// let precomputed = trigivs::prelude::precompute_givens(&sup, &diag, &sub).unwrap();
    ///
    /// let x = precomputed.solve_givens_rhs(&rhs).unwrap();
    /// ```
    pub fn solve_givens_rhs(&self, rhs: &[T; D]) -> Result<[T; D], SolverErrors> {
        let mut rhsl = rhs.clone();

        if let Some(sins_cosins) = &self.sins_cosins {
            solve_givens_sc_rhs_body(sins_cosins, &mut rhsl);
        }

        let mut x_buffer = [T::zero(); D];

        let sup2 = self.sup2.as_ref().map_or(&[][..], |v| {
            let len = S.saturating_sub(1);
            &v[..len]
        });

        compute_x(
            &mut x_buffer,
            &rhsl,
            &self.diag,
            self.sup1.as_ref().map_or(&[], |v| v.as_slice()),
            sup2
        )?;

        Ok(x_buffer)
    }
}

impl<T: Float, const D: usize, const S: usize> TridiagSysRuizPrecomp<T, D, S> {
    /// Solves precomputed system with a provided right hand side without heap allocation
    ///
    /// # Arguments
    ///
    /// * `rhs` - right-hand side vector, length n
    ///
    /// # Example
    ///
    /// ```
    /// let sup = [-4.];
    /// let diag = [3., 2.];
    /// let sub = [5.];
    ///
    /// let rhs = [-3., 21.];
    ///
    /// let precomputed = trigivs::prelude::precompute_givens_ruiz(&sup, &diag, &sub, 5, 0.001).unwrap();
    ///
    /// let x = precomputed.solve_givens_rhs(&rhs).unwrap();
    /// ```
    pub fn solve_givens_rhs(&self, rhs: &[T; D]) -> Result<[T; D], SolverErrors> {
        let mut rhsl = rhs.clone();
        rhsl.iter_mut()
            .zip(self.row_mul)
            .for_each(|(r, row)| *r = *r * row);

        let mut x = self.sys.solve_givens_rhs(&rhsl)?;
        x.iter_mut()
            .zip(self.col_mul)
            .for_each(|(x, col)| *x = *x * col);

        Ok(x)
    }
}

/// Refines a tridiagonal system solution using the Kaczmarz iterative method without heap allocation
///
/// # Arguments
///
/// * `sup` - superdiagonal elements, length n-1
/// * `diag` - main diagonal elements, length n
/// * `sub` - subdiagonal elements, length n-1
/// * `rhs` - right-hand side vector, length n
/// * `x_init` - initial solution approximation, length n
/// * `iter` - maximum number of Kaczmarz iterations to perform
/// * `eps` - convergence tolerance
///
/// # Example
///
/// ```
/// let sub = [5.];
/// let diag = [3., 2.];
/// let sup = [-4.];
/// let rhs = [-3., 21.];
///
/// let x_init = [1., 1.];
/// let x_refined = trigivs::prelude::tridiag_iter_kaczmarz(
///     &sub,
///     &diag,
///     &sup,
///     &rhs,
///     &x_init,
///     1000,
///     1e-6
/// ).unwrap();
/// ```
///
pub fn tridiag_iter_kaczmarz<T: Float, const D: usize, const S: usize>(
    sup: &[T; S],
    diag: &[T; D],
    sub: &[T; S],
    rhs: &[T; D],
    x_init: &[T; D],
    iter: usize,
    eps: T,
) -> Result<[T; D], SolverErrors> {
    const {
        assert!(
            D == S + 1,
            "Sub and sup diagonals must be exctly 1 element smaller than main diagonal"
        )
    };

    let mut x = x_init.clone();
    let mut ai_ai_dotproducts = [T::zero(); D];
    if D == 1 {
        ai_ai_dotproducts[0] = diag[0].powi(2);
    } else {
        ai_ai_dotproducts[0] = diag[0].powi(2) + sup[0].powi(2);
        ai_ai_dotproducts
            .iter_mut()
            .enumerate()
            .skip(1)
            .take(D - 2)
            .for_each(|(i, elem)| *elem = diag[i].powi(2) + sup[i].powi(2) + sub[i - 1].powi(2));
        ai_ai_dotproducts[D - 1] = sub[D - 2].powi(2) + diag[D - 1].powi(2)
    }
    kaczmarz_body(
        sub,
        diag,
        sup,
        rhs,
        &ai_ai_dotproducts,
        &mut x,
        D,
        iter,
        eps,
    )?;
    Ok(x)
}

/// Compute row and column scaling parameters using Ruiz preconditioning without heap allocation
///
/// # Arguments
///
/// * `sup` - superdiagonal elements, length n-1
/// * `diag` - main diagonal elements, length n
/// * `sub` - subdiagonal elements, length n-1
/// * `r_iter` - maximum amount of iterations for Ruiz preconditioning (impractical over 10)
/// * `r_eps` - finishing criteria for Ruiz preconditioning through maximum column/row inf-norm deviation from 1
///
/// # Output
///
/// (R, C)
/// * `R` - row scaling parmeters
/// * `C` - column scaling parmeters
///
/// # Example
///
/// ```
/// let sup = [-4.];
/// let diag = [3., 2.];
/// let sub = [5.];
///
/// let (row_s, column_s) = trigivs::prelude::compute_ruiz_scaling(&sup, &diag, &sub, 5, 0.01).unwrap();
/// ```
pub fn compute_ruiz_scaling<T: Float, const D: usize, const S: usize>(
    sup: &[T; S],
    diag: &[T; D],
    sub: &[T; S],
    iter: usize,
    eps: T,
) -> Result<([T; D], [T; D]), SolverErrors> {
    const {
        assert!(
            D == S + 1,
            "Sub and sup diagonals must be exctly 1 element smaller than main diagonal"
        )
    };

    let mut row_b = [T::one(); D];
    let mut col_b = [T::one(); D];
    get_ruiz_equilibrium_mul(diag, sup, sub, &mut row_b, &mut col_b, iter, eps)?;
    Ok((row_b, col_b))
}

/// Solves a tridiagonal system using Givens rotations with Ruiz preconditioning without heap allocation
///
/// # Arguments
///
/// * `sup` - superdiagonal elements, length n-1
/// * `diag` - main diagonal elements, length n
/// * `sub` - subdiagonal elements, length n-1
/// * `r_iter` - maximum amount of iterations for Ruiz preconditioning (impractical over 10)
/// * `r_eps` - finishing criteria for Ruiz preconditioning through maximum column/row inf-norm deviation from 1
///
/// # Example
///
/// ```
/// let sub = [5.];
/// let diag = [3., 2.];
/// let sup = [-4.];
/// let rhs = [-3., 21.];
///
/// let x = trigivs::prelude::solve_givens_ruiz_precond(
///     &sub,
///     &diag,
///     &sup,
///     &rhs,
///     5,
///     0.001
/// ).unwrap();
/// ```
///
pub fn solve_givens_ruiz_precond<T: Float, const D: usize, const S: usize>(
    sup: &[T; S],
    diag: &[T; D],
    sub: &[T; S],
    rhs: &[T; D],
    r_iter: usize,
    r_eps: T,
) -> Result<[T; D], SolverErrors> {
    const {
        assert!(
            D == S + 1,
            "Sub and sup diagonals must be exctly 1 element smaller than main diagonal"
        )
    };

    let mut sub_b = sub.clone();
    let mut sup_b = sup.clone();
    let mut diag_b = diag.clone();
    let mut rhs = rhs.clone();

    let mut col_b = [T::one(); D];

    {
        let mut row_b = [T::one(); D];
        get_ruiz_equilibrium_mul(
            &diag_b, &sup_b, &sub_b, &mut row_b, &mut col_b, r_iter, r_eps,
        )?;
        apply_ruiz(&mut diag_b, &mut sup_b, &mut sub_b, &row_b, &col_b);
        rhs.iter_mut()
            .zip(row_b)
            .for_each(|(rhs, row)| *rhs = *rhs * row);
    }

    let mut ur = [T::zero(); S];
    let u = if S > 1 { &mut ur[..S - 1] } else { &mut [] };

    let mut x = [T::zero(); D];

    solve_givens_body(&sub_b, &mut diag_b, &mut sup_b, u, &mut rhs, &mut x)?;

    x.iter_mut().zip(col_b).for_each(|(x, col)| *x = *x * col);

    Ok(x)
}