CSR/CSC: Provide constructor for unsorted but otherwise valid data
This commit is contained in:
parent
7f236d88aa
commit
9e85c9e2b6
|
@ -170,6 +170,24 @@ impl<T> CsrMatrix<T> {
|
|||
Self::try_from_pattern_and_values(pattern, values)
|
||||
}
|
||||
|
||||
/// Try to construct a CSR matrix from raw CSR data with unsorted columns.
|
||||
pub fn try_from_unsorted_csr_data(
|
||||
num_rows: usize,
|
||||
num_cols: usize,
|
||||
row_offsets: Vec<usize>,
|
||||
col_indices: Vec<usize>,
|
||||
values: Vec<T>,
|
||||
) -> Result<Self, SparseFormatError> {
|
||||
let sorted_num_cols: Vec<usize> = row_offsets[0..row_offsets.len() - 1]
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(index, &offset)| {
|
||||
Self::sorted(col_indices[offset..row_offsets[index + 1]].to_vec())
|
||||
})
|
||||
.collect();
|
||||
return Self::try_from_csr_data(num_rows, num_cols, row_offsets, sorted_num_cols, values);
|
||||
}
|
||||
|
||||
/// Try to construct a CSR matrix from a sparsity pattern and associated non-zero values.
|
||||
///
|
||||
/// Returns an error if the number of values does not match the number of minor indices
|
||||
|
@ -190,6 +208,15 @@ impl<T> CsrMatrix<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Return sorted vector.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn sorted(row_offsets: Vec<usize>) -> Vec<usize> {
|
||||
let mut sorted = row_offsets.clone();
|
||||
sorted.sort();
|
||||
return sorted;
|
||||
}
|
||||
|
||||
/// The number of rows in the matrix.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
|
|
|
@ -171,6 +171,29 @@ fn csr_matrix_valid_data() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csr_matrix_valid_data_unsorted_column_indices() {
|
||||
let csr = CsrMatrix::try_from_unsorted_csr_data(
|
||||
3,
|
||||
4,
|
||||
vec![0, 1, 2, 5],
|
||||
vec![1, 3, 2, 3, 0],
|
||||
vec![5, 4, 1, 1, 4],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let expected_csr = CsrMatrix::try_from_csr_data(
|
||||
3,
|
||||
4,
|
||||
vec![0, 1, 2, 5],
|
||||
vec![1, 3, 0, 2, 3],
|
||||
vec![5, 4, 1, 1, 4],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(csr, expected_csr);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csr_matrix_try_from_invalid_csr_data() {
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue