File indexing completed on 2025-01-18 09:43:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef _BOOST_UBLAS_IO_
0014 #define _BOOST_UBLAS_IO_
0015
0016
0017 #include <iosfwd>
0018 #include <sstream>
0019 #include <boost/numeric/ublas/matrix_expression.hpp>
0020
0021
0022 namespace boost { namespace numeric { namespace ublas {
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 template<class E, class T, class VE>
0047
0048 std::basic_ostream<E, T> &operator << (std::basic_ostream<E, T> &os,
0049 const vector_expression<VE> &v) {
0050 typedef typename VE::size_type size_type;
0051 size_type size = v ().size ();
0052 std::basic_ostringstream<E, T, std::allocator<E> > s;
0053 s.flags (os.flags ());
0054 s.imbue (os.getloc ());
0055 s.precision (os.precision ());
0056 s << '[' << size << "](";
0057 if (size > 0)
0058 s << v () (0);
0059 for (size_type i = 1; i < size; ++ i)
0060 s << ',' << v () (i);
0061 s << ')';
0062 return os << s.str ().c_str ();
0063 }
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 template<class E, class T, class VT, class VA>
0088
0089 std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
0090 vector<VT, VA> &v) {
0091 typedef typename vector<VT, VA>::size_type size_type;
0092 E ch;
0093 size_type size;
0094 if (is >> ch && ch != '[') {
0095 is.putback (ch);
0096 is.setstate (std::ios_base::failbit);
0097 } else if (is >> size >> ch && ch != ']') {
0098 is.putback (ch);
0099 is.setstate (std::ios_base::failbit);
0100 } else if (! is.fail ()) {
0101 vector<VT, VA> s (size);
0102 if (is >> ch && ch != '(') {
0103 is.putback (ch);
0104 is.setstate (std::ios_base::failbit);
0105 } else if (! is.fail ()) {
0106 for (size_type i = 0; i < size; i ++) {
0107 if (is >> s (i) >> ch && ch != ',') {
0108 is.putback (ch);
0109 if (i < size - 1)
0110 is.setstate (std::ios_base::failbit);
0111 break;
0112 }
0113 }
0114 if (is >> ch && ch != ')') {
0115 is.putback (ch);
0116 is.setstate (std::ios_base::failbit);
0117 }
0118 }
0119 if (! is.fail ())
0120 v.swap (s);
0121 }
0122 return is;
0123 }
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 template<class E, class T, class ME>
0149
0150 std::basic_ostream<E, T> &operator << (std::basic_ostream<E, T> &os,
0151 const matrix_expression<ME> &m) {
0152 typedef typename ME::size_type size_type;
0153 size_type size1 = m ().size1 ();
0154 size_type size2 = m ().size2 ();
0155 std::basic_ostringstream<E, T, std::allocator<E> > s;
0156 s.flags (os.flags ());
0157 s.imbue (os.getloc ());
0158 s.precision (os.precision ());
0159 s << '[' << size1 << ',' << size2 << "](";
0160 if (size1 > 0) {
0161 s << '(' ;
0162 if (size2 > 0)
0163 s << m () (0, 0);
0164 for (size_type j = 1; j < size2; ++ j)
0165 s << ',' << m () (0, j);
0166 s << ')';
0167 }
0168 for (size_type i = 1; i < size1; ++ i) {
0169 s << ",(" ;
0170 if (size2 > 0)
0171 s << m () (i, 0);
0172 for (size_type j = 1; j < size2; ++ j)
0173 s << ',' << m () (i, j);
0174 s << ')';
0175 }
0176 s << ')';
0177 return os << s.str ().c_str ();
0178 }
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 template<class E, class T, class MT, class MF, class MA>
0200
0201 std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
0202 matrix<MT, MF, MA> &m) {
0203 typedef typename matrix<MT, MF, MA>::size_type size_type;
0204 E ch;
0205 size_type size1, size2;
0206 if (is >> ch && ch != '[') {
0207 is.putback (ch);
0208 is.setstate (std::ios_base::failbit);
0209 } else if (is >> size1 >> ch && ch != ',') {
0210 is.putback (ch);
0211 is.setstate (std::ios_base::failbit);
0212 } else if (is >> size2 >> ch && ch != ']') {
0213 is.putback (ch);
0214 is.setstate (std::ios_base::failbit);
0215 } else if (! is.fail ()) {
0216 matrix<MT, MF, MA> s (size1, size2);
0217 if (is >> ch && ch != '(') {
0218 is.putback (ch);
0219 is.setstate (std::ios_base::failbit);
0220 } else if (! is.fail ()) {
0221 for (size_type i = 0; i < size1; i ++) {
0222 if (is >> ch && ch != '(') {
0223 is.putback (ch);
0224 is.setstate (std::ios_base::failbit);
0225 break;
0226 }
0227 for (size_type j = 0; j < size2; j ++) {
0228 if (is >> s (i, j) >> ch && ch != ',') {
0229 is.putback (ch);
0230 if (j < size2 - 1) {
0231 is.setstate (std::ios_base::failbit);
0232 break;
0233 }
0234 }
0235 }
0236 if (is >> ch && ch != ')') {
0237 is.putback (ch);
0238 is.setstate (std::ios_base::failbit);
0239 break;
0240 }
0241 if (is >> ch && ch != ',') {
0242 is.putback (ch);
0243 if (i < size1 - 1) {
0244 is.setstate (std::ios_base::failbit);
0245 break;
0246 }
0247 }
0248 }
0249 if (is >> ch && ch != ')') {
0250 is.putback (ch);
0251 is.setstate (std::ios_base::failbit);
0252 }
0253 }
0254 if (! is.fail ())
0255 m.swap (s);
0256 }
0257 return is;
0258 }
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281 template<class E, class T, class MT, class MF1, class MF2, class MA>
0282
0283 std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
0284 symmetric_matrix<MT, MF1, MF2, MA> &m) {
0285 typedef typename symmetric_matrix<MT, MF1, MF2, MA>::size_type size_type;
0286 E ch;
0287 size_type size1, size2;
0288 MT value;
0289 if (is >> ch && ch != '[') {
0290 is.putback (ch);
0291 is.setstate (std::ios_base::failbit);
0292 } else if (is >> size1 >> ch && ch != ',') {
0293 is.putback (ch);
0294 is.setstate (std::ios_base::failbit);
0295 } else if (is >> size2 >> ch && (size2 != size1 || ch != ']')) {
0296 is.putback (ch);
0297 is.setstate (std::ios_base::failbit);
0298 } else if (! is.fail ()) {
0299 symmetric_matrix<MT, MF1, MF2, MA> s (size1, size2);
0300 if (is >> ch && ch != '(') {
0301 is.putback (ch);
0302 is.setstate (std::ios_base::failbit);
0303 } else if (! is.fail ()) {
0304 for (size_type i = 0; i < size1; i ++) {
0305 if (is >> ch && ch != '(') {
0306 is.putback (ch);
0307 is.setstate (std::ios_base::failbit);
0308 break;
0309 }
0310 for (size_type j = 0; j < size2; j ++) {
0311 if (is >> value >> ch && ch != ',') {
0312 is.putback (ch);
0313 if (j < size2 - 1) {
0314 is.setstate (std::ios_base::failbit);
0315 break;
0316 }
0317 }
0318 if (i <= j) {
0319
0320 s(i,j) = value;
0321 }
0322 else if ( s(i,j) != value ) {
0323
0324 is.setstate (std::ios_base::failbit);
0325 break;
0326 }
0327 }
0328 if (is >> ch && ch != ')') {
0329 is.putback (ch);
0330 is.setstate (std::ios_base::failbit);
0331 break;
0332 }
0333 if (is >> ch && ch != ',') {
0334 is.putback (ch);
0335 if (i < size1 - 1) {
0336 is.setstate (std::ios_base::failbit);
0337 break;
0338 }
0339 }
0340 }
0341 if (is >> ch && ch != ')') {
0342 is.putback (ch);
0343 is.setstate (std::ios_base::failbit);
0344 }
0345 }
0346 if (! is.fail ())
0347 m.swap (s);
0348 }
0349 return is;
0350 }
0351
0352
0353 }}}
0354
0355 #endif