Update contrib.
2 // Copyright (c) 2000-2002
3 // Joerg Walter, Mathias Koch
5 // Permission to use, copy, modify, distribute and sell this software
6 // and its documentation for any purpose is hereby granted without fee,
7 // provided that the above copyright notice appear in all copies and
8 // that both that copyright notice and this permission notice appear
9 // in supporting documentation. The authors make no representations
10 // about the suitability of this software for any purpose.
11 // It is provided "as is" without express or implied warranty.
13 // The authors gratefully acknowledge the support of
14 // GeNeSys mbH & Co. KG in producing this work.
17 #ifndef _BOOST_UBLAS_IO_
18 #define _BOOST_UBLAS_IO_
20 // Only forward definition required to define stream operations
22 #include <boost/numeric/ublas/matrix_expression.hpp>
25 namespace boost { namespace numeric { namespace ublas {
27 template<class E, class T, class VE>
28 // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
29 std::basic_ostream<E, T> &operator << (std::basic_ostream<E, T> &os,
30 const vector_expression<VE> &v) {
31 typedef typename VE::size_type size_type;
32 size_type size = v ().size ();
33 std::basic_ostringstream<E, T, std::allocator<E> > s;
34 s.flags (os.flags ());
35 s.imbue (os.getloc ());
36 s.precision (os.precision ());
37 s << '[' << size << "](";
40 for (size_type i = 1; i < size; ++ i)
43 return os << s.str ().c_str ();
46 template<class E, class T, class VT, class VA>
47 // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
48 std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
50 typedef typename vector<VT, VA>::size_type size_type;
53 if (is >> ch && ch != '[') {
55 is.setstate (std::ios_base::failbit);
56 } else if (is >> size >> ch && ch != ']') {
58 is.setstate (std::ios_base::failbit);
59 } else if (! is.fail ()) {
60 vector<VT, VA> s (size);
61 if (is >> ch && ch != '(') {
63 is.setstate (std::ios_base::failbit);
64 } else if (! is.fail ()) {
65 for (size_type i = 0; i < size; i ++) {
66 if (is >> s (i) >> ch && ch != ',') {
69 is.setstate (std::ios_base::failbit);
73 if (is >> ch && ch != ')') {
75 is.setstate (std::ios_base::failbit);
84 template<class E, class T, class ME>
85 // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
86 std::basic_ostream<E, T> &operator << (std::basic_ostream<E, T> &os,
87 const matrix_expression<ME> &m) {
88 typedef typename ME::size_type size_type;
89 size_type size1 = m ().size1 ();
90 size_type size2 = m ().size2 ();
91 std::basic_ostringstream<E, T, std::allocator<E> > s;
92 s.flags (os.flags ());
93 s.imbue (os.getloc ());
94 s.precision (os.precision ());
95 s << '[' << size1 << ',' << size2 << "](";
100 for (size_type j = 1; j < size2; ++ j)
101 s << ',' << m () (0, j);
104 for (size_type i = 1; i < size1; ++ i) {
108 for (size_type j = 1; j < size2; ++ j)
109 s << ',' << m () (i, j);
113 return os << s.str ().c_str ();
116 template<class E, class T, class MT, class MF, class MA>
117 // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
118 std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
119 matrix<MT, MF, MA> &m) {
120 typedef typename matrix<MT, MF, MA>::size_type size_type;
122 size_type size1, size2;
123 if (is >> ch && ch != '[') {
125 is.setstate (std::ios_base::failbit);
126 } else if (is >> size1 >> ch && ch != ',') {
128 is.setstate (std::ios_base::failbit);
129 } else if (is >> size2 >> ch && ch != ']') {
131 is.setstate (std::ios_base::failbit);
132 } else if (! is.fail ()) {
133 matrix<MT, MF, MA> s (size1, size2);
134 if (is >> ch && ch != '(') {
136 is.setstate (std::ios_base::failbit);
137 } else if (! is.fail ()) {
138 for (size_type i = 0; i < size1; i ++) {
139 if (is >> ch && ch != '(') {
141 is.setstate (std::ios_base::failbit);
144 for (size_type j = 0; j < size2; j ++) {
145 if (is >> s (i, j) >> ch && ch != ',') {
148 is.setstate (std::ios_base::failbit);
153 if (is >> ch && ch != ')') {
155 is.setstate (std::ios_base::failbit);
158 if (is >> ch && ch != ',') {
161 is.setstate (std::ios_base::failbit);
166 if (is >> ch && ch != ')') {
168 is.setstate (std::ios_base::failbit);
177 // Special input operator for symmetrix_matrix
178 template<class E, class T, class MT, class MF1, class MF2, class MA>
179 // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
180 std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
181 symmetric_matrix<MT, MF1, MF2, MA> &m) {
182 typedef typename symmetric_matrix<MT, MF1, MF2, MA>::size_type size_type;
184 size_type size1, size2;
186 if (is >> ch && ch != '[') {
188 is.setstate (std::ios_base::failbit);
189 } else if (is >> size1 >> ch && ch != ',') {
191 is.setstate (std::ios_base::failbit);
192 } else if (is >> size2 >> ch && (size2 != size1 || ch != ']')) { // symmetric matrix must be square
194 is.setstate (std::ios_base::failbit);
195 } else if (! is.fail ()) {
196 symmetric_matrix<MT, MF1, MF2, MA> s (size1, size2);
197 if (is >> ch && ch != '(') {
199 is.setstate (std::ios_base::failbit);
200 } else if (! is.fail ()) {
201 for (size_type i = 0; i < size1; i ++) {
202 if (is >> ch && ch != '(') {
204 is.setstate (std::ios_base::failbit);
207 for (size_type j = 0; j < size2; j ++) {
208 if (is >> value >> ch && ch != ',') {
211 is.setstate (std::ios_base::failbit);
216 // this is the first time we read this element - set the value
219 else if ( s(i,j) != value ) {
220 // matrix is not symmetric
221 is.setstate (std::ios_base::failbit);
225 if (is >> ch && ch != ')') {
227 is.setstate (std::ios_base::failbit);
230 if (is >> ch && ch != ',') {
233 is.setstate (std::ios_base::failbit);
238 if (is >> ch && ch != ')') {
240 is.setstate (std::ios_base::failbit);