ostream_diagmat.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2009 NICTA
00002 // 
00003 // Authors:
00004 // - Conrad Sanderson (conradsand at ieee dot org)
00005 // 
00006 // This file is part of the Armadillo C++ library.
00007 // It is provided without any warranty of fitness
00008 // for any purpose. You can redistribute this file
00009 // and/or modify it under the terms of the GNU
00010 // Lesser General Public License (LGPL) as published
00011 // by the Free Software Foundation, either version 3
00012 // of the License or (at your option) any later version.
00013 // (see http://www.opensource.org/licenses for more info)
00014 
00015 
00016 //! \addtogroup ostream
00017 //! @{
00018 
00019 
00020 //! Print a diagonal matrix to the specified stream.
00021 template<typename T1>
00022 inline
00023 std::ostream&
00024 operator<< (std::ostream& o, const Op<T1,op_diagmat>& X)
00025   {
00026   arma_extra_debug_sigprint();
00027   
00028   typedef typename T1::elem_type eT;
00029   
00030   const unwrap<T1> tmp(X.m);
00031   const Mat<eT>& m = tmp.M;
00032     
00033   arma_debug_check( ((m.is_vec() == false) && (m.is_square() == false)), "operator<<(): incompatible dimensions for diagmat operation" );
00034   
00035   const ios::fmtflags orig_flags = o.flags();
00036   const u32 cell_width = arma_ostream::set_flags(o, m);
00037   
00038   const u32 local_n_rows = (std::max)(m.n_rows, m.n_cols);
00039   
00040   for(u32 row=0; row < local_n_rows; ++row)
00041     {
00042     for(u32 col=0; col < local_n_rows; ++col)
00043       {
00044       if(row != col)
00045         {
00046         o.width(cell_width);
00047         if(is_complex<eT>::value == false)
00048           {
00049           o << "0.0";
00050           }
00051         else
00052           {
00053           o << "(0.0,0.0)";
00054           }
00055         }
00056       else
00057         {
00058         const eT val = m.is_vec() ? m.mem[row] : m.at(row,row);
00059         
00060         o.width(cell_width);
00061         o << val;
00062         }
00063       }
00064       o << '\n';
00065     }
00066   
00067   o.flags(orig_flags);
00068   return o;
00069   }
00070 
00071 //! @}