2 // Copyright (c) 2000-2010
3 // Joerg Walter, Mathias Koch, David Bellot
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // The authors gratefully acknowledge the support of
10 // GeNeSys mbH & Co. KG in producing this work.
13 #ifndef _BOOST_UBLAS_IO_
14 #define _BOOST_UBLAS_IO_
16 // Only forward definition required to define stream operations
19 #include <boost/numeric/ublas/matrix_expression.hpp>
22 namespace boost { namespace numeric { namespace ublas {
24 /** \brief output stream operator for vector expressions
26 * Any vector expressions can be written to a standard output stream
27 * as defined in the C++ standard library. For example:
29 * vector<float> v1(3),v2(3);
30 * for(size_t i=0; i<3; i++)
35 * cout << v1+v2 << endl;
37 * will display the some of the 2 vectors like this:
42 * \param os is a standard basic output stream
43 * \param v is a vector expression
44 * \return a reference to the resulting output stream
46 template<class E, class T, class VE>
47 // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
48 std::basic_ostream<E, T> &operator << (std::basic_ostream<E, T> &os,
49 const vector_expression<VE> &v) {
50 typedef typename VE::size_type size_type;
51 size_type size = v ().size ();
52 std::basic_ostringstream<E, T, std::allocator<E> > s;
53 s.flags (os.flags ());
54 s.imbue (os.getloc ());
55 s.precision (os.precision ());
56 s << '[' << size << "](";
59 for (size_type i = 1; i < size; ++ i)
62 return os << s.str ().c_str ();
65 /** \brief input stream operator for vectors
67 * This is used to feed in vectors with data stored as an ASCII representation
68 * from a standard input stream.
70 * From a file or any valid stream, the format is:
71 * \c [<vector size>](<data1>,<data2>,...<dataN>) like for example:
73 * [5](1,2.1,3.2,3.14,0.2)
76 * You can use it like this
78 * my_input_stream >> my_vector;
81 * You can only put data into a valid \c vector<> not a \c vector_expression
83 * \param is is a standard basic input stream
84 * \param v is a vector
85 * \return a reference to the resulting input stream
87 template<class E, class T, class VT, class VA>
88 // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
89 std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
91 typedef typename vector<VT, VA>::size_type size_type;
94 if (is >> ch && ch != '[') {
96 is.setstate (std::ios_base::failbit);
97 } else if (is >> size >> ch && ch != ']') {
99 is.setstate (std::ios_base::failbit);
100 } else if (! is.fail ()) {
101 vector<VT, VA> s (size);
102 if (is >> ch && ch != '(') {
104 is.setstate (std::ios_base::failbit);
105 } else if (! is.fail ()) {
106 for (size_type i = 0; i < size; i ++) {
107 if (is >> s (i) >> ch && ch != ',') {
110 is.setstate (std::ios_base::failbit);
114 if (is >> ch && ch != ')') {
116 is.setstate (std::ios_base::failbit);
125 /** \brief output stream operator for matrix expressions
127 * it outpus the content of a \f$(M \times N)\f$ matrix to a standard output
128 * stream using the following format:
129 * \c[<rows>,<columns>]((<m00>,<m01>,...,<m0N>),...,(<mM0>,<mM1>,...,<mMN>))
133 * matrix<float> m(3,3) = scalar_matrix<float>(3,3,1.0) - diagonal_matrix<float>(3,3,1.0);
138 * [3,3]((0,1,1),(1,0,1),(1,1,0))
140 * This output is made for storing and retrieving matrices in a simple way but you can
141 * easily recognize the following:
142 * \f[ \left( \begin{array}{ccc} 1 & 1 & 1\\ 1 & 1 & 1\\ 1 & 1 & 1 \end{array} \right) - \left( \begin{array}{ccc} 1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{array} \right) = \left( \begin{array}{ccc} 0 & 1 & 1\\ 1 & 0 & 1\\ 1 & 1 & 0 \end{array} \right) \f]
144 * \param os is a standard basic output stream
145 * \param m is a matrix expression
146 * \return a reference to the resulting output stream
148 template<class E, class T, class ME>
149 // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
150 std::basic_ostream<E, T> &operator << (std::basic_ostream<E, T> &os,
151 const matrix_expression<ME> &m) {
152 typedef typename ME::size_type size_type;
153 size_type size1 = m ().size1 ();
154 size_type size2 = m ().size2 ();
155 std::basic_ostringstream<E, T, std::allocator<E> > s;
156 s.flags (os.flags ());
157 s.imbue (os.getloc ());
158 s.precision (os.precision ());
159 s << '[' << size1 << ',' << size2 << "](";
164 for (size_type j = 1; j < size2; ++ j)
165 s << ',' << m () (0, j);
168 for (size_type i = 1; i < size1; ++ i) {
172 for (size_type j = 1; j < size2; ++ j)
173 s << ',' << m () (i, j);
177 return os << s.str ().c_str ();
180 /** \brief input stream operator for matrices
182 * This is used to feed in matrices with data stored as an ASCII representation
183 * from a standard input stream.
185 * From a file or any valid standard stream, the format is:
186 * \c[<rows>,<columns>]((<m00>,<m01>,...,<m0N>),...,(<mM0>,<mM1>,...,<mMN>))
188 * You can use it like this
190 * my_input_stream >> my_matrix;
193 * You can only put data into a valid \c matrix<> not a \c matrix_expression
195 * \param is is a standard basic input stream
196 * \param m is a matrix
197 * \return a reference to the resulting input stream
199 template<class E, class T, class MT, class MF, class MA>
200 // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
201 std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
202 matrix<MT, MF, MA> &m) {
203 typedef typename matrix<MT, MF, MA>::size_type size_type;
205 size_type size1, size2;
206 if (is >> ch && ch != '[') {
208 is.setstate (std::ios_base::failbit);
209 } else if (is >> size1 >> ch && ch != ',') {
211 is.setstate (std::ios_base::failbit);
212 } else if (is >> size2 >> ch && ch != ']') {
214 is.setstate (std::ios_base::failbit);
215 } else if (! is.fail ()) {
216 matrix<MT, MF, MA> s (size1, size2);
217 if (is >> ch && ch != '(') {
219 is.setstate (std::ios_base::failbit);
220 } else if (! is.fail ()) {
221 for (size_type i = 0; i < size1; i ++) {
222 if (is >> ch && ch != '(') {
224 is.setstate (std::ios_base::failbit);
227 for (size_type j = 0; j < size2; j ++) {
228 if (is >> s (i, j) >> ch && ch != ',') {
231 is.setstate (std::ios_base::failbit);
236 if (is >> ch && ch != ')') {
238 is.setstate (std::ios_base::failbit);
241 if (is >> ch && ch != ',') {
244 is.setstate (std::ios_base::failbit);
249 if (is >> ch && ch != ')') {
251 is.setstate (std::ios_base::failbit);
260 /** \brief special input stream operator for symmetric matrices
262 * This is used to feed in symmetric matrices with data stored as an ASCII
263 * representation from a standard input stream.
265 * You can simply write your matrices in a file or any valid stream and read them again
266 * at a later time with this function. The format is the following:
267 * \code [<rows>,<columns>]((<m00>,<m01>,...,<m0N>),...,(<mM0>,<mM1>,...,<mMN>)) \endcode
269 * You can use it like this
271 * my_input_stream >> my_symmetric_matrix;
274 * You can only put data into a valid \c symmetric_matrix<>, not in a \c matrix_expression
275 * This function also checks that input data form a valid symmetric matrix
277 * \param is is a standard basic input stream
278 * \param m is a \c symmetric_matrix
279 * \return a reference to the resulting input stream
281 template<class E, class T, class MT, class MF1, class MF2, class MA>
282 // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
283 std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
284 symmetric_matrix<MT, MF1, MF2, MA> &m) {
285 typedef typename symmetric_matrix<MT, MF1, MF2, MA>::size_type size_type;
287 size_type size1, size2;
289 if (is >> ch && ch != '[') {
291 is.setstate (std::ios_base::failbit);
292 } else if (is >> size1 >> ch && ch != ',') {
294 is.setstate (std::ios_base::failbit);
295 } else if (is >> size2 >> ch && (size2 != size1 || ch != ']')) { // symmetric matrix must be square
297 is.setstate (std::ios_base::failbit);
298 } else if (! is.fail ()) {
299 symmetric_matrix<MT, MF1, MF2, MA> s (size1, size2);
300 if (is >> ch && ch != '(') {
302 is.setstate (std::ios_base::failbit);
303 } else if (! is.fail ()) {
304 for (size_type i = 0; i < size1; i ++) {
305 if (is >> ch && ch != '(') {
307 is.setstate (std::ios_base::failbit);
310 for (size_type j = 0; j < size2; j ++) {
311 if (is >> value >> ch && ch != ',') {
314 is.setstate (std::ios_base::failbit);
319 // this is the first time we read this element - set the value
322 else if ( s(i,j) != value ) {
323 // matrix is not symmetric
324 is.setstate (std::ios_base::failbit);
328 if (is >> ch && ch != ')') {
330 is.setstate (std::ios_base::failbit);
333 if (is >> ch && ch != ',') {
336 is.setstate (std::ios_base::failbit);
341 if (is >> ch && ch != ')') {
343 is.setstate (std::ios_base::failbit);