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

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

Addresses #436.

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