• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

/home/hauberg/Dokumenter/Capture/humim-tracker-0.1/src/OpenTissue/OpenTissue/core/math/big/io/big_matlab_write.h

Go to the documentation of this file.
00001 #ifndef OPENTISSUE_CORE_MATH_BIG_IO_BIG_MATLAB_WRITE_H
00002 #define OPENTISSUE_CORE_MATH_BIG_IO_BIG_MATLAB_WRITE_H
00003 //
00004 // OpenTissue Template Library
00005 // - A generic toolbox for physics-based modeling and simulation.
00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen.
00007 //
00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php
00009 //
00010 #include <OpenTissue/configuration.h>
00011 
00012 #include <OpenTissue/core/math/big/big_types.h>
00013 
00014 #include <iosfwd>
00015 #include <stdexcept>
00016 
00017 namespace OpenTissue
00018 {
00019   namespace math
00020   {
00021     namespace big
00022     {
00031       template<class E, class T, class VE>
00032       inline std::basic_ostream<E, T> & operator << (
00033         std::basic_ostream<E, T> &os
00034         , ublas::vector_expression<VE> const & v) 
00035       {
00036         typedef typename VE::size_type size_type;
00037         size_type size = v ().size ();
00038         std::basic_ostringstream<E, T, std::allocator<E> > s;
00039         s.flags (os.flags ());
00040         s.imbue (os.getloc ());
00041         s.precision (os.precision ());
00042         s << '[';
00043         if (size > 0)
00044           s << v () (0);
00045         for (size_type i = 1; i < size; ++ i)
00046           s << ',' << v () (i);
00047         s << ']';
00048         return os << s.str ().c_str ();
00049       }
00050 
00059       template<class T, class F>
00060       inline std::ostream & operator << (
00061         std::ostream & os
00062         , ublas::compressed_matrix<T, F> const & A
00063         )
00064       {
00065         //--- get dimensions
00066         unsigned int m = static_cast<unsigned int>(A.size1());
00067         unsigned int n = static_cast<unsigned int>(A.size2());
00068         unsigned int nzeros = 0;
00069         unsigned int idx = 0;
00070         nzeros = 0;
00071         for(unsigned int i=0;i<A.size1();++i)
00072           for(unsigned int j=0;j<A.size2();++j)
00073             if(A(i,j))
00074               ++nzeros;
00075 
00076         //--- allocate space...
00077         ublas::vector<unsigned int> idx1(nzeros);
00078         ublas::vector<unsigned int> idx2(nzeros);
00079         ublas::vector<T> value(nzeros);
00080 
00081         for(unsigned int i=0;i<A.size1();++i)
00082           for(unsigned int j=0;j<A.size2();++j)
00083             if(A(i,j))
00084             {
00085               idx1(idx) = i + 1;
00086               idx2(idx) = j + 1;
00087               value(idx) = A(i,j);
00088               ++idx;
00089             }
00090             //--- create matlab output
00091             os << "sparse(" << idx1 << "," << idx2 << "," << value << "," << m << "," << n << ")";
00092             return os;         
00093       };
00094 
00103       template<class E, class T, class ME>
00104       inline std::basic_ostream<E, T> & operator << (
00105         std::basic_ostream<E, T> & os
00106         , ublas::matrix_expression<ME> const & m
00107         ) 
00108       {
00109         typedef typename ME::size_type size_type;
00110         size_type size1 = m ().size1 ();
00111         size_type size2 = m ().size2 ();
00112         std::basic_ostringstream<E, T, std::allocator<E> > s;
00113         s.flags (os.flags ());
00114         s.imbue (os.getloc ());
00115         s.precision (os.precision ());
00116         s << '[';
00117         if (size1 > 0) {
00118           s << '[' ;
00119           if (size2 > 0)
00120             s << m () (0, 0);
00121           for (size_type j = 1; j < size2; ++ j)
00122             s << ',' << m () (0, j);
00123           s << ']';
00124         }
00125         for (size_type i = 1; i < size1; ++ i) {
00126           s << ";[" ;
00127           if (size2 > 0)
00128             s << m () (i, 0);
00129           for (size_type j = 1; j < size2; ++ j)
00130             s << ',' << m () (i, j);
00131           s << ']';
00132         }
00133         s << ']';
00134         return os << s.str ().c_str ();
00135       }
00136 
00137     } // end of namespace big
00138   } // end of namespace math
00139 } // end of namespace OpenTissue
00140 
00141 // OPENTISSUE_CORE_MATH_BIG_IO_BIG_MATLAB_WRITE_H
00142 #endif
00143 
00144 
00145 

Generated on Thu Dec 1 2011 12:52:05 for HUMIM Tracker by  doxygen 1.7.1