00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00028 #ifndef __PRINT_MATRIX_H_
00029 #define __PRINT_MATRIX_H_
00030
00031 #include <linalg.h>
00032 #include <iterator>
00033
00034 namespace std {
00035
00038 template <typename _Tp, int NN, int IND_OFFSET>
00039 ostream& operator<<(ostream& o, const mtl::light1D<_Tp,NN,IND_OFFSET>& a)
00040 {
00041 if(a.size() == 1)
00042 o << a[0];
00043 else if(a.size() <= 0)
00044 {
00045 typename mtl::light1D<_Tp,NN,IND_OFFSET>::const_iterator e(a.end());
00046 copy(a.begin(), --e, ostream_iterator<_Tp>(o, ", "));
00047 o << *e;
00048 }
00049 return o;
00050 }
00051
00054 template <typename _Tp>
00055 ostream& operator<<(ostream &s, const linalg::matrix<_Tp> &A)
00056 {
00057
00058 if (A.nrows()==0 && A.ncols()==0)
00059 s << "[ Zero ]";
00060 else
00061 {
00062
00063 if (A.output_format == linalg::linalg_output_sparse)
00064 {
00065 s << "[";
00066 bool zero = true;
00067 typename linalg::matrix<_Tp>::const_iterator i;
00068 typename linalg::matrix<_Tp>::OneD::const_iterator j;
00069 typename linalg::matrix<_Tp>::const_iterator i_max;
00070 typename linalg::matrix<_Tp>::OneD::const_iterator j_max;
00071 i_max = A.end();
00072 for(i = A.begin(); i < i_max; ++i)
00073 {
00074 j_max = (*i).end();
00075 for(j = (*i).begin(); j < j_max; ++j)
00076 {
00077 if(*j != _Tp())
00078 {
00079 if(zero)
00080 zero = false;
00081 else
00082 s << ",";
00083 s << "(" << j.row() << "," << j.column() << "):" << *j;
00084 }
00085 }
00086 }
00087 s << ((zero) ? " Zero ]":"]");
00088 }
00089 else
00090 {
00091
00092 int i_max, j_max;
00093 s << "[";
00094 i_max = A.nrows() - 1;
00095 j_max = A.ncols() - 1;
00096 for(int i=0; i < i_max; ++i)
00097 {
00098 s << "[";
00099 for(int j=0; j < j_max; ++j)
00100 s << A[i][j] <<",";
00101 s << A[i][j_max] << "],";
00102 }
00103
00104 s << "[";
00105 for(int j=0; j < j_max; ++j)
00106 s << A[i_max][j] << ",";
00107 s << A[i_max][j_max] << "]";
00108 s << "]";
00109 }
00110 }
00111 return s;
00112 }
00113
00114 #if 0
00115 template <class _Tp>
00116 ostream& operator<<(ostream& o, const typename linalg::matrix<_Tp>::Row& r)
00117 {
00118 typename linalg::matrix<_Tp>::Row::const_iterator _x(r.begin());
00119 o << *_x;
00120 for(++_x; _x != r.end(); ++_x)
00121 o << ',' << *_x;
00122 }
00123 #endif
00124
00127 template <typename _Tp>
00128 ostream& operator<<(ostream &s, const linalg::c_matrix<_Tp> &A)
00129 {
00130
00131 if (A.nrows()==0 && A.ncols()==0)
00132 s << "[ Zero ]";
00133 else
00134
00135 {
00136 if (A.output_format == linalg::linalg_output_sparse)
00137 {
00138 s << "[";
00139 bool zero = true;
00140 typename linalg::c_matrix<_Tp>::const_iterator i;
00141 typename linalg::c_matrix<_Tp>::OneD::const_iterator j;
00142 typename linalg::c_matrix<_Tp>::const_iterator i_max;
00143 typename linalg::c_matrix<_Tp>::OneD::const_iterator j_max;
00144 i_max = A.end();
00145 for(i = A.begin(); i < i_max; ++i)
00146 {
00147 j_max = (*i).end();
00148 for(j = (*i).begin(); j < j_max; ++j)
00149 {
00150 if(*j != _Tp())
00151 {
00152 if(zero)
00153 zero = false;
00154 else
00155 s << ",";
00156 s << "(" << j.row() << "," << j.column() << "):" << *j;
00157 }
00158 }
00159 }
00160 s << ((zero) ? " Zero ]":"]");
00161 }
00162 else
00163 {
00164 int i_max, j_max;
00165 s << "[";
00166 i_max = A.nrows() - 1;
00167 j_max = A.ncols() - 1;
00168 for(int i=0; i < i_max ; ++i)
00169 {
00170 s << "[";
00171 for(int j=0; j < j_max; ++j)
00172 s << A[i][j] << ",";
00173 s << A[i][j_max] << "],";
00174 }
00175
00176 s << "[";
00177 for(int j=0; j < j_max; ++j)
00178 s << A[i_max][j] << ",";
00179 s << A[i_max][j_max] << "]";
00180 s << "]";
00181 }
00182 }
00183 return s;
00184 }
00185
00186 }
00187
00188 #endif