1use std::io::Write;
10use std::path::Path;
11
12use sprs::CsMat;
13
14use crate::{Error, Result};
15
16pub fn emit_mtx(matrix: &CsMat<f64>, path: impl AsRef<Path>) -> Result<()> {
17 commit_one_file(path.as_ref(), to_mtx_bytes(matrix)?)
18}
19
20pub fn to_mtx_bytes(matrix: &CsMat<f64>) -> Result<Vec<u8>> {
23 if is_exactly_symmetric(matrix) {
24 symmetric_mtx_bytes(matrix)
25 } else {
26 general_mtx_bytes(matrix)
27 }
28}
29
30pub(crate) fn commit_one_file(path: &Path, bytes: Vec<u8>) -> Result<()> {
34 let artifact = powerio_core::MemoryArtifact::new(
39 powerio_core::ArtifactPath::new("case").expect("static placeholder name"),
40 bytes,
41 );
42 powerio_core::Destination::path(path).__commit_artifacts(
43 false,
44 powerio_core::Fidelity::Canonical,
45 vec![artifact],
46 Vec::new(),
47 )?;
48 Ok(())
49}
50
51fn symmetric_mtx_bytes(matrix: &CsMat<f64>) -> Result<Vec<u8>> {
52 let mut w = Vec::new();
53 writeln!(w, "%%MatrixMarket matrix coordinate real symmetric")?;
54 writeln!(w, "% written by powerio")?;
55
56 let nnz = matrix
58 .iter()
59 .filter(|&(_, (i, j))| i >= j)
60 .filter(|&(&v, _)| v != 0.0)
61 .count();
62 writeln!(w, "{} {} {}", matrix.rows(), matrix.cols(), nnz)?;
63
64 for (&v, (i, j)) in matrix {
65 if i < j || v == 0.0 {
66 continue;
67 }
68 writeln!(w, "{} {} {:.16e}", i + 1, j + 1, v)?;
69 }
70 Ok(w)
71}
72
73fn general_mtx_bytes(matrix: &CsMat<f64>) -> Result<Vec<u8>> {
74 let mut w = Vec::new();
75 writeln!(w, "%%MatrixMarket matrix coordinate real general")?;
76 writeln!(w, "% written by powerio")?;
77 writeln!(w, "{} {} {}", matrix.rows(), matrix.cols(), matrix.nnz())?;
78 for (&v, (i, j)) in matrix {
79 writeln!(w, "{} {} {:.16e}", i + 1, j + 1, v)?;
80 }
81 Ok(w)
82}
83
84pub fn read_mtx(path: impl AsRef<Path>) -> Result<CsMat<f64>> {
86 let tri: sprs::TriMat<f64> =
87 sprs::io::read_matrix_market(path).map_err(|e| Error::Mtx(e.to_string()))?;
88 Ok(tri.to_csr())
89}
90
91pub fn read_vector_mtx(path: impl AsRef<Path>) -> Result<Vec<f64>> {
94 let text = std::fs::read_to_string(path)?;
95 let mut lines = text.lines().filter(|l| !l.starts_with('%'));
96 let header = lines
97 .next()
98 .ok_or_else(|| Error::Mtx("empty vector file".into()))?;
99 let len: usize = header
100 .split_whitespace()
101 .next()
102 .and_then(|t| t.parse().ok())
103 .ok_or_else(|| Error::Mtx(format!("bad vector dimensions line: {header:?}")))?;
104 let values = lines
105 .take(len)
106 .map(|l| {
107 l.trim()
108 .parse::<f64>()
109 .map_err(|_| Error::Mtx(format!("bad vector entry: {l:?}")))
110 })
111 .collect::<Result<Vec<_>>>()?;
112 if values.len() != len {
113 return Err(Error::Mtx(format!(
114 "expected {len} entries, got {}",
115 values.len()
116 )));
117 }
118 Ok(values)
119}
120
121pub fn emit_vector_mtx(values: &[f64], path: impl AsRef<Path>) -> Result<()> {
123 commit_one_file(path.as_ref(), to_vector_mtx_bytes(values)?)
124}
125
126pub fn to_vector_mtx_bytes(values: &[f64]) -> Result<Vec<u8>> {
128 let mut w = Vec::new();
129 writeln!(w, "%%MatrixMarket matrix array real general")?;
130 writeln!(w, "% written by powerio")?;
131 writeln!(w, "{} 1", values.len())?;
132 for v in values {
133 writeln!(w, "{v:.16e}")?;
134 }
135 Ok(w)
136}
137
138fn is_exactly_symmetric(a: &CsMat<f64>) -> bool {
145 if a.rows() != a.cols() {
146 return false;
147 }
148 for (i, row) in a.outer_iterator().enumerate() {
149 for (j, &v) in row.iter() {
150 match a.get(j, i) {
153 Some(&mirror) if mirror.to_bits() == v.to_bits() => {}
154 _ => return false,
155 }
156 }
157 }
158 true
159}
160
161#[cfg(test)]
162mod tests {
163 use sprs::TriMat;
164
165 use super::emit_mtx;
166
167 #[test]
168 fn single_file_writers_take_any_platform_legal_target_name() {
169 let base = std::env::temp_dir().join(format!(
173 "powerio-mtx-target-names-{}-{}",
174 std::process::id(),
175 std::time::SystemTime::now()
176 .duration_since(std::time::UNIX_EPOCH)
177 .unwrap()
178 .as_nanos()
179 ));
180 std::fs::create_dir_all(&base).unwrap();
181 #[cfg(unix)]
182 let nonportable = ["aux.mtx", "trailing.", "with space "];
183 #[cfg(not(unix))]
184 let nonportable = ["with space.mtx"];
185 for name in nonportable {
186 let target = base.join(name);
187 super::emit_vector_mtx(&[3.0], &target).unwrap_or_else(|error| {
188 panic!("{name}: {error}");
189 });
190 assert_eq!(super::read_vector_mtx(&target).unwrap(), vec![3.0]);
191 let error = super::emit_vector_mtx(&[4.0], &target).unwrap_err();
193 assert!(matches!(error, crate::Error::Commit(_)), "{error:?}");
194 assert_eq!(super::read_vector_mtx(&target).unwrap(), vec![3.0]);
195 }
196 #[cfg(target_os = "linux")]
200 {
201 use std::os::unix::ffi::OsStrExt;
202 let raw =
203 std::ffi::OsStr::from_bytes(&[b'r', b'a', b'w', 0xFF, b'.', b'm', b't', b'x']);
204 let target = base.join(raw);
205 super::emit_vector_mtx(&[5.0], &target).unwrap();
206 assert_eq!(super::read_vector_mtx(&target).unwrap(), vec![5.0]);
207 }
208 let _ = std::fs::remove_dir_all(&base);
209 }
210
211 #[test]
212 fn single_file_writers_never_replace_an_existing_entry() {
213 let path = temp_path("no-clobber-single");
214 std::fs::write(&path, b"precious").unwrap();
215 let error = super::emit_vector_mtx(&[1.0, 2.0], &path).unwrap_err();
216 assert!(matches!(error, crate::Error::Commit(_)), "{error:?}");
217 assert_eq!(std::fs::read(&path).unwrap(), b"precious");
218 let _ = std::fs::remove_file(&path);
219
220 let fresh = temp_path("no-clobber-single-fresh");
222 super::emit_vector_mtx(&[1.0, 2.0], &fresh).unwrap();
223 assert_eq!(super::read_vector_mtx(&fresh).unwrap(), vec![1.0, 2.0]);
224 let _ = std::fs::remove_file(&fresh);
225 }
226
227 #[test]
228 fn value_asymmetric_matrix_writes_general_mtx() {
229 let mut tri = TriMat::new((2, 2));
230 tri.add_triplet(0, 0, 2.0);
231 tri.add_triplet(0, 1, -1.0);
232 tri.add_triplet(1, 0, -2.0);
233 tri.add_triplet(1, 1, 2.0);
234 let matrix = tri.to_csr();
235
236 let path = temp_path("value-asymmetric");
237 emit_mtx(&matrix, &path).unwrap();
238 let text = std::fs::read_to_string(&path).unwrap();
239 let _ = std::fs::remove_file(&path);
240
241 assert!(
242 text.lines().next().unwrap().ends_with("general"),
243 "value-asymmetric matrices must not be written with a symmetric header"
244 );
245 }
246
247 #[test]
248 fn a_matrix_asymmetric_below_the_old_tolerance_writes_general() {
249 let mut tri = TriMat::new((2, 2));
254 tri.add_triplet(0, 0, 5.0);
255 tri.add_triplet(0, 1, 3.000_000_000_000_001);
256 tri.add_triplet(1, 0, 3.0);
257 tri.add_triplet(1, 1, 5.0);
258 let matrix = tri.to_csr();
259
260 let path = temp_path("near-symmetric");
261 emit_mtx(&matrix, &path).unwrap();
262 let text = std::fs::read_to_string(&path).unwrap();
263 let _ = std::fs::remove_file(&path);
264
265 assert!(
266 text.lines().next().unwrap().ends_with("general"),
267 "a matrix that is only nearly symmetric must carry both triangles:\n{text}"
268 );
269 }
270
271 #[test]
272 fn a_structurally_asymmetric_matrix_writes_general() {
273 let mut tri = TriMat::new((2, 2));
276 tri.add_triplet(0, 0, 5.0);
277 tri.add_triplet(0, 1, 0.0);
278 tri.add_triplet(1, 1, 5.0);
279 let matrix = tri.to_csr();
280
281 let path = temp_path("structurally-asymmetric");
282 emit_mtx(&matrix, &path).unwrap();
283 let text = std::fs::read_to_string(&path).unwrap();
284 let _ = std::fs::remove_file(&path);
285
286 assert!(
287 text.lines().next().unwrap().ends_with("general"),
288 "an unmirrored stored entry must not claim a symmetric header:\n{text}"
289 );
290 }
291
292 #[test]
293 fn an_exactly_symmetric_matrix_still_writes_symmetric() {
294 let mut tri = TriMat::new((2, 2));
295 tri.add_triplet(0, 0, 5.0);
296 tri.add_triplet(0, 1, -3.0);
297 tri.add_triplet(1, 0, -3.0);
298 tri.add_triplet(1, 1, 5.0);
299 let matrix = tri.to_csr();
300
301 let path = temp_path("exactly-symmetric");
302 emit_mtx(&matrix, &path).unwrap();
303 let text = std::fs::read_to_string(&path).unwrap();
304 let _ = std::fs::remove_file(&path);
305
306 assert!(
307 text.lines().next().unwrap().ends_with("symmetric"),
308 "an exactly symmetric matrix keeps the compact form:\n{text}"
309 );
310 }
311
312 fn temp_path(stem: &str) -> std::path::PathBuf {
313 let mut path = std::env::temp_dir();
314 let nanos = std::time::SystemTime::now()
315 .duration_since(std::time::UNIX_EPOCH)
316 .map_or(0, |d| d.as_nanos());
317 path.push(format!("powerio-{stem}-{nanos}.mtx"));
318 path
319 }
320}