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

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

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1// $Id: MatrixWeighted.cc 1487 2008-09-10 08:41:36Z jari $
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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "MatrixWeighted.h"
26
27#include "DataIterator.h"
28#include "Matrix.h"
29#include "WeightIterator.h"
30
31#include <cassert>
32#include <vector>
33
34namespace theplu {
35namespace yat {
36namespace utility {
37
38
39  MatrixWeighted::MatrixWeighted(void)
40    : columns_(0)
41  {
42  }
43
44
45  MatrixWeighted::MatrixWeighted(size_t r, size_t c, double x, double w)
46    : vec_(std::vector<DataWeight>(r*c, DataWeight(x,w))), columns_(c)
47  {}
48
49 
50  MatrixWeighted::MatrixWeighted(const MatrixWeighted& other)
51    : vec_(other.vec_), columns_(other.columns_)
52  {}
53
54
55  MatrixWeighted::MatrixWeighted(const Matrix& data)
56  {
57    copy(data);
58  }
59
60 
61  MatrixWeighted::MatrixWeighted(std::istream& is, char sep)
62  {
63    Matrix data(is, sep);
64    copy(data);
65  }
66
67 
68  MatrixWeighted::iterator MatrixWeighted::begin(void)
69  {
70    return iterator(vec_.begin());
71  }
72
73
74  MatrixWeighted::const_iterator MatrixWeighted::begin(void) const
75  {
76    return const_iterator(vec_.begin());
77  }
78
79
80  MatrixWeighted::iterator MatrixWeighted::begin_column(size_t i)
81  {
82    return iterator(vec_.begin()+i, columns_);
83  }
84
85
86  MatrixWeighted::const_iterator MatrixWeighted::begin_column(size_t i) const
87  {
88    return const_iterator(vec_.begin()+i, columns_);
89  }
90
91
92  MatrixWeighted::iterator MatrixWeighted::begin_row(size_t i)
93  {
94    return iterator(vec_.begin()+columns_*i);
95  }
96
97
98  MatrixWeighted::const_iterator MatrixWeighted::begin_row(size_t i) const
99  {
100    return const_iterator(vec_.begin()+columns_*i);
101  }
102
103
104  size_t MatrixWeighted::columns(void) const
105  {
106    return columns_;
107  }
108
109
110  void MatrixWeighted::copy(const Matrix& data)
111  {
112    Matrix weight;
113    nan(data, weight);
114    copy(data, weight);
115  }
116
117
118  void MatrixWeighted::copy(const Matrix& data, const Matrix& weight)
119  {
120    assert(data.rows()==weight.rows());
121    assert(data.columns()==weight.columns());
122    columns_ = data.columns();
123    resize(data.rows(), data.columns());
124    assert(rows()==data.rows());
125    assert(columns()==data.columns());
126    std::copy(data.begin(), data.end(), data_iterator(vec_.begin()));
127    std::copy(weight.begin(), weight.end(), weight_iterator(vec_.begin()));
128  }
129
130
131  MatrixWeighted::iterator MatrixWeighted::end(void)
132  {
133    return iterator(vec_.end());
134  }
135
136
137  MatrixWeighted::const_iterator MatrixWeighted::end(void) const
138  {
139    return const_iterator(vec_.end());
140  }
141
142
143  MatrixWeighted::iterator MatrixWeighted::end_column(size_t i)
144  {
145    return iterator(vec_.begin()+i+vec_.size(), columns_);
146  }
147
148
149  MatrixWeighted::const_iterator MatrixWeighted::end_column(size_t i) const
150  {
151    return const_iterator(vec_.begin()+i+vec_.size(), columns_);
152  }
153
154
155  MatrixWeighted::iterator MatrixWeighted::end_row(size_t i)
156  {
157    return iterator(vec_.begin()+columns_*(i+1));
158  }
159
160
161  MatrixWeighted::const_iterator MatrixWeighted::end_row(size_t i) const
162  {
163    return const_iterator(vec_.begin()+columns_*(i+1));
164  }
165
166
167  void MatrixWeighted::resize(size_t rows, size_t columns)
168  {
169    columns_ = columns;
170    vec_.resize(rows*columns);
171    assert(this->rows()==rows);
172    assert(this->columns()==columns);
173  }
174
175 
176  size_t MatrixWeighted::rows(void) const
177  {
178    if (vec_.size())
179      return vec_.size()/columns_;
180    return 0;
181  }
182
183
184  DataWeight& MatrixWeighted::operator()(size_t row, size_t column)
185  {
186    assert(column<columns_);
187    assert(row*column < vec_.size());
188    return vec_[row*columns_ + column];
189  }
190
191
192  const DataWeight& MatrixWeighted::operator()(size_t row, size_t column) const
193  {
194    assert(column<columns_);
195    assert(row*column < vec_.size());
196    return vec_[row*columns_ + column];
197  }
198
199}}} // of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.