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

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

Addresses #436.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
RevLine 
[42]1// $Id: MatrixWeighted.cc 1486 2008-09-09 21:17:19Z jari $
[12]2
[570]3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
[831]5  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
[1275]6  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
[1260]7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
[570]8
[1469]9  This file is part of the yat library, http://dev.thep.lu.se/yat
[570]10
[675]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
[1486]13  published by the Free Software Foundation; either version 3 of the
[675]14  License, or (at your option) any later version.
[570]15
[675]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
[570]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
[1380]27#include "MatrixWeighted.h"
[273]28
[1381]29#include "DataIterator.h"
30#include "Matrix.h"
31#include "WeightIterator.h"
32
[792]33#include <cassert>
[17]34#include <vector>
35
[42]36namespace theplu {
[680]37namespace yat {
[616]38namespace utility {
[12]39
40
[1380]41  MatrixWeighted::MatrixWeighted(void)
42    : columns_(0)
[703]43  {
44  }
[12]45
[703]46
[1381]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
[1384]57  MatrixWeighted::MatrixWeighted(const Matrix& data)
58  {
59    copy(data);
60  }
61
62 
[1381]63  MatrixWeighted::MatrixWeighted(std::istream& is, char sep)
64  {
65    Matrix data(is, sep);
[1384]66    copy(data);
[1381]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
[1384]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();
[1483]125    resize(data.rows(), data.columns());
126    assert(rows()==data.rows());
127    assert(columns()==data.columns());
[1404]128    std::copy(data.begin(), data.end(), data_iterator(vec_.begin()));
129    std::copy(weight.begin(), weight.end(), weight_iterator(vec_.begin()));
[1384]130  }
131
132
[1381]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);
[1483]173    assert(this->rows()==rows);
174    assert(this->columns()==columns);
[1381]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
[680]201}}} // of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.