source: trunk/yat/utility/MatrixWeighted.cc @ 1483

Last change on this file since 1483 was 1483, checked in by Peter, 13 years ago

refs #396 - removing a constructor in MatrixLookupWeighted? - needed to re-organize tests in ncc_test to track down an error

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1// $Id: MatrixWeighted.cc 1483 2008-09-09 16:03:15Z peter $
2
3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
5  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "MatrixWeighted.h"
28
29#include "DataIterator.h"
30#include "Matrix.h"
31#include "WeightIterator.h"
32
33#include <cassert>
34#include <vector>
35
36namespace theplu {
37namespace yat {
38namespace utility {
39
40
41  MatrixWeighted::MatrixWeighted(void)
42    : columns_(0)
43  {
44  }
45
46
47  MatrixWeighted::MatrixWeighted(size_t r, size_t c, double x, double w)
48    : vec_(std::vector<DataWeight>(r*c, DataWeight(x,w))), columns_(c)
49  {}
50
51 
52  MatrixWeighted::MatrixWeighted(const MatrixWeighted& other)
53    : vec_(other.vec_), columns_(other.columns_)
54  {}
55
56
57  MatrixWeighted::MatrixWeighted(const Matrix& data)
58  {
59    copy(data);
60  }
61
62 
63  MatrixWeighted::MatrixWeighted(std::istream& is, char sep)
64  {
65    Matrix data(is, sep);
66    copy(data);
67  }
68
69 
70  MatrixWeighted::iterator MatrixWeighted::begin(void)
71  {
72    return iterator(vec_.begin());
73  }
74
75
76  MatrixWeighted::const_iterator MatrixWeighted::begin(void) const
77  {
78    return const_iterator(vec_.begin());
79  }
80
81
82  MatrixWeighted::iterator MatrixWeighted::begin_column(size_t i)
83  {
84    return iterator(vec_.begin()+i, columns_);
85  }
86
87
88  MatrixWeighted::const_iterator MatrixWeighted::begin_column(size_t i) const
89  {
90    return const_iterator(vec_.begin()+i, columns_);
91  }
92
93
94  MatrixWeighted::iterator MatrixWeighted::begin_row(size_t i)
95  {
96    return iterator(vec_.begin()+columns_*i);
97  }
98
99
100  MatrixWeighted::const_iterator MatrixWeighted::begin_row(size_t i) const
101  {
102    return const_iterator(vec_.begin()+columns_*i);
103  }
104
105
106  size_t MatrixWeighted::columns(void) const
107  {
108    return columns_;
109  }
110
111
112  void MatrixWeighted::copy(const Matrix& data)
113  {
114    Matrix weight;
115    nan(data, weight);
116    copy(data, weight);
117  }
118
119
120  void MatrixWeighted::copy(const Matrix& data, const Matrix& weight)
121  {
122    assert(data.rows()==weight.rows());
123    assert(data.columns()==weight.columns());
124    columns_ = data.columns();
125    resize(data.rows(), data.columns());
126    assert(rows()==data.rows());
127    assert(columns()==data.columns());
128    std::copy(data.begin(), data.end(), data_iterator(vec_.begin()));
129    std::copy(weight.begin(), weight.end(), weight_iterator(vec_.begin()));
130  }
131
132
133  MatrixWeighted::iterator MatrixWeighted::end(void)
134  {
135    return iterator(vec_.end());
136  }
137
138
139  MatrixWeighted::const_iterator MatrixWeighted::end(void) const
140  {
141    return const_iterator(vec_.end());
142  }
143
144
145  MatrixWeighted::iterator MatrixWeighted::end_column(size_t i)
146  {
147    return iterator(vec_.begin()+i+vec_.size(), columns_);
148  }
149
150
151  MatrixWeighted::const_iterator MatrixWeighted::end_column(size_t i) const
152  {
153    return const_iterator(vec_.begin()+i+vec_.size(), columns_);
154  }
155
156
157  MatrixWeighted::iterator MatrixWeighted::end_row(size_t i)
158  {
159    return iterator(vec_.begin()+columns_*(i+1));
160  }
161
162
163  MatrixWeighted::const_iterator MatrixWeighted::end_row(size_t i) const
164  {
165    return const_iterator(vec_.begin()+columns_*(i+1));
166  }
167
168
169  void MatrixWeighted::resize(size_t rows, size_t columns)
170  {
171    columns_ = columns;
172    vec_.resize(rows*columns);
173    assert(this->rows()==rows);
174    assert(this->columns()==columns);
175  }
176
177 
178  size_t MatrixWeighted::rows(void) const
179  {
180    if (vec_.size())
181      return vec_.size()/columns_;
182    return 0;
183  }
184
185
186  DataWeight& MatrixWeighted::operator()(size_t row, size_t column)
187  {
188    assert(column<columns_);
189    assert(row*column < vec_.size());
190    return vec_[row*columns_ + column];
191  }
192
193
194  const DataWeight& MatrixWeighted::operator()(size_t row, size_t column) const
195  {
196    assert(column<columns_);
197    assert(row*column < vec_.size());
198    return vec_[row*columns_ + column];
199  }
200
201}}} // of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.