forked from M-Labs/nalgebra
add more tests; use bufwritter; fix typo; use temp_dir to test with files
This commit is contained in:
parent
0cae584262
commit
8e48d26767
@ -13,7 +13,7 @@ use std::convert::TryFrom;
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fmt::Formatter;
|
use std::fmt::Formatter;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::Write;
|
use std::io::{BufWriter, Write};
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
use std::num::TryFromIntError;
|
use std::num::TryFromIntError;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
@ -1432,25 +1432,26 @@ fn next_dense_coordinate(
|
|||||||
|
|
||||||
/// Write a sparse matrix into Matrix Market format string.
|
/// Write a sparse matrix into Matrix Market format string.
|
||||||
///
|
///
|
||||||
/// Our exporter only writes matrix into `coordiante` and `general` format.
|
/// The exporter only writes matrix into `coordinate` and `general` format.
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
/// Examples
|
/// Examples
|
||||||
/// --------
|
/// --------
|
||||||
/// ```
|
/// ```
|
||||||
/// # use matrixcompare::assert_matrix_eq;
|
/// # use nalgebra_sparse::CooMatrix;
|
||||||
/// use nalgebra_sparse::io::{write_to_matrix_market_str,load_coo_from_matrix_market_str};
|
/// use nalgebra_sparse::io::{write_to_matrix_market_str};
|
||||||
/// let str = r#"
|
/// let expected_str = r#"%%matrixmarket matrix coordinate integer general
|
||||||
/// %%matrixmarket matrix coordinate integer general
|
/// % matrixmarket file generated by nalgebra-sparse.
|
||||||
/// 5 4 2
|
/// 5 4 2
|
||||||
/// 1 1 10
|
/// 1 1 10
|
||||||
/// 2 3 5
|
/// 2 3 5
|
||||||
/// "#;
|
/// "#;
|
||||||
/// let matrix = load_coo_from_matrix_market_str::<i32>(&str).unwrap();
|
/// let row_indices = vec![0,1];
|
||||||
/// let generated_matrixmarket_string = write_to_matrix_market_str(&matrix);
|
/// let col_indices = vec![0,2];
|
||||||
/// // 'generated_matrixmarket' should equal to the 'matrix'
|
/// let values = vec![10,5];
|
||||||
/// let generated_matrixmarket = load_coo_from_matrix_market_str::<i32>(&generated_matrixmarket_string).unwrap();
|
/// let matrix = CooMatrix::try_from_triplets(5,4,row_indices,col_indices,values).unwrap();
|
||||||
/// assert_matrix_eq!(matrix,generated_matrixmarket);
|
/// let generated_matrixmarket_str = write_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 write_to_matrix_market_str<T: MatrixMarketScalar, S: MatrixMarketExport<T>>(
|
||||||
sparse_matrix: &S,
|
sparse_matrix: &S,
|
||||||
@ -1467,7 +1468,7 @@ pub fn write_to_matrix_market_str<T: MatrixMarketScalar, S: MatrixMarketExport<T
|
|||||||
|
|
||||||
/// Write a sparse matrix into Matrix Market format file.
|
/// Write a sparse matrix into Matrix Market format file.
|
||||||
///
|
///
|
||||||
/// Our exporter only writes matrix into `coordiante` and `general` format.
|
/// The exporter only writes matrix into `coordinate` and `general` format.
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
/// Errors
|
/// Errors
|
||||||
@ -1477,7 +1478,7 @@ pub fn write_to_matrix_market_str<T: MatrixMarketScalar, S: MatrixMarketExport<T
|
|||||||
///
|
///
|
||||||
/// Examples
|
/// Examples
|
||||||
/// --------
|
/// --------
|
||||||
/// ```
|
/// ```no_run
|
||||||
/// use nalgebra_sparse::io::{write_to_matrix_market_file,load_coo_from_matrix_market_str};
|
/// use nalgebra_sparse::io::{write_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
|
||||||
@ -1486,10 +1487,10 @@ 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();
|
||||||
/// let res = write_to_matrix_market_file(&matrix,"path/to/matrix.mtx");
|
/// // create a temporary file 'temp.mtx'
|
||||||
/// if res.is_err(){
|
/// let mut tempdir = std::env::temp_dir();
|
||||||
/// // do something
|
/// tempdir.push("temp.mtx");
|
||||||
/// }
|
/// write_to_matrix_market_file(&matrix,tempdir).unwrap();
|
||||||
/// ```
|
/// ```
|
||||||
pub fn write_to_matrix_market_file<
|
pub fn write_to_matrix_market_file<
|
||||||
T: MatrixMarketScalar,
|
T: MatrixMarketScalar,
|
||||||
@ -1499,13 +1500,18 @@ pub fn write_to_matrix_market_file<
|
|||||||
sparse_matrix: &S,
|
sparse_matrix: &S,
|
||||||
path: P,
|
path: P,
|
||||||
) -> Result<(), std::io::Error> {
|
) -> Result<(), std::io::Error> {
|
||||||
let mut file = File::create(path)?;
|
let file = File::create(path)?;
|
||||||
|
let mut file = BufWriter::new(file);
|
||||||
write_to_matrix_market(&mut file, sparse_matrix)?;
|
write_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()
|
||||||
|
.expect("Unexpected error when flushing the buffer data to File");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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
|
||||||
fn write_to_matrix_market<T: MatrixMarketScalar, S: MatrixMarketExport<T>, W: Write>(
|
pub fn write_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> {
|
||||||
@ -1534,7 +1540,7 @@ fn write_to_matrix_market<T: MatrixMarketScalar, S: MatrixMarketExport<T>, W: Wr
|
|||||||
buffer.clear();
|
buffer.clear();
|
||||||
d.write_matrix_market(&mut buffer)
|
d.write_matrix_market(&mut buffer)
|
||||||
.expect("Unexpected format error was generated when write to String");
|
.expect("Unexpected format error was generated when write to String");
|
||||||
writeln!(w, "{} {} {} ", r + 1, c + 1, buffer)?;
|
writeln!(w, "{} {} {}", r + 1, c + 1, buffer)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -32,8 +32,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_file,
|
load_coo_from_matrix_market_file, load_coo_from_matrix_market_str, write_to_matrix_market,
|
||||||
write_to_matrix_market_str, MatrixMarketError, MatrixMarketErrorKind, MatrixMarketExport,
|
write_to_matrix_market_file, write_to_matrix_market_str, MatrixMarketError,
|
||||||
MatrixMarketScalar,
|
MatrixMarketErrorKind, MatrixMarketExport, MatrixMarketScalar,
|
||||||
};
|
};
|
||||||
mod matrix_market;
|
mod matrix_market;
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
use matrixcompare::assert_matrix_eq;
|
use matrixcompare::assert_matrix_eq;
|
||||||
use nalgebra::dmatrix;
|
use nalgebra::matrix;
|
||||||
use nalgebra::Complex;
|
use nalgebra::Complex;
|
||||||
use nalgebra_sparse::io::{load_coo_from_matrix_market_str, write_to_matrix_market_str};
|
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,
|
||||||
|
};
|
||||||
|
use nalgebra_sparse::proptest::coo_no_duplicates;
|
||||||
use nalgebra_sparse::CooMatrix;
|
use nalgebra_sparse::CooMatrix;
|
||||||
|
use proptest::prelude::*;
|
||||||
|
|
||||||
|
type C64 = Complex<f64>;
|
||||||
|
type C32 = Complex<f32>;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_sparse_real_general_empty() {
|
fn test_matrixmarket_load_sparse_real_general_empty() {
|
||||||
// Test several valid zero-shapes of a sparse matrix
|
// Test several valid zero-shapes of a sparse matrix
|
||||||
let shapes = vec![ (0, 0), (1, 0), (0, 1) ];
|
let shapes = vec![ (0, 0), (1, 0), (0, 1) ];
|
||||||
let strings: Vec<String> = shapes
|
let strings: Vec<String> = shapes
|
||||||
@ -19,16 +27,12 @@ fn test_matrixmarket_sparse_real_general_empty() {
|
|||||||
assert_eq!(sparse_mat.nrows(), shape.0);
|
assert_eq!(sparse_mat.nrows(), shape.0);
|
||||||
assert_eq!(sparse_mat.ncols(), shape.1);
|
assert_eq!(sparse_mat.ncols(), shape.1);
|
||||||
assert_eq!(sparse_mat.nnz(), 0);
|
assert_eq!(sparse_mat.nnz(), 0);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<f32>(&generated_matrixmarket_str).unwrap();
|
|
||||||
assert_matrix_eq!(sparse_mat, generated_matrix);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_dense_real_general_empty() {
|
fn test_matrixmarket_load_dense_real_general_empty() {
|
||||||
// Test several valid zero-shapes of a dense matrix
|
// Test several valid zero-shapes of a dense matrix
|
||||||
let shapes = vec![ (0, 0), (1, 0), (0, 1) ];
|
let shapes = vec![ (0, 0), (1, 0), (0, 1) ];
|
||||||
let strings: Vec<String> = shapes
|
let strings: Vec<String> = shapes
|
||||||
@ -41,16 +45,12 @@ fn test_matrixmarket_dense_real_general_empty() {
|
|||||||
assert_eq!(sparse_mat.nrows(), shape.0);
|
assert_eq!(sparse_mat.nrows(), shape.0);
|
||||||
assert_eq!(sparse_mat.ncols(), shape.1);
|
assert_eq!(sparse_mat.ncols(), shape.1);
|
||||||
assert_eq!(sparse_mat.nnz(), 0);
|
assert_eq!(sparse_mat.nnz(), 0);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<f32>(&generated_matrixmarket_str).unwrap();
|
|
||||||
assert_matrix_eq!(sparse_mat, generated_matrix);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_sparse_real_general() {
|
fn test_matrixmarket_load_sparse_real_general() {
|
||||||
let file_str = r#"
|
let file_str = r#"
|
||||||
%%MatrixMarket matrix CoOrdinate real general
|
%%MatrixMarket matrix CoOrdinate real general
|
||||||
% This is also an example of free-format features.
|
% This is also an example of free-format features.
|
||||||
@ -97,7 +97,7 @@ fn test_matrixmarket_sparse_real_general() {
|
|||||||
5 5 1.200e+01
|
5 5 1.200e+01
|
||||||
"#;
|
"#;
|
||||||
let sparse_mat = load_coo_from_matrix_market_str::<f32>(file_str).unwrap();
|
let sparse_mat = load_coo_from_matrix_market_str::<f32>(file_str).unwrap();
|
||||||
let expected = dmatrix![
|
let expected = matrix![
|
||||||
1.0, 0.0, 0.0, 6.0, 0.0;
|
1.0, 0.0, 0.0, 6.0, 0.0;
|
||||||
0.0, 10.5, 0.0, 0.0, 0.0;
|
0.0, 10.5, 0.0, 0.0, 0.0;
|
||||||
0.0, 0.0, 0.015, 0.0, 0.0;
|
0.0, 0.0, 0.015, 0.0, 0.0;
|
||||||
@ -105,15 +105,11 @@ fn test_matrixmarket_sparse_real_general() {
|
|||||||
0.0, 0.0, 0.0, 0.0, 12.0;
|
0.0, 0.0, 0.0, 0.0, 12.0;
|
||||||
];
|
];
|
||||||
assert_matrix_eq!(sparse_mat, expected);
|
assert_matrix_eq!(sparse_mat, expected);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<f32>(&generated_matrixmarket_str).unwrap();
|
|
||||||
assert_matrix_eq!(expected, generated_matrix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_sparse_int_symmetric() {
|
fn test_matrixmarket_load_sparse_int_symmetric() {
|
||||||
let file_str = r#"
|
let file_str = r#"
|
||||||
%%MatrixMarket matrix coordinate integer symmetric
|
%%MatrixMarket matrix coordinate integer symmetric
|
||||||
%
|
%
|
||||||
@ -129,7 +125,7 @@ fn test_matrixmarket_sparse_int_symmetric() {
|
|||||||
5 5 55
|
5 5 55
|
||||||
"#;
|
"#;
|
||||||
let sparse_mat = load_coo_from_matrix_market_str::<i128>(file_str).unwrap();
|
let sparse_mat = load_coo_from_matrix_market_str::<i128>(file_str).unwrap();
|
||||||
let expected = dmatrix![
|
let expected = matrix![
|
||||||
11, 0, 0, 0, -15;
|
11, 0, 0, 0, -15;
|
||||||
0, 22, 23, 24, 0;
|
0, 22, 23, 24, 0;
|
||||||
0, 23, 33, 0, 35;
|
0, 23, 33, 0, 35;
|
||||||
@ -137,15 +133,11 @@ fn test_matrixmarket_sparse_int_symmetric() {
|
|||||||
-15, 0, 35, 0, 55;
|
-15, 0, 35, 0, 55;
|
||||||
];
|
];
|
||||||
assert_matrix_eq!(sparse_mat, expected);
|
assert_matrix_eq!(sparse_mat, expected);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<i128>(&generated_matrixmarket_str).unwrap();
|
|
||||||
assert_matrix_eq!(expected, generated_matrix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_sparse_complex_hermitian() {
|
fn test_matrixmarket_load_sparse_complex_hermitian() {
|
||||||
let file_str = r#"
|
let file_str = r#"
|
||||||
%%MatrixMarket matrix coordinate complex hermitian
|
%%MatrixMarket matrix coordinate complex hermitian
|
||||||
%
|
%
|
||||||
@ -160,23 +152,19 @@ fn test_matrixmarket_sparse_complex_hermitian() {
|
|||||||
|
|
||||||
"#;
|
"#;
|
||||||
let sparse_mat = load_coo_from_matrix_market_str::<Complex<f64>>(file_str).unwrap();
|
let sparse_mat = load_coo_from_matrix_market_str::<Complex<f64>>(file_str).unwrap();
|
||||||
let expected = dmatrix![
|
let expected = matrix![
|
||||||
Complex::<f64>{re:1.0,im:0.0}, Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:0.0,im:0.0},Complex::<f64>{re:0.0,im:0.0};
|
C64{re:1.0,im:0.0}, C64{re:0.0,im:0.0}, C64{re:0.0,im:0.0}, C64{re:0.0,im:0.0},C64{re:0.0,im:0.0};
|
||||||
Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:10.5,im:0.0}, Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:250.5,im:-22.22},Complex::<f64>{re:0.0,im:0.0};
|
C64{re:0.0,im:0.0}, C64{re:10.5,im:0.0}, C64{re:0.0,im:0.0}, C64{re:250.5,im:-22.22},C64{re:0.0,im:0.0};
|
||||||
Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:0.015,im:0.0}, Complex::<f64>{re:0.0,im:0.0},Complex::<f64>{re:0.0,im:0.0};
|
C64{re:0.0,im:0.0}, C64{re:0.0,im:0.0}, C64{re:0.015,im:0.0}, C64{re:0.0,im:0.0},C64{re:0.0,im:0.0};
|
||||||
Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:250.5,im:22.22}, Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:-280.0,im:0.0},Complex::<f64>{re:0.0,im:-33.32};
|
C64{re:0.0,im:0.0}, C64{re:250.5,im:22.22}, C64{re:0.0,im:0.0}, C64{re:-280.0,im:0.0},C64{re:0.0,im:-33.32};
|
||||||
Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:0.0,im:0.0}, Complex::<f64>{re:0.0,im:33.32},Complex::<f64>{re:12.0,im:0.0};
|
C64{re:0.0,im:0.0}, C64{re:0.0,im:0.0}, C64{re:0.0,im:0.0}, C64{re:0.0,im:33.32},C64{re:12.0,im:0.0};
|
||||||
];
|
];
|
||||||
assert_matrix_eq!(sparse_mat, expected);
|
assert_matrix_eq!(sparse_mat, expected);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<Complex<f64>>(&generated_matrixmarket_str).unwrap();
|
|
||||||
assert_matrix_eq!(expected, generated_matrix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_sparse_real_skew() {
|
fn test_matrixmarket_load_sparse_real_skew() {
|
||||||
let file_str = r#"
|
let file_str = r#"
|
||||||
%%MatrixMarket matrix coordinate real skew-symmetric
|
%%MatrixMarket matrix coordinate real skew-symmetric
|
||||||
%
|
%
|
||||||
@ -187,7 +175,7 @@ fn test_matrixmarket_sparse_real_skew() {
|
|||||||
5 3 -35.0
|
5 3 -35.0
|
||||||
"#;
|
"#;
|
||||||
let sparse_mat = load_coo_from_matrix_market_str::<f64>(file_str).unwrap();
|
let sparse_mat = load_coo_from_matrix_market_str::<f64>(file_str).unwrap();
|
||||||
let expected = dmatrix![
|
let expected = matrix![
|
||||||
0.0, 0.0, 0.0, 0.0, 15.0;
|
0.0, 0.0, 0.0, 0.0, 15.0;
|
||||||
0.0, 0.0, 23.0, 24.0, 0.0;
|
0.0, 0.0, 23.0, 24.0, 0.0;
|
||||||
0.0, -23.0, 0.0, 0.0, 35.0;
|
0.0, -23.0, 0.0, 0.0, 35.0;
|
||||||
@ -195,15 +183,11 @@ fn test_matrixmarket_sparse_real_skew() {
|
|||||||
-15.0, 0.0, -35.0, 0.0, 0.0;
|
-15.0, 0.0, -35.0, 0.0, 0.0;
|
||||||
];
|
];
|
||||||
assert_matrix_eq!(sparse_mat, expected);
|
assert_matrix_eq!(sparse_mat, expected);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<f64>(&generated_matrixmarket_str).unwrap();
|
|
||||||
assert_matrix_eq!(expected, generated_matrix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_sparse_pattern_general() {
|
fn test_matrixmarket_load_sparse_pattern_general() {
|
||||||
let file_str = r#"
|
let file_str = r#"
|
||||||
%%MatrixMarket matrix coordinate pattern general
|
%%MatrixMarket matrix coordinate pattern general
|
||||||
%
|
%
|
||||||
@ -225,7 +209,7 @@ fn test_matrixmarket_sparse_pattern_general() {
|
|||||||
let (row_idx, col_idx, val) = pattern_matrix.clone().disassemble();
|
let (row_idx, col_idx, val) = pattern_matrix.clone().disassemble();
|
||||||
let values = vec![1; val.len()];
|
let values = vec![1; val.len()];
|
||||||
let sparse_mat = CooMatrix::try_from_triplets(nrows, ncols, row_idx, col_idx, values).unwrap();
|
let sparse_mat = CooMatrix::try_from_triplets(nrows, ncols, row_idx, col_idx, values).unwrap();
|
||||||
let expected = dmatrix![
|
let expected = matrix![
|
||||||
1, 0, 0, 0, 1;
|
1, 0, 0, 0, 1;
|
||||||
0, 0, 1, 1, 0;
|
0, 0, 1, 1, 0;
|
||||||
0, 1, 0, 0, 1;
|
0, 1, 0, 0, 1;
|
||||||
@ -233,22 +217,11 @@ fn test_matrixmarket_sparse_pattern_general() {
|
|||||||
0, 1, 0, 1, 1;
|
0, 1, 0, 1, 1;
|
||||||
];
|
];
|
||||||
assert_matrix_eq!(sparse_mat, expected);
|
assert_matrix_eq!(sparse_mat, expected);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&pattern_matrix);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<()>(&generated_matrixmarket_str).unwrap();
|
|
||||||
|
|
||||||
let nrows = generated_matrix.nrows();
|
|
||||||
let ncols = generated_matrix.ncols();
|
|
||||||
let (row_idx, col_idx, val) = generated_matrix.clone().disassemble();
|
|
||||||
let values = vec![1; val.len()];
|
|
||||||
let generated_sparse_mat = CooMatrix::try_from_triplets(nrows, ncols, row_idx, col_idx, values).unwrap();
|
|
||||||
|
|
||||||
assert_matrix_eq!(expected, generated_sparse_mat);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_dense_real_general() {
|
fn test_matrixmarket_load_dense_real_general() {
|
||||||
let file_str = r#"
|
let file_str = r#"
|
||||||
%%MatrixMarket matrix array real general
|
%%MatrixMarket matrix array real general
|
||||||
%
|
%
|
||||||
@ -268,22 +241,18 @@ fn test_matrixmarket_dense_real_general() {
|
|||||||
|
|
||||||
"#;
|
"#;
|
||||||
let sparse_mat = load_coo_from_matrix_market_str::<f32>(file_str).unwrap();
|
let sparse_mat = load_coo_from_matrix_market_str::<f32>(file_str).unwrap();
|
||||||
let expected = dmatrix![
|
let expected = matrix![
|
||||||
1.0, 5.0, 9.0;
|
1.0, 5.0, 9.0;
|
||||||
2.0, 6.0, 10.0;
|
2.0, 6.0, 10.0;
|
||||||
3.0, 7.0, 11.0;
|
3.0, 7.0, 11.0;
|
||||||
4.0, 8.0, 12.0;
|
4.0, 8.0, 12.0;
|
||||||
];
|
];
|
||||||
assert_matrix_eq!(sparse_mat, expected);
|
assert_matrix_eq!(sparse_mat, expected);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<f32>(&generated_matrixmarket_str).unwrap();
|
|
||||||
assert_matrix_eq!(expected, generated_matrix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_dense_real_symmetric() {
|
fn test_matrixmarket_load_dense_real_symmetric() {
|
||||||
let file_str = r#"
|
let file_str = r#"
|
||||||
%%MatrixMarket matrix array real symmetric
|
%%MatrixMarket matrix array real symmetric
|
||||||
%
|
%
|
||||||
@ -301,22 +270,18 @@ fn test_matrixmarket_dense_real_symmetric() {
|
|||||||
|
|
||||||
"#;
|
"#;
|
||||||
let sparse_mat = load_coo_from_matrix_market_str::<f32>(file_str).unwrap();
|
let sparse_mat = load_coo_from_matrix_market_str::<f32>(file_str).unwrap();
|
||||||
let expected = dmatrix![
|
let expected = matrix![
|
||||||
1.0, 2.0, 3.0, 4.0;
|
1.0, 2.0, 3.0, 4.0;
|
||||||
2.0, 5.0, 6.0, 7.0;
|
2.0, 5.0, 6.0, 7.0;
|
||||||
3.0, 6.0, 8.0, 9.0;
|
3.0, 6.0, 8.0, 9.0;
|
||||||
4.0, 7.0, 9.0, 10.0;
|
4.0, 7.0, 9.0, 10.0;
|
||||||
];
|
];
|
||||||
assert_matrix_eq!(sparse_mat, expected);
|
assert_matrix_eq!(sparse_mat, expected);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<f32>(&generated_matrixmarket_str).unwrap();
|
|
||||||
assert_matrix_eq!(expected, generated_matrix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_dense_complex_hermitian() {
|
fn test_matrixmarket_load_dense_complex_hermitian() {
|
||||||
let file_str = r#"
|
let file_str = r#"
|
||||||
%%MatrixMarket matrix array complex hermitian
|
%%MatrixMarket matrix array complex hermitian
|
||||||
%
|
%
|
||||||
@ -333,23 +298,19 @@ fn test_matrixmarket_dense_complex_hermitian() {
|
|||||||
10.0 0.0
|
10.0 0.0
|
||||||
|
|
||||||
"#;
|
"#;
|
||||||
let sparse_mat = load_coo_from_matrix_market_str::<Complex<f64>>(file_str).unwrap();
|
let sparse_mat = load_coo_from_matrix_market_str::<C64>(file_str).unwrap();
|
||||||
let expected = dmatrix![
|
let expected = matrix![
|
||||||
Complex::<f64>{re:1.0,im:0.0}, Complex::<f64>{re:2.0,im:-2.0} ,Complex::<f64>{re:3.0,im:-3.0} ,Complex::<f64>{re:4.0,im:-4.0};
|
C64{re:1.0,im:0.0}, C64{re:2.0,im:-2.0} ,C64{re:3.0,im:-3.0} ,C64{re:4.0,im:-4.0};
|
||||||
Complex::<f64>{re:2.0,im:2.0}, Complex::<f64>{re:5.0,im:0.0} ,Complex::<f64>{re:6.0,im:-6.0} ,Complex::<f64>{re:7.0,im:-7.0};
|
C64{re:2.0,im:2.0}, C64{re:5.0,im:0.0} ,C64{re:6.0,im:-6.0} ,C64{re:7.0,im:-7.0};
|
||||||
Complex::<f64>{re:3.0,im:3.0}, Complex::<f64>{re:6.0,im:6.0} ,Complex::<f64>{re:8.0,im:0.0} ,Complex::<f64>{re:9.0,im:-9.0};
|
C64{re:3.0,im:3.0}, C64{re:6.0,im:6.0} ,C64{re:8.0,im:0.0} ,C64{re:9.0,im:-9.0};
|
||||||
Complex::<f64>{re:4.0,im:4.0}, Complex::<f64>{re:7.0,im:7.0} ,Complex::<f64>{re:9.0,im:9.0} ,Complex::<f64>{re:10.0,im:0.0};
|
C64{re:4.0,im:4.0}, C64{re:7.0,im:7.0} ,C64{re:9.0,im:9.0} ,C64{re:10.0,im:0.0};
|
||||||
];
|
];
|
||||||
assert_matrix_eq!(sparse_mat, expected);
|
assert_matrix_eq!(sparse_mat, expected);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<Complex<f64>>(&generated_matrixmarket_str).unwrap();
|
|
||||||
assert_matrix_eq!(expected, generated_matrix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_dense_int_skew() {
|
fn test_matrixmarket_load_dense_int_skew() {
|
||||||
let file_str = r#"
|
let file_str = r#"
|
||||||
%%MatrixMarket matrix array integer skew-symmetric
|
%%MatrixMarket matrix array integer skew-symmetric
|
||||||
%
|
%
|
||||||
@ -362,22 +323,18 @@ fn test_matrixmarket_dense_int_skew() {
|
|||||||
6
|
6
|
||||||
"#;
|
"#;
|
||||||
let sparse_mat = load_coo_from_matrix_market_str::<i32>(file_str).unwrap();
|
let sparse_mat = load_coo_from_matrix_market_str::<i32>(file_str).unwrap();
|
||||||
let expected = dmatrix![
|
let expected = matrix![
|
||||||
0,-1,-2,-3;
|
0,-1,-2,-3;
|
||||||
1, 0,-4,-5;
|
1, 0,-4,-5;
|
||||||
2, 4, 0,-6;
|
2, 4, 0,-6;
|
||||||
3, 5, 6, 0;
|
3, 5, 6, 0;
|
||||||
];
|
];
|
||||||
assert_matrix_eq!(sparse_mat, expected);
|
assert_matrix_eq!(sparse_mat, expected);
|
||||||
|
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<i32>(&generated_matrixmarket_str).unwrap();
|
|
||||||
assert_matrix_eq!(expected, generated_matrix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_matrixmarket_dense_complex_general() {
|
fn test_matrixmarket_load_dense_complex_general() {
|
||||||
let file_str = r#"
|
let file_str = r#"
|
||||||
%%MatrixMarket matrix array complex general
|
%%MatrixMarket matrix array complex general
|
||||||
%
|
%
|
||||||
@ -387,14 +344,123 @@ fn test_matrixmarket_dense_complex_general() {
|
|||||||
1 0
|
1 0
|
||||||
1 0
|
1 0
|
||||||
"#;
|
"#;
|
||||||
let sparse_mat = load_coo_from_matrix_market_str::<Complex<f32>>(file_str).unwrap();
|
let sparse_mat = load_coo_from_matrix_market_str::<C32>(file_str).unwrap();
|
||||||
let expected = dmatrix![
|
let expected = matrix![
|
||||||
Complex::<f32>{re:1.0,im:0.0},Complex::<f32>{re:1.0,im:0.0};
|
C32{re:1.0,im:0.0},C32{re:1.0,im:0.0};
|
||||||
Complex::<f32>{re:1.0,im:0.0},Complex::<f32>{re:1.0,im:0.0};
|
C32{re:1.0,im:0.0},C32{re:1.0,im:0.0};
|
||||||
];
|
];
|
||||||
assert_matrix_eq!(sparse_mat, expected);
|
assert_matrix_eq!(sparse_mat, expected);
|
||||||
|
}
|
||||||
let generated_matrixmarket_str = write_to_matrix_market_str(&sparse_mat);
|
|
||||||
let generated_matrix = load_coo_from_matrix_market_str::<Complex<f32>>(&generated_matrixmarket_str).unwrap();
|
#[test]
|
||||||
assert_matrix_eq!(expected, generated_matrix);
|
#[rustfmt::skip]
|
||||||
|
fn test_matrixmarket_write_real(){
|
||||||
|
let dense_matrix = matrix![
|
||||||
|
1.0, 2.0, 3.0;
|
||||||
|
2.0, 0.0, 3.0;
|
||||||
|
];
|
||||||
|
let row_indices = vec![0,1,0,0,1];
|
||||||
|
let col_indices = vec![0,0,1,2,2];
|
||||||
|
let values = vec![1.0,2.0,2.0,3.0,3.0];
|
||||||
|
let coo_matrix = CooMatrix::try_from_triplets(2, 3, row_indices, col_indices, values).unwrap();
|
||||||
|
assert_matrix_eq!(dense_matrix,coo_matrix);
|
||||||
|
let expected = r#"%%matrixmarket matrix coordinate real general
|
||||||
|
% matrixmarket file generated by nalgebra-sparse.
|
||||||
|
2 3 5
|
||||||
|
1 1 1
|
||||||
|
2 1 2
|
||||||
|
1 2 2
|
||||||
|
1 3 3
|
||||||
|
2 3 3
|
||||||
|
"#;
|
||||||
|
let matrixmarket_str = write_to_matrix_market_str(&coo_matrix);
|
||||||
|
assert_eq!(matrixmarket_str,expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_matrixmarket_write_int() {
|
||||||
|
let dense_matrix = matrix![
|
||||||
|
1,2,3;
|
||||||
|
2,0,3;
|
||||||
|
];
|
||||||
|
let row_indices = vec![0, 1, 0, 0, 1];
|
||||||
|
let col_indices = vec![0, 0, 1, 2, 2];
|
||||||
|
let values = vec![1, 2, 2, 3, 3];
|
||||||
|
let coo_matrix = CooMatrix::try_from_triplets(2, 3, row_indices, col_indices, values).unwrap();
|
||||||
|
assert_matrix_eq!(dense_matrix, coo_matrix);
|
||||||
|
let expected = r#"%%matrixmarket matrix coordinate integer general
|
||||||
|
% matrixmarket file generated by nalgebra-sparse.
|
||||||
|
2 3 5
|
||||||
|
1 1 1
|
||||||
|
2 1 2
|
||||||
|
1 2 2
|
||||||
|
1 3 3
|
||||||
|
2 3 3
|
||||||
|
"#;
|
||||||
|
let matrixmarket_str = write_to_matrix_market_str(&coo_matrix);
|
||||||
|
assert_eq!(matrixmarket_str, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_matrixmarket_write_pattern() {
|
||||||
|
let row_indices = vec![0, 1, 0, 0, 1];
|
||||||
|
let col_indices = vec![0, 0, 1, 2, 2];
|
||||||
|
let values = vec![(), (), (), (), ()];
|
||||||
|
let coo_matrix = CooMatrix::try_from_triplets(2, 3, row_indices, col_indices, values).unwrap();
|
||||||
|
let expected = r#"%%matrixmarket matrix coordinate pattern general
|
||||||
|
% matrixmarket file generated by nalgebra-sparse.
|
||||||
|
2 3 5
|
||||||
|
1 1
|
||||||
|
2 1
|
||||||
|
1 2
|
||||||
|
1 3
|
||||||
|
2 3
|
||||||
|
"#;
|
||||||
|
let matrixmarket_str = write_to_matrix_market_str(&coo_matrix);
|
||||||
|
assert_eq!(matrixmarket_str, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_matrixmarket_write_complex() {
|
||||||
|
let row_indices = vec![0, 1, 0, 0, 1];
|
||||||
|
let col_indices = vec![0, 0, 1, 2, 2];
|
||||||
|
let values = vec![
|
||||||
|
C64 { re: 1.0, im: 2.0 },
|
||||||
|
C64 { re: 2.0, im: 3.0 },
|
||||||
|
C64 { re: 3.0, im: 4.0 },
|
||||||
|
C64 { re: 4.0, im: 5.0 },
|
||||||
|
C64 { re: 5.0, im: 6.0 },
|
||||||
|
];
|
||||||
|
let coo_matrix = CooMatrix::try_from_triplets(2, 3, row_indices, col_indices, values).unwrap();
|
||||||
|
let expected = r#"%%matrixmarket matrix coordinate complex general
|
||||||
|
% matrixmarket file generated by nalgebra-sparse.
|
||||||
|
2 3 5
|
||||||
|
1 1 1 2
|
||||||
|
2 1 2 3
|
||||||
|
1 2 3 4
|
||||||
|
1 3 4 5
|
||||||
|
2 3 5 6
|
||||||
|
"#;
|
||||||
|
let matrixmarket_str = write_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_matrix = load_coo_from_matrix_market_str(&generated_matrixmarket_string).unwrap();
|
||||||
|
assert_matrix_eq!(generated_matrix, coo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
proptest! {
|
||||||
|
#[test]
|
||||||
|
fn coo_matrix_market_roundtrip_file(coo in coo_no_duplicates(-10 ..= 10, 0 ..= 10, 0..= 10, 100)) {
|
||||||
|
let mut tempdir = std::env::temp_dir();
|
||||||
|
tempdir.push("temp.mtx");
|
||||||
|
write_to_matrix_market_file(&coo,&tempdir).unwrap();
|
||||||
|
let generated_matrix = load_coo_from_matrix_market_file(tempdir).unwrap();
|
||||||
|
assert_matrix_eq!(generated_matrix, coo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user