Change write -> save in order to be consistent with load terminology

This commit is contained in:
Andreas Longva 2022-05-09 09:51:55 +02:00
parent ef9a3dd767
commit 9b32195558
3 changed files with 24 additions and 25 deletions

View File

@ -775,7 +775,7 @@ mm_complex_impl!(f64);
mm_pattern_impl!(()); mm_pattern_impl!(());
/// A marker trait for supported sparse matrix types. /// A marker trait for sparse matrix types that can be exported to the matrix market format.
/// ///
/// This is a sealed trait; it cannot be implemented by external crates. This is done in order to prevent leaking /// This is a sealed trait; it cannot be implemented by external crates. This is done in order to prevent leaking
/// some of the implementation details we currently rely on. We may relax this restriction in the future. /// some of the implementation details we currently rely on. We may relax this restriction in the future.
@ -1439,7 +1439,7 @@ fn next_dense_coordinate(
/// -------- /// --------
/// ``` /// ```
/// # use nalgebra_sparse::CooMatrix; /// # use nalgebra_sparse::CooMatrix;
/// use nalgebra_sparse::io::{write_to_matrix_market_str}; /// use nalgebra_sparse::io::{save_to_matrix_market_str};
/// let expected_str = r#"%%matrixmarket matrix coordinate integer general /// let expected_str = r#"%%matrixmarket matrix coordinate integer general
/// % matrixmarket file generated by nalgebra-sparse. /// % matrixmarket file generated by nalgebra-sparse.
/// 5 4 2 /// 5 4 2
@ -1450,26 +1450,25 @@ fn next_dense_coordinate(
/// let col_indices = vec![0,2]; /// let col_indices = vec![0,2];
/// let values = vec![10,5]; /// let values = vec![10,5];
/// let matrix = CooMatrix::try_from_triplets(5,4,row_indices,col_indices,values).unwrap(); /// let matrix = CooMatrix::try_from_triplets(5,4,row_indices,col_indices,values).unwrap();
/// let generated_matrixmarket_str = write_to_matrix_market_str(&matrix); /// let generated_matrixmarket_str = save_to_matrix_market_str(&matrix);
/// assert_eq!(expected_str,generated_matrixmarket_str); /// assert_eq!(expected_str,generated_matrixmarket_str);
/// ``` /// ```
pub fn write_to_matrix_market_str<T: MatrixMarketScalar, S: MatrixMarketExport<T>>( pub fn save_to_matrix_market_str<T: MatrixMarketScalar, S: MatrixMarketExport<T>>(
sparse_matrix: &S, sparse_matrix: &S,
) -> String { ) -> String {
let mut bytes = Vec::<u8>::new(); let mut bytes = Vec::<u8>::new();
// This will call impl<A: Allocator> Write for Vec<u8, A> // This will call impl<A: Allocator> Write for Vec<u8, A>
// The vector will grow as needed. // The vector will grow as needed.
// So, unwrap here won't cause any issue. // So, unwrap here won't cause any issue.
write_to_matrix_market(&mut bytes, sparse_matrix).unwrap(); save_to_matrix_market(&mut bytes, sparse_matrix).unwrap();
String::from_utf8(bytes) String::from_utf8(bytes)
.expect("Unexpected non UTF-8 data was generated when export to matrix market string") .expect("Unexpected non UTF-8 data was generated when export to matrix market string")
} }
/// Write a sparse matrix into Matrix Market format file. /// Save a sparse matrix to a Matrix Market format file.
///
/// The exporter only writes matrix into `coordinate` and `general` format.
/// ///
/// The exporter only saves the matrix with the `coordinate` and `general` matrix market formats.
/// ///
/// Errors /// Errors
/// -------- /// --------
@ -1479,7 +1478,7 @@ pub fn write_to_matrix_market_str<T: MatrixMarketScalar, S: MatrixMarketExport<T
/// Examples /// Examples
/// -------- /// --------
/// ```no_run /// ```no_run
/// use nalgebra_sparse::io::{write_to_matrix_market_file,load_coo_from_matrix_market_str}; /// use nalgebra_sparse::io::{save_to_matrix_market_file,load_coo_from_matrix_market_str};
/// let str = r#" /// let str = r#"
/// %%matrixmarket matrix coordinate integer general /// %%matrixmarket matrix coordinate integer general
/// 5 4 2 /// 5 4 2
@ -1487,9 +1486,9 @@ pub fn write_to_matrix_market_str<T: MatrixMarketScalar, S: MatrixMarketExport<T
/// 2 3 5 /// 2 3 5
/// "#; /// "#;
/// let matrix = load_coo_from_matrix_market_str::<i32>(&str).unwrap(); /// let matrix = load_coo_from_matrix_market_str::<i32>(&str).unwrap();
/// write_to_matrix_market_file(&matrix,"path/to/matrix.mtx").unwrap(); /// save_to_matrix_market_file(&matrix,"path/to/matrix.mtx").unwrap();
/// ``` /// ```
pub fn write_to_matrix_market_file< pub fn save_to_matrix_market_file<
T: MatrixMarketScalar, T: MatrixMarketScalar,
S: MatrixMarketExport<T>, S: MatrixMarketExport<T>,
P: AsRef<Path>, P: AsRef<Path>,
@ -1499,7 +1498,7 @@ pub fn write_to_matrix_market_file<
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {
let file = File::create(path)?; let file = File::create(path)?;
let mut file = BufWriter::new(file); let mut file = BufWriter::new(file);
write_to_matrix_market(&mut file, sparse_matrix)?; save_to_matrix_market(&mut file, sparse_matrix)?;
// Quote from BufWriter doc. // Quote from BufWriter doc.
// > It is critical to call flush before BufWriter<W> is dropped. Though dropping will attempt to flush the contents of the buffer, any errors that happen in the process of dropping will be ignored. Calling flush ensures that the buffer is empty and thus dropping will not even attempt file operations. // > It is critical to call flush before BufWriter<W> is dropped. Though dropping will attempt to flush the contents of the buffer, any errors that happen in the process of dropping will be ignored. Calling flush ensures that the buffer is empty and thus dropping will not even attempt file operations.
file.flush() file.flush()
@ -1508,7 +1507,7 @@ pub fn write_to_matrix_market_file<
} }
/// low level implementation of writing sparse matrix into any [std::io::Write] object /// low level implementation of writing sparse matrix into any [std::io::Write] object
pub fn write_to_matrix_market<T: MatrixMarketScalar, S: MatrixMarketExport<T>, W: Write>( pub fn save_to_matrix_market<T: MatrixMarketScalar, S: MatrixMarketExport<T>, W: Write>(
mut w: W, mut w: W,
sparse_matrix: &S, sparse_matrix: &S,
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {

View File

@ -21,10 +21,10 @@
//! We currently offer functionality for importing a Matrix market file to an instance of a //! We currently offer functionality for importing a Matrix market file to an instance of a
//! [CooMatrix](crate::CooMatrix) through the function [load_coo_from_matrix_market_file], //! [CooMatrix](crate::CooMatrix) through the function [load_coo_from_matrix_market_file],
//! as well as functionality for writing various sparse matrices to the matrix market format //! as well as functionality for writing various sparse matrices to the matrix market format
//! through [write_to_matrix_market_file]. It is also possible to load //! through [save_to_matrix_market_file]. It is also possible to load
//! a matrix stored as a string in the matrix market format with the function //! a matrix stored as a string in the matrix market format with the function
//! [load_coo_from_matrix_market_str], or similarly write to a string with //! [load_coo_from_matrix_market_str], or similarly write to a string with
//! [write_to_matrix_market_str]. //! [save_to_matrix_market_str].
//! //!
//! Our implementation is based on the [format description](https://math.nist.gov/MatrixMarket/formats.html) //! Our implementation is based on the [format description](https://math.nist.gov/MatrixMarket/formats.html)
//! on the Matrix Market website and the //! on the Matrix Market website and the
@ -34,8 +34,8 @@
//! > "*The Matrix Market Exchange Formats: Initial Design.*" (1996). //! > "*The Matrix Market Exchange Formats: Initial Design.*" (1996).
pub use self::matrix_market::{ pub use self::matrix_market::{
load_coo_from_matrix_market_file, load_coo_from_matrix_market_str, write_to_matrix_market, load_coo_from_matrix_market_file, load_coo_from_matrix_market_str, save_to_matrix_market,
write_to_matrix_market_file, write_to_matrix_market_str, MatrixMarketError, save_to_matrix_market_file, save_to_matrix_market_str, MatrixMarketError,
MatrixMarketErrorKind, MatrixMarketExport, MatrixMarketScalar, MatrixMarketErrorKind, MatrixMarketExport, MatrixMarketScalar,
}; };
mod matrix_market; mod matrix_market;

View File

@ -2,8 +2,8 @@ use matrixcompare::assert_matrix_eq;
use nalgebra::matrix; use nalgebra::matrix;
use nalgebra::Complex; use nalgebra::Complex;
use nalgebra_sparse::io::{ use nalgebra_sparse::io::{
load_coo_from_matrix_market_file, load_coo_from_matrix_market_str, write_to_matrix_market_file, load_coo_from_matrix_market_file, load_coo_from_matrix_market_str, save_to_matrix_market_file,
write_to_matrix_market_str, save_to_matrix_market_str,
}; };
use nalgebra_sparse::proptest::coo_no_duplicates; use nalgebra_sparse::proptest::coo_no_duplicates;
use nalgebra_sparse::CooMatrix; use nalgebra_sparse::CooMatrix;
@ -374,7 +374,7 @@ fn test_matrixmarket_write_real(){
1 3 3 1 3 3
2 3 3 2 3 3
"#; "#;
let matrixmarket_str = write_to_matrix_market_str(&coo_matrix); let matrixmarket_str = save_to_matrix_market_str(&coo_matrix);
assert_eq!(matrixmarket_str,expected); assert_eq!(matrixmarket_str,expected);
} }
@ -398,7 +398,7 @@ fn test_matrixmarket_write_int() {
1 3 3 1 3 3
2 3 3 2 3 3
"#; "#;
let matrixmarket_str = write_to_matrix_market_str(&coo_matrix); let matrixmarket_str = save_to_matrix_market_str(&coo_matrix);
assert_eq!(matrixmarket_str, expected); assert_eq!(matrixmarket_str, expected);
} }
@ -417,7 +417,7 @@ fn test_matrixmarket_write_pattern() {
1 3 1 3
2 3 2 3
"#; "#;
let matrixmarket_str = write_to_matrix_market_str(&coo_matrix); let matrixmarket_str = save_to_matrix_market_str(&coo_matrix);
assert_eq!(matrixmarket_str, expected); assert_eq!(matrixmarket_str, expected);
} }
@ -442,14 +442,14 @@ fn test_matrixmarket_write_complex() {
1 3 4 5 1 3 4 5
2 3 5 6 2 3 5 6
"#; "#;
let matrixmarket_str = write_to_matrix_market_str(&coo_matrix); let matrixmarket_str = save_to_matrix_market_str(&coo_matrix);
assert_eq!(matrixmarket_str, expected); assert_eq!(matrixmarket_str, expected);
} }
proptest! { proptest! {
#[test] #[test]
fn coo_matrix_market_roundtrip_str(coo in coo_no_duplicates(-10 ..= 10, 0 ..= 10, 0..= 10, 100)) { fn coo_matrix_market_roundtrip_str(coo in coo_no_duplicates(-10 ..= 10, 0 ..= 10, 0..= 10, 100)) {
let generated_matrixmarket_string = write_to_matrix_market_str(&coo); let generated_matrixmarket_string = save_to_matrix_market_str(&coo);
let generated_matrix = load_coo_from_matrix_market_str(&generated_matrixmarket_string).unwrap(); let generated_matrix = load_coo_from_matrix_market_str(&generated_matrixmarket_string).unwrap();
assert_matrix_eq!(generated_matrix, coo); assert_matrix_eq!(generated_matrix, coo);
} }
@ -460,7 +460,7 @@ proptest! {
fn coo_matrix_market_roundtrip_file(coo in coo_no_duplicates(-10 ..= 10, 0 ..= 10, 0..= 10, 100)) { fn coo_matrix_market_roundtrip_file(coo in coo_no_duplicates(-10 ..= 10, 0 ..= 10, 0..= 10, 100)) {
let temp_dir = tempdir().expect("Unable to create temporary directory"); let temp_dir = tempdir().expect("Unable to create temporary directory");
let file_path = temp_dir.path().join("temp.mtx"); let file_path = temp_dir.path().join("temp.mtx");
write_to_matrix_market_file(&coo,&file_path).unwrap(); save_to_matrix_market_file(&coo,&file_path).unwrap();
let generated_matrix = load_coo_from_matrix_market_file(file_path).unwrap(); let generated_matrix = load_coo_from_matrix_market_file(file_path).unwrap();
assert_matrix_eq!(generated_matrix, coo); assert_matrix_eq!(generated_matrix, coo);
temp_dir.close().expect("Unable to delete temporary directory"); temp_dir.close().expect("Unable to delete temporary directory");