Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:43:06

0001 //
0002 //  Copyright (c) 2000-2010
0003 //  Joerg Walter, Mathias Koch, David Bellot
0004 //
0005 //  Distributed under the Boost Software License, Version 1.0. (See
0006 //  accompanying file LICENSE_1_0.txt or copy at
0007 //  http://www.boost.org/LICENSE_1_0.txt)
0008 //
0009 //  The authors gratefully acknowledge the support of
0010 //  GeNeSys mbH & Co. KG in producing this work.
0011 //
0012 
0013 #ifndef _BOOST_UBLAS_IO_
0014 #define _BOOST_UBLAS_IO_
0015 
0016 // Only forward definition required to define stream operations
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     /** \brief output stream operator for vector expressions
0025      *
0026      * Any vector expressions can be written to a standard output stream
0027      * as defined in the C++ standard library. For example:
0028      * \code
0029      * vector<float> v1(3),v2(3);
0030      * for(size_t i=0; i<3; i++)
0031      * {
0032      *       v1(i) = i+0.2;
0033      *       v2(i) = i+0.3;
0034      * }
0035      * cout << v1+v2 << endl;
0036      * \endcode
0037      * will display the some of the 2 vectors like this:
0038      * \code
0039      * [3](0.5,2.5,4.5)
0040      * \endcode
0041      *
0042      * \param os is a standard basic output stream
0043      * \param v is a vector expression
0044      * \return a reference to the resulting output stream
0045      */
0046     template<class E, class T, class VE>
0047     // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
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     /** \brief input stream operator for vectors
0066      *
0067      * This is used to feed in vectors with data stored as an ASCII representation
0068      * from a standard input stream.
0069      *
0070      * From a file or any valid stream, the format is: 
0071      * \c [<vector size>](<data1>,<data2>,...<dataN>) like for example:
0072      * \code
0073      * [5](1,2.1,3.2,3.14,0.2)
0074      * \endcode
0075      *
0076      * You can use it like this
0077      * \code
0078      * my_input_stream >> my_vector;
0079      * \endcode
0080      *
0081      * You can only put data into a valid \c vector<> not a \c vector_expression
0082      *
0083      * \param is is a standard basic input stream
0084      * \param v is a vector
0085      * \return a reference to the resulting input stream
0086      */
0087     template<class E, class T, class VT, class VA>
0088     // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
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     /** \brief output stream operator for matrix expressions
0126      *
0127      * it outpus the content of a \f$(M \times N)\f$ matrix to a standard output 
0128      * stream using the following format:
0129      * \c[<rows>,<columns>]((<m00>,<m01>,...,<m0N>),...,(<mM0>,<mM1>,...,<mMN>))
0130      *
0131      * For example:
0132      * \code
0133      * matrix<float> m(3,3) = scalar_matrix<float>(3,3,1.0) - diagonal_matrix<float>(3,3,1.0);
0134      * cout << m << endl;
0135      * \encode
0136      * will display
0137      * \code
0138      * [3,3]((0,1,1),(1,0,1),(1,1,0))
0139      * \endcode
0140      * This output is made for storing and retrieving matrices in a simple way but you can
0141      * easily recognize the following: 
0142      * \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]
0143      *
0144      * \param os is a standard basic output stream
0145      * \param m is a matrix expression
0146      * \return a reference to the resulting output stream
0147      */
0148     template<class E, class T, class ME>
0149     // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
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     /** \brief input stream operator for matrices
0181      *
0182      * This is used to feed in matrices with data stored as an ASCII representation
0183      * from a standard input stream.
0184      *
0185      * From a file or any valid standard stream, the format is:
0186      * \c[<rows>,<columns>]((<m00>,<m01>,...,<m0N>),...,(<mM0>,<mM1>,...,<mMN>))
0187      *
0188      * You can use it like this
0189      * \code
0190      * my_input_stream >> my_matrix;
0191      * \endcode
0192      *
0193      * You can only put data into a valid \c matrix<> not a \c matrix_expression
0194      *
0195      * \param is is a standard basic input stream
0196      * \param m is a matrix
0197      * \return a reference to the resulting input stream
0198      */
0199     template<class E, class T, class MT, class MF, class MA>
0200     // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
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     /** \brief special input stream operator for symmetric matrices
0261      *
0262      * This is used to feed in symmetric matrices with data stored as an ASCII 
0263      * representation from a standard input stream.
0264      *
0265      * You can simply write your matrices in a file or any valid stream and read them again 
0266      * at a later time with this function. The format is the following:
0267      * \code [<rows>,<columns>]((<m00>,<m01>,...,<m0N>),...,(<mM0>,<mM1>,...,<mMN>)) \endcode
0268      *
0269      * You can use it like this
0270      * \code
0271      * my_input_stream >> my_symmetric_matrix;
0272      * \endcode
0273      *
0274      * You can only put data into a valid \c symmetric_matrix<>, not in a \c matrix_expression
0275      * This function also checks that input data form a valid symmetric matrix
0276      *
0277      * \param is is a standard basic input stream
0278      * \param m is a \c symmetric_matrix
0279      * \return a reference to the resulting input stream
0280      */
0281     template<class E, class T, class MT, class MF1, class MF2, class MA>
0282     // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
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 != ']')) { // symmetric matrix must be square
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                              // this is the first time we read this element - set the value
0320                             s(i,j) = value;
0321                         }
0322                         else if ( s(i,j) != value ) {
0323                             // matrix is not symmetric
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