diff --git a/nalgebra-sparse/src/io/matrix_market.rs b/nalgebra-sparse/src/io/matrix_market.rs index fc9b59b2..489449a3 100644 --- a/nalgebra-sparse/src/io/matrix_market.rs +++ b/nalgebra-sparse/src/io/matrix_market.rs @@ -1430,9 +1430,9 @@ fn next_dense_coordinate( } } -/// Write a sparse matrix into Matrix Market format string. +/// Save a sparse matrix as a Matrix Market format string. /// -/// The exporter only writes matrix into `coordinate` and `general` format. +/// The exporter only writes the matrix into `coordinate` and `general` format. /// /// /// Examples @@ -1453,9 +1453,13 @@ fn next_dense_coordinate( /// let generated_matrixmarket_str = save_to_matrix_market_str(&matrix); /// assert_eq!(expected_str,generated_matrixmarket_str); /// ``` -pub fn save_to_matrix_market_str>( +pub fn save_to_matrix_market_str( sparse_matrix: &S, -) -> String { +) -> String +where + T: MatrixMarketScalar, + S: MatrixMarketExport +{ let mut bytes = Vec::::new(); // This will call impl Write for Vec // The vector will grow as needed. @@ -1488,14 +1492,15 @@ pub fn save_to_matrix_market_str /// let matrix = load_coo_from_matrix_market_str::(&str).unwrap(); /// save_to_matrix_market_file(&matrix,"path/to/matrix.mtx").unwrap(); /// ``` -pub fn save_to_matrix_market_file< +pub fn save_to_matrix_market_file( + sparse_matrix: &S, + path: P, +) -> Result<(), std::io::Error> +where T: MatrixMarketScalar, S: MatrixMarketExport, P: AsRef, ->( - sparse_matrix: &S, - path: P, -) -> Result<(), std::io::Error> { +{ let file = File::create(path)?; let mut file = BufWriter::new(file); save_to_matrix_market(&mut file, sparse_matrix)?; @@ -1506,11 +1511,19 @@ pub fn save_to_matrix_market_file< Ok(()) } -/// low level implementation of writing sparse matrix into any [std::io::Write] object -pub fn save_to_matrix_market, W: Write>( +/// Save a sparse matrix to an [std::io::Write] instance. +/// +/// This is the most general save functionality. See [save_to_matrix_market_file] and +/// [save_to_matrix_market_str] for higher-level functionality. +pub fn save_to_matrix_market( mut w: W, sparse_matrix: &S, -) -> Result<(), std::io::Error> { +) -> Result<(), std::io::Error> +where + T: MatrixMarketScalar, + S: MatrixMarketExport, + W: Write +{ // write header writeln!( w,