Logo ROOT   6.08/07
Reference Guide
SVKernelMatrix.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andrzej Zemla
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : SVKernelMatrix *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation *
12  * *
13  * Authors (alphabetical): *
14  * Marcin Wolter <Marcin.Wolter@cern.ch> - IFJ PAN, Krakow, Poland *
15  * Andrzej Zemla <azemla@cern.ch> - IFJ PAN, Krakow, Poland *
16  * (IFJ PAN: Henryk Niewodniczanski Inst. Nucl. Physics, Krakow, Poland) *
17  * *
18  * Minor modification to improve optimisation of kernel values: *
19  * Adrian Bevan <adrian.bevan@cern.ch> - Queen Mary *
20  * University of London, UK *
21  * Tom Stevenson <thomas.james.stevenson@cern.ch> - Queen Mary *
22  * University of London, UK *
23  * *
24  * Copyright (c) 2005: *
25  * CERN, Switzerland *
26  * MPI-K Heidelberg, Germany *
27  * PAN, Krakow, Poland *
28  * *
29  * Redistribution and use in source and binary forms, with or without *
30  * modification, are permitted according to the terms listed in LICENSE *
31  * (http://tmva.sourceforge.net/LICENSE) *
32  **********************************************************************************/
33 
34 #include "TMVA/SVKernelMatrix.h"
35 
36 #include "TMVA/MsgLogger.h"
37 #include "TMVA/SVEvent.h"
38 #include "TMVA/SVKernelFunction.h"
39 #include "TMVA/Types.h"
40 
41 #include "RtypesCore.h"
42 
43 #include <iostream>
44 #include <stdexcept>
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// constructor
48 
50  : fSize(0),
51  fKernelFunction(0),
52  fSVKernelMatrix(0),
53  fLogger( new MsgLogger("ResultsRegression", kINFO) )
54 {
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// constructor
59 
60 TMVA::SVKernelMatrix::SVKernelMatrix( std::vector<TMVA::SVEvent*>* inputVectors, SVKernelFunction* kernelFunction )
61  : fSize(inputVectors->size()),
62  fKernelFunction(kernelFunction),
63  fLogger( new MsgLogger("SVKernelMatrix", kINFO) )
64 {
66  try{
67  for (UInt_t i = 0; i < fSize; i++) fSVKernelMatrix[i] = new Float_t[i+1];
68  }catch(...){
69  Log() << kFATAL << "Input data too large. Not enough memory to allocate memory for Support Vector Kernel Matrix. Please reduce the number of input events or use a different method."<<Endl;
70  }
71  // We compute the diagonal and one half of the off diagonal. When reading back we use
72  // the symmetry of i,j to j,i to ensure the correct values are returned.
73  for (UInt_t i = 0; i < fSize; i++) {
74  for (UInt_t j = 0; j <=i; j++) {
75  fSVKernelMatrix[i][j] = fKernelFunction->Evaluate((*inputVectors)[i], (*inputVectors)[j]);
76  }
77  }
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// destructor
82 
84 {
85  for (UInt_t i = fSize -1; i > 0; i--) {
86  delete[] fSVKernelMatrix[i];
87  fSVKernelMatrix[i] = 0;
88  }
89  delete[] fSVKernelMatrix;
90  fSVKernelMatrix = 0;
91 }
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// returns a row of the kernel matrix
95 
97 {
98  Float_t* fLine = NULL;
99  if (line >= fSize) {
100  return NULL;
101  }
102  else {
103  fLine = new Float_t[fSize];
104  for( UInt_t i = 0; i <line; i++)
105  fLine[i] = fSVKernelMatrix[line][i];
106  for( UInt_t i = line; i < fSize; i++)
107  fLine[i] = fSVKernelMatrix[i][line];
108  return fLine;
109  }
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// returns an element of the kernel matrix
114 
116 {
117  if (i > j) return fSVKernelMatrix[i][j];
118  else return fSVKernelMatrix[j][i]; // it's symmetric, ;)
119 }
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
Float_t * GetLine(UInt_t)
returns a row of the kernel matrix
TLine * line
float Float_t
Definition: RtypesCore.h:53
SVKernelFunction * fKernelFunction
Float_t ** fSVKernelMatrix
MsgLogger & Log() const
message logger
Float_t GetElement(UInt_t i, UInt_t j)
returns an element of the kernel matrix
~SVKernelMatrix()
destructor
unsigned int UInt_t
Definition: RtypesCore.h:42
SVKernelMatrix()
constructor
#define NULL
Definition: Rtypes.h:82
Float_t Evaluate(SVEvent *ev1, SVEvent *ev2)