scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
//! Utility functions for matrix operations using work-stealing
//!
//! This module provides various utility functions including band matrix solvers,
//! matrix function computations, batch operations, and specialized norm computations.

use crate::error::{LinalgError, LinalgResult};
use scirs2_core::ndarray::{s, Array1, Array2, ArrayView1, ArrayView2, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign, One, Zero};
use std::iter::Sum;

use super::super::core::{BandSolveWorkItem, WorkItem};
use super::super::scheduler::WorkStealingScheduler;
use super::gemm::parallel_gemm_work_stealing;

/// Parallel Band matrix solver with optimized memory access
pub fn parallel_band_solve<F>(
    bandmatrix: &ArrayView2<F>,
    rhs: &ArrayView1<F>,
    bandwidth: usize,
    num_workers: usize,
) -> LinalgResult<Array1<F>>
where
    F: Float + NumAssign + Zero + One + Sum + Send + Sync + ScalarOperand + 'static,
{
    let n = bandmatrix.nrows();
    if n != rhs.len() {
        return Err(LinalgError::ShapeError(
            "Matrix and RHS dimensions don't match".to_string(),
        ));
    }

    let mut x = rhs.to_owned();
    let scheduler = WorkStealingScheduler::new(num_workers);

    // Forward substitution with parallel band processing
    for i in 0..n {
        let start_j = i.saturating_sub(bandwidth);
        let end_j = (i + bandwidth + 1).min(n);

        if end_j > i + 1 {
            let work_items: Vec<BandSolveWorkItem<F>> = ((i + 1)..end_j)
                .map(|j| WorkItem::new(j, (i, j, start_j, bandmatrix.to_owned(), x.clone())))
                .collect();

            if !work_items.is_empty() {
                scheduler.submit_work(work_items)?;
                let results = scheduler.execute(move |(i, j, start_j, matrix, x_vec)| {
                    let mut sum = F::zero();
                    for k in start_j..i {
                        sum += matrix[(j, k)] * x_vec[k];
                    }
                    (j, sum)
                })?;

                // Update x vector
                for (j, sum) in results {
                    x[j] -= sum / bandmatrix[(j, j)];
                }
            }
        }
    }

    Ok(x)
}

