source: trunk/yat/classifier/Kernel.cc

Last change on this file was 2919, checked in by Peter, 9 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.3 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
6  Copyright (C) 2010, 2012 Peter Johansson
7
8  This file is part of the yat library, http://dev.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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include <config.h>
25
26#include "Kernel.h"
27#include "DataLookup1D.h"
28#include "DataLookupWeighted1D.h"
29#include "KernelFunction.h"
30#include "MatrixLookup.h"
31#include "MatrixLookupWeighted.h"
32
33#include "yat/utility/Exception.h"
34
35#include <cassert>
36#include <vector>
37
38namespace theplu {
39namespace yat {
40namespace classifier {
41
42  Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf, 
43                 const bool own) 
44    : ml_(&data), mlw_(0), kf_(&kf), ref_count_w_(NULL)
45  {
46    if (own)
47      ref_count_ = new unsigned int(1);
48    else
49      ref_count_ = NULL;
50  }
51
52
53  Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf,
54                 const bool own) 
55    : ml_(NULL), mlw_(&data), kf_(&kf)
56  {
57    if (own){
58      ref_count_w_ = new unsigned int(1);
59    }
60    else {
61      ref_count_w_ = NULL;
62    }
63    ref_count_ = NULL;
64  }
65
66
67  Kernel::Kernel(const Kernel& other, const std::vector<size_t>& index)
68    : kf_(other.kf_)
69  {
70   
71    if (other.weighted()){
72      mlw_ = new MatrixLookupWeighted(*other.mlw_, utility::Index(index),
73                                      utility::Index(other.mlw_->columns()));
74      ref_count_w_ = new unsigned int(1);
75      ml_=NULL;
76      ref_count_ = NULL;
77    }
78    else{
79      ml_ = new MatrixLookup(*other.ml_, utility::Index(index),true);
80      ref_count_ = new unsigned int(1);
81      mlw_=NULL;
82      ref_count_w_ = NULL;
83    }
84
85  }
86
87
88  Kernel::~Kernel()
89  {
90    if (ref_count_)
91      if (!--(*ref_count_))
92        delete ml_;
93
94    if (ref_count_w_)
95      if (!--(*ref_count_w_))
96        delete mlw_;
97
98  }
99
100 
101  const MatrixLookup& Kernel::data(void) const
102  {
103    if (weighted())
104      throw utility::runtime_error("Kernel::data when Kernel is weighted");
105    assert(ml_);
106    return *ml_;
107  }
108
109
110  const MatrixLookupWeighted& Kernel::data_weighted(void) const
111  {
112    if (!weighted())
113      throw utility::runtime_error("Kernel:data_weighted when Kernel is unweighted");
114    assert(mlw_);
115    return *mlw_;
116  }
117
118
119  double Kernel::element(const DataLookup1D& vec, const size_t i) const
120  {
121    if (weighted())
122      return kf_->operator()(vec, DataLookupWeighted1D(*mlw_,i, false)); 
123    else
124      return kf_->operator()(vec,DataLookup1D(*ml_,i, false)); 
125  }
126
127
128  double Kernel::element(const DataLookupWeighted1D& vec, const size_t i) const
129  {
130    if (weighted())
131      return kf_->operator()(vec, DataLookupWeighted1D(*mlw_,i, false)); 
132    else
133      return kf_->operator()(vec, DataLookup1D(*ml_,i, false)); 
134  }
135
136
137  size_t Kernel::size(void) const
138  {
139    if (weighted())
140      return mlw_->columns();
141    assert(ml_);
142    return ml_->columns();
143  }
144
145
146  bool Kernel::weighted(void) const
147  {
148    return mlw_;
149  }
150
151}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.