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

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

refs #366 - weighted percentile
Added a class Percentile that can calculate the percentile for both
unweighted and weighted ranges. In order to make sense of the weighted
case, I had to modify the definition of percentile (slightly). The old
function percentile is using the old definition, but a new function,
percentile2, is using the new function and is simply calling the
Percentile class. The median is the same for the two definitions and
therefore it makes no difference which function to call, but to enable
calculation of weighted median percentile2 is called.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1// $Id: MatrixWeighted.cc 1404 2008-08-07 22:47:18Z 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://trac.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.columns(), data.rows());
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  }
172
173 
174  size_t MatrixWeighted::rows(void) const
175  {
176    if (vec_.size())
177      return vec_.size()/columns_;
178    return 0;
179  }
180
181
182  DataWeight& MatrixWeighted::operator()(size_t row, size_t column)
183  {
184    assert(column<columns_);
185    assert(row*column < vec_.size());
186    return vec_[row*columns_ + column];
187  }
188
189
190  const DataWeight& MatrixWeighted::operator()(size_t row, size_t column) const
191  {
192    assert(column<columns_);
193    assert(row*column < vec_.size());
194    return vec_[row*columns_ + column];
195  }
196
197}}} // of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.