1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
pub mod error;
pub mod matrix;
pub mod iter;
pub mod vector;
#[cfg(test)]
mod test;
use matrix::sparse::{Matrix,Element};
use error::Error;
use std::fs::File;
use std::io::{BufReader,BufRead};
use std::path::Path;
pub fn from_file(filename: &Path) -> Result<Matrix<f64>,Error> {
let f = File::open(filename)?;
let reader = BufReader::new(f);
let mut lines = reader.lines();
let first = lines.next().ok_or(Error::InvalidFile)??;
let (ty,prop) = get_type_and_prop(&first)?;
let mut next_line: String;
loop {
next_line = lines.next().ok_or(Error::InvalidFile)??;
if !next_line.starts_with("%") {
break;
}
}
let elements = next_line.split_whitespace()
.map(|e| e.parse::<usize>()
.map_err(|_| Error::InvalidFile))
.collect::<Result<Vec<usize>,Error>>()?;
if elements.len() < 3 {
return Err(Error::InvalidFile);
}
let (rows,columns,num_entries) = (elements[0],elements[1],elements[2]);
let mut entries: Vec<Element<f64>> = Vec::with_capacity(num_entries);
for _ in 0 .. num_entries {
let line = lines.next().ok_or(Error::InvalidFile)??;
let (row,column,data) = read_line(line,&ty)?;
match prop {
Prop::General => {},
_ => if column != row { entries.push(Element(column,row,data)) }
}
entries.push(Element(row,column,data));
}
Matrix::new(rows,columns,entries)
}
#[derive(PartialEq,Eq)]
enum Type {
Real,
Integer,
Complex,
Pattern
}
fn word_to_type(s: &str) -> Option<Type> {
Some(match s {
"real" => Type::Real,
"integer" => Type::Integer,
"complex" => Type::Complex,
"pattern" => Type::Pattern,
_ => return None
})
}
#[derive(PartialEq,Eq)]
enum Prop {
General,
Symmetric,
SkewSymmetric,
Hermitian
}
fn word_to_prop(s: &str) -> Option<Prop> {
Some(match s {
"general" => Prop::General,
"symmetric" => Prop::Symmetric,
"skew-symmetric" => Prop::SkewSymmetric,
"hermitian" => Prop::Hermitian,
_ => return None
})
}
fn get_type_and_prop(first: &String) -> Result<(Type,Prop),Error> {
let mut words = first.split_whitespace();
if "%%MatrixMarket" != words.next().ok_or(Error::InvalidFile)? {
return Err(Error::InvalidFile)
}
if "matrix" != words.next().ok_or(Error::InvalidFile)? {
return Err(Error::InvalidFile)
}
if "coordinate" != words.next().ok_or(Error::InvalidFile)? {
return Err(Error::InvalidFile)
}
let ty = word_to_type(words.next().ok_or(Error::InvalidFile)?)
.ok_or(Error::InvalidFile)?;
let prop = word_to_prop(words.next().ok_or(Error::InvalidFile)?)
.ok_or(Error::InvalidFile)?;
Ok((ty,prop))
}
fn read_line(line: String, ty: &Type) -> Result<(usize,usize,f64),Error> {
let words = line.split_whitespace();
let elements = words.clone().take(2)
.map(|e| e.parse::<usize>()
.map_err(|_| Error::InvalidFile))
.collect::<Result<Vec<usize>,Error>>()?;
if elements.len() < 2 {
return Err(Error::InvalidFile);
}
let (row,column) = (elements[0],elements[1]);
let data: f64 = match ty {
Type::Pattern => 1.0,
_ => {
let word = words.skip(2).next().ok_or(Error::InvalidFile)?;
word.parse::<f64>()?
}
};
Ok((row,column,data))
}