1 | // $Id: DataLookupWeighted1D.cc 1088 2008-02-14 14:26:19Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2007 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 2 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "DataLookupWeighted1D.h" |
---|
26 | #include "MatrixLookupWeighted.h" |
---|
27 | |
---|
28 | #include <cassert> |
---|
29 | #include <iostream> |
---|
30 | #include <iomanip> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace classifier { |
---|
35 | |
---|
36 | DataLookupWeighted1D::DataLookupWeighted1D(const MatrixLookupWeighted& m, |
---|
37 | const size_t i, |
---|
38 | const bool row_vector) |
---|
39 | : column_vector_(!row_vector), index_(i), matrix_(&m), owner_(false) |
---|
40 | { |
---|
41 | assert( !column_vector_ || i<m.columns()); |
---|
42 | assert( column_vector_ || i<m.rows()); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | DataLookupWeighted1D::DataLookupWeighted1D(const size_t size, |
---|
47 | const double value, |
---|
48 | const double weight) |
---|
49 | : column_vector_(false), index_(0), owner_(true) |
---|
50 | { |
---|
51 | matrix_ = new MatrixLookupWeighted(1,size,value,weight); |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | DataLookupWeighted1D::DataLookupWeighted1D(const DataLookupWeighted1D& other) |
---|
56 | : column_vector_(other.column_vector_), index_(other.index_), |
---|
57 | matrix_(other.matrix_), owner_(false) |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | DataLookupWeighted1D::~DataLookupWeighted1D() |
---|
63 | { |
---|
64 | if (owner_) |
---|
65 | delete matrix_; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | DataLookupWeighted1D::const_iterator DataLookupWeighted1D::begin(void) const |
---|
70 | { |
---|
71 | if (column_vector_) |
---|
72 | return const_iterator(const_iterator::iterator_type(*matrix_, 0, index_), |
---|
73 | matrix_->columns()); |
---|
74 | return const_iterator(const_iterator::iterator_type(*matrix_, index_, 0),1); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | double DataLookupWeighted1D::data(const size_t i) const |
---|
79 | { |
---|
80 | return column_vector_? matrix_->data(i,index_) : matrix_->data(index_,i); |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | DataLookupWeighted1D::const_iterator DataLookupWeighted1D::end(void) const |
---|
85 | { |
---|
86 | if (column_vector_) |
---|
87 | return const_iterator(const_iterator::iterator_type(*matrix_, |
---|
88 | matrix_->rows(), |
---|
89 | index_), |
---|
90 | matrix_->columns()); |
---|
91 | return const_iterator(const_iterator::iterator_type(*matrix_,index_+1,0),1); |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | size_t DataLookupWeighted1D::size(void) const |
---|
97 | { |
---|
98 | return column_vector_ ? matrix_->rows() : matrix_->columns(); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | double sum_weight(const DataLookupWeighted1D& x) |
---|
103 | { |
---|
104 | double r=0; |
---|
105 | for (size_t i=0; i<x.size(); ++i) |
---|
106 | r += x.weight(i); |
---|
107 | return r; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | double DataLookupWeighted1D::weight(const size_t i) const |
---|
112 | { |
---|
113 | return column_vector_? matrix_->weight(i,index_) : matrix_->weight(index_,i); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | double DataLookupWeighted1D::operator()(const size_t i) const |
---|
118 | { |
---|
119 | return column_vector_ ? (*matrix_)(i,index_) : (*matrix_)(index_,i); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | double DataLookupWeighted1D::operator[](const size_t i) const |
---|
124 | { |
---|
125 | return this->operator()(i); |
---|
126 | } |
---|
127 | |
---|
128 | }}} // of namespace classifier, yat, and theplu |
---|