source: trunk/yat/classifier/Kernel.cc @ 747

Last change on this file since 747 was 747, checked in by Peter, 15 years ago

replaced includes in header files with forward declarations when possible. Added some includes in cc files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 2.8 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Kernel.h"
25#include "DataLookup1D.h"
26#include "DataLookupWeighted1D.h"
27#include "KernelFunction.h"
28#include "MatrixLookup.h"
29#include "MatrixLookupWeighted.h"
30
31#include <vector>
32
33namespace theplu {
34namespace yat {
35namespace classifier {
36
37  Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf, 
38                 const bool own) 
39    : data_(&data), data_w_(0), kf_(&kf), ref_count_w_(NULL)
40  {
41    if (own)
42      ref_count_ = new u_int(1);
43    else
44      ref_count_ = NULL;
45  }
46
47
48  Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf,
49                 const bool own) 
50    : data_(&data), data_w_(&data), kf_(&kf)
51  {
52    if (own){
53      ref_count_ = new u_int(1);
54      ref_count_w_ = new u_int(1);
55    }
56    else {
57      ref_count_ = NULL;
58      ref_count_w_ = NULL;
59    }
60  }
61
62
63  Kernel::Kernel(const Kernel& other, const std::vector<size_t>& index)
64    : kf_(other.kf_)
65  {
66    data_ = other.data_->selected(index);
67    ref_count_ = new u_int(1);
68   
69    if (other.data_w_){
70      data_w_ = other.data_w_->selected(index);
71      ref_count_w_ = new u_int(1);
72    }
73    else{
74      data_w_=NULL;
75      ref_count_w_ = NULL;
76    }
77
78  }
79
80
81  Kernel::~Kernel()
82  {
83    if (ref_count_)
84      if (!--(*ref_count_))
85        delete data_;
86
87    if (ref_count_w_)
88      if (!--(*ref_count_w_))
89        delete data_w_;
90
91  }
92
93 
94  const DataLookup2D& Kernel::data(void) const
95  {
96    return *data_;
97  }
98
99
100  double Kernel::element(const DataLookup1D& vec, const size_t i) const
101  {
102    if (data_w_)
103      return kf_->operator()(vec, DataLookupWeighted1D(*data_w_,i, false)); 
104    else
105      return kf_->operator()(vec, DataLookup1D(*data_,i, false)); 
106  }
107
108
109  double Kernel::element(const DataLookupWeighted1D& vec, const size_t i) const
110  {
111    if (data_w_)
112      return kf_->operator()(vec, DataLookupWeighted1D(*data_w_,i, false)); 
113    else
114      return kf_->operator()(vec, DataLookup1D(*data_,i, false)); 
115  }
116
117
118  size_t Kernel::size(void) const
119  {
120    return data_->columns();
121  }
122
123
124  bool Kernel::weighted(void) const
125  {
126    return data_w_;
127  }
128
129}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.