1 | // $Id: DataLookupWeighted1D.cc 1392 2008-07-28 19:35:30Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
6 | Copyright (C) 2008 Peter Johansson |
---|
7 | |
---|
8 | This file is part of the yat library, http://dev.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 "DataLookupWeighted1D.h" |
---|
27 | #include "MatrixLookupWeighted.h" |
---|
28 | |
---|
29 | #include <cassert> |
---|
30 | #include <iostream> |
---|
31 | #include <iomanip> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace classifier { |
---|
36 | |
---|
37 | DataLookupWeighted1D::DataLookupWeighted1D(const MatrixLookupWeighted& m, |
---|
38 | const size_t i, |
---|
39 | const bool row_vector) |
---|
40 | : column_vector_(!row_vector), index_(i), matrix_(&m), owner_(false) |
---|
41 | { |
---|
42 | assert( !column_vector_ || i<m.columns()); |
---|
43 | assert( column_vector_ || i<m.rows()); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | DataLookupWeighted1D::DataLookupWeighted1D(const size_t size, |
---|
48 | const double value, |
---|
49 | const double weight) |
---|
50 | : column_vector_(false), index_(0), owner_(true) |
---|
51 | { |
---|
52 | matrix_ = new MatrixLookupWeighted(1,size,value,weight); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | DataLookupWeighted1D::DataLookupWeighted1D(const DataLookupWeighted1D& other) |
---|
57 | : column_vector_(other.column_vector_), index_(other.index_), |
---|
58 | matrix_(other.matrix_), owner_(false) |
---|
59 | { |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | DataLookupWeighted1D::~DataLookupWeighted1D() |
---|
64 | { |
---|
65 | if (owner_) |
---|
66 | delete matrix_; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | DataLookupWeighted1D::const_iterator DataLookupWeighted1D::begin(void) const |
---|
71 | { |
---|
72 | if (column_vector_) |
---|
73 | return const_iterator(const_iterator::iterator_type(*matrix_, 0, index_), |
---|
74 | matrix_->columns()); |
---|
75 | return const_iterator(const_iterator::iterator_type(*matrix_, index_, 0),1); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | double DataLookupWeighted1D::data(const size_t i) const |
---|
80 | { |
---|
81 | return column_vector_? matrix_->data(i,index_) : matrix_->data(index_,i); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | DataLookupWeighted1D::const_iterator DataLookupWeighted1D::end(void) const |
---|
86 | { |
---|
87 | if (column_vector_) |
---|
88 | return const_iterator(const_iterator::iterator_type(*matrix_, |
---|
89 | matrix_->rows(), |
---|
90 | index_), |
---|
91 | matrix_->columns()); |
---|
92 | return const_iterator(const_iterator::iterator_type(*matrix_,index_+1,0),1); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | size_t DataLookupWeighted1D::size(void) const |
---|
98 | { |
---|
99 | return column_vector_ ? matrix_->rows() : matrix_->columns(); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | double sum_weight(const DataLookupWeighted1D& x) |
---|
104 | { |
---|
105 | double r=0; |
---|
106 | for (size_t i=0; i<x.size(); ++i) |
---|
107 | r += x.weight(i); |
---|
108 | return r; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | double DataLookupWeighted1D::weight(const size_t i) const |
---|
113 | { |
---|
114 | return column_vector_? matrix_->weight(i,index_) : matrix_->weight(index_,i); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | std::pair<double,double> DataLookupWeighted1D::operator()(const size_t i) const |
---|
119 | { |
---|
120 | return column_vector_ ? (*matrix_)(i,index_) : (*matrix_)(index_,i); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | }}} // of namespace classifier, yat, and theplu |
---|