yat/classifier/Kernel.cc

Code
Comments
Other
Rev Date Author Line
549 07 Mar 06 peter 1 // $Id$
549 07 Mar 06 peter 2
675 10 Oct 06 jari 3 /*
4359 23 Aug 23 peter 4   Copyright (C) 2006 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 5   Copyright (C) 2007 Peter Johansson
2119 12 Dec 09 peter 6   Copyright (C) 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
4359 23 Aug 23 peter 7   Copyright (C) 2010, 2012 Peter Johansson
549 07 Mar 06 peter 8
1437 25 Aug 08 peter 9   This file is part of the yat library, http://dev.thep.lu.se/yat
549 07 Mar 06 peter 10
675 10 Oct 06 jari 11   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 12   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 13   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 14   License, or (at your option) any later version.
675 10 Oct 06 jari 15
675 10 Oct 06 jari 16   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 10 Oct 06 jari 19   General Public License for more details.
675 10 Oct 06 jari 20
675 10 Oct 06 jari 21   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 22   along with yat. If not, see <http://www.gnu.org/licenses/>.
675 10 Oct 06 jari 23 */
675 10 Oct 06 jari 24
2881 18 Nov 12 peter 25 #include <config.h>
2881 18 Nov 12 peter 26
680 11 Oct 06 jari 27 #include "Kernel.h"
680 11 Oct 06 jari 28 #include "DataLookup1D.h"
680 11 Oct 06 jari 29 #include "DataLookupWeighted1D.h"
680 11 Oct 06 jari 30 #include "KernelFunction.h"
680 11 Oct 06 jari 31 #include "MatrixLookup.h"
747 11 Feb 07 peter 32 #include "MatrixLookupWeighted.h"
675 10 Oct 06 jari 33
2210 05 Mar 10 peter 34 #include "yat/utility/Exception.h"
2210 05 Mar 10 peter 35
1163 26 Feb 08 peter 36 #include <cassert>
549 07 Mar 06 peter 37 #include <vector>
549 07 Mar 06 peter 38
549 07 Mar 06 peter 39 namespace theplu {
680 11 Oct 06 jari 40 namespace yat {
549 07 Mar 06 peter 41 namespace classifier {
549 07 Mar 06 peter 42
4200 19 Aug 22 peter 43   Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf,
4200 19 Aug 22 peter 44                  const bool own)
1163 26 Feb 08 peter 45     : ml_(&data), mlw_(0), kf_(&kf), ref_count_w_(NULL)
549 07 Mar 06 peter 46   {
659 26 Sep 06 peter 47     if (own)
1271 09 Apr 08 peter 48       ref_count_ = new unsigned int(1);
659 26 Sep 06 peter 49     else
659 26 Sep 06 peter 50       ref_count_ = NULL;
549 07 Mar 06 peter 51   }
549 07 Mar 06 peter 52
549 07 Mar 06 peter 53
658 25 Sep 06 peter 54   Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf,
4200 19 Aug 22 peter 55                  const bool own)
1163 26 Feb 08 peter 56     : ml_(NULL), mlw_(&data), kf_(&kf)
549 07 Mar 06 peter 57   {
659 26 Sep 06 peter 58     if (own){
1271 09 Apr 08 peter 59       ref_count_w_ = new unsigned int(1);
659 26 Sep 06 peter 60     }
659 26 Sep 06 peter 61     else {
659 26 Sep 06 peter 62       ref_count_w_ = NULL;
659 26 Sep 06 peter 63     }
1163 26 Feb 08 peter 64     ref_count_ = NULL;
549 07 Mar 06 peter 65   }
549 07 Mar 06 peter 66
549 07 Mar 06 peter 67
549 07 Mar 06 peter 68   Kernel::Kernel(const Kernel& other, const std::vector<size_t>& index)
659 26 Sep 06 peter 69     : kf_(other.kf_)
549 07 Mar 06 peter 70   {
4200 19 Aug 22 peter 71
1163 26 Feb 08 peter 72     if (other.weighted()){
2223 19 Mar 10 peter 73       mlw_ = new MatrixLookupWeighted(*other.mlw_, utility::Index(index),
2223 19 Mar 10 peter 74                                       utility::Index(other.mlw_->columns()));
1271 09 Apr 08 peter 75       ref_count_w_ = new unsigned int(1);
1163 26 Feb 08 peter 76       ml_=NULL;
1163 26 Feb 08 peter 77       ref_count_ = NULL;
628 05 Sep 06 peter 78     }
628 05 Sep 06 peter 79     else{
1168 26 Feb 08 peter 80       ml_ = new MatrixLookup(*other.ml_, utility::Index(index),true);
1271 09 Apr 08 peter 81       ref_count_ = new unsigned int(1);
1163 26 Feb 08 peter 82       mlw_=NULL;
659 26 Sep 06 peter 83       ref_count_w_ = NULL;
628 05 Sep 06 peter 84     }
628 05 Sep 06 peter 85
549 07 Mar 06 peter 86   }
549 07 Mar 06 peter 87
720 26 Dec 06 jari 88
549 07 Mar 06 peter 89   Kernel::~Kernel()
549 07 Mar 06 peter 90   {
659 26 Sep 06 peter 91     if (ref_count_)
659 26 Sep 06 peter 92       if (!--(*ref_count_))
1163 26 Feb 08 peter 93         delete ml_;
659 26 Sep 06 peter 94
659 26 Sep 06 peter 95     if (ref_count_w_)
659 26 Sep 06 peter 96       if (!--(*ref_count_w_))
1163 26 Feb 08 peter 97         delete mlw_;
659 26 Sep 06 peter 98
549 07 Mar 06 peter 99   }
549 07 Mar 06 peter 100
4200 19 Aug 22 peter 101
1165 26 Feb 08 peter 102   const MatrixLookup& Kernel::data(void) const
720 26 Dec 06 jari 103   {
1163 26 Feb 08 peter 104     if (weighted())
2210 05 Mar 10 peter 105       throw utility::runtime_error("Kernel::data when Kernel is weighted");
1165 26 Feb 08 peter 106     assert(ml_);
1163 26 Feb 08 peter 107     return *ml_;
720 26 Dec 06 jari 108   }
720 26 Dec 06 jari 109
720 26 Dec 06 jari 110
1165 26 Feb 08 peter 111   const MatrixLookupWeighted& Kernel::data_weighted(void) const
1165 26 Feb 08 peter 112   {
1165 26 Feb 08 peter 113     if (!weighted())
2210 05 Mar 10 peter 114       throw utility::runtime_error("Kernel:data_weighted when Kernel is unweighted");
1165 26 Feb 08 peter 115     assert(mlw_);
1165 26 Feb 08 peter 116     return *mlw_;
1165 26 Feb 08 peter 117   }
1165 26 Feb 08 peter 118
1165 26 Feb 08 peter 119
628 05 Sep 06 peter 120   double Kernel::element(const DataLookup1D& vec, const size_t i) const
628 05 Sep 06 peter 121   {
1163 26 Feb 08 peter 122     if (weighted())
4200 19 Aug 22 peter 123       return kf_->operator()(vec, DataLookupWeighted1D(*mlw_,i, false));
628 05 Sep 06 peter 124     else
4200 19 Aug 22 peter 125       return kf_->operator()(vec,DataLookup1D(*ml_,i, false));
628 05 Sep 06 peter 126   }
628 05 Sep 06 peter 127
628 05 Sep 06 peter 128
628 05 Sep 06 peter 129   double Kernel::element(const DataLookupWeighted1D& vec, const size_t i) const
628 05 Sep 06 peter 130   {
1163 26 Feb 08 peter 131     if (weighted())
4200 19 Aug 22 peter 132       return kf_->operator()(vec, DataLookupWeighted1D(*mlw_,i, false));
628 05 Sep 06 peter 133     else
4200 19 Aug 22 peter 134       return kf_->operator()(vec, DataLookup1D(*ml_,i, false));
628 05 Sep 06 peter 135   }
628 05 Sep 06 peter 136
720 26 Dec 06 jari 137
720 26 Dec 06 jari 138   size_t Kernel::size(void) const
720 26 Dec 06 jari 139   {
1163 26 Feb 08 peter 140     if (weighted())
1163 26 Feb 08 peter 141       return mlw_->columns();
1163 26 Feb 08 peter 142     assert(ml_);
1163 26 Feb 08 peter 143     return ml_->columns();
720 26 Dec 06 jari 144   }
720 26 Dec 06 jari 145
720 26 Dec 06 jari 146
720 26 Dec 06 jari 147   bool Kernel::weighted(void) const
720 26 Dec 06 jari 148   {
1163 26 Feb 08 peter 149     return mlw_;
720 26 Dec 06 jari 150   }
720 26 Dec 06 jari 151
680 11 Oct 06 jari 152 }}} // of namespace classifier, yat, and theplu