numrs2 0.3.1

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Distributed Linear Algebra Operations
//!
//! This module provides distributed implementations of common linear algebra operations
//! for large-scale matrix computations across multiple processes.
//!
//! # Operations
//!
//! - Matrix multiplication (SUMMA algorithm)
//! - Dot product
//! - Matrix-vector operations
//! - Matrix decompositions (SVD, QR, Cholesky)
//! - Linear system solving
//!
//! # Example
//!
//! ```rust,no_run
//! use numrs2::distributed::linalg::*;
//! use numrs2::distributed::array::*;
//! use numrs2::distributed::process::*;
//!
//! # async fn example() -> Result<(), DistributedLinalgError> {
//! let world = init().await.map_err(|e| DistributedLinalgError::LinalgError(e.to_string()))?;
//!
//! // Create distributed matrices
//! let local_a = vec![1.0_f64; 100];
//! let dist_a = DistributedArray::from_local(
//!     local_a,
//!     DistributionStrategy::Block,
//!     400,
//!     &world
//! )?;
//!
//! let local_b = vec![2.0_f64; 100];
//! let dist_b = DistributedArray::from_local(
//!     local_b,
//!     DistributionStrategy::Block,
//!     400,
//!     &world
//! )?;
//!
//! // Distributed dot product
//! let result = distributed_dot(&dist_a, &dist_b).await?;
//! if world.is_root() {
//!     println!("Dot product: {}", result);
//! }
//!
//! finalize(world).await.map_err(|e| DistributedLinalgError::LinalgError(e.to_string()))?;
//! # Ok(())
//! # }
//! ```

use super::array::{DistributedArray, DistributedArrayError, DistributionStrategy};
use super::collective::{allreduce, CollectiveError, ReduceOp};
use super::process::Communicator;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_linalg::{qr, svd};
use serde::{Deserialize, Serialize};
use std::ops::{Add, Mul};
use thiserror::Error;

