[622] | 1 | // $Id: DataLookupWeighted1D.cc 1552 2008-10-06 14:36:56Z peter $ |
---|
| 2 | |
---|
[675] | 3 | /* |
---|
[831] | 4 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
---|
[1275] | 5 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
| 6 | Copyright (C) 2008 Peter Johansson |
---|
[622] | 7 | |
---|
[1437] | 8 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
[675] | 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 |
---|
[1486] | 12 | published by the Free Software Foundation; either version 3 of the |
---|
[675] | 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 |
---|
[1487] | 21 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
[675] | 22 | */ |
---|
| 23 | |
---|
[680] | 24 | #include "DataLookupWeighted1D.h" |
---|
| 25 | #include "MatrixLookupWeighted.h" |
---|
[675] | 26 | |
---|
[622] | 27 | #include <cassert> |
---|
| 28 | #include <iostream> |
---|
| 29 | #include <iomanip> |
---|
| 30 | |
---|
| 31 | namespace theplu { |
---|
[680] | 32 | namespace yat { |
---|
[622] | 33 | namespace classifier { |
---|
| 34 | |
---|
| 35 | DataLookupWeighted1D::DataLookupWeighted1D(const MatrixLookupWeighted& m, |
---|
| 36 | const size_t i, |
---|
| 37 | const bool row_vector) |
---|
| 38 | : column_vector_(!row_vector), index_(i), matrix_(&m), owner_(false) |
---|
| 39 | { |
---|
| 40 | assert( !column_vector_ || i<m.columns()); |
---|
| 41 | assert( column_vector_ || i<m.rows()); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | DataLookupWeighted1D::DataLookupWeighted1D(const size_t size, |
---|
| 46 | const double value, |
---|
| 47 | const double weight) |
---|
| 48 | : column_vector_(false), index_(0), owner_(true) |
---|
| 49 | { |
---|
| 50 | matrix_ = new MatrixLookupWeighted(1,size,value,weight); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | DataLookupWeighted1D::DataLookupWeighted1D(const DataLookupWeighted1D& other) |
---|
| 55 | : column_vector_(other.column_vector_), index_(other.index_), |
---|
| 56 | matrix_(other.matrix_), owner_(false) |
---|
| 57 | { |
---|
| 58 | } |
---|
| 59 | |
---|
[720] | 60 | |
---|
[622] | 61 | DataLookupWeighted1D::~DataLookupWeighted1D() |
---|
| 62 | { |
---|
| 63 | if (owner_) |
---|
| 64 | delete matrix_; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
[890] | 68 | DataLookupWeighted1D::const_iterator DataLookupWeighted1D::begin(void) const |
---|
| 69 | { |
---|
[1088] | 70 | if (column_vector_) |
---|
[1526] | 71 | return matrix_->begin_column(index_); |
---|
| 72 | return matrix_->begin_row(index_); |
---|
[890] | 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
[720] | 76 | double DataLookupWeighted1D::data(const size_t i) const |
---|
| 77 | { |
---|
| 78 | return column_vector_? matrix_->data(i,index_) : matrix_->data(index_,i); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
[890] | 82 | DataLookupWeighted1D::const_iterator DataLookupWeighted1D::end(void) const |
---|
| 83 | { |
---|
[1088] | 84 | if (column_vector_) |
---|
[1526] | 85 | return matrix_->end_column(index_); |
---|
| 86 | return matrix_->end_row(index_); |
---|
[890] | 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
[720] | 91 | size_t DataLookupWeighted1D::size(void) const |
---|
| 92 | { |
---|
| 93 | return column_vector_ ? matrix_->rows() : matrix_->columns(); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
[779] | 97 | double sum_weight(const DataLookupWeighted1D& x) |
---|
| 98 | { |
---|
| 99 | double r=0; |
---|
| 100 | for (size_t i=0; i<x.size(); ++i) |
---|
| 101 | r += x.weight(i); |
---|
| 102 | return r; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | |
---|
[720] | 106 | double DataLookupWeighted1D::weight(const size_t i) const |
---|
| 107 | { |
---|
| 108 | return column_vector_? matrix_->weight(i,index_) : matrix_->weight(index_,i); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
[1552] | 112 | DataLookupWeighted1D::const_reference |
---|
| 113 | DataLookupWeighted1D::operator()(const size_t i) const |
---|
[720] | 114 | { |
---|
| 115 | return column_vector_ ? (*matrix_)(i,index_) : (*matrix_)(index_,i); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
[680] | 119 | }}} // of namespace classifier, yat, and theplu |
---|