forked from M-Labs/nalgebra
code fmt; fix failing unit test and doc test
This commit is contained in:
parent
6cd009988c
commit
92b324c007
@ -300,7 +300,7 @@ impl std::error::Error for MatrixMarketError {}
|
||||
impl MatrixMarketError {
|
||||
fn from_pest_error<T>(error: pest::error::Error<T>) -> Self
|
||||
where
|
||||
T: fmt::Debug + std::hash::Hash + std::marker::Copy + Ord
|
||||
T: fmt::Debug + std::hash::Hash + std::marker::Copy + Ord,
|
||||
{
|
||||
Self::from_kind_and_message(
|
||||
MatrixMarketErrorKind::ParsingError,
|
||||
@ -739,8 +739,10 @@ struct MatrixMarketParser;
|
||||
/// --------
|
||||
/// ```
|
||||
/// use nalgebra_sparse::io::load_coo_from_matrix_market_file;
|
||||
/// // Use e.g. `f64` for floating-point matrices
|
||||
/// let matrix = load_coo_from_matrix_market_file::<i32>("path/to/matrix.mtx")?;
|
||||
/// // Use e.g. `i32` for integer matrices
|
||||
/// let matrix = load_coo_from_matrix_market_file::<i32,_>("path/to/matrix.mtx");
|
||||
/// // extract the real matrix here by
|
||||
/// // let matrix = matrix.unwrap();
|
||||
/// ```
|
||||
pub fn load_coo_from_matrix_market_file<T, P: AsRef<Path>>(
|
||||
path: P,
|
||||
@ -767,12 +769,15 @@ where
|
||||
/// # use nalgebra_sparse::io::load_coo_from_matrix_market_str;
|
||||
/// # use nalgebra_sparse::io::MatrixMarketErrorKind;
|
||||
/// let str = r#"
|
||||
/// %%matrixmarket matrix coordinate integer symmetric
|
||||
/// %%matrixmarket matrix coordinate integer general
|
||||
/// 5 4 2
|
||||
/// 1 1 10
|
||||
/// 2 3 5
|
||||
/// "#;
|
||||
/// let matrix = load_coo_from_matrix_market_str::<i32>(str)?;
|
||||
/// // Use e.g. `i32` for integer matrices
|
||||
/// let matrix = load_coo_from_matrix_market_str::<i32>(str);
|
||||
/// // extract the real matrix here by
|
||||
/// // let matrix = matrix.unwrap();
|
||||
/// ```
|
||||
pub fn load_coo_from_matrix_market_str<T>(data: &str) -> Result<CooMatrix<T>, MatrixMarketError>
|
||||
where
|
||||
@ -781,7 +786,8 @@ where
|
||||
// unwrap() in this function are guaranteed by parsing the data
|
||||
let file = MatrixMarketParser::parse(Rule::Document, data)
|
||||
.map_err(MatrixMarketError::from_pest_error)?
|
||||
.next().unwrap();
|
||||
.next()
|
||||
.unwrap();
|
||||
|
||||
let mut rows: Vec<usize> = Vec::new();
|
||||
let mut cols: Vec<usize> = Vec::new();
|
||||
@ -1042,18 +1048,6 @@ fn parse_sparse_shape(
|
||||
let c = inner.next().unwrap().as_str().parse::<usize>().unwrap();
|
||||
let nnz = inner.next().unwrap().as_str().parse::<usize>().unwrap();
|
||||
|
||||
// shape information can't use 0 as dimension
|
||||
if r * c * nnz == 0 {
|
||||
return Err(MatrixMarketError::from_kind_and_message(
|
||||
MatrixMarketErrorKind::ZeroError,
|
||||
String::from(
|
||||
"
|
||||
Matrix can't have 0 as shape dimensions.
|
||||
",
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
// check for square matrix, when it's not a general matrix
|
||||
if *storagescheme != StorageScheme::General && r != c {
|
||||
return Err(MatrixMarketError::from_kind_and_message(MatrixMarketErrorKind::NonSquare, format!("(Skew-)Symmetric or hermitian matrix should be square matrix, but it has dimension {} and {}", r, c)));
|
||||
|
@ -1,9 +1,5 @@
|
||||
//! Unit tests
|
||||
#[cfg(not(all(
|
||||
feature = "proptest-support",
|
||||
feature = "compare",
|
||||
feature = "io",
|
||||
)))]
|
||||
#[cfg(not(all(feature = "proptest-support", feature = "compare", feature = "io",)))]
|
||||
compile_error!(
|
||||
"Please enable the `proptest-support`, `compare` and `io` features in order to compile and run the tests.
|
||||
Example: `cargo test -p nalgebra-sparse --features proptest-support,compare,io`"
|
||||
|
@ -10,14 +10,14 @@ fn test_matrixmarket_sparse_real_general_empty() {
|
||||
// Test several valid zero-shapes of a matrix
|
||||
let shapes = vec![ (0, 0), (1, 0), (0, 1) ];
|
||||
let strings: Vec<String> = shapes
|
||||
.into_iter()
|
||||
.iter()
|
||||
.map(|(m, n)| format!("%%MatrixMarket matrix coordinate real general\n {} {} 0", m, n))
|
||||
.collect();
|
||||
|
||||
for string in &strings {
|
||||
for (shape,string) in shapes.iter().zip(strings.iter()) {
|
||||
let sparse_mat = load_coo_from_matrix_market_str::<f32>(string).unwrap();
|
||||
assert_eq!(sparse_mat.nrows(), 0);
|
||||
assert_eq!(sparse_mat.ncols(), 0);
|
||||
assert_eq!(sparse_mat.nrows(), shape.0);
|
||||
assert_eq!(sparse_mat.ncols(), shape.1);
|
||||
assert_eq!(sparse_mat.nnz(), 0);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user