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

Last change on this file since 831 was 831, checked in by Peter, 14 years ago

Refs #185.

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