forked from M-Labs/nalgebra
Change write -> save in order to be consistent with load terminology
This commit is contained in:
parent
ef9a3dd767
commit
9b32195558
@ -775,7 +775,7 @@ mm_complex_impl!(f64);
|
||||
|
||||
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
|
||||
/// 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::io::{write_to_matrix_market_str};
|
||||
/// use nalgebra_sparse::io::{save_to_matrix_market_str};
|
||||
/// let expected_str = r#"%%matrixmarket matrix coordinate integer general
|
||||
/// % matrixmarket file generated by nalgebra-sparse.
|
||||
/// 5 4 2
|
||||
@ -1450,26 +1450,25 @@ fn next_dense_coordinate(
|
||||
/// let col_indices = vec![0,2];
|
||||
/// let values = vec![10,5];
|
||||
/// 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);
|
||||
/// ```
|
||||
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,
|
||||
) -> String {
|
||||
let mut bytes = Vec::<u8>::new();
|
||||
// This will call impl<A: Allocator> Write for Vec<u8, A>
|
||||
// The vector will grow as needed.
|
||||
// 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)
|
||||
.expect("Unexpected non UTF-8 data was generated when export to matrix market string")
|
||||
}
|
||||
|
||||
/// Write a sparse matrix into Matrix Market format file.
|
||||
///
|
||||
/// The exporter only writes matrix into `coordinate` and `general` format.
|
||||
/// Save a sparse matrix to a Matrix Market format file.
|
||||
///
|
||||
/// The exporter only saves the matrix with the `coordinate` and `general` matrix market formats.
|
||||
///
|
||||
/// Errors
|
||||
/// --------
|
||||
@ -1479,7 +1478,7 @@ pub fn write_to_matrix_market_str<T: MatrixMarketScalar, S: MatrixMarketExport<T
|
||||
/// Examples
|
||||
/// --------
|
||||
/// ```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#"
|
||||
/// %%matrixmarket matrix coordinate integer general
|
||||
/// 5 4 2
|
||||
@ -1487,9 +1486,9 @@ pub fn write_to_matrix_market_str<T: MatrixMarketScalar, S: MatrixMarketExport<T
|
||||
/// 2 3 5
|
||||
/// "#;
|
||||
/// 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,
|
||||
S: MatrixMarketExport<T>,
|
||||
P: AsRef<Path>,
|
||||
@ -1499,7 +1498,7 @@ pub fn write_to_matrix_market_file<
|
||||
) -> Result<(), std::io::Error> {
|
||||
let file = File::create(path)?;
|
||||
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.
|
||||
// > 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()
|
||||
@ -1508,7 +1507,7 @@ pub fn write_to_matrix_market_file<
|
||||
}
|
||||
|
||||
/// 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,
|
||||
sparse_matrix: &S,
|
||||
) -> Result<(), std::io::Error> {
|
||||
|
@ -21,10 +21,10 @@
|
||||
//! 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],
|
||||
//! 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
|
||||
//! [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)
|
||||
//! on the Matrix Market website and the
|
||||
@ -34,8 +34,8 @@
|
||||
//! > "*The Matrix Market Exchange Formats: Initial Design.*" (1996).
|
||||
|
||||
pub use self::matrix_market::{
|
||||
load_coo_from_matrix_market_file, load_coo_from_matrix_market_str, write_to_matrix_market,
|
||||
write_to_matrix_market_file, write_to_matrix_market_str, MatrixMarketError,
|
||||
load_coo_from_matrix_market_file, load_coo_from_matrix_market_str, save_to_matrix_market,
|
||||
save_to_matrix_market_file, save_to_matrix_market_str, MatrixMarketError,
|
||||
MatrixMarketErrorKind, MatrixMarketExport, MatrixMarketScalar,
|
||||
};
|
||||
mod matrix_market;
|
||||
|
@ -2,8 +2,8 @@ use matrixcompare::assert_matrix_eq;
|
||||
use nalgebra::matrix;
|
||||
use nalgebra::Complex;
|
||||
use nalgebra_sparse::io::{
|
||||
load_coo_from_matrix_market_file, load_coo_from_matrix_market_str, write_to_matrix_market_file,
|
||||
write_to_matrix_market_str,
|
||||
load_coo_from_matrix_market_file, load_coo_from_matrix_market_str, save_to_matrix_market_file,
|
||||
save_to_matrix_market_str,
|
||||
};
|
||||
use nalgebra_sparse::proptest::coo_no_duplicates;
|
||||
use nalgebra_sparse::CooMatrix;
|
||||
@ -374,7 +374,7 @@ fn test_matrixmarket_write_real(){
|
||||
1 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);
|
||||
}
|
||||
|
||||
@ -398,7 +398,7 @@ fn test_matrixmarket_write_int() {
|
||||
1 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);
|
||||
}
|
||||
|
||||
@ -417,7 +417,7 @@ fn test_matrixmarket_write_pattern() {
|
||||
1 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);
|
||||
}
|
||||
|
||||
@ -442,14 +442,14 @@ fn test_matrixmarket_write_complex() {
|
||||
1 3 4 5
|
||||
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);
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
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();
|
||||
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)) {
|
||||
let temp_dir = tempdir().expect("Unable to create temporary directory");
|
||||
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();
|
||||
assert_matrix_eq!(generated_matrix, coo);
|
||||
temp_dir.close().expect("Unable to delete temporary directory");
|
||||
|
Loading…
Reference in New Issue
Block a user