Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
LinAlg
BlockMatrix2x2.h
Go to the documentation of this file.
1
//===========================================================================
2
/*!
3
*
4
*
5
* \brief Kernel matrix for SVM regression.
6
*
7
*
8
* \par
9
*
10
*
11
*
12
* \author T. Glasmachers
13
* \date 2007-2012
14
*
15
*
16
* \par Copyright 1995-2017 Shark Development Team
17
*
18
* <BR><HR>
19
* This file is part of Shark.
20
* <https://shark-ml.github.io/Shark/>
21
*
22
* Shark is free software: you can redistribute it and/or modify
23
* it under the terms of the GNU Lesser General Public License as published
24
* by the Free Software Foundation, either version 3 of the License, or
25
* (at your option) any later version.
26
*
27
* Shark is distributed in the hope that it will be useful,
28
* but WITHOUT ANY WARRANTY; without even the implied warranty of
29
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
* GNU Lesser General Public License for more details.
31
*
32
* You should have received a copy of the GNU Lesser General Public License
33
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
34
*
35
*/
36
//===========================================================================
37
38
39
#ifndef SHARK_LINALG_BLOCKMATRIX2X2_H
40
#define SHARK_LINALG_BLOCKMATRIX2X2_H
41
42
#include <
shark/Data/Dataset.h
>
43
#include <
shark/LinAlg/Base.h
>
44
45
#include <vector>
46
#include <cmath>
47
48
49
namespace
shark
{
50
51
52
///
53
/// \brief SVM regression matrix
54
///
55
/// \par
56
/// The BlockMatrix2x2 class is a \f$ 2n \times 2n \f$ block matrix of the form<br>
57
/// \f$ \left( \begin{array}{lr} M & M \\ M & M \end{array} \right) \f$ <br>
58
/// where M is an \f$ n \times n \f$ matrix.
59
/// This matrix form is needed in SVM regression problems.
60
///
61
template
<
class
Matrix>
62
class
BlockMatrix2x2
63
{
64
public
:
65
typedef
typename
Matrix::QpFloatType
QpFloatType
;
66
67
/// Constructor.
68
/// \param base underlying matrix M, see class description of BlockMatrix2x2.
69
BlockMatrix2x2
(Matrix* base)
70
{
71
m_base
= base;
72
73
m_mapping
.resize(
size
());
74
75
std::size_t ic =
m_base
->size();
76
for
(std::size_t i = 0; i < ic; i++)
77
{
78
m_mapping
[i] = i;
79
m_mapping
[i + ic] = i;
80
}
81
}
82
83
84
/// return a single matrix entry
85
QpFloatType
operator ()
(std::size_t i, std::size_t j)
const
86
{
return
entry
(i, j); }
87
88
/// return a single matrix entry
89
QpFloatType
entry
(std::size_t i, std::size_t j)
const
90
{
91
return
m_base
->entry(
m_mapping
[i],
m_mapping
[j]);
92
}
93
94
/// \brief Computes the i-th row of the kernel matrix.
95
///
96
///The entries start,...,end of the i-th row are computed and stored in storage.
97
///There must be enough room for this operation preallocated.
98
void
row
(std::size_t i, std::size_t start,std::size_t end,
QpFloatType
* storage)
const
{
99
for
(std::size_t j = start; j < end; j++){
100
storage[j-start] =
m_base
->entry(
m_mapping
[i],
m_mapping
[j]);
101
}
102
}
103
104
/// \brief Computes the kernel-matrix
105
template
<
class
M>
106
void
matrix
(
107
blas::matrix_expression<M, blas::cpu_tag> & storage
108
)
const
{
109
for
(std::size_t i = 0; i !=
size
(); ++i){
110
for
(std::size_t j = 0; j !=
size
(); ++j){
111
storage()(i,j) =
entry
(i,j);
112
}
113
}
114
}
115
116
/// swap two variables
117
void
flipColumnsAndRows
(std::size_t i, std::size_t j)
118
{
119
std::swap(
m_mapping
[i],
m_mapping
[j]);
120
}
121
122
/// return the size of the quadratic matrix
123
std::size_t
size
()
const
124
{
return
2 *
m_base
->size(); }
125
126
protected
:
127
/// underlying KernelMatrix object
128
Matrix*
m_base
;
129
130
/// coordinate permutation
131
std::vector<std::size_t>
m_mapping
;
132
};
133
134
}
135
#endif