source: trunk/test/matrix_test.cc @ 616

Last change on this file since 616 was 616, checked in by Jari Häkkinen, 17 years ago

Removed gslapi namespace and put the code into utility namespace.
Move #ifndef _header_ idiom to top of touched header files.
Removed unneccesary #includes, and added needed #includes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1// $Id: matrix_test.cc 616 2006-08-31 08:52:02Z jari $
2
3#include <c++_tools/utility/matrix.h>
4
5#include <unistd.h>
6#include <fstream>
7#include <iostream>
8
9class matrixwrapper
10{
11public:
12  matrixwrapper(size_t i,size_t j,double value=7)
13    : m_(i,j,value) {}
14
15  inline theplu::utility::vector
16  row(const size_t& i) { return theplu::utility::vector(m_,i); }
17
18  inline const theplu::utility::matrix& matrix(void) const { return m_; }
19
20private:
21  theplu::utility::matrix m_;
22};
23
24
25int main(const int argc,const char* argv[])
26{
27  using namespace theplu;
28  std::ostream* error;
29  if (argc>1 && argv[1]==std::string("-v"))
30    error = &std::cerr;
31  else {
32    error = new std::ofstream("/dev/null");
33    if (argc>1)
34      std::cout << "matrix_test -v : for printing extra information\n";
35  }
36
37  *error << "Testing matrix class" << std::endl;
38  bool ok = true;
39
40  *error << "\tcopy constructor and operator!=" << std::endl;
41  utility::matrix m(3,3,9);
42  utility::matrix m2(m);
43  if (m2!=m)
44    ok=false;
45
46  *error << "\toutput operator and istream constructor" << std::endl;
47  // Checking that the matrix output operator writes a file that the
48  // input operator can read.
49  std::ofstream my_out("data/tmp_test_matrix.txt");
50  my_out << m2;
51  my_out.close();
52  std::ifstream is("data/tmp_test_matrix.txt");
53  utility::matrix m3(is);
54  is.close();
55  if (m3!=m2)
56    ok=false;
57  unlink("data/tmp_test_matrix.txt");
58
59  *error << "\toperator*(double)" << std::endl;
60  utility::matrix m4(3,3,1);
61  m4 *= 9;
62  if (m4!=m) {
63    ok=false;
64    *error << "error operator*=(double)" << std::endl;
65  }
66
67  *error << "\tsub-matrix" << std::endl;
68  // Checking that sub-matrices work, i.e. mutation to the view are
69  // reflected in the viewed object.
70  is.open("data/knni_matrix.data");
71  // The stream input is a proper matrix file, with some stray empty
72  // lines and other whitespaces. The file is not expected to break
73  // things.
74  utility::matrix m5(is);
75  is.close();
76  double m5_sum=0;
77  for (size_t i=0; i<m5.rows(); ++i)
78    for (size_t j=0; j<m5.columns(); ++j)
79      m5_sum+=m5(i,j);
80  utility::matrix* m5sub=new utility::matrix(m5,3,3,3,3);
81  double m5sub_sum=0;
82  for (size_t i=0; i<m5sub->rows(); ++i)
83    for (size_t j=0; j<m5sub->columns(); ++j) {
84      m5sub_sum+=(*m5sub)(i,j);
85      (*m5sub)(i,j)=1;
86    }
87  delete m5sub;
88  double m5_sum2=0;
89  for (size_t i=0; i<m5.rows(); ++i)
90    for (size_t j=0; j<m5.columns(); ++j)
91      m5_sum2+=m5(i,j);
92  if (m5_sum2-m5_sum-9+m5sub_sum>1e-13) {
93    ok=false;
94    *error << "error sub-matrix test" << std::endl;
95  }
96
97  *error << "\tsub-(row)vector" << std::endl;
98  // Checking that the row view works, i.e. mutation to the view are
99  // reflected in the viewed object.
100  utility::vector v5subrow(m5,3);
101  double v5subrow_sum=0;
102  for (size_t i=0; i<v5subrow.size(); ++i) {
103    v5subrow_sum+=v5subrow(i);
104    v5subrow[i]=0;
105  }
106  double m5_sum3=0;
107  for (size_t i=0; i<m5.rows(); ++i)
108    for (size_t j=0; j<m5.columns(); ++j)
109      m5_sum3+=m5(i,j);
110  if (m5_sum3-m5_sum2+v5subrow_sum>1e-13) {
111    ok=false;
112    *error << "error sub-vector test 1" << std::endl;
113  }
114
115  *error << "\tsub-(column)vector" << std::endl;
116  // Checking that the column view works, i.e. mutation to the view
117  // are reflected in the viewed object.
118  utility::vector v5subcolumn(m5,0,false);
119  double v5subcolumn_sum=0;
120  for (size_t i=0; i<v5subcolumn.size(); ++i) {
121    v5subcolumn_sum+=v5subcolumn(i);
122    v5subcolumn(i)=1;
123  }
124  double m5_sum4=0;
125  for (size_t i=0; i<m5.rows(); ++i)
126    for (size_t j=0; j<m5.columns(); ++j)
127      m5_sum4+=m5(i,j);
128  if (m5_sum4-m5_sum3-v5subcolumn.size()+v5subcolumn_sum>1e-13) {
129    ok=false;
130    *error << "error sub-vector test 2" << std::endl;
131  }
132  // Checking that the column view above mutates the values in the row
133  // view.
134  double v5subrow_sum2=0;
135  for (size_t i=0; i<v5subrow.size(); ++i)
136    v5subrow_sum2+=v5subrow(i);
137  if (v5subrow_sum2-v5subcolumn(3)>1e-13) {
138    ok=false;
139    *error << "error sub-vector test 3" << std::endl;
140  }
141
142  *error << "\tsub-vector and vector copying" << std::endl;
143  // Checking that a view is not inherited through the copy
144  // contructor.
145  utility::vector v6(v5subrow);
146  v6.set_all(2);
147  double v5subrow_sum3=0;
148  for (size_t i=0; i<v5subrow.size(); ++i)
149    v5subrow_sum3+=v5subrow(i);
150  if (v5subrow_sum3-v5subrow_sum2>1e-13) {
151    ok=false;
152    *error << "error sub-vector test 4" << std::endl;
153  }
154  // Checking that values in a vector is copied into a viewed matrix.
155  v5subrow.set(v6);
156  double m5_sum5=0;
157  for (size_t i=0; i<m5.rows(); ++i)
158    for (size_t j=0; j<m5.columns(); ++j)
159      m5_sum5+=m5(i,j);
160  if (m5_sum5-m5_sum4-v5subrow.size()*2+v5subrow_sum3>1e-13) {
161    ok=false;
162    *error << "error sub-vector test 5" << std::endl;
163  }
164  // Checking that vector::operator= indeed makes a view to become a
165  // "normal" vector.
166  v5subrow=utility::vector(23,22);
167  double m5_sum6=0;
168  for (size_t i=0; i<m5.rows(); ++i)
169    for (size_t j=0; j<m5.columns(); ++j)
170      m5_sum6+=m5(i,j);
171  if (m5_sum6-m5_sum5>1e-13) {
172    ok=false;
173    *error << "error sub-vector test 6" << std::endl;
174  }
175
176  // Checking that the memberfunction matrixwrapper::row() returns a
177  // view
178  *error << "\tthat class member returns a view" << std::endl;
179  matrixwrapper mw(5,2);
180  utility::vector mwrow=mw.row(2);
181  if (mwrow.gsl_vector_p()->data != &(mw.matrix()(2,0))) {
182    ok=false;
183    *error << "error sub-vector test 7" << std::endl;
184  }
185
186  *error << "\tsub-matrix of a sub-matrix" << std::endl;
187  // Checking that a sub-matrix of a sub-matrix can be created.
188  utility::matrix sub(m5,3,3,3,3);
189  utility::matrix subsub(sub,2,1,1,2);
190  subsub(0,0)=23221121;
191  if (&sub(2,1)!=&subsub(0,0) || subsub(0,0)!=m5(5,4) || &subsub(0,0)!=&m5(5,4)){
192    ok=false;
193    *error << "error sub-matrix test 1" << std::endl;
194  }
195
196  *error << "\tmatrix::nan()" << std::endl;
197  is.open("data/sorlie_centroids.txt");
198  utility::matrix* m_nan = new utility::matrix(is,'\t');
199  utility::matrix m_weight;
200  m_nan->nan(m_weight);
201  is.close();
202  if (m_weight(0,0)!=1){
203    *error << "error in matrix::nan(): element(0,0) is " << m_weight(0,0)
204           << " expected 1." << std::endl;
205    ok=false;
206  }
207  if (m_weight(3,2)!=0){
208    *error << "error in matrix::nan(): element(3,2) is " << m_weight(3,2)
209           << " expected 0." << std::endl;
210    ok=false;
211  }
212  delete m_nan;
213
214  if (error!=&std::cerr)
215    delete error;
216
217  return (ok ? 0 : -1);
218}
Note: See TracBrowser for help on using the repository browser.