vnl_matlab_print.hxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_matlab_print.hxx
2 // It is different from vnl_matlab_print.cxx
3 #ifndef vnl_matlab_print_hxx_
4 #define vnl_matlab_print_hxx_
5 // \author fsm
6 // Adapted from awf's MatOps class.
7 
8 #include <iostream>
9 #include "vnl_matlab_print.h"
10 
11 #ifdef _MSC_VER
12 # include <vcl_msvc_warnings.h>
13 #endif
14 
15 #include <vnl/vnl_vector.h>
16 #include <vnl/vnl_matrix.h>
17 #include <vnl/vnl_vector_fixed.h>
18 #include <vnl/vnl_matrix_fixed.h>
19 #include <vnl/vnl_matrix_ref.h>
20 #include <vnl/vnl_diag_matrix.h>
22 
23 //--------------------------------------------------------------------------------
24 
25 template <class T>
26 std::ostream &vnl_matlab_print(std::ostream& s,
27  T const* array,
28  unsigned length,
30 {
31  char buf[1024];
32  for (unsigned j=0; j<length; j++ ) {
33  // Format according to selected style
34  // In both cases an exact 0 goes out as such
35  vnl_matlab_print_scalar(array[j], buf, format);
36  s << buf;
37  }
38 
39  return s;
40 }
41 
42 template <class T>
43 std::ostream &vnl_matlab_print(std::ostream &s,
44  T const * const *array,
45  unsigned rows, unsigned cols,
47 {
48  for (unsigned i=0; i<rows; ++i)
49  vnl_matlab_print(s, array[i], cols, format) << '\n';
50  return s;
51 }
52 
53 template <class T>
54 std::ostream& vnl_matlab_print(std::ostream& s,
55  vnl_diag_matrix<T> const& D,
56  char const* variable_name,
58 {
59  if (variable_name)
60  s << variable_name << " = diag([ ";
61 
62  vnl_matlab_print(s, D.begin(), D.size(), format);
63 
64  if (variable_name)
65  s << " ])\n";
66 
67  return s;
68 }
69 
70 template <class T>
71 std::ostream& vnl_matlab_print(std::ostream& s,
72  vnl_matrix<T> const& M,
73  char const* variable_name,
75 {
76  if (variable_name)
77  s << variable_name << " = [ ...\n";
78 
79  if (variable_name && M.rows() == 0)
80  return s << "];\n";
81 
82  for (unsigned int i=0; i<M.rows(); i++ ) {
83  vnl_matlab_print(s, M[i], M.cols(), format);
84 
85  if (variable_name && (i == M.rows()-1))
86  s << " ]";
87 
88  s << '\n';
89  }
90 
91  return s;
92 }
93 
94 template <class T>
95 std::ostream& vnl_matlab_print(std::ostream& s,
96  vnl_vector<T> const & v,
97  char const* variable_name,
99 {
100  if (variable_name)
101  s << variable_name << " = [ ";
102 
103  vnl_matlab_print(s, v.begin(), v.size(), format);
104 
105  if (variable_name)
106  s << " ]\n";
107 
108  return s;
109 }
110 
111 template <class T, unsigned int n, unsigned int m>
112 std::ostream& vnl_matlab_print(std::ostream& s,
113  vnl_matrix_fixed<T,n,m> const& M,
114  char const* variable_name,
116 {
117  if (variable_name)
118  s << variable_name << " = [ ...\n";
119 
120  if (variable_name && M.rows() == 0)
121  return s << "];\n";
122 
123  for (unsigned int i=0; i<n; ++i ) {
124  vnl_matlab_print(s, M[i], m, format);
125 
126  if (variable_name && (i == n-1))
127  s << " ]";
128 
129  s << '\n';
130  }
131 
132  return s;
133 }
134 
135 template <class T>
136 std::ostream& vnl_matlab_print(std::ostream& s,
137  vnl_matrix_ref<T> const& M,
138  char const* variable_name,
140 {
141  if (variable_name)
142  s << variable_name << " = [ ...\n";
143 
144  if (variable_name && M.rows() == 0)
145  return s << "];\n";
146 
147  for (unsigned int i=0; i<M.rows(); ++i )
148  {
149  vnl_matlab_print(s, M[i], M.cols(), format);
150 
151  if (variable_name && (i == M.rows()-1))
152  s << " ]";
153 
154  s << '\n';
155  }
156 
157  return s;
158 }
159 
160 template <class T, unsigned int n>
161 std::ostream& vnl_matlab_print(std::ostream& s,
162  vnl_vector_fixed<T,n> const & v,
163  char const* variable_name,
165 {
166  if (variable_name)
167  s << variable_name << " = [ ";
168 
169  vnl_matlab_print(s, v.begin(), n, format);
170 
171  if (variable_name)
172  s << " ]\n";
173 
174  return s;
175 }
176 
177 //--------------------------------------------------------------------------------
178 
179 #undef VNL_MATLAB_PRINT_INSTANTIATE
180 #define VNL_MATLAB_PRINT_INSTANTIATE(T) \
181 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, T const*, unsigned, vnl_matlab_print_format); \
182 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, T const* const*, unsigned, unsigned, vnl_matlab_print_format); \
183 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_diag_matrix<T > const&, char const *, vnl_matlab_print_format); \
184 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix<T > const&, char const*, vnl_matlab_print_format); \
185 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector<T > const&, char const*, vnl_matlab_print_format); \
186 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_ref<T > const&, char const*, vnl_matlab_print_format); \
187 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,1,1> const&, char const*, vnl_matlab_print_format); \
188 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,2,2> const&, char const*, vnl_matlab_print_format); \
189 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,2,3> const&, char const*, vnl_matlab_print_format); \
190 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,3,2> const&, char const*, vnl_matlab_print_format); \
191 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,3,3> const&, char const*, vnl_matlab_print_format); \
192 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,2,4> const&, char const*, vnl_matlab_print_format); \
193 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,3,4> const&, char const*, vnl_matlab_print_format); \
194 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,4,3> const&, char const*, vnl_matlab_print_format); \
195 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,4,4> const&, char const*, vnl_matlab_print_format); \
196 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,6,8> const&, char const*, vnl_matlab_print_format); \
197 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,5,5> const&, char const*, vnl_matlab_print_format); \
198 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,6,6> const&, char const*, vnl_matlab_print_format); \
199 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,7,7> const&, char const*, vnl_matlab_print_format); \
200 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,8,8> const&, char const*, vnl_matlab_print_format); \
201 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,9,9> const&, char const*, vnl_matlab_print_format); \
202 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_matrix_fixed<T,10,10> const&, char const*, vnl_matlab_print_format); \
203 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector_fixed<T,1> const&, char const*, vnl_matlab_print_format); \
204 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector_fixed<T,2> const&, char const*, vnl_matlab_print_format); \
205 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector_fixed<T,3> const&, char const*, vnl_matlab_print_format); \
206 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector_fixed<T,4> const&, char const*, vnl_matlab_print_format); \
207 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector_fixed<T,5> const&, char const*, vnl_matlab_print_format); \
208 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector_fixed<T,6> const&, char const*, vnl_matlab_print_format); \
209 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector_fixed<T,7> const&, char const*, vnl_matlab_print_format); \
210 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector_fixed<T,8> const&, char const*, vnl_matlab_print_format); \
211 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector_fixed<T,9> const&, char const*, vnl_matlab_print_format); \
212 template VNL_EXPORT std::ostream &vnl_matlab_print(std::ostream &, vnl_vector_fixed<T,10> const&, char const*, vnl_matlab_print_format)
213 
214 #endif // vnl_matlab_print_hxx_
unsigned int size() const
Return the total number of elements stored by the matrix.
unsigned int cols() const
Return the number of columns.
Definition: vnl_matrix.h:183
Print matrices and vectors in nice MATLAB format.
VNL_EXPORT std::ostream & vnl_matlab_print(std::ostream &, vnl_diag_matrix< T > const &, char const *variable_name=nullptr, vnl_matlab_print_format=vnl_matlab_print_format_default)
print a vnl_diagonal_matrix<T>.
An ordinary mathematical matrix.
#define m
Definition: vnl_vector.h:43
Fixed size, stack-stored, space-efficient matrix.
Definition: vnl_fwd.h:23
unsigned int rows() const
Return the number of rows.
#define v
Definition: vnl_vector.h:42
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
void vnl_matlab_print_scalar(int v, char *buf, vnl_matlab_print_format)
vnl_matlab_print_format
pretty-printing matlab formats.
Mathematical vector class, templated by type of element.
Definition: vnl_fwd.h:16
Fixed length stack-stored, space-efficient vector.
Definition: vnl_fwd.h:22
fixed size matrix
Fixed length stack-stored vector.
stores a diagonal matrix as a single vector.
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
Contains class for diagonal matrices.
vnl_matrix reference to user-supplied storage.
Definition: vnl_fwd.h:20
vnl_matrix reference to user-supplied storage.