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

Last change on this file since 720 was 720, checked in by Jari Häkkinen, 15 years ago

Fixes #170. Almost all inlines removed, some classes have no cc file.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 2.7 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
30#include <vector>
31
32namespace theplu {
33namespace yat {
34namespace classifier {
35
36  Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf, 
37                 const bool own) 
38    : data_(&data), data_w_(0), kf_(&kf), ref_count_w_(NULL)
39  {
40    if (own)
41      ref_count_ = new u_int(1);
42    else
43      ref_count_ = NULL;
44  }
45
46
47  Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf,
48                 const bool own) 
49    : data_(&data), data_w_(&data), kf_(&kf)
50  {
51    if (own){
52      ref_count_ = new u_int(1);
53      ref_count_w_ = new u_int(1);
54    }
55    else {
56      ref_count_ = NULL;
57      ref_count_w_ = NULL;
58    }
59  }
60
61
62  Kernel::Kernel(const Kernel& other, const std::vector<size_t>& index)
63    : kf_(other.kf_)
64  {
65    data_ = other.data_->selected(index);
66    ref_count_ = new u_int(1);
67   
68    if (other.data_w_){
69      data_w_ = other.data_w_->selected(index);
70      ref_count_w_ = new u_int(1);
71    }
72    else{
73      data_w_=NULL;
74      ref_count_w_ = NULL;
75    }
76
77  }
78
79
80  Kernel::~Kernel()
81  {
82    if (ref_count_)
83      if (!--(*ref_count_))
84        delete data_;
85
86    if (ref_count_w_)
87      if (!--(*ref_count_w_))
88        delete data_w_;
89
90  }
91
92 
93  const DataLookup2D& Kernel::data(void) const
94  {
95    return *data_;
96  }
97
98
99  double Kernel::element(const DataLookup1D& vec, const size_t i) const
100  {
101    if (data_w_)
102      return kf_->operator()(vec, DataLookupWeighted1D(*data_w_,i, false)); 
103    else
104      return kf_->operator()(vec, DataLookup1D(*data_,i, false)); 
105  }
106
107
108  double Kernel::element(const DataLookupWeighted1D& vec, const size_t i) const
109  {
110    if (data_w_)
111      return kf_->operator()(vec, DataLookupWeighted1D(*data_w_,i, false)); 
112    else
113      return kf_->operator()(vec, DataLookup1D(*data_,i, false)); 
114  }
115
116
117  size_t Kernel::size(void) const
118  {
119    return data_->columns();
120  }
121
122
123  bool Kernel::weighted(void) const
124  {
125    return data_w_;
126  }
127
128}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.