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

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

Addresses #153. Introduced yat namespace. Removed alignment namespace. Clean up of code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 2.5 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  Kernel::~Kernel()
80  {
81    if (ref_count_)
82      if (!--(*ref_count_))
83        delete data_;
84
85    if (ref_count_w_)
86      if (!--(*ref_count_w_))
87        delete data_w_;
88
89  }
90
91 
92  double Kernel::element(const DataLookup1D& vec, const size_t i) const
93  {
94    if (data_w_)
95      return kf_->operator()(vec, DataLookupWeighted1D(*data_w_,i, false)); 
96    else
97      return kf_->operator()(vec, DataLookup1D(*data_,i, false)); 
98  }
99
100
101  double Kernel::element(const DataLookupWeighted1D& vec, const size_t i) const
102  {
103    if (data_w_)
104      return kf_->operator()(vec, DataLookupWeighted1D(*data_w_,i, false)); 
105    else
106      return kf_->operator()(vec, DataLookup1D(*data_,i, false)); 
107  }
108
109}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.