2 #ifndef vnl_matrix_fixed_ref_hxx_ 3 #define vnl_matrix_fixed_ref_hxx_ 13 # include <vcl_msvc_warnings.h> 22 template<
class T,
unsigned nrows,
unsigned ncols>
26 for (
unsigned int i = 0; i < nrows; i++)
27 for (
unsigned int j = 0; j < ncols; j++)
33 template<
class T,
unsigned nrows,
unsigned ncols>
37 for (
unsigned int i = 0; i < nrows && i < ncols; i++)
43 template<
class T,
unsigned nrows,
unsigned ncols>
47 assert(diag.
size() >= nrows || diag.
size() >= ncols);
51 for (
unsigned int i = 0; i < nrows && i < ncols; ++i)
52 (*
this)(i,i) = diag[i];
57 template<
class T,
unsigned nrows,
unsigned ncols>
61 for (
unsigned int i = 0; i < nrows; i++)
64 for (
unsigned int j = 1; j < ncols; j++)
65 os <<
' ' << (*
this)(i,j);
71 template <
class T,
unsigned nrows,
unsigned ncols>
80 template <
class T,
unsigned nrows,
unsigned ncols>
92 template<
class T,
unsigned nrows,
unsigned ncols>
97 for (
unsigned int i = 0; i < cols(); i++)
98 for (
unsigned int j = 0; j < rows(); j++)
99 result(i,j) = (*this)(j,i);
103 template<
class T,
unsigned nrows,
unsigned ncols>
114 template<
class T,
unsigned nrows,
unsigned ncols>
117 unsigned top,
unsigned left)
const 119 const unsigned int bottom = top +
m.rows();
120 const unsigned int right = left +
m.cols();
122 if (nrows < bottom || ncols < right)
124 bottom, right,
m.rows(),
m.cols());
126 for (
unsigned int i = top; i < bottom; i++)
127 for (
unsigned int j = left; j < right; j++)
128 (*
this)(i,j) =
m(i-top,j-left);
133 template<
class T,
unsigned nrows,
unsigned ncols>
136 unsigned top,
unsigned left)
const 139 unsigned int bottom = top + rowz;
140 unsigned int right = left + colz;
141 if ((nrows < bottom) || (ncols < right))
143 nrows, ncols, bottom, right);
146 for (
unsigned int i = 0; i < rowz; i++)
147 for (
unsigned int j = 0; j < colz; j++)
148 result(i,j) = (*this)(top+i,left+j);
153 template<
class T,
unsigned nrows,
unsigned ncols>
157 T* dp = this->data_block();
158 unsigned int i = nrows * ncols;
164 template<
class T,
unsigned nrows,
unsigned ncols>
167 T
const* dp = this->data_block();
168 unsigned int i = nrows*ncols;
173 template<
class T,
unsigned nrows,
unsigned ncols>
179 for (
unsigned int i = 0; i < nrows; ++i)
180 for (
unsigned int j = 0; j < ncols; ++j)
182 for (
unsigned int i = 0; i < nrows && i < ncols; ++i)
189 template<
class T,
unsigned nrows,
unsigned ncols>
194 for (
unsigned int i = 0; i < nrows; i++)
197 for (
unsigned int j = 0; j < ncols; j++)
203 real_t scale = real_t(1)/std::sqrt((real_t)norm);
204 for (
unsigned int j = 0; j < ncols; j++)
208 (*this)(i,j) *= (T)(scale);
215 template<
class T,
unsigned nrows,
unsigned ncols>
220 for (
unsigned int j = 0; j < ncols; j++) {
222 for (
unsigned int i = 0; i < nrows; i++)
228 real_t scale = real_t(1)/std::sqrt((real_t)norm);
229 for (
unsigned int i = 0; i < nrows; i++)
233 (*this)(i,j) *= (T)(scale);
240 template<
class T,
unsigned nrows,
unsigned ncols>
245 if (row_index >= nrows)
248 for (
unsigned int j = 0; j < ncols; j++)
249 (*
this)(row_index,j) *= value;
253 template<
class T,
unsigned nrows,
unsigned ncols>
258 if (column_index >= ncols)
261 for (
unsigned int j = 0; j < nrows; j++)
262 (*
this)(j,column_index) *= value;
267 template<
class T,
unsigned nrows,
unsigned ncols>
280 template<
class T,
unsigned nrows,
unsigned ncols>
285 if (column + n > ncols)
290 for (
unsigned int c = 0; c < n; ++c)
291 for (
unsigned int r = 0; r < nrows; ++r)
292 result(r, c) = (*this)(r,column + c);
297 template<
class T,
unsigned nrows,
unsigned ncols>
301 for (
unsigned int j = 0; j < nrows && j < ncols; ++j)
308 template<
class T,
unsigned nrows,
unsigned ncols>
312 for (
unsigned int j = 0; j < ncols; j++)
313 (*
this)(row_index,j) =
v[j];
317 template<
class T,
unsigned nrows,
unsigned ncols>
321 set_row(row_index,
v.data_block());
325 template<
class T,
unsigned nrows,
unsigned ncols>
329 set_row(row_index,
v.data_block());
333 template<
class T,
unsigned nrows,
unsigned ncols>
337 for (
unsigned int j = 0; j < ncols; j++)
338 (*
this)(row_index,j) =
v;
344 template<
class T,
unsigned nrows,
unsigned ncols>
348 for (
unsigned int i = 0; i < nrows; i++)
349 (*
this)(i,column_index) =
v[i];
353 template<
class T,
unsigned nrows,
unsigned ncols>
357 set_column(column_index,
v.data_block());
361 template<
class T,
unsigned nrows,
unsigned ncols>
365 set_column(column_index,
v.data_block());
369 template<
class T,
unsigned nrows,
unsigned ncols>
373 for (
unsigned int j = 0; j < nrows; j++)
374 (*
this)(j,column_index) =
v;
379 template<
class T,
unsigned nrows,
unsigned ncols>
384 if (nrows !=
m.rows() ||
385 ncols <
m.cols() + starting_column)
391 for (
unsigned int j = 0; j <
m.cols(); ++j)
392 for (
unsigned int i = 0; i < nrows; i++)
393 (*
this)(i,starting_column + j) =
m(i,j);
398 template <
class T,
unsigned nrows,
unsigned ncols>
404 for (
unsigned int i = 0; i < nrows; ++i)
405 for (
unsigned int j = 0; j < ncols; ++j)
408 if ( !((i == j) ? (xm == one) : (xm == zero)) )
415 template <
class T,
unsigned nrows,
unsigned ncols>
420 for (
unsigned int i = 0; i < nrows; ++i)
421 for (
unsigned int j = 0; j < ncols; ++j)
431 template <
class T,
unsigned nrows,
unsigned ncols>
436 for (
unsigned int i = 0; i < nrows; ++i)
437 for (
unsigned int j = 0; j < ncols; ++j)
438 if ( !( (*
this)(i, j) == zero) )
444 template <
class T,
unsigned nrows,
unsigned ncols>
448 for (
unsigned int i = 0; i < nrows; ++i)
449 for (
unsigned int j = 0; j < ncols; ++j)
456 template <
class T,
unsigned nrows,
unsigned ncols>
460 for (
unsigned int i = 0; i < nrows; ++i)
461 for (
unsigned int j = 0; j < ncols; ++j)
468 template <
class T,
unsigned nrows,
unsigned ncols>
472 for (
unsigned int i = 0; i < nrows; ++i)
473 for (
unsigned int j = 0; j < ncols; ++j)
481 template <
class T,
unsigned nrows,
unsigned ncols>
488 std::cerr <<
"\n\n" << __FILE__
" : " << __LINE__ <<
": matrix has non-finite elements\n";
490 if (rows() <= 20 && cols() <= 20)
491 std::cerr << __FILE__
": here it is:\n" << *
this <<
'\n';
494 std::cerr << __FILE__
": it is quite big (" << rows() <<
'x' << cols() <<
")\n" 495 << __FILE__
": in the following picture '-' means finite and '*' means non-finite:\n";
497 for (
unsigned int i=0; i<rows(); ++i)
499 for (
unsigned int j=0; j<cols(); ++j)
504 std::cerr << __FILE__
": calling abort()\n";
509 template <
class T,
unsigned nrows,
unsigned ncols>
513 if (nrows!=rs || ncols!=cs)
515 std::cerr << __FILE__
": size is " << nrows <<
'x' << ncols
516 <<
". should be " << rs <<
'x' << cs << std::endl;
521 template <
class T,
unsigned nrows,
unsigned ncols>
527 std::cerr << __FILE__
": vnl_matrix_fixed_ref_const<T,nrows,ncols>::read_ascii: Called with bad stream\n";
531 for (
unsigned int i = 0; i < nrows; ++i)
532 for (
unsigned int j = 0; j < ncols; ++j)
535 return s.good() || s.eof();
539 template <
class T,
unsigned nrows,
unsigned ncols>
543 for (
unsigned int r1 = 0; 2*r1+1 < nrows; ++r1)
545 const unsigned int r2 = nrows - 1 - r1;
546 for (
unsigned int c = 0; c < ncols; ++c)
548 const T tmp = (*this)(r1, c);
549 (*this)(r1, c) = (*
this)(r2, c);
550 (*this)(r2, c) = tmp;
557 template <
class T,
unsigned nrows,
unsigned ncols>
561 for (
unsigned int c1 = 0; 2*c1+1 < ncols; ++c1)
563 const unsigned int c2 = ncols - 1 - c1;
564 for (
unsigned int r = 0; r < nrows; ++r)
566 const T tmp = (*this)(r, c1);
567 (*this)(r, c1) = (*
this)(r, c2);
568 (*this)(r, c2) = tmp;
574 template <
class T,
unsigned nrows,
unsigned ncols>
579 for (
unsigned int j=0; j<ncols; ++j)
582 for (
unsigned int i=0; i<nrows; ++i)
590 template <
class T,
unsigned nrows,
unsigned ncols>
595 for (
unsigned int i=0; i<nrows; ++i)
598 for (
unsigned int j=0; j<ncols; ++j)
607 template <
class T,
unsigned nrows,
unsigned ncols>
611 assert(nrows==ncols);
612 for (
unsigned i = 0; i < nrows; ++i)
613 for (
unsigned j = i+1; j < ncols; ++j)
616 (*this)(i,j) = (*
this)(j,i);
623 #define VNL_MATRIX_FIXED_REF_INSTANTIATE(T,m,n) \ 624 template class VNL_EXPORT vnl_matrix_fixed_ref_const<T, m, n >; \ 625 template class VNL_EXPORT vnl_matrix_fixed_ref<T, m, n > 627 #endif // vnl_matrix_fixed_ref_hxx_
vnl_matrix_fixed< T, num_cols, num_rows > conjugate_transpose() const
Return conjugate transpose.
static void conjugate(T const *, T *, unsigned)
vnl_matrix_fixed_ref const & set_identity() const
Sets this matrix to an identity matrix, then returns "*this".
vnl_matrix_fixed_ref const & set_columns(unsigned starting_column, vnl_matrix< T > const &M) const
Set columns to those in M, starting at starting_column, then return *this.
vnl_vector< T > get_diagonal() const
Return a vector with the content of the (main) diagonal.
T const * data_block() const
Access the contiguous block storing the elements in the matrix row-wise. O(1).
bool read_ascii(std::istream &s) const
vnl_matrix_fixed_ref const & scale_row(unsigned row, T value) const
Scales elements in given row by a factor T, and returns "*this".
vnl_matrix_fixed< T, num_rows, num_cols > apply(T(*f)(T)) const
Make a new matrix by applying function to each element.
size_t size() const
Return the length, number of elements, dimension of this vector.
vnl_matrix_fixed_ref & flipud()
Reverses the order of rows, and returns "*this".
vnl_matrix< T > get_n_columns(unsigned colstart, unsigned n) const
Get n columns beginning at colstart.
Namespace with standard math functions.
bool is_finite() const
Return true if finite.
abs_t operator_inf_norm() const
void vnl_error_matrix_row_index(char const *fcn, int r)
Raise exception for invalid row index.
Fixed size, stack-stored, space-efficient matrix.
void vnl_error_matrix_col_index(char const *fcn, int c)
Raise exception for invalid col index.
abs_t operator_one_norm() const
vnl_matrix_fixed_ref const & normalize_columns() const
Normalizes each column so it is a unit vector, and returns "*this".
vnl_c_vector< T >::abs_t abs_t
Type def for norms.
vnl_matrix_fixed_ref const & update(vnl_matrix< T > const &, unsigned top=0, unsigned left=0) const
Set values of this matrix to those of M, starting at [top,left].
vnl_bignum squared_magnitude(vnl_bignum const &x)
static void apply(T const *, unsigned, T(*f)(T), T *v_out)
vnl_matrix_fixed_ref const & normalize_rows() const
Normalizes each row so it is a unit vector, and returns "*this".
bool isnan(vnl_bignum const &)
vnl_matrix_fixed_ref const & fill_diagonal(T) const
Sets all diagonal elements of matrix to specified value; returns "*this".
vnl_matrix_fixed_ref const & set_row(unsigned i, T const *v) const
Set the elements of the i'th row to v[i] (No bounds checking).
vnl_matrix< T > extract(unsigned rowz, unsigned colz, unsigned top=0, unsigned left=0) const
Extract a sub-matrix of size rows x cols, starting at (top,left).
bool is_zero() const
Return true if all elements equal to zero.
vnl_matrix_fixed_ref const & set_column(unsigned i, T const *v) const
Set the elements of the i'th column to v[i] (No bounds checking).
vnl_matrix_fixed_ref & fliplr()
Reverses the order of columns, and returns "*this".
vnl_matrix_fixed_ref const & copy_in(T const *) const
Fills (laminates) this matrix with the given data, then returns it.
An ordinary mathematical matrix.
bool is_identity() const
Return true if all elements equal to identity.
vnl_matrix_fixed_ref const & set_diagonal(vnl_vector< T > const &) const
Sets the diagonal elements of this matrix to the specified list of values.
bool has_nans() const
Return true if matrix contains NaNs.
bool isfinite(vnl_bignum const &x)
void assert_size_internal(unsigned, unsigned) const
Abort unless M has the given size.
vnl_matrix_fixed_ref const & scale_column(unsigned col, T value) const
Scales elements in given column by a factor T, and returns "*this".
Mathematical vector class, templated by type of element.
vnl_matrix_fixed_ref const & fill(T) const
Sets all elements of matrix to specified value, and returns "*this".
vnl_matrix_fixed_ref const & inplace_transpose() const
Fills the given array with this matrix.
Fixed length stack-stored, space-efficient vector.
void print(std::ostream &os) const
Print matrix to os in some hopefully sensible format.
Fixed size stack-stored vnl_matrix.
vnl_bignum abs(vnl_bignum const &x)
void vnl_error_matrix_dimension(char const *fcn, int r1, int c1, int r2, int c2)
Raise exception for invalid dimensions.
vnl_matrix< T > get_n_rows(unsigned rowstart, unsigned n) const
Get n rows beginning at rowstart.
void assert_finite_internal() const
Abort if any element of M is inf or nan.
iterator begin()
Iterator pointing to start of data.
unsigned int size() const
Return the total number of elements stored by the matrix.
vnl_matrix_fixed< T, num_cols, num_rows > transpose() const
Return transpose.