2 #ifndef vnl_matrix_fixed_hxx_ 3 #define vnl_matrix_fixed_hxx_ 12 # include <vcl_msvc_warnings.h> 21 template<
class T,
unsigned num_rows,
unsigned num_cols>
25 fill(
v);
return *
this;
28 template<
class T,
unsigned num_rows,
unsigned num_cols>
32 assert(rhs.
rows() == num_rows && rhs.
columns() == num_cols);
33 std::memcpy(data_[0], rhs.
data_block(), num_rows*num_cols *
sizeof(T));
37 template<
class T,
unsigned nrows,
unsigned ncols>
41 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG) 45 return this->data_[r][c];
48 template<
class T,
unsigned nrows,
unsigned ncols>
52 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG) 56 return this->data_[r][c];
59 template<
class T,
unsigned nrows,
unsigned ncols>
63 unsigned int count = nrows*ncols;
65 *(r++) = *(a++) + *(b++);
69 template<
class T,
unsigned nrows,
unsigned ncols>
73 unsigned int count = nrows*ncols;
78 template<
class T,
unsigned nrows,
unsigned ncols>
82 unsigned int count = nrows*ncols;
84 *(r++) = *(a++) - *(b++);
87 template<
class T,
unsigned nrows,
unsigned ncols>
91 unsigned int count = nrows*ncols;
96 template<
class T,
unsigned nrows,
unsigned ncols>
100 unsigned int count = nrows*ncols;
105 template<
class T,
unsigned nrows,
unsigned ncols>
109 unsigned int count = nrows*ncols;
111 *(r++) = *(a++) * *(b++);
114 template<
class T,
unsigned nrows,
unsigned ncols>
118 unsigned int count = nrows*ncols;
123 template<
class T,
unsigned nrows,
unsigned ncols>
127 unsigned int count = nrows*ncols;
129 *(r++) = *(a++) / *(b++);
132 template<
class T,
unsigned nrows,
unsigned ncols>
136 unsigned int count = nrows*ncols;
141 template<
class T,
unsigned nrows,
unsigned ncols>
145 unsigned int count = nrows*ncols;
147 if ( *(a++) != *(b++) )
return false;
154 template<
class T,
unsigned nrows,
unsigned ncols>
158 for (
unsigned int i = 0; i < nrows; ++i)
159 for (
unsigned int j = 0; j < ncols; ++j)
160 this->data_[i][j] = value;
165 template<
class T,
unsigned nrows,
unsigned ncols>
169 for (
unsigned int i = 0; i < nrows && i < ncols; ++i)
170 this->data_[i][i] = value;
175 template<
class T,
unsigned nrows,
unsigned ncols>
179 assert(diag.
size() >= nrows || diag.
size() >= ncols);
183 for (
unsigned int i = 0; i < nrows && i < ncols; ++i)
184 this->data_[i][i] = diag[i];
189 template<
class T,
unsigned nrows,
unsigned ncols>
193 for (
unsigned int i = 0; i < nrows; ++i)
195 os << this->data_[i][0];
196 for (
unsigned int j = 1; j < ncols; ++j)
197 os <<
' ' << this->data_[i][j];
203 template <
class T,
unsigned nrows,
unsigned ncols>
213 template <
class T,
unsigned nrows,
unsigned ncols>
224 template <
class T,
unsigned nrows,
unsigned ncols>
230 for (
unsigned int i = 0; i < nrows; ++i)
231 v.put(i,f(this->get_row(i)));
236 template <
class T,
unsigned nrows,
unsigned ncols>
242 for (
unsigned int i = 0; i < ncols; ++i)
243 v.put(i,f(this->get_column(i)));
250 template<
class T,
unsigned nrows,
unsigned ncols>
255 for (
unsigned int i = 0; i < cols(); ++i)
256 for (
unsigned int j = 0; j < rows(); ++j)
257 result(i,j) = this->data_[j][i];
261 template<
class T,
unsigned nrows,
unsigned ncols>
272 template<
class T,
unsigned nrows,
unsigned ncols>
275 unsigned top,
unsigned left)
277 const unsigned int bottom = top +
m.rows();
278 const unsigned int right = left +
m.cols();
280 if (nrows < bottom || ncols < right)
282 bottom, right,
m.rows(),
m.cols());
284 for (
unsigned int i = top; i < bottom; ++i)
285 for (
unsigned int j = left; j < right; ++j)
286 this->data_[i][j] =
m(i-top,j-left);
291 template<
class T,
unsigned nrows,
unsigned ncols>
294 unsigned top,
unsigned left)
const 297 this->extract( result, top, left );
302 template<
class T,
unsigned nrows,
unsigned ncols>
305 unsigned top,
unsigned left)
const 307 unsigned int rowz = sub_matrix.
rows();
308 unsigned int colz = sub_matrix.
cols();
310 unsigned int bottom = top + rowz;
311 unsigned int right = left + colz;
312 if ((nrows < bottom) || (ncols < right))
314 nrows, ncols, bottom, right);
316 for (
unsigned int i = 0; i < rowz; ++i)
317 for (
unsigned int j = 0; j < colz; ++j)
318 sub_matrix(i,j) = this->data_[top+i][left+j];
322 template<
class T,
unsigned nrows,
unsigned ncols>
326 T* dp = this->data_block();
327 std::copy( p, p + nrows * ncols, dp );
331 template<
class T,
unsigned nrows,
unsigned ncols>
334 T
const* dp = this->data_block();
335 std::copy( dp, dp + nrows * ncols, p );
338 template<
class T,
unsigned nrows,
unsigned ncols>
344 for (
unsigned int i = 0; i < nrows; ++i)
345 for (
unsigned int j = 0; j < ncols; ++j)
346 this->data_[i][j] = T(0);
347 for (
unsigned int i = 0; i < nrows && i < ncols; ++i)
348 this->data_[i][i] = T(1);
354 template<
class T,
unsigned nrows,
unsigned ncols>
358 for (
unsigned int i = 0; i < nrows; ++i)
361 for (
unsigned int j = 0; j < ncols; ++j)
367 real_t scale = real_t(1)/std::sqrt((real_t)norm);
368 for (
unsigned int j = 0; j < ncols; ++j)
372 this->data_[i][j] *= T(scale);
379 template<
class T,
unsigned nrows,
unsigned ncols>
383 for (
unsigned int j = 0; j < ncols; ++j) {
385 for (
unsigned int i = 0; i < nrows; ++i)
391 real_t scale = real_t(1)/std::sqrt((real_t)norm);
392 for (
unsigned int i = 0; i < nrows; ++i)
396 this->data_[i][j] *= T(scale);
403 template<
class T,
unsigned nrows,
unsigned ncols>
408 if (row_index >= nrows)
411 for (
unsigned int j = 0; j < ncols; ++j)
412 this->data_[row_index][j] *= value;
416 template<
class T,
unsigned nrows,
unsigned ncols>
421 if (column_index >= ncols)
424 for (
unsigned int j = 0; j < nrows; ++j)
425 this->data_[j][column_index] *= value;
429 template <
class T,
unsigned int nrows,
unsigned int ncols>
434 for (
unsigned int r = 0; r < nrows; ++r)
436 for (
unsigned int c = 0; c < ncols; ++c)
444 template<
class T,
unsigned nrows,
unsigned ncols>
457 template<
class T,
unsigned nrows,
unsigned ncols>
462 if (column + n > ncols)
467 for (
unsigned int c = 0; c < n; ++c)
468 for (
unsigned int r = 0; r < nrows; ++r)
469 result(r, c) = this->data_[r][column + c];
474 template<
class T,
unsigned nrows,
unsigned ncols>
477 #ifdef ERROR_CHECKING 478 if (row_index >= nrows)
483 for (
unsigned int j = 0; j < ncols; ++j)
484 v[j] = this->data_[row_index][j];
489 template<
class T,
unsigned nrows,
unsigned ncols>
492 #ifdef ERROR_CHECKING 493 if (column_index >= ncols)
498 for (
unsigned int j = 0; j < nrows; ++j)
499 v[j] = this->data_[j][column_index];
504 template <
class T,
unsigned int nrows,
unsigned int ncols>
510 for (
unsigned int j = 0; j < i.
size(); ++j)
511 m.set_row(j, this->get_row(i.
get(j)));
516 template <
class T,
unsigned int nrows,
unsigned int ncols>
522 for (
unsigned int j = 0; j < i.
size(); ++j)
523 m.set_column(j, this->get_column(i.
get(j)));
528 template<
class T,
unsigned nrows,
unsigned ncols>
532 for (
unsigned int j = 0; j < nrows && j < ncols; ++j)
533 v[j] = this->data_[j][j];
538 template <
class T,
unsigned nrows,
unsigned ncols>
542 v.copy_in(this->data_block());
547 template <
class T,
unsigned nrows,
unsigned ncols>
551 for (
unsigned int c = 0; c < ncols; ++c)
552 for (
unsigned int r = 0; r < nrows; ++r)
553 v[c*nrows+r] = this->data_[r][c];
559 template<
class T,
unsigned nrows,
unsigned ncols>
563 for (
unsigned int j = 0; j < ncols; ++j)
564 this->data_[row_index][j] =
v[j];
568 template<
class T,
unsigned nrows,
unsigned ncols>
572 if (
v.size() >= ncols)
573 set_row(row_index,
v.data_block());
575 for (
unsigned int j = 0; j <
v.size(); ++j)
576 this->data_[row_index][j] =
v[j];
580 template<
class T,
unsigned nrows,
unsigned ncols>
584 set_row(row_index,
v.data_block());
588 template<
class T,
unsigned nrows,
unsigned ncols>
592 for (
unsigned int j = 0; j < ncols; ++j)
593 this->data_[row_index][j] =
v;
599 template<
class T,
unsigned nrows,
unsigned ncols>
603 for (
unsigned int i = 0; i < nrows; ++i)
604 this->data_[i][column_index] =
v[i];
608 template<
class T,
unsigned nrows,
unsigned ncols>
612 if (
v.size() >= nrows)
613 set_column(column_index,
v.data_block());
615 for (
unsigned int i = 0; i <
v.size(); ++i)
616 this->data_[i][column_index] =
v[i];
620 template<
class T,
unsigned nrows,
unsigned ncols>
624 set_column(column_index,
v.data_block());
628 template<
class T,
unsigned nrows,
unsigned ncols>
632 for (
unsigned int j = 0; j < nrows; ++j)
633 this->data_[j][column_index] =
v;
638 template<
class T,
unsigned nrows,
unsigned ncols>
642 for (
unsigned int j = 0; j <
m.cols() && starting_column+j < ncols; ++j)
643 for (
unsigned int i = 0; i < nrows && i <
m.rows(); ++i)
644 this->data_[i][starting_column + j] =
m(i,j);
649 template <
class T,
unsigned nrows,
unsigned ncols>
655 for (
unsigned int i = 0; i < nrows; ++i)
656 for (
unsigned int j = 0; j < ncols; ++j)
658 T xm = this->data_[i][j];
659 if ( !((i == j) ? (xm == one) : (xm == zero)) )
666 template <
class T,
unsigned nrows,
unsigned ncols>
671 for (
unsigned int i = 0; i < nrows; ++i)
672 for (
unsigned int j = 0; j < ncols; ++j)
674 T xm = this->data_[i][j];
682 template <
class T,
unsigned nrows,
unsigned ncols>
687 for (
unsigned int i = 0; i < nrows; ++i)
688 for (
unsigned int j = 0; j < ncols; ++j)
689 if ( !( this->data_[i][ j] == zero) )
695 template <
class T,
unsigned nrows,
unsigned ncols>
702 if (this->rows() != rhs.
rows() || this->cols() != rhs.
cols())
705 for (
unsigned int i = 0; i < nrows; ++i)
706 for (
unsigned int j = 0; j < ncols; ++j)
713 template <
class T,
unsigned nrows,
unsigned ncols>
717 for (
unsigned int i = 0; i < nrows; ++i)
718 for (
unsigned int j = 0; j < ncols; ++j)
725 template <
class T,
unsigned nrows,
unsigned ncols>
729 for (
unsigned int i = 0; i < nrows; ++i)
730 for (
unsigned int j = 0; j < ncols; ++j)
737 template <
class T,
unsigned nrows,
unsigned ncols>
741 for (
unsigned int i = 0; i < nrows; ++i)
742 for (
unsigned int j = 0; j < ncols; ++j)
750 template <
class T,
unsigned nrows,
unsigned ncols>
757 std::cerr <<
"\n\n" __FILE__
": " << __LINE__ <<
": matrix has non-finite elements\n";
759 if (rows() <= 20 && cols() <= 20)
760 std::cerr << __FILE__
": here it is:\n" << *
this <<
'\n';
763 std::cerr << __FILE__
": it is quite big (" << rows() <<
'x' << cols() <<
")\n" 764 << __FILE__
": in the following picture '-' means finite and '*' means non-finite:\n";
766 for (
unsigned int i=0; i<rows(); ++i)
768 for (
unsigned int j=0; j<cols(); ++j)
773 std::cerr << __FILE__
": calling abort()\n";
778 template <
class T,
unsigned nrows,
unsigned ncols>
782 if (nrows!=rs || ncols!=cs)
784 std::cerr << __FILE__
": size is " << nrows <<
'x' << ncols
785 <<
". should be " << rs <<
'x' << cs << std::endl;
790 template <
class T,
unsigned nrows,
unsigned ncols>
796 std::cerr << __FILE__
": vnl_matrix_fixed<T,nrows,ncols>::read_ascii: Called with bad stream\n";
800 for (
unsigned int i = 0; i < nrows; ++i)
801 for (
unsigned int j = 0; j < ncols; ++j)
802 s >> this->data_[i][j];
804 return s.good() || s.eof();
808 template <
class T,
unsigned nrows,
unsigned ncols>
812 for (
unsigned int r1 = 0; 2*r1+1 < nrows; ++r1)
814 const unsigned int r2 = nrows - 1 - r1;
815 for (
unsigned int c = 0; c < ncols; ++c)
817 std::swap(this->data_[r1][c], this->data_[r2][c]);
824 template <
class T,
unsigned nrows,
unsigned ncols>
828 for (
unsigned int c1 = 0; 2*c1+1 < ncols; ++c1)
830 const unsigned int c2 = ncols - 1 - c1;
831 for (
unsigned int r = 0; r < nrows; ++r)
833 std::swap(this->data_[r][c1], this->data_[r][c2]);
839 template <
class T,
unsigned nrows,
unsigned ncols>
844 for (
unsigned int j=0; j<ncols; ++j)
847 for (
unsigned int i=0; i<nrows; ++i)
855 template <
class T,
unsigned nrows,
unsigned ncols>
860 for (
unsigned int i=0; i<nrows; ++i)
863 for (
unsigned int j=0; j<ncols; ++j)
872 template <
class T,
unsigned nrows,
unsigned ncols>
876 assert(nrows==ncols);
877 for (
unsigned i = 0; i < nrows; ++i)
878 for (
unsigned j = i+1; j < ncols; ++j)
880 T t = this->data_[i][j];
881 this->data_[i][j] = this->data_[j][i];
882 this->data_[j][i] = t;
887 template <
class T,
unsigned m,
unsigned n>
894 template <
class T,
unsigned m,
unsigned n>
901 template <
class T,
unsigned m,
unsigned n>
906 for (
unsigned int i = 0; i <
m; ++i)
907 for (
unsigned int j = 0; j < n; ++j)
908 out[i][j] = a[i] * b[j];
912 #define VNL_OUTER_PRODUCT_FIXED_INSTANTIATE( T, M, N ) \ 913 template VNL_EXPORT vnl_matrix_fixed<T,M,N > outer_product(vnl_vector_fixed<T,M > const&,\ 914 vnl_vector_fixed<T,N > const& ) 916 #undef VNL_MATRIX_FIXED_INSTANTIATE 917 #define VNL_MATRIX_FIXED_INSTANTIATE(T, M, N) \ 918 template class VNL_EXPORT vnl_matrix_fixed<T,M,N >; \ 919 VNL_OUTER_PRODUCT_FIXED_INSTANTIATE( T, M, N ) 921 #endif // vnl_matrix_fixed_hxx_ unsigned int cols() const
Return the number of columns.
vnl_vector_fixed< T, num_rows > apply_rowwise(T(*f)(vnl_vector_fixed< T, num_cols > const &)) const
Make a vector by applying a function across rows.
static void conjugate(T const *, T *, unsigned)
vnl_matrix_fixed & inplace_transpose()
Transposes this matrix efficiently, if it is square, and returns it.
vnl_vector_fixed< T, num_rows > get_column(unsigned col) const
Get a vector equal to the given column.
vnl_matrix_fixed & normalize_columns()
Normalizes each column so it is a unit vector, and returns "*this".
vnl_matrix_fixed & fliplr()
Reverses the order of columns, and returns "*this".
void swap(double &a, double &b)
unsigned int cols() const
Return the number of columns.
Complex additions to vnl_math.
void assert_finite_internal() const
Abort if any element of M is inf or nan.
vnl_vector_fixed< T, num_rows *num_cols > flatten_column_major() const
Flatten column-major (Fortran-style).
vnl_matrix_fixed & set_diagonal(vnl_vector< T > const &)
Sets the diagonal elements of this matrix to the specified list of values.
vnl_matrix< T > get_rows(vnl_vector< unsigned int > i) const
Get a matrix composed of rows from the indices specified in the supplied vector.
bool is_equal(vnl_matrix_fixed< T, num_rows, num_cols > const &rhs, double tol) const
Return true if all elements of both matrices are equal, within given tolerance.
bool is_zero() const
Return true if all elements equal to zero.
T const * data_block() const
Access the contiguous block storing the elements in the matrix row-wise. O(1).
vnl_matrix_fixed & set_row(unsigned i, T const *v)
Set the elements of the i'th row to v[i] (No bounds checking).
static void mul(const T *a, const T *b, T *r)
size_t size() const
Return the length, number of elements, dimension of this vector.
vnl_matrix_fixed & update(vnl_matrix< T > const &, unsigned top=0, unsigned left=0)
Set values of this matrix to those of M, starting at [top,left].
vnl_matrix< T > get_n_columns(unsigned colstart, unsigned n) const
Get n columns beginning at colstart.
vnl_matrix_fixed & scale_column(unsigned col, T value)
Scales elements in given column by a factor T, and returns "*this".
bool is_identity() const
Return true if all elements equal to identity.
Namespace with standard math functions.
static void sub(const T *a, const T *b, T *r)
void vnl_error_matrix_row_index(char const *fcn, int r)
Raise exception for invalid row index.
Fixed size, stack-stored, space-efficient matrix.
vnl_matrix_fixed & scale_row(unsigned row, T value)
Scales elements in given row by a factor T, and returns "*this".
void vnl_error_matrix_col_index(char const *fcn, int c)
Raise exception for invalid col index.
unsigned int rows() const
Return the number of rows.
bool has_nans() const
Return true if matrix contains NaNs.
vnl_bignum squared_magnitude(vnl_bignum const &x)
static void apply(T const *, unsigned, T(*f)(T), T *v_out)
void assert_size_internal(unsigned, unsigned) const
Abort unless M has the given size.
vnl_vector< T > get_diagonal() const
Return a vector with the content of the (main) diagonal.
abs_t operator_inf_norm() const
vnl_matrix_fixed & operator=(const vnl_matrix_fixed &rhs)=default
Copy another vnl_matrix_fixed<T,m,n> into this.
bool isnan(vnl_bignum const &)
bool is_finite() const
Return true if finite.
T data_[num_rows][num_cols]
bool read_ascii(std::istream &s)
vnl_matrix_fixed & set_identity()
Sets this matrix to an identity matrix, then returns "*this".
void swap(vnl_matrix_fixed< T, num_rows, num_cols > &that)
Swap this matrix with that matrix.
vnl_matrix< T > extract(unsigned r, unsigned c, unsigned top=0, unsigned left=0) const
Extract a sub-matrix of size r x c, starting at (top,left).
An ordinary mathematical matrix.
vnl_matrix_fixed< T, num_cols, num_rows > transpose() const
Return transpose.
vnl_matrix< T > get_columns(vnl_vector< unsigned int > i) const
Get a matrix composed of columns from the indices specified in the supplied vector.
void print(std::ostream &os) const
Print matrix to os in some hopefully sensible format.
vnl_matrix_fixed & fill(T)
Sets all elements of matrix to specified value, and returns "*this".
bool isfinite(vnl_bignum const &x)
static bool equal(const T *a, const T *b)
Mathematical vector class, templated by type of element.
VNL_EXPORT vnl_matrix_fixed< T, m, n > outer_product(vnl_vector_fixed< T, m > const &a, vnl_vector_fixed< T, n > const &b)
T const * data_block() const
Access the contiguous block storing the elements in the matrix row-wise. O(1).
Fixed length stack-stored, space-efficient vector.
vnl_vector_fixed< T, num_rows *num_cols > flatten_row_major() const
Flatten row-major (C-style).
T & operator()(unsigned r, unsigned c)
Access an element for reading or writing.
Fixed length stack-stored vector.
abs_t operator_one_norm() const
vnl_matrix_fixed apply(T(*f)(T)) const
Make a new matrix by applying function to each element.
vnl_bignum abs(vnl_bignum const &x)
vnl_matrix_fixed & copy_in(T const *)
Fills (laminates) this matrix with the given data, then returns it.
void vnl_error_matrix_dimension(char const *fcn, int r1, int c1, int r2, int c2)
Raise exception for invalid dimensions.
unsigned int rows() const
Return the number of rows.
vnl_matrix_fixed & fill_diagonal(T)
Sets all diagonal elements of matrix to specified value; returns "*this".
vnl_matrix< T > get_n_rows(unsigned rowstart, unsigned n) const
Get n rows beginning at rowstart.
vnl_matrix_fixed & set_column(unsigned i, T const *v)
Set the elements of the i'th column to v[i] (No bounds checking).
void copy_out(T *) const
Fills the given array with this matrix.
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 > conjugate_transpose() const
Return conjugate transpose.
static void div(const T *a, const T *b, T *r)
vnl_vector_fixed< T, num_cols > apply_columnwise(T(*f)(vnl_vector_fixed< T, num_rows > const &)) const
Make a vector by applying a function across columns.
T get(size_t i) const
Get value at element i.
vnl_matrix_fixed & normalize_rows()
Normalizes each row so it is a unit vector, and returns "*this".
unsigned int columns() const
Return the number of columns.
vnl_matrix_fixed & flipud()
Reverses the order of rows, and returns "*this".
vnl_matrix_fixed & set_columns(unsigned starting_column, vnl_matrix< T > const &M)
Set columns to those in M, starting at starting_column, then return *this.
static void add(const T *a, const T *b, T *r)
vnl_vector_fixed< T, num_cols > get_row(unsigned row) const
Get a vector equal to the given row.
vnl_c_vector< T >::abs_t abs_t
Type def for norms.