vnl_io_sym_matrix.hxx
Go to the documentation of this file.
1 // This is core/vnl/io/vnl_io_sym_matrix.hxx
2 #ifndef vnl_io_sym_matrix_hxx_
3 #define vnl_io_sym_matrix_hxx_
4 //:
5 // \file
6 
7 #include "vnl_io_sym_matrix.h"
8 #include <vnl/vnl_sym_matrix.h>
9 #include <vsl/vsl_b_read_block_old.h>
10 #include <vsl/vsl_block_binary.h>
11 #include <vsl/vsl_indent.h>
12 
13 //=================================================================================
14 //: Binary save self to stream.
15 template<class T>
16 void vsl_b_write(vsl_b_ostream & os, const vnl_sym_matrix<T> & p)
17 {
18  constexpr short version_no = 2;
19  vsl_b_write(os, version_no);
20  vsl_b_write(os, p.rows());
21 
22  // Calling p.begin() on empty matrix causes segfault
23  if (p.size()>0)
24  vsl_block_binary_write(os, p.data_block(), p.size());
25 }
26 
27 //=================================================================================
28 //: Binary load self from stream.
29 template<class T>
30 void vsl_b_read(vsl_b_istream &is, vnl_sym_matrix<T> & p)
31 {
32  if (!is) return;
33 
34  short v;
35  unsigned n;
36  vsl_b_read(is, v);
37  switch (v)
38  {
39  case 1:
40  vsl_b_read(is, n);
41  p.set_size(n);
42  // Calling begin() on empty matrix causes segfault
43  if (n>0)
44  vsl_b_read_block_old(is, p.data_block(), p.size());
45  break;
46 
47  case 2:
48  vsl_b_read(is, n);
49  p.set_size(n);
50  // Calling begin() on empty matrix causes segfault
51  if (n>0)
52  vsl_block_binary_read(is, p.data_block(), p.size());
53  break;
54 
55  default:
56  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_sym_matrix<T>&)\n"
57  << " Unknown version number "<< v << '\n';
58  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
59  return;
60  }
61 }
62 
63 //====================================================================================
64 //: Output a human readable summary to the stream
65 template<class T>
66 void vsl_print_summary(std::ostream & os,const vnl_sym_matrix<T> & p)
67 {
68  os<<"Size: "<<p.rows()<<" x "<<p.cols()<<std::endl;
69 
70  unsigned int n = 5;
71 
72 
73  if (n>p.cols()) n=p.cols();
74 
75  vsl_indent_inc(os);
76  for (unsigned int i=0;i<n;i++)
77  {
78  os<<vsl_indent()<<" (";
79 
80  for ( unsigned int j=0; j<=i; j++)
81  os<<p(i,j)<<' ';
82  os << std::endl;
83  }
84  if (p.rows()>n) os <<vsl_indent()<<" (...\n";
85  vsl_indent_dec(os);
86 }
87 
88 
89 #define VNL_IO_SYM_MATRIX_INSTANTIATE(T) \
90 template VNL_EXPORT void vsl_print_summary(std::ostream &, const vnl_sym_matrix<T > &); \
91 template VNL_EXPORT void vsl_b_read(vsl_b_istream &, vnl_sym_matrix<T > &); \
92 template VNL_EXPORT void vsl_b_write(vsl_b_ostream &, const vnl_sym_matrix<T > &)
93 
94 #endif // vnl_io_sym_matrix_hxx_
void vsl_print_summary(std::ostream &os, vnl_bignum const &b)
Print human readable summary of object to a stream.
unsigned int cols() const
Return the number of columns.
Contains class for symmetric matrices.
#define v
Definition: vnl_vector.h:42
unsigned int rows() const
Return the number of rows.
unsigned int size() const
Return the total number of elements stored by the matrix.
void vsl_b_read(vsl_b_istream &is, vnl_bignum &v)
Binary load vnl_bignum from stream.
void vsl_b_write(vsl_b_ostream &os, vnl_bignum const &v)
Binary save vnl_bignum to stream.
void set_size(int n)
Resize matrix to n by n.
T * data_block()
Return pointer to the lower triangular elements as a contiguous 1D C array;.
stores a symmetric matrix as just the diagonal and lower triangular part.