vnl_sym_matrix.hxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_sym_matrix.hxx
2 #ifndef vnl_sym_matrix_hxx_
3 #define vnl_sym_matrix_hxx_
4 //:
5 // \file
6 
7 #include <iostream>
8 #include "vnl_sym_matrix.h"
9 #ifdef _MSC_VER
10 # include <vcl_msvc_warnings.h>
11 #endif
12 #include <vnl/vnl_config.h> // for VNL_CONFIG_CHECK_BOUNDS
13 
14 // ==========================================================================
15 //: Replaces the symmetric submatrix of THIS matrix, starting at top left corner, by the elements of matrix m.
16 // O(m*m).
17 template<class T>
19  unsigned diagonal_start)
20 {
21  unsigned int end_val = diagonal_start + m.nn_;
22 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
23  if (this->nn_ < end_val)
24  vnl_error_matrix_dimension ("vnl_sym_matrix::update",
25  end_val, end_val, m.nn_, m.nn_);
26 #endif
27  for (unsigned int i = diagonal_start; i < end_val; i++)
28  for (unsigned int j = diagonal_start; j <= i; j++)
29  this->fast(i,j) = m.fast(i-diagonal_start,j-diagonal_start);
30  return *this;
31 }
32 
33 // ==========================================================================
34 //: Swap contents of m with THIS
35 template <class T>
37 {
38  unsigned nn = nn_;
39  T **index = index_;
40  T *data = data_;
41  nn_ =m.nn_;
42  index_ =m.index_;
43  data_ =m.data_;
44  m.nn_ =nn;
45  m.index_ =index;
46  m.data_ =data;
47 }
48 
49 // ==========================================================================
50 
51 template <class T>
53 {
54  if (&that == this) return *this;
55 
56  set_size(that.rows());
57  update(that);
58  return *this;
59 }
60 // ==========================================================================
61 template <class T>
63  {
64  vnl_c_vector<T>::deallocate(data_, static_cast<std::size_t>( size() ) );
65  vnl_c_vector<T>::deallocate(index_, static_cast<std::size_t> ( nn_ ) );
66  }
67 
68 
69 template <class T>
71 {
72  T * data = data_;
73  for (unsigned i=0; i< nn_; ++i) { index_[i] = data; data += i+1; }
74 }
75 
76 // ==========================================================================
77 //: Set the first i values of row i
78 // or the top i values of column i
79 template <class T>
80 void vnl_sym_matrix<T>::set_half_row (const vnl_vector<T> &half_row, unsigned i)
81 {
82 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
83  if (half_row.size() != i+1)
84  vnl_error_vector_dimension ("vnl_sym_matrix::set_half_row wrong size for half row",
85  half_row.size(), i+1);
86  if ( i > nn_)
87  vnl_error_vector_dimension ("vnl_sym_matrix::set_half_row wrong sizes",
88  i+1, rows());
89 #endif
90  half_row.copy_out(index_[i]);
91 }
92 
93 // ==========================================================================
94 //: print in lower triangular form
95 template <class T>
96 std::ostream& operator<< (std::ostream& s, const vnl_sym_matrix<T>& M)
97 {
98  for (unsigned i=0; i<M.rows(); ++i)
99  {
100  for (unsigned j=0; j<=i; ++j)
101  s << M.fast(i,j) << ' ';
102  s << '\n';
103  }
104  return s;
105 }
106 
107 // ==========================================================================
108 
109 template <class T>
111 {
112  if (a.rows() != b.rows()) return false;
113  const T* a_data = a.data_block();
114  const T* b_data = b.data_block();
115  const unsigned mn = a.size();
116  for (unsigned i = 0; i < mn; ++i)
117  if (a_data[i] != b_data[i]) return false;
118  return true;
119 }
120 
121 // ==========================================================================
122 
123 template <class T>
125 {
126  if (a.rows() != b.rows() || a.cols() != b.cols()) return false;
127 
128  const unsigned n = a.rows();
129  for (unsigned i=0; i< n; ++i)
130  {
131  for (unsigned j=0; j<i; ++j)
132  if (a.fast(i,j) != b(i,j) || a.fast(i,j) != b(j,i)) return false;
133  if (a.fast(i,i) != b(i,i)) return false;
134  }
135  return true;
136 }
137 
138 // ==========================================================================
139 
140 template <class T>
142 {
143  return operator==(b,a);
144 }
145 
146 // ==========================================================================
147 
148 #undef VNL_SYM_MATRIX_INSTANTIATE
149 #define VNL_SYM_MATRIX_INSTANTIATE(T) \
150 template class VNL_EXPORT vnl_sym_matrix<T >; \
151 template VNL_EXPORT std::ostream& operator<< (std::ostream& s, vnl_sym_matrix<T > const &); \
152 template VNL_EXPORT bool operator==(const vnl_sym_matrix<T > &a, const vnl_sym_matrix<T > &b); \
153 template VNL_EXPORT bool operator==(const vnl_sym_matrix<T > &a, const vnl_matrix<T > &b); \
154 template VNL_EXPORT bool operator==(const vnl_matrix<T > &a, const vnl_sym_matrix<T > &b)
155 
156 #endif // vnl_sym_matrix_hxx_
static void deallocate(T **, const std::size_t n_when_allocated)
unsigned int cols() const
Return the number of columns.
Definition: vnl_matrix.h:183
void swap(vnl_sym_matrix &m)
Swap contents of m with THIS.
vnl_sym_matrix< T > & update(vnl_sym_matrix< T > const &m, unsigned diag_start=0)
Replaces the symmetric submatrix of THIS matrix with the elements of matrix m.
#define m
Definition: vnl_vector.h:43
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
T fast(unsigned i, unsigned j) const
fast access, however i >= j.
vnl_sym_matrix< T > & operator=(vnl_sym_matrix< T > const &that)
unsigned int cols() const
Return the number of columns.
Contains class for symmetric matrices.
std::ostream & operator<<(std::ostream &s, vnl_decnum const &r)
decimal output.
Definition: vnl_decnum.h:393
void vnl_error_vector_dimension(char const *fcn, int l1, int l2)
Raise exception for invalid dimension.
Definition: vnl_error.cxx:30
unsigned int rows() const
Return the number of rows.
bool operator==(int r1, vnl_finite_int< N > const &r2)
Definition: vnl_finite.h:385
unsigned int size() const
Return the total number of elements stored by the matrix.
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
Mathematical vector class, templated by type of element.
Definition: vnl_fwd.h:16
void vnl_error_matrix_dimension(char const *fcn, int r1, int c1, int r2, int c2)
Raise exception for invalid dimensions.
Definition: vnl_error.cxx:70
T * data_block()
Return pointer to the lower triangular elements as a contiguous 1D C array;.
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
void setup_index()
Set up the index array.
void copy_out(T *) const
Copy elements to ptr[i].
Definition: vnl_vector.hxx:336
void set_half_row(const vnl_vector< T > &half_row, unsigned i)
Set the first i values of row i.
stores a symmetric matrix as just the diagonal and lower triangular part.