pub fn transpose<T: Numeric<T>>(a: &Sprs<T>) -> Sprs<T>Expand description
C = A’
The algorithm for transposing a sparse matrix (C = A^T) it can be viewed not just as a linear algebraic function but as a method for converting a compressed-column sparse matrix into a compressed-row sparse matrix as well. The algorithm computes the row counts of A, computes the cumulative sum to obtain the row pointers, and then iterates over each nonzero entry in A, placing the entry in its appropriate row vector. If the resulting sparse matrix C is interpreted as a matrix in compressed-row form, then C is equal to A, just in a different format. If C is viewed as a compressed-column matrix, then C contains A^T.
§Example
let a = [
vec![2.1615, 2.0044, 2.1312, 0.8217, 2.2074],
vec![2.2828, 1.9089, 1.9295, 0.9412, 2.0017],
vec![2.2156, 1.8776, 1.9473, 1.0190, 1.8352],
vec![1.0244, 0.8742, 0.9177, 0.7036, 0.7551],
vec![2.0367, 1.5642, 1.4313, 0.8668, 1.7571],
];
let mut a_sparse = rsparse::data::Sprs::new();
a_sparse.from_vec(&a);
assert_eq!(
rsparse::transpose(&a_sparse).to_dense(),
vec![
vec![2.1615, 2.2828, 2.2156, 1.0244, 2.0367],
vec![2.0044, 1.9089, 1.8776, 0.8742, 1.5642],
vec![2.1312, 1.9295, 1.9473, 0.9177, 1.4313],
vec![0.8217, 0.9412, 1.0190, 0.7036, 0.8668],
vec![2.2074, 2.0017, 1.8352, 0.7551, 1.7571]
]
)