23 #ifndef O2SCL_COLUMNIFY_H 24 #define O2SCL_COLUMNIFY_H 34 #include <boost/numeric/ublas/vector.hpp> 35 #include <boost/numeric/ublas/matrix.hpp> 37 #include <o2scl/misc.h> 38 #include <o2scl/string_conv.h> 40 #ifndef DOXYGEN_NO_O2NS 95 template<
class mat_
string_t,
class vec_
string_t,
class vec_
int_t>
96 int align(
const mat_string_t &
table,
size_t ncols,
size_t nrows,
97 vec_string_t &ctable, vec_int_t &align_spec) {
102 for(
size_t i=0;i<ncols;i++) {
109 for(
size_t i=0;i<ncols;i++) {
110 for(
size_t j=0;j<nrows;j++) {
116 if (align_spec[i]==align_dp) {
117 size_t loc=table[i][j].find(
'.');
118 std::string left, right;
119 if (loc!=std::string::npos) {
120 left=table[i][j].substr(0,loc+1);
121 right=table[i][j].substr(loc+1,
122 table[i][j].length()-loc-1);
124 left=table[i][j]+
' ';
127 if (left.length()>csizes[i]) csizes[i]=left.length();
128 if (right.length()>csizes2[i]) csizes2[i]=right.length();
133 if (table[i][j].length()>csizes[i]) csizes[i]=table[i][j].length();
142 for(
size_t j=0;j<nrows;j++) {
146 for(
size_t i=0;i<ncols;i++) {
149 if (align_spec[i]==align_right) {
151 for(
size_t k=table[i][j].length();k<csizes[i];k++) {
155 if (i!=ncols-1) tmp+=
' ';
157 }
else if (align_spec[i]==align_left) {
160 for(
size_t k=table[i][j].length();k<csizes[i];k++) {
163 if (i!=ncols-1) tmp+=
' ';
165 }
else if (align_spec[i]==align_lmid) {
167 size_t le=(csizes[i]-table[i][j].length())/2;
168 size_t ri=csizes[i]-table[i][j].length()-le;
169 for(
size_t k=0;k<le;k++) tmp+=
' ';
171 for(
size_t k=0;k<ri;k++) tmp+=
' ';
172 if (i!=ncols-1) tmp+=
' ';
174 }
else if (align_spec[i]==align_rmid) {
176 size_t ri=(csizes[i]-table[i][j].length())/2;
177 size_t le=csizes[i]-table[i][j].length()-ri;
178 for(
size_t k=0;k<le;k++) tmp+=
' ';
180 for(
size_t k=0;k<ri;k++) tmp+=
' ';
181 if (i!=ncols-1) tmp+=
' ';
183 }
else if (align_spec[i]==align_dp) {
185 size_t loc=table[i][j].find(
'.');
186 std::string left, right;
187 if (loc!=std::string::npos) {
188 left=table[i][j].substr(0,loc+1);
189 right=table[i][j].substr(loc+1,
190 table[i][j].length()-loc-1);
192 left=table[i][j]+
' ';
196 for(
size_t k=left.length();k<csizes[i];k++) tmp+=
' ';
199 for(
size_t k=right.length();k<csizes2[i];k++) tmp+=
' ';
200 if (i!=ncols-1) tmp+=
' ';
202 }
else if (align_spec[i]==align_lnum) {
204 if (table[i][j].length()==csizes[i]) {
207 if (table[i][j][0]>=
'0' && table[i][j][0]<=
'9') {
210 for(
size_t k=table[i][j].length();k<csizes[i]-1;k++) {
215 for(
size_t k=table[i][j].length();k<csizes[i];k++) {
220 if (i!=ncols-1) tmp+=
' ';
254 template<
class mat_t>
int matrix_out(std::ostream &os,
size_t nrows,
255 size_t ncols, mat_t &A) {
258 std::vector<std::vector<std::string> > stab(ncols);
259 std::vector<std::string> ctable(nrows);
260 std::vector<int> alig(ncols);
262 for(
size_t j=0;j<ncols;j++) {
264 for(
size_t i=0;i<nrows;i++) {
265 stab[j].push_back(
dtos(A(i,j),os));
268 co.
align(stab,ncols,nrows,ctable,alig);
269 for(
size_t i=0;i<nrows;i++) {
270 os << ctable[i] << std::endl;
294 template<
class mat_t>
int matrix_out(std::ostream &os, mat_t &A) {
296 size_t nrows=A.size1();
297 size_t ncols=A.size2();
300 std::vector<std::vector<std::string> > stab(ncols);
301 std::vector<std::string> ctable(nrows);
302 std::vector<int> alig(ncols);
304 for(
size_t j=0;j<ncols;j++) {
306 for(
size_t i=0;i<nrows;i++) {
307 stab[j].push_back(
dtos(A(i,j),os));
310 co.
align(stab,ncols,nrows,ctable,alig);
311 for(
size_t i=0;i<nrows;i++) {
312 os << ctable[i] << std::endl;
335 size_t ncols, mat_t &A) {
338 std::vector<std::vector<std::string> > stab(nrows);
339 std::vector<std::string> ctable(ncols);
340 std::vector<int> alig(nrows);
342 for(
size_t i=0;i<nrows;i++) {
344 for(
size_t j=0;j<ncols;j++) {
345 stab[i].push_back(
dtos(A(i,j),os));
348 co.
align(stab,nrows,ncols,ctable,alig);
349 for(
size_t i=0;i<ncols;i++) {
350 os << ctable[i] << std::endl;
376 size_t nrows=A.size1();
377 size_t ncols=A.size2();
380 std::vector<std::vector<std::string> > stab(nrows);
381 std::vector<std::string> ctable(ncols);
382 std::vector<int> alig(nrows);
384 for(
size_t i=0;i<nrows;i++) {
386 for(
size_t j=0;j<ncols;j++) {
387 stab[i].push_back(
dtos(A(i,j),os));
390 co.
align(stab,nrows,ncols,ctable,alig);
391 for(
size_t i=0;i<ncols;i++) {
392 os << ctable[i] << std::endl;
419 size_t ncols, mat_t &A) {
422 std::vector<std::string> *stab=
new std::vector<std::string>[ncols];
423 std::vector<std::string> ctable(nrows);
424 std::vector<int> alig(ncols);
426 for(
size_t j=0;j<ncols;j++) {
428 for(
size_t i=0;i<nrows;i++) {
429 stab[j].push_back(
dtos(A[i][j],os));
432 co.
align(stab,ncols,nrows,ctable,alig);
433 for(
size_t i=0;i<nrows;i++) {
434 os << ctable[i] << std::endl;
463 size_t ncols, mat_t &A) {
466 std::vector<std::string> *stab=
new std::vector<std::string>[nrows];
467 std::vector<std::string> ctable(ncols);
468 std::vector<int> alig(nrows);
470 for(
size_t i=0;i<nrows;i++) {
472 for(
size_t j=0;j<ncols;j++) {
473 stab[i].push_back(
dtos(A[i][j],os));
476 co.
align(stab,nrows,ncols,ctable,alig);
477 for(
size_t i=0;i<ncols;i++) {
478 os << ctable[i] << std::endl;
487 #ifndef DOXYGEN_NO_O2NS The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
int align(const mat_string_t &table, size_t ncols, size_t nrows, vec_string_t &ctable, vec_int_t &align_spec)
Take table and create a new object ctable with appropriately formatted columns.
Create nicely formatted columns from a table of strings.
static const int align_left
Align the left-hand sides.
int matrix_out(std::ostream &os, size_t nrows, size_t ncols, mat_t &A)
A operator for simple matrix output using operator()
static const int align_lnum
Align negative numbers to the left and use a space for positive numbers.
static const int align_right
Align the right-hand sides.
static const int align_rmid
Center, slightly to the right if spacing is uneven.
int matrix_trans_out(std::ostream &os, size_t nrows, size_t ncols, mat_t &A)
A operator for simple matrix output using operator()
std::string dtos(double x, int prec=6, bool auto_prec=false)
Convert a double to a string.
int array_2d_trans_out(std::ostream &os, size_t nrows, size_t ncols, mat_t &A)
A operator for simple matrix output using operator[].
int array_2d_out(std::ostream &os, size_t nrows, size_t ncols, mat_t &A)
A operator for simple matrix output using operator[].
static const int align_lmid
Center, slightly to the left if spacing is uneven.
static const int align_dp
Align with decimal points.