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

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

fixes #281. Change all throws of std::runtime_error to theplu::yat::utility::runtime_error to clarify that the error comes from yat. Also removed some throw declarations.

  • 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 Jari Häkkinen, 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Kernel.h"
24#include "DataLookup1D.h"
25#include "DataLookupWeighted1D.h"
26#include "KernelFunction.h"
27#include "MatrixLookup.h"
28#include "MatrixLookupWeighted.h"
29
30#include "yat/utility/Exception.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 utility::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 utility::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.