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

Last change on this file since 1437 was 1437, checked in by Peter, 13 years ago

merge patch release 0.4.2 to trunk. Delta 0.4.2-0.4.1

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