source: trunk/test/matrix_test.cc @ 1151

Last change on this file since 1151 was 1151, checked in by Peter, 14 years ago

remove operator [] in Vector.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1// $Id: matrix_test.cc 1151 2008-02-25 22:32:04Z peter $
2
3/*
4  Copyright (C) 2005 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
6  Copyright (C) 2007 Jari Häkkinen
7
8  This file is part of the yat library, http://trac.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "yat/utility/Matrix.h"
27
28#include <cstdio>
29#include <fstream>
30#include <iostream>
31
32class matrixwrapper
33{
34public:
35  matrixwrapper(size_t i,size_t j,double value=7)
36    : m_(i,j,value) {}
37
38  inline theplu::yat::utility::VectorView
39  row(const size_t& i) { return m_.row_view(i); }
40
41  inline const theplu::yat::utility::Matrix& matrix(void) const { return m_; }
42
43private:
44  theplu::yat::utility::Matrix m_;
45};
46
47
48int main(const int argc,const char* argv[])
49{
50  using namespace theplu::yat;
51  std::ostream* error;
52  if (argc>1 && argv[1]==std::string("-v"))
53    error = &std::cerr;
54  else {
55    error = new std::ofstream("/dev/null");
56    if (argc>1)
57      std::cout << "matrix_test -v : for printing extra information\n";
58  }
59
60  *error << "Testing matrix class" << std::endl;
61  bool ok = true;
62  utility::Matrix unit3x3(3,3);
63  for (size_t i=0; i<unit3x3.rows(); ++i)
64    unit3x3(i,i)=1;
65
66  *error << "\tcopy constructor and operator!=" << std::endl;
67  utility::Matrix m(3,3,9);
68  utility::Matrix m2(m);
69  if (m2!=m)
70    ok=false;
71
72  *error << "\toutput operator and istream constructor" << std::endl;
73  // Checking that the matrix output operator writes a file that the
74  // input operator can read.
75  std::ofstream my_out("data/tmp_test_matrix.txt");
76  my_out << m2;
77  my_out.close();
78  std::ifstream is("data/tmp_test_matrix.txt");
79  utility::Matrix m3(is);
80  is.close();
81  if (m3!=m2)
82    ok=false;
83  std::remove("data/tmp_test_matrix.txt");
84
85  *error << "\toperator*=(double)" << std::endl;
86  utility::Matrix m4(3,3,1);
87  m4 *= 9;
88  if (m4!=m) {
89    ok=false;
90    *error << "error operator*=(double)" << std::endl;
91  }
92
93  is.open("data/knni_matrix.data");
94  // The stream input is a proper matrix file, with some stray empty
95  // lines and other whitespaces. The file is not expected to break
96  // things.
97  utility::Matrix m5(is);
98  is.close();
99  double m5_sum=0;
100  for (size_t i=0; i<m5.rows(); ++i)
101    for (size_t j=0; j<m5.columns(); ++j)
102      m5_sum+=m5(i,j);
103
104  // checking that copy constructor creates an independent object when
105  // a non-view matrix is copied
106  {
107    *error << "\tcopy constructor" << std::endl;
108    utility::Matrix m2(m5);
109    ok &= (m2.rows()==m5.rows());
110    ok &= (m2.columns()==m5.columns());
111    ok &= (m2==m5);
112    ok &= (&m2 != &m5);
113  }
114
115  *error << "\tsub-(row)vector" << std::endl;
116  // Checking that the row view works, i.e. mutation to the view are
117  // reflected in the viewed object.
118  double m5_sum2=0;
119  for (size_t i=0; i<m5.rows(); ++i)
120    for (size_t j=0; j<m5.columns(); ++j)
121      m5_sum2+=m5(i,j);
122  utility::VectorView v5subrow(m5,3);
123  double v5subrow_sum=0;
124  for (size_t i=0; i<v5subrow.size(); ++i) {
125    v5subrow_sum+=v5subrow(i);
126    v5subrow(i)=0;
127  }
128  double m5_sum3=0;
129  for (size_t i=0; i<m5.rows(); ++i)
130    for (size_t j=0; j<m5.columns(); ++j)
131      m5_sum3+=m5(i,j);
132  if (m5_sum3-m5_sum2+v5subrow_sum>1e-13) {
133    ok=false;
134    *error << "error sub-vector test 1" << std::endl;
135  }
136
137  *error << "\tsub-(column)vector" << std::endl;
138  // Checking that the column view works, i.e. mutation to the view
139  // are reflected in the viewed object.
140  m5_sum3=0;
141  for (size_t i=0; i<m5.rows(); ++i)
142    for (size_t j=0; j<m5.columns(); ++j)
143      m5_sum3+=m5(i,j);
144  utility::VectorView v5subcolumn = m5.column_view(0);
145  double v5subcolumn_sum=0;
146  for (size_t i=0; i<v5subcolumn.size(); ++i) {
147    v5subcolumn_sum+=v5subcolumn(i);
148    v5subcolumn(i)=1;
149  }
150  double m5_sum4=0;
151  for (size_t i=0; i<m5.rows(); ++i)
152    for (size_t j=0; j<m5.columns(); ++j)
153      m5_sum4+=m5(i,j);
154  if (m5_sum4-m5_sum3-v5subcolumn.size()+v5subcolumn_sum>1e-13) {
155    ok=false;
156    *error << "error sub-vector test 2" << std::endl;
157  }
158
159  // Checking that the column view above mutates the values in the row
160  // view.
161  double v5subrow_sum2=0;
162  for (size_t i=0; i<v5subrow.size(); ++i)
163    v5subrow_sum2+=v5subrow(i);
164  if (v5subrow_sum2-v5subcolumn(3)>1e-13) {
165    ok=false;
166    *error << "error sub-vector test 3" << std::endl;
167  }
168
169  *error << "\tsub-vector and vector copying" << std::endl;
170  // Checking that a view is not inherited through the copy
171  // contructor.
172  utility::Vector v6(v5subrow);
173  v6.all(2);
174  double v5subrow_sum3=0;
175  for (size_t i=0; i<v5subrow.size(); ++i)
176    v5subrow_sum3+=v5subrow(i);
177  if (v5subrow_sum3-v5subrow_sum2>1e-13) {
178    ok=false;
179    *error << "error sub-vector test 4" << std::endl;
180  }
181  // Checking that values in a vector is copied into a viewed matrix.
182  v5subrow = v6;
183  double m5_sum5=0;
184  for (size_t i=0; i<m5.rows(); ++i)
185    for (size_t j=0; j<m5.columns(); ++j)
186      m5_sum5+=m5(i,j);
187  if (m5_sum5-m5_sum4-v5subrow.size()*2+v5subrow_sum3>1e-13) {
188    ok=false;
189    *error << "error sub-vector test 5" << std::endl;
190  }
191
192  // Checking that the memberfunction matrixwrapper::row() returns a
193  // view
194  *error << "\tthat class member returns a view" << std::endl;
195  matrixwrapper mw(5,2);
196  utility::VectorView mwrow=mw.row(2);
197  if (mwrow.gsl_vector_p()->data != &(mw.matrix()(2,0))) {
198    ok=false;
199    *error << "error sub-vector test 7" << std::endl;
200  }
201
202  *error << "\tmatrix::nan()" << std::endl;
203  is.open("data/sorlie_centroid_data.txt");
204  utility::Matrix* m_nan = new utility::Matrix(is,'\t');
205  utility::Matrix m_weight;
206  utility::nan(*m_nan,m_weight);
207  is.close();
208  if (m_weight(0,0)!=1){
209    *error << "error in matrix::nan(): element(0,0) is " << m_weight(0,0)
210           << " expected 1." << std::endl;
211    ok=false;
212  }
213  if (m_weight(3,2)!=0){
214    *error << "error in matrix::nan(): element(3,2) is " << m_weight(3,2)
215           << " expected 0." << std::endl;
216    ok=false;
217  }
218  delete m_nan;
219
220  *error << "\toperator*=(matrix&)" << std::endl;
221  utility::Matrix m6(unit3x3);
222  m6 *= m;
223  if (m6!=m) {
224    ok=false;
225    *error << "error operator*=(matrix) 1" << std::endl;
226  }
227  m6 *= unit3x3;
228  if (m6!=m) {
229    ok=false;
230    *error << "error operator*=(matrix) 2" << std::endl;
231  }
232  m6*= utility::Matrix(3,4,1.0);
233  m6*= utility::Matrix(4,3,1.0);
234  m6*= utility::Matrix(3,5,2.0);
235  m6*= utility::Matrix(5,5,2.0);
236  m6*= utility::Matrix(5,3,2.0);
237  m6*= unit3x3;
238
239  if (error!=&std::cerr)
240    delete error;
241
242  return (ok ? 0 : -1);
243}
Note: See TracBrowser for help on using the repository browser.