/// Errors that can occur during distributed linear algebra operations
#[derive(Error, Debug)]
pub enum DistributedLinalgError {
    #[error("Distributed array error: {0}")]
    Array(#[from] DistributedArrayError),

    #[error("Collective operation error: {0}")]
    Collective(#[from] CollectiveError),

    #[error("Dimension mismatch: {0}")]
    DimensionMismatch(String),

    #[error("Invalid matrix dimensions: rows={rows}, cols={cols}")]
    InvalidDimensions { rows: usize, cols: usize },

    #[error("Singular matrix")]
    SingularMatrix,

    #[error("Convergence failed after {0} iterations")]
    ConvergenceFailed(usize),

    #[error("Linear algebra error: {0}")]
    LinalgError(String),

    #[error("Not yet implemented: {0}")]
    NotImplemented(String),
}

/// Distributed dot product of two vectors
///
/// Computes the dot product of two distributed vectors using parallel reduction.
///
/// # Arguments
///
/// * `x` - First distributed vector
/// * `y` - Second distributed vector
///
/// # Returns
///
/// The scalar dot product result (same value on all processes)
///
/// # Example
///
/// ```rust,no_run
/// # use numrs2::distributed::linalg::*;
/// # use numrs2::distributed::array::*;
/// # async fn example(x: &DistributedArray<f64>, y: &DistributedArray<f64>)
/// #     -> Result<(), DistributedLinalgError> {
/// let dot_product = distributed_dot(x, y).await?;
/// println!("Dot product: {}", dot_product);
/// # Ok(())
/// # }
/// ```
pub async fn distributed_dot<T>(
    x: &DistributedArray<T>,
    y: &DistributedArray<T>,
) -> Result<T, DistributedLinalgError>
where
    T: Serialize
        + for<'de> Deserialize<'de>
        + Clone
        + Add<Output = T>
        + Mul<Output = T>
        + PartialOrd
        + Send
        + 'static,
    T: std::iter::Sum,
{
    // Check dimensions
    if x.global_size() != y.global_size() {
        return Err(DistributedLinalgError::DimensionMismatch(format!(
            "Vector sizes don't match: {} vs {}",
            x.global_size(),
            y.global_size()
        )));
    }

    // Compute local dot product
    let local_x = x.local_data();
    let local_y = y.local_data();

    let local_result = local_x
        .iter()
        .zip(local_y.iter())
        .map(|(a, b)| a.clone() * b.clone())
        .sum::<T>();

    // Global reduction (sum)
    let global_result = allreduce(&[local_result], ReduceOp::Sum, x.comm()).await?;

    global_result
        .into_iter()
        .next()
        .ok_or_else(|| DistributedLinalgError::LinalgError("Empty reduction result".to_string()))
}

/// Distributed matrix-vector multiplication
///
/// Computes y = A * x where A is a distributed matrix and x is a distributed vector.
///
/// # Arguments
///
/// * `a` - Distributed matrix (row-distributed)
/// * `x` - Distributed vector
///
/// # Returns
///
/// Distributed result vector y
///
/// # Example
///
/// ```rust,no_run
/// # use numrs2::distributed::linalg::*;
/// # use numrs2::distributed::array::*;
/// # async fn example(a: &DistributedArray<f64>, x: &DistributedArray<f64>)
/// #     -> Result<(), DistributedLinalgError> {
/// let y = distributed_matvec(a, x).await?;
/// # Ok(())
/// # }
/// ```
pub async fn distributed_matvec<T>(
    _a: &DistributedArray<T>,
    _x: &DistributedArray<T>,
) -> Result<DistributedArray<T>, DistributedLinalgError>
where
    T: Serialize
        + for<'de> Deserialize<'de>
        + Clone
        + Add<Output = T>
        + Mul<Output = T>
        + PartialOrd
        + Send
        + 'static,
{
    // Placeholder implementation
    // Real implementation would perform distributed matrix-vector multiply
    Err(DistributedLinalgError::NotImplemented(
        "Distributed matrix-vector multiplication".to_string(),
    ))
}

/// Distributed matrix multiplication using SUMMA algorithm
///
/// Computes C = A * B where A and B are distributed matrices.
/// Uses the Scalable Universal Matrix Multiplication Algorithm (SUMMA).
///
/// # Arguments
///
/// * `a` - First distributed matrix
/// * `b` - Second distributed matrix
///
/// # Returns
///
/// Distributed result matrix C
///
/// # Example
///
/// ```rust,no_run
/// # use numrs2::distributed::linalg::*;
/// # use numrs2::distributed::array::*;
/// # async fn example(a: &DistributedArray<f64>, b: &DistributedArray<f64>)
/// #     -> Result<(), DistributedLinalgError> {
/// let c = distributed_matmul(a, b).await?;
/// # Ok(())
/// # }
/// ```
pub async fn distributed_matmul<T>(
    _a: &DistributedArray<T>,
    _b: &DistributedArray<T>,
) -> Result<DistributedArray<T>, DistributedLinalgError>
where
    T: Serialize
        + for<'de> Deserialize<'de>
        + Clone
        + Add<Output = T>
        + Mul<Output = T>
        + PartialOrd
        + Send
        + 'static,
{
    // Placeholder implementation
    // Real implementation would use SUMMA algorithm:
    // 1. Partition matrices into blocks
    // 2. Broadcast blocks along rows/columns
    // 3. Perform local matrix multiplications
    // 4. Accumulate results
    Err(DistributedLinalgError::NotImplemented(
        "Distributed matrix multiplication (SUMMA)".to_string(),
    ))
}

/// Distributed Singular Value Decomposition (SVD)
///
/// Computes A = U * Σ * V^T where A is a distributed matrix.
///
/// # Arguments
///
/// * `a` - Distributed matrix to decompose
///
/// # Returns
///
/// Tuple of (U, singular_values, Vt) as distributed arrays
///
/// # Example
///
/// ```rust,no_run
/// # use numrs2::distributed::linalg::*;
/// # use numrs2::distributed::array::*;
/// # async fn example(a: &DistributedArray<f64>)
/// #     -> Result<(), DistributedLinalgError> {
/// let (u, s, vt) = distributed_svd(a).await?;
/// # Ok(())
/// # }
/// ```
pub async fn distributed_svd<T>(
    _a: &DistributedArray<T>,
) -> Result<(DistributedArray<T>, Vec<T>, DistributedArray<T>), DistributedLinalgError>
where
    T: Serialize + for<'de> Deserialize<'de> + Clone + Send + 'static,
{
    // Placeholder implementation
    // Real implementation would use distributed SVD algorithms:
    // - Tall-skinny SVD for tall matrices
    // - Block-based algorithms for large matrices
    // - Iterative methods for largest singular values
    Err(DistributedLinalgError::NotImplemented(
        "Distributed SVD".to_string(),
    ))
}

/// Distributed QR decomposition
///
/// Computes A = Q * R where Q is orthogonal and R is upper triangular.
///
/// # Arguments
///
/// * `a` - Distributed matrix to decompose
///
/// # Returns
///
/// Tuple of (Q, R) as distributed arrays
///
/// # Example
///
/// ```rust,no_run
/// # use numrs2::distributed::linalg::*;
/// # use numrs2::distributed::array::*;
/// # async fn example(a: &DistributedArray<f64>)
/// #     -> Result<(), DistributedLinalgError> {
/// let (q, r) = distributed_qr(a).await?;
/// # Ok(())
/// # }
/// ```
pub async fn distributed_qr<T>(
    _a: &DistributedArray<T>,
) -> Result<(DistributedArray<T>, DistributedArray<T>), DistributedLinalgError>
where
    T: Serialize + for<'de> Deserialize<'de> + Clone + Send + 'static,
{
    // Placeholder implementation
    // Real implementation would use:
    // - Block Householder QR
    // - Communication-avoiding QR (CAQR)
    // - Tree-based QR for tall-skinny matrices
    Err(DistributedLinalgError::NotImplemented(
        "Distributed QR decomposition".to_string(),
    ))
}

/// Distributed Cholesky factorization
///
/// Computes A = L * L^T for symmetric positive definite matrix A.
///
/// # Arguments
///
/// * `a` - Distributed symmetric positive definite matrix
///
/// # Returns
///
/// Lower triangular factor L as distributed array
///
/// # Example
///
/// ```rust,no_run
/// # use numrs2::distributed::linalg::*;
/// # use numrs2::distributed::array::*;
/// # async fn example(a: &DistributedArray<f64>)
/// #     -> Result<(), DistributedLinalgError> {
/// let l = distributed_cholesky(a).await?;
/// # Ok(())
/// # }
/// ```
pub async fn distributed_cholesky<T>(
    _a: &DistributedArray<T>,
) -> Result<DistributedArray<T>, DistributedLinalgError>
where
    T: Serialize + for<'de> Deserialize<'de> + Clone + Send + 'static,
{
    // Placeholder implementation
    // Real implementation would use block Cholesky with broadcasts
    Err(DistributedLinalgError::NotImplemented(
        "Distributed Cholesky factorization".to_string(),
    ))
}

/// Distributed linear system solver
///
/// Solves A * x = b for x where A is a distributed matrix and b is a distributed vector.
///
/// # Arguments
///
/// * `a` - Distributed coefficient matrix
/// * `b` - Distributed right-hand side vector
///
/// # Returns
///
/// Distributed solution vector x
///
/// # Example
///
/// ```rust,no_run
/// # use numrs2::distributed::linalg::*;
/// # use numrs2::distributed::array::*;
/// # async fn example(a: &DistributedArray<f64>, b: &DistributedArray<f64>)
/// #     -> Result<(), DistributedLinalgError> {
/// let x = distributed_solve(a, b).await?;
/// # Ok(())
/// # }
/// ```
pub async fn distributed_solve<T>(
    _a: &DistributedArray<T>,
    _b: &DistributedArray<T>,
) -> Result<DistributedArray<T>, DistributedLinalgError>
where
    T: Serialize + for<'de> Deserialize<'de> + Clone + Send + 'static,
{
    // Placeholder implementation
    // Real implementation would use:
    // - Distributed LU factorization + triangular solves
    // - Iterative methods (CG, GMRES) for large sparse systems
    // - Block algorithms for dense systems
    Err(DistributedLinalgError::NotImplemented(
        "Distributed linear system solve".to_string(),
    ))
}

/// Distributed norm computation
///
/// Computes various norms of a distributed vector.
pub async fn distributed_norm<T>(
    x: &DistributedArray<T>,
    p: f64,
) -> Result<f64, DistributedLinalgError>
where
    T: Serialize + for<'de> Deserialize<'de> + Clone + Send + 'static,
    T: Into<f64> + Copy,
{
    let local_x = x.local_data();

    let local_sum = if p == f64::INFINITY {
        // Infinity norm: max absolute value
        local_x
            .iter()
            .map(|&v| Into::<f64>::into(v).abs())
            .fold(0.0, f64::max)
    } else if p == 2.0 {
        // L2 norm: sqrt(sum of squares)
        local_x
            .iter()
            .map(|&v| {
                let val = Into::<f64>::into(v);
                val * val
            })
            .sum::<f64>()
    } else {
        // Lp norm: (sum of |x|^p)^(1/p)
        local_x
            .iter()
            .map(|&v| Into::<f64>::into(v).abs().powf(p))
            .sum::<f64>()
    };

    // Global reduction
    let global_sum = if p == f64::INFINITY {
        // Use max reduction for infinity norm
        let result = allreduce(&[local_sum], ReduceOp::Max, x.comm()).await?;
        result[0]
    } else {
        // Use sum reduction for other norms
        let result = allreduce(&[local_sum], ReduceOp::Sum, x.comm()).await?;
        if p == 2.0 {
            result[0].sqrt()
        } else {
            result[0].powf(1.0 / p)
        }
    };

    Ok(global_sum)
}

/// Helper structure for matrix dimensions
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct MatrixDims {
    /// Number of rows
    pub rows: usize,
    /// Number of columns
    pub cols: usize,
}

impl MatrixDims {
    /// Create new matrix dimensions
    pub fn new(rows: usize, cols: usize) -> Result<Self, DistributedLinalgError> {
        if rows == 0 || cols == 0 {
            return Err(DistributedLinalgError::InvalidDimensions { rows, cols });
        }
        Ok(Self { rows, cols })
    }

    /// Check if dimensions are compatible for matrix multiplication
    pub fn can_multiply(&self, other: &MatrixDims) -> bool {
        self.cols == other.rows
    }

    /// Get result dimensions for matrix multiplication
    pub fn multiply_result(&self, other: &MatrixDims) -> Option<MatrixDims> {
        if self.can_multiply(other) {
            Some(MatrixDims {
                rows: self.rows,
                cols: other.cols,
            })
        } else {
            None
        }
    }
}

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

    #[test]
    fn test_matrix_dims() {
        let dims = MatrixDims::new(3, 4).expect("Valid dimensions");
        assert_eq!(dims.rows, 3);
        assert_eq!(dims.cols, 4);
    }

    #[test]
    fn test_matrix_dims_invalid() {
        assert!(MatrixDims::new(0, 4).is_err());
        assert!(MatrixDims::new(3, 0).is_err());
    }

    #[test]
    fn test_matrix_dims_can_multiply() {
        let a = MatrixDims::new(3, 4).expect("Valid");
        let b = MatrixDims::new(4, 5).expect("Valid");
        let c = MatrixDims::new(5, 2).expect("Valid");

        assert!(a.can_multiply(&b));
        assert!(b.can_multiply(&c));
        assert!(!a.can_multiply(&c));
    }

    #[test]
    fn test_matrix_dims_multiply_result() {
        let a = MatrixDims::new(3, 4).expect("Valid");
        let b = MatrixDims::new(4, 5).expect("Valid");

        let result = a.multiply_result(&b).expect("Compatible");
        assert_eq!(result.rows, 3);
        assert_eq!(result.cols, 5);
    }

    #[test]
    fn test_matrix_dims_multiply_incompatible() {
        let a = MatrixDims::new(3, 4).expect("Valid");
        let b = MatrixDims::new(5, 2).expect("Valid");

        assert!(a.multiply_result(&b).is_none());
    }
}