/// Parallel matrix exponential computation using work stealing
///
/// Computes the matrix exponential exp(A) using parallel scaling and squaring
/// method with Padé approximation.
///
/// # Arguments
///
/// * `a` - Input square matrix
/// * `workers` - Number of worker threads
///
/// # Returns
///
/// * Matrix exponential exp(A)
pub fn parallel_matrix_exponential_work_stealing<F>(
    a: &ArrayView2<F>,
    workers: usize,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Zero + One + Sum + Send + Sync + ScalarOperand + 'static,
{
    let n = a.nrows();
    if n != a.ncols() {
        return Err(LinalgError::ShapeError(
            "Matrix must be square for matrix exponential".to_string(),
        ));
    }

    // For small matrices, use sequential algorithm
    if n < 32 || workers == 1 {
        return crate::matrix_functions::expm(a, None);
    }

    // Compute matrix norm for scaling
    let norm_a = crate::norm::matrix_norm(a, "1", Some(workers))?;
    let log2_norm = norm_a.ln() / F::from(2.0).expect("Operation failed").ln();
    let scaling_factor = log2_norm.ceil().max(F::zero()).to_usize().unwrap_or(0);

    // Scale matrix
    let scaled_factor = F::from(2.0)
        .expect("Operation failed")
        .powi(-(scaling_factor as i32));
    let mut scaled_matrix = a.to_owned();
    scaled_matrix *= scaled_factor;

    // Parallel Padé approximation
    let result = parallel_pade_approximation(&scaled_matrix.view(), 13, workers)?;

    // Square the result `scaling_factor` times
    let mut final_result = result;
    for _ in 0..scaling_factor {
        final_result =
            parallel_gemm_work_stealing(&final_result.view(), &final_result.view(), workers)?;
    }

    Ok(final_result)
}

/// Parallel matrix square root computation using work stealing
///
/// Computes the matrix square root using parallel Newton-Schulz iteration.
///
/// # Arguments
///
/// * `a` - Input positive definite matrix
/// * `workers` - Number of worker threads
///
/// # Returns
///
/// * Matrix square root
pub fn parallel_matrix_sqrt_work_stealing<F>(
    a: &ArrayView2<F>,
    workers: usize,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Zero + One + Sum + Send + Sync + ScalarOperand + 'static,
{
    let n = a.nrows();
    if n != a.ncols() {
        return Err(LinalgError::ShapeError(
            "Matrix must be square for matrix square root".to_string(),
        ));
    }

    // For small matrices, use sequential algorithm
    if n < 32 || workers == 1 {
        let max_iter = 20;
        let tolerance = F::epsilon().sqrt();
        return crate::matrix_functions::sqrtm(a, max_iter, tolerance);
    }

    // Initialize with scaled identity matrix
    let trace = (0..n).map(|i| a[[i, i]]).fold(F::zero(), |acc, x| acc + x);
    let initial_scaling = (trace / F::from(n).expect("Operation failed")).sqrt();
    let mut x = Array2::eye(n) * initial_scaling;
    let mut z = Array2::eye(n);

    let max_iterations = 20;
    let tolerance = F::epsilon().sqrt();

    for _iter in 0..max_iterations {
        // Newton-Schulz iteration with parallel matrix operations
        let x_squared = parallel_gemm_work_stealing(&x.view(), &x.view(), workers)?;
        let z_squared = parallel_gemm_work_stealing(&z.view(), &z.view(), workers)?;

        // Convergence check
        let error_matrix = &x_squared - a;
        let error_norm = parallel_matrix_norm_work_stealing(&error_matrix.view(), "fro", workers)?;

        if error_norm < tolerance {
            break;
        }

        // Update x and z using Newton-Schulz iteration
        let three = F::from(3.0).expect("Operation failed");
        let two = F::from(2.0).expect("Operation failed");

        // Create 3*I - Z² where I is identity matrix
        let three_i = Array2::eye(n) * three;
        let three_minus_z_squared = three_i - &z_squared;

        let temp_x = &x * &three_minus_z_squared / two;
        let temp_z = &z * &three_minus_z_squared / two;

        x = temp_x;
        z = temp_z;
    }

    Ok(x)
}

/// Parallel batch matrix operations using work stealing
///
/// Performs the same operation on multiple matrices in parallel.
///
/// # Arguments
///
/// * `matrices` - Vector of input matrices
/// * `operation` - Function to apply to each matrix
/// * `workers` - Number of worker threads
///
/// # Returns
///
/// * Vector of results
pub fn parallel_batch_operations_work_stealing<F, Op, R>(
    matrices: &[ArrayView2<F>],
    operation: Op,
    workers: usize,
) -> LinalgResult<Vec<R>>
where
    F: Float + NumAssign + Zero + One + Sum + Send + Sync + ScalarOperand + 'static,
    Op: Fn(&ArrayView2<F>) -> LinalgResult<R> + Send + Sync,
    R: Send + Sync,
{
    if matrices.is_empty() {
        return Ok(Vec::new());
    }

    // For small batches, use sequential processing
    if matrices.len() < workers || workers == 1 {
        return matrices.iter().map(&operation).collect();
    }

    // Process matrices in parallel using chunks
    let chunk_size = matrices.len().div_ceil(workers);

    let results = std::thread::scope(|s| {
        let handles: Vec<_> = (0..workers)
            .map(|worker_id| {
                let start_idx = worker_id * chunk_size;
                let end_idx = ((worker_id + 1) * chunk_size).min(matrices.len());
                let op_ref = &operation;

                s.spawn(move || {
                    matrices[start_idx..end_idx]
                        .iter()
                        .map(op_ref)
                        .collect::<Result<Vec<_>, _>>()
                })
            })
            .collect();

        let mut results = Vec::new();
        for handle in handles {
            let chunk_results = handle.join().expect("Operation failed")?;
            results.extend(chunk_results);
        }
        Ok::<Vec<R>, LinalgError>(results)
    })?;

    Ok(results)
}

/// Parallel specialized matrix norm computation using work stealing
///
/// Computes various matrix norms using parallel algorithms optimized
/// for different norm types.
///
/// # Arguments
///
/// * `a` - Input matrix
/// * `norm_type` - Type of norm ("fro", "nuc", "1", "2", "inf")
/// * `workers` - Number of worker threads
///
/// # Returns
///
/// * Computed norm value
pub fn parallel_matrix_norm_work_stealing<F>(
    a: &ArrayView2<F>,
    norm_type: &str,
    workers: usize,
) -> LinalgResult<F>
where
    F: Float + NumAssign + Zero + One + Sum + Send + Sync + ScalarOperand + 'static,
{
    match norm_type {
        "fro" | "frobenius" => parallel_frobenius_norm(a, workers),
        "nuc" | "nuclear" => parallel_nuclear_norm(a, workers),
        "1" => parallel_matrix_1_norm(a, workers),
        "2" | "spectral" => parallel_spectral_norm(a, workers),
        "inf" | "infinity" => parallel_matrix_inf_norm(a, workers),
        _ => Err(LinalgError::InvalidInputError(format!(
            "Unknown norm type: {norm_type}"
        ))),
    }
}

/// Parallel Frobenius norm computation
pub fn parallel_frobenius_norm<F>(a: &ArrayView2<F>, workers: usize) -> LinalgResult<F>
where
    F: Float + NumAssign + Zero + Sum + Send + Sync + ScalarOperand + 'static,
{
    let (m, n) = a.dim();
    let scheduler = WorkStealingScheduler::new(workers);

    // Create work items for chunks of the matrix
    let chunk_size = (m * n / workers).max(1);
    let mut work_items = Vec::new();

    for chunk_start in (0..m * n).step_by(chunk_size) {
        let chunk_end = (chunk_start + chunk_size).min(m * n);
        let indices: Vec<(usize, usize)> = (chunk_start..chunk_end)
            .map(|idx| (idx / n, idx % n))
            .collect();

        work_items.push(WorkItem::new(chunk_start, (indices, a.to_owned())));
    }

    scheduler.submit_work(work_items)?;

    let results = scheduler.execute(|(indices, matrix)| {
        indices
            .into_iter()
            .map(|(i, j)| {
                let val = matrix[(i, j)];
                val * val
            })
            .sum::<F>()
    })?;

    let sum_of_squares: F = results.into_iter().sum();
    Ok(sum_of_squares.sqrt())
}

/// Parallel nuclear norm computation
pub fn parallel_nuclear_norm<F>(a: &ArrayView2<F>, workers: usize) -> LinalgResult<F>
where
    F: Float + NumAssign + Zero + One + Sum + Send + Sync + ScalarOperand + 'static,
{
    // Nuclear norm is the sum of singular values
    use super::decomposition::parallel_svd_work_stealing;
    let (_, s, _) = parallel_svd_work_stealing(a, workers)?;
    Ok(s.iter().cloned().sum())
}

/// Parallel matrix 1-norm computation (maximum column sum)
pub fn parallel_matrix_1_norm<F>(a: &ArrayView2<F>, workers: usize) -> LinalgResult<F>
where
    F: Float + NumAssign + Zero + Sum + Send + Sync + ScalarOperand + 'static,
{
    let (_, n) = a.dim();
    let scheduler = WorkStealingScheduler::new(workers);

    // Create work items for each column
    let work_items: Vec<WorkItem<(usize, Array2<F>)>> = (0..n)
        .map(|j| WorkItem::new(j, (j, a.to_owned())))
        .collect();

    scheduler.submit_work(work_items)?;

    let results =
        scheduler.execute(|(j, matrix)| matrix.column(j).iter().map(|x| x.abs()).sum::<F>())?;

    Ok(results.into_iter().fold(F::zero(), |acc, x| acc.max(x)))
}

/// Parallel spectral norm computation (largest singular value)
pub fn parallel_spectral_norm<F>(a: &ArrayView2<F>, workers: usize) -> LinalgResult<F>
where
    F: Float + NumAssign + Zero + One + Sum + Send + Sync + ScalarOperand + 'static,
{
    // Spectral norm is the largest singular value
    use super::decomposition::parallel_svd_work_stealing;
    let (_, s, _) = parallel_svd_work_stealing(a, workers)?;
    Ok(s.iter().fold(F::zero(), |acc, &x| acc.max(x)))
}

/// Parallel matrix infinity norm computation (maximum row sum)
pub fn parallel_matrix_inf_norm<F>(a: &ArrayView2<F>, workers: usize) -> LinalgResult<F>
where
    F: Float + NumAssign + Zero + Sum + Send + Sync + ScalarOperand + 'static,
{
    let (m, _) = a.dim();
    let scheduler = WorkStealingScheduler::new(workers);

    // Create work items for each row
    let work_items: Vec<WorkItem<(usize, Array2<F>)>> = (0..m)
        .map(|i| WorkItem::new(i, (i, a.to_owned())))
        .collect();

    scheduler.submit_work(work_items)?;

    let results =
        scheduler.execute(|(i, matrix)| matrix.row(i).iter().map(|x| x.abs()).sum::<F>())?;

    Ok(results.into_iter().fold(F::zero(), |acc, x| acc.max(x)))
}

/// Parallel Padé approximation for matrix exponential
fn parallel_pade_approximation<F>(
    a: &ArrayView2<F>,
    degree: usize,
    workers: usize,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Zero + One + Sum + Send + Sync + ScalarOperand + 'static,
{
    let n = a.nrows();
    let mut result = Array2::<F>::eye(n);
    let mut power = Array2::<F>::eye(n);

    // Compute powers of A and coefficients in parallel
    for k in 1..=degree {
        power = parallel_gemm_work_stealing(&power.view(), a, workers)?;

        // Padé coefficient (simplified)
        let coeff = F::from(1.0).expect("Operation failed")
            / F::from(factorial(k)).expect("Operation failed");
        result = result + power.mapv(|x| x * coeff);
    }

    Ok(result)
}

/// Simple factorial function for Padé coefficients
fn factorial(n: usize) -> f64 {
    if n <= 1 {
        1.0
    } else {
        (2..=n).fold(1.0, |acc, x| acc * x as f64)
    }
}