source: trunk/yat/classifier/DataLookup1D.cc @ 1221

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

changing operator() in weighted lookups and removing operator[] in 1D lookups

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.8 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) 2005 Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
6  Copyright (C) 2007 Peter Johansson
7
8  This file is part of the yat library, http://trac.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 "DataLookup1D.h"
27#include "MatrixLookup.h"
28
29#include "yat/utility/Matrix.h"
30#include "yat/utility/Vector.h"
31
32#include <cassert>
33#include <iostream>
34#include <iomanip>
35
36namespace theplu {
37namespace yat {
38namespace classifier {
39
40  DataLookup1D::DataLookup1D(const MatrixLookup& m, const size_t i, 
41                             const bool row_vector)
42    : column_vector_(!row_vector), index_(i), matrix_(&m), owner_(false)
43  {
44    assert( !column_vector_ || i<m.columns());
45    assert( column_vector_ || i<m.rows());
46  }
47 
48
49  DataLookup1D::DataLookup1D(const size_t size, const double value)
50    : column_vector_(false), index_(0), owner_(true)
51  {
52    matrix_ = new MatrixLookup(1,size,value);
53  }
54
55
56  DataLookup1D::DataLookup1D(const DataLookup1D& other)
57    : column_vector_(other.column_vector_), index_(other.index_),
58      matrix_(other.matrix_), owner_(false)
59  {
60  }
61
62
63  DataLookup1D::DataLookup1D(const utility::VectorBase& v, 
64                             const std::vector<size_t>& index)
65    : column_vector_(true), index_(0), owner_(true)
66  {
67    utility::Matrix* m = new utility::Matrix(1,index.size());
68    for (size_t i=0; i<index.size(); ++i){
69      assert(index[i]<v.size());
70      (*m)(0,i)=v(index[i]);
71    }
72    matrix_ = new MatrixLookup(*m, true);
73  }
74
75
76  DataLookup1D::DataLookup1D(const utility::VectorBase& v)
77    : column_vector_(false), index_(0), owner_(true)
78  {
79    utility::Matrix* m = new utility::Matrix(1,v.size());
80    for (size_t i=0; i<v.size(); ++i){
81      (*m)(0,i)=v(i);
82    }
83    matrix_ = new MatrixLookup(*m, true);
84  }
85
86
87  DataLookup1D::~DataLookup1D()
88  {
89    if (owner_)
90      delete matrix_;
91  }
92
93
94  DataLookup1D::const_iterator DataLookup1D::begin(void) const
95  {
96    if (column_vector_)
97      return const_iterator(const_iterator::iterator_type(*matrix_, 0, index_), 
98                            matrix_->columns());
99    return const_iterator(const_iterator::iterator_type(*matrix_, index_, 0),1);
100  }
101
102
103  DataLookup1D::const_iterator DataLookup1D::end(void) const
104  {
105    if (column_vector_)
106      return const_iterator(const_iterator::iterator_type(*matrix_,
107                                                          matrix_->rows(), 
108                                                          index_), 
109                            matrix_->columns());
110    return const_iterator(const_iterator::iterator_type(*matrix_,index_+1,0), 1);
111  }
112
113
114  size_t DataLookup1D::size(void) const
115  {
116    return column_vector_ ? matrix_->rows() : matrix_->columns();
117  }
118
119
120  double DataLookup1D::operator()(const size_t i) const
121  { 
122    assert(i<size());
123    return column_vector_ ? (*matrix_)(i,index_) : (*matrix_)(index_,i);
124  }
125
126
127  double DataLookup1D::operator*(const DataLookup1D& other) const
128  {
129    assert(other.size()==size());
130    double res=0;
131    for (size_t i = 0; i<size(); i++)
132      res += (*this)(i)*other(i);
133    return res;
134  }
135
136
137  std::ostream& operator<<(std::ostream& os, const DataLookup1D& x)
138  {
139    os.setf(std::ios::dec);
140    os.precision(12);
141   
142    for (size_t i=0; i<x.size(); ++i) {
143      os << x(i);
144      if ((i+1)<x.size())
145        os << os.fill();
146    }
147    return os;
148  }
149
150}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.