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
//! Sparse matrix merge algorithms
//!
//! This module implements parameterized merge patterns for sparse matrix
//! element-wise operations, eliminating code duplication across CSR, CSC, and COO formats.
//!
//! # Design Pattern
//!
//! Instead of having separate implementations for add, subtract, multiply, and
//! divide, we have:
//! - ONE generic merge function per format (CSR, CSC, COO)
//! - Strategy enum to control which positions to keep (union vs intersection)
//! - Semantics enum to control empty matrix handling
//! - Operation closures to define element-wise computation
//!
//! # Benefits
//!
//! 1. **Code Reuse**: Single merge implementation handles all operations
//! - Before: ~900 lines (6 specialized functions)
//! - After: ~300 lines (2 parameterized functions)
//!
//! 2. **Consistency**: All operations use identical merge logic, reducing bugs
//!
//! 3. **Extensibility**: New operations (e.g., max, min) require only:
//! - Adding semantics to enum
//! - Writing operation closure
//! - No new merge code needed
//!
//! 4. **Testability**: Testing merge logic once covers all operations
//!
//! # Architecture
//!
//! ```text
//! User-facing trait method (add_csr, sub_csr, mul_csr, div_csr)
//! │
//! ├─> Calls merge_csr_impl with:
//! │ - MergeStrategy (Union or Intersection)
//! │ - OperationSemantics (Add, Subtract, Multiply, Divide)
//! │ - Operation closure: |a, b| a + b
//! │ - Transform closures: |a| a, |b| -b
//! │
//! └─> merge_csr_impl:
//! 1. Check empty matrices (using semantics)
//! 2. Merge based on strategy (union/intersection)
//! 3. Apply operations via closures
//! 4. Filter near-zeros
//! ```
//!
//! # Example Usage
//!
//! ```ignore
//! // Addition: union merge, identity transforms
//! fn add_csr<T>(...) -> Result<...> {
//! merge_csr_impl(
//! ...,
//! MergeStrategy::Union,
//! OperationSemantics::Add,
//! |a, b| a + b, // Both exist
//! |a| a, // A-only: keep
//! |b| b, // B-only: keep
//! )
//! }
//!
//! // Subtraction: union merge, negate B
//! fn sub_csr<T>(...) -> Result<...> {
//! merge_csr_impl(
//! ...,
//! MergeStrategy::Union,
//! OperationSemantics::Subtract,
//! |a, b| a - b, // Both exist
//! |a| a, // A-only: keep
//! |b| -b, // B-only: negate
//! )
//! }
//!
//! // Multiplication: intersection merge (0 * x = 0)
//! fn mul_csr<T>(...) -> Result<...> {
//! merge_csr_impl(
//! ...,
//! MergeStrategy::Intersection,
//! OperationSemantics::Multiply,
//! |a, b| a * b, // Both exist
//! |a| a, // Unused (intersection only)
//! |b| b, // Unused (intersection only)
//! )
//! }
//! ```
//!
//! # Empty Matrix Handling
//!
//! OperationSemantics controls what happens when one or both matrices are empty:
//!
//! | Operation | A empty, B not | A not, B empty | Both empty |
//! |-----------|----------------|----------------|------------|
//! | Add | Return B | Return A | Empty |
//! | Subtract | Return -B | Return A | Empty |
//! | Multiply | Empty (0*x=0) | Empty (x*0=0) | Empty |
//! | Divide | Empty (0/x=0) | Error (x/0) | Empty |
//!
//! This logic is centralized in `common::handle_empty_compressed` and avoids
//! duplicating empty-check code across operations.
// Re-export zero_tolerance from shared utilities module
// See runtime::sparse_utils::zero_tolerance for full documentation
pub use cratezero_tolerance;
pub use ;
pub use ;
pub use merge_csc_impl;
pub use merge_csr_impl;