include
ecl
linear_algebra
eigen
eigen/formatters.hpp
Go to the documentation of this file.
1
8
/*****************************************************************************
9
** Ifdefs
10
*****************************************************************************/
11
12
#ifndef ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_
13
#define ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_
14
15
/*****************************************************************************
16
** Includes
17
*****************************************************************************/
18
19
#include <
ecl/exceptions/standard_exception.hpp
>
20
#include <
ecl/formatters.hpp
>
21
#include <
ecl/mpl/enable_if.hpp
>
22
#include <
ecl/type_traits/fundamental_types.hpp
>
23
24
/*****************************************************************************
25
** Namespaces
26
*****************************************************************************/
27
28
namespace
Eigen
{
29
30
/*****************************************************************************
31
** Interface [MatrixFormatter]
32
*****************************************************************************/
33
40
template
<
typename
Derived,
typename
Scalar,
typename
Enable =
void
>
41
class
MatrixFormatter {
42
private
:
43
MatrixFormatter
() {}
44
};
45
46
/*****************************************************************************
47
** Interface [FloatMatrixFormatter]
48
*****************************************************************************/
49
60
template
<
typename
Derived>
61
class
FloatMatrixFormatter
{
62
public
:
63
/******************************************
64
** C&D's
65
*******************************************/
73
FloatMatrixFormatter
(
const
int
&w = -1,
const
unsigned
int
&p = 2) :
74
tmp_formatting
(
false
),
75
ready_to_format
(
false
),
76
_matrix
(NULL)
77
{
78
format
.width(w);
79
format
.precision(p);
80
}
81
virtual
~FloatMatrixFormatter
() {}
82
83
/******************************************
84
** Configuration
85
*******************************************/
94
FloatMatrixFormatter<Derived>
&
precision
(
const
unsigned
int
&p ) {
95
format
.precision(p);
96
return
*
this
;
97
}
98
107
FloatMatrixFormatter<Derived>&
width
(
const
int
&w ) {
108
format
.width(w);
109
return
*
this
;
110
}
111
116
unsigned
int
precision
() {
return
format
.precision(); }
117
122
int
width
() {
return
format
.width(); }
123
124
/******************************************
125
** Format a value
126
*******************************************/
140
FloatMatrixFormatter< Derived >
&
operator()
(
const
Derived & matrix ) {
141
_matrix
= &matrix;
142
ready_to_format
=
true
;
143
return
(*
this
);
144
}
162
FloatMatrixFormatter< Derived >
&
operator()
(
const
Derived & matrix,
const
int
&w,
const
unsigned
int
&p) {
163
_matrix
= &matrix;
164
tmp_precision
= p;
165
tmp_width
= w;
166
tmp_formatting
=
true
;
167
ready_to_format
=
true
;
168
return
(*
this
);
169
}
170
184
template
<
typename
OutputStream,
typename
Derived_ >
185
friend
OutputStream&
operator <<
( OutputStream& ostream,
FloatMatrixFormatter<Derived_>
& formatter );
186
187
private
:
188
ecl::Format<typename Derived::Scalar>
format
;
189
int
tmp_width
;
190
unsigned
int
tmp_precision
;
191
bool
tmp_formatting
;
192
bool
ready_to_format
;
193
const
Derived *
_matrix
;
194
};
195
196
template
<
typename
OutputStream,
typename
Derived_ >
197
OutputStream&
operator <<
(OutputStream& ostream,
FloatMatrixFormatter< Derived_ >
& formatter )
198
{
199
ecl_assert_throw
( formatter.
_matrix
,
ecl::StandardException
(LOC,
ecl::UsageError
,
"The formatter cannot print any data - "
200
"_matrix was not initialised "
201
"please pass the your matrix through () operator"
) );
202
ecl_assert_throw
(formatter.
ready_to_format
,
ecl::StandardException
(LOC,
ecl::UsageError
,
"The formatter cannot print any data - "
203
"either there is no data available, or you have tried to use the "
204
"formatter more than once in a single streaming operation. "
205
"C++ produces unspecified results when functors are used multiply "
206
"in the same stream sequence, so this is not permitted here."
) );
207
208
209
if
( formatter.
ready_to_format
) {
210
unsigned
int
prm_precision = formatter.
format
.precision();;
211
int
prm_width = formatter.
format
.width();
212
if
( formatter.
tmp_formatting
) {
213
formatter.
format
.precision(formatter.
tmp_precision
);
214
formatter.
format
.width(formatter.
tmp_width
);
215
}
216
217
/*********************
218
** Stream Matrix
219
**********************/
220
// write matrix data
221
// @note norma matrix allows matrix(i,j) however sparse matrix does not.
222
// So i put coeff(i,j) function which can be used for accessing each element for both of sparse and dense.
223
int
rows = formatter.
_matrix
->rows();
224
int
cols = formatter.
_matrix
->cols();
225
for
(
int
i=0; i<rows; i++ )
226
{
227
for
(
int
j=0; j<cols; j++ )
228
{
229
ostream << formatter.
format
(formatter.
_matrix
->coeff( i, j )) <<
" "
;
230
}
231
if
( rows != 1 ) {
// special condition for row vectors so we can do inline the formatting.
232
ostream <<
"\n"
;
233
}
234
}
235
236
if
( formatter.
tmp_formatting
) {
237
formatter.
format
.precision(prm_precision);
238
formatter.
format
.width(prm_width);
239
formatter.
tmp_formatting
=
false
;
240
}
241
formatter.
ready_to_format
=
false
;
242
}
243
return
ostream;
244
}
245
246
/*****************************************************************************
247
** Interface [MatrixFormatter][Float Types]
248
*****************************************************************************/
249
250
template
<
typename
Derived,
typename
Scalar>
251
class
MatrixFormatter<Derived, Scalar, typename
ecl
::enable_if< ecl::is_float<Scalar> >::type> :
public
FloatMatrixFormatter<Derived> {
252
public
:
253
MatrixFormatter
(
const
int
&w = -1,
const
unsigned
int
&p = 2) : FloatMatrixFormatter<Derived>(w, p) {};
254
};
255
256
}
// namespace Eigen
257
258
#endif
/* ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_ */
Eigen::FloatMatrixFormatter::operator<<
friend OutputStream & operator<<(OutputStream &ostream, FloatMatrixFormatter< Derived_ > &formatter)
Stream the formatter.
Definition:
eigen/formatters.hpp:203
Eigen
ecl::UsageError
UsageError
Eigen::FloatMatrixFormatter::format
ecl::Format< typename Derived::Scalar > format
Definition:
eigen/formatters.hpp:196
formatters.hpp
Eigen::FloatMatrixFormatter::operator()
FloatMatrixFormatter< Derived > & operator()(const Derived &matrix)
Format a matrix with permanently stored precision/width.
Definition:
eigen/formatters.hpp:148
Eigen::FloatMatrixFormatter::tmp_formatting
bool tmp_formatting
Definition:
eigen/formatters.hpp:199
Eigen::FloatMatrixFormatter::FloatMatrixFormatter
FloatMatrixFormatter(const int &w=-1, const unsigned int &p=2)
Default constructor.
Definition:
eigen/formatters.hpp:81
Eigen::FloatMatrixFormatter::tmp_precision
unsigned int tmp_precision
Definition:
eigen/formatters.hpp:198
Eigen::MatrixFormatter::MatrixFormatter
MatrixFormatter()
Definition:
eigen/formatters.hpp:57
Eigen::FloatMatrixFormatter::ready_to_format
bool ready_to_format
Definition:
eigen/formatters.hpp:200
Eigen::operator<<
OutputStream & operator<<(OutputStream &ostream, FloatMatrixFormatter< Derived_ > &formatter)
Definition:
eigen/formatters.hpp:203
Eigen::FloatMatrixFormatter::~FloatMatrixFormatter
virtual ~FloatMatrixFormatter()
Definition:
eigen/formatters.hpp:89
Eigen::FloatMatrixFormatter::tmp_width
int tmp_width
Definition:
eigen/formatters.hpp:197
ecl::StandardException
Eigen::FloatMatrixFormatter::precision
unsigned int precision()
Returns the current precision setting.
Definition:
eigen/formatters.hpp:124
enable_if.hpp
fundamental_types.hpp
ecl::Format< typename Derived::Scalar >
ecl_assert_throw
#define ecl_assert_throw(expression, exception)
Eigen::FloatMatrixFormatter::_matrix
const Derived * _matrix
Definition:
eigen/formatters.hpp:201
Eigen::FloatMatrixFormatter
Formatter for Eigen matrix type.
Definition:
eigen/formatters.hpp:67
standard_exception.hpp
Eigen::FloatMatrixFormatter::width
int width()
Returns the current width setting.
Definition:
eigen/formatters.hpp:130
ecl
ecl_linear_algebra
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:29