-
Notifications
You must be signed in to change notification settings - Fork 3
/
oFmt.h
155 lines (137 loc) · 3.72 KB
/
oFmt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#pragma once
#include <iostream>
#include <iomanip>
#include <locale>
using namespace std;
#include <cmath>
#include <iomanip>
#include <sstream>
#include <string>
std::string fixed_precision_string(double num) {
// Magic numbers
static const int prec_limit = 14; // Change to 15 if you wish
static const double log10_fuzz = 1e-15; // In case log10 is slightly off
static const char decimal_pt = '.'; // Better: use std::locale
if (num == 0.0) {
return "0";
}
std::string result;
if (num < 0.0) {
result = '-';
num = -num;
}
int ndigs = int(std::log10(num) + log10_fuzz);
std::stringstream ss;
if (ndigs >= prec_limit) {
ss << std::fixed
<< std::setprecision(0)
<< num;
result += ss.str();
}
else {
ss << std::fixed
<< std::setprecision(prec_limit - ndigs)
<< num;
result += ss.str();
auto last_non_zero = result.find_last_not_of('0');
if (result[last_non_zero] == decimal_pt) {
result.erase(last_non_zero);
}
else if (last_non_zero + 1 < result.length()) {
result.erase(last_non_zero + 1);
}
}
return result;
}
// from oFmt is from a discussion board.
// needs work. Still too much customization to create a well shaped report.
class oFmt
{
int myWidth;
int myPrecision;
public:
oFmt(int width, int precision)
: myWidth(width)
, myPrecision(precision)
{
}
friend std::ostream&
operator<<(std::ostream& dest, oFmt const& format)
{
dest.setf(std::ios_base::fixed, std::ios_base::floatfield);
dest.precision(format.myPrecision);
dest.internal;
dest.width(format.myWidth);
return dest;
}
};
template< typename T>
void print_2Dmatrix(T& matrix) {
int rows = sizeof matrix / sizeof matrix[0]; // 2 rows
int cols = sizeof matrix[0] / sizeof(double); // 5 cols
for (int i = 0; i < rows; ++i) {
cout << "\t\t";
for (int j = 0; j < cols; ++j) {
cout << oFmt(8, 4) << ((double*)matrix)[i*cols + j] << "\t";
}
cout << endl;
}
}
/**
* \prints with header, y axis labels and fills with data
* \param matrix is data, double[][]
* \param headers.
* \param labels is y axis labels
*
* Needs work. Too much customization to create a report. ie. oFmt(8,0), oFmt(10,6)
*/
void print_2Dmatrix2(double** matrix, std::vector<double>& headers, std::vector<double>&labels) {
int rows = labels.size();
int cols = headers.size();
// first header is 'p' correlation. This is not in headers.
std::cout << "\tp\t";
for (int i = 0; i < headers.size(); ++i) {
cout << oFmt(8, 0) << "X=" << (int)headers[i] << "\t";
}
cout << endl;
for (int i = 0; i < rows; ++i) {
cout << oFmt(10, 6) << labels[i] << "\t";
for (int j = 0; j < cols; ++j) {
cout << oFmt(10, 6) << matrix[i][j] << "\t";
}
cout << endl;
}
}
template< typename T>
void print_2Dmatrix(T& matrix, std::vector<double>& headers, std::vector<double>&labels) {
int rows = sizeof matrix / sizeof matrix[0];
int cols = sizeof matrix[0] / sizeof(double);
// first header is 'p' correlation. This is not in headers.
std::cout << "\tp\t";
for (int i = 0; i < headers.size(); ++i) {
cout << oFmt(8, 0) << "X=" << (int)headers[i] << "\t";
}
cout << endl;
for (int i = 0; i < rows; ++i) {
cout << oFmt(10, 6) << labels[i] << "\t";
for (int j = 0; j < cols; ++j) {
cout << oFmt(10, 6) << ((double*)matrix)[i*cols + j] << "\t";
}
cout << endl;
}
}
// prints a single row w/o headers or y axis labels
template< typename T>
void print_matrix_row(T* matrix, int row, int cols) {
for (int j = 0; j < cols; ++j) {
cout << FFmt(8, 4) << matrix[row*cols + j] << "\t";
}
cout << endl;
}
// prints a single column w/o headers or y axis labels
template< typename T>
void print_matrix_col(T* matrix, int rows, int col) {
for (int i = 0; i < rows; ++i) {
cout << FFmt(8, 4) << matrix[i*rows + col] << "\t" << endl;
}
}