pub trait SparseElement:
Copy
+ PartialEq
+ Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Default
+ Debug {
// Required methods
fn sparse_zero() -> Self;
fn sparse_one() -> Self;
// Provided methods
fn zero() -> Self { ... }
fn one() -> Self { ... }
fn is_zero(&self) -> bool { ... }
}Expand description
Trait for types that can be used as sparse matrix elements
This trait captures the minimal requirements for sparse matrix element types, supporting both integer and floating-point types. It provides the basic arithmetic operations and identity elements needed for sparse matrix operations.
§Design Philosophy
Unlike Float or ScientificNumber, this trait is intentionally minimal to support
the widest range of numeric types in sparse matrices, including:
- Integer types (u8, i32, u64, etc.) for graph adjacency matrices, binary operators, etc.
- Floating-point types (f32, f64) for numerical computation
§Examples
use scirs2_core::numeric::SparseElement;
// Integer sparse matrix element
let a: u8 = 1;
let b: u8 = 2;
assert_eq!(a + b, 3);
assert_eq!(u8::sparse_zero(), 0);
assert_eq!(u8::sparse_one(), 1);
assert!(u8::sparse_zero().is_zero());
// Float sparse matrix element
let x: f64 = 1.0;
let y: f64 = 2.0;
assert_eq!(x + y, 3.0);
assert_eq!(f64::sparse_zero(), 0.0);
assert_eq!(f64::sparse_one(), 1.0);
assert!(f64::sparse_zero().is_zero());Required Methods§
Sourcefn sparse_zero() -> Self
fn sparse_zero() -> Self
The zero element (additive identity) for sparse matrix operations
This method is prefixed with sparse_ to avoid ambiguity with num_traits::Zero::zero().
Sourcefn sparse_one() -> Self
fn sparse_one() -> Self
The one element (multiplicative identity) for sparse matrix operations
This method is prefixed with sparse_ to avoid ambiguity with num_traits::One::one().
Provided Methods§
Sourcefn zero() -> Self
👎Deprecated since 0.1.0-rc.2: Use sparse_zero() instead to avoid ambiguity with num_traits::Zero
fn zero() -> Self
sparse_zero() instead to avoid ambiguity with num_traits::ZeroThe zero element (additive identity)
§Deprecation Notice
This method is deprecated to avoid ambiguity with num_traits::Zero::zero().
Use sparse_zero() instead.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.