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

Last change on this file since 1580 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • 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 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 <cassert>
31#include <vector>
32
33namespace theplu {
34namespace yat {
35namespace classifier {
36
37  Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf, 
38                 const bool own) 
39    : ml_(&data), mlw_(0), kf_(&kf), ref_count_w_(NULL)
40  {
41    if (own)
42      ref_count_ = new unsigned int(1);
43    else
44      ref_count_ = NULL;
45  }
46
47
48  Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf,
49                 const bool own) 
50    : ml_(NULL), mlw_(&data), kf_(&kf)
51  {
52    if (own){
53      ref_count_w_ = new unsigned int(1);
54    }
55    else {
56      ref_count_w_ = NULL;
57    }
58    ref_count_ = NULL;
59  }
60
61
62  Kernel::Kernel(const Kernel& other, const std::vector<size_t>& index)
63    : kf_(other.kf_)
64  {
65   
66    if (other.weighted()){
67      mlw_ = new MatrixLookupWeighted(*other.mlw_, utility::Index(index),true);
68      ref_count_w_ = new unsigned int(1);
69      ml_=NULL;
70      ref_count_ = NULL;
71    }
72    else{
73      ml_ = new MatrixLookup(*other.ml_, utility::Index(index),true);
74      ref_count_ = new unsigned int(1);
75      mlw_=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 ml_;
87
88    if (ref_count_w_)
89      if (!--(*ref_count_w_))
90        delete mlw_;
91
92  }
93
94 
95  const MatrixLookup& Kernel::data(void) const
96  {
97    if (weighted())
98      throw std::runtime_error("Kernel::data when Kernel is weighted");
99    assert(ml_);
100    return *ml_;
101  }
102
103
104  const MatrixLookupWeighted& Kernel::data_weighted(void) const
105  {
106    if (!weighted())
107      throw std::runtime_error("Kernel:data_weighted when Kernel is unweighted");
108    assert(mlw_);
109    return *mlw_;
110  }
111
112
113  double Kernel::element(const DataLookup1D& vec, const size_t i) const
114  {
115    if (weighted())
116      return kf_->operator()(vec, DataLookupWeighted1D(*mlw_,i, false)); 
117    else
118      return kf_->operator()(vec,DataLookup1D(*ml_,i, false)); 
119  }
120
121
122  double Kernel::element(const DataLookupWeighted1D& vec, const size_t i) const
123  {
124    if (weighted())
125      return kf_->operator()(vec, DataLookupWeighted1D(*mlw_,i, false)); 
126    else
127      return kf_->operator()(vec, DataLookup1D(*ml_,i, false)); 
128  }
129
130
131  size_t Kernel::size(void) const
132  {
133    if (weighted())
134      return mlw_->columns();
135    assert(ml_);
136    return ml_->columns();
137  }
138
139
140  bool Kernel::weighted(void) const
141  {
142    return mlw_;
143  }
144
145}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.