Line data Source code
1 : /**
2 : * @file gensvm_simplex.c
3 : * @author G.J.J. van den Burg
4 : * @date 2016-05-01
5 : * @brief Function for generating the simplex matrix
6 : *
7 : * @details
8 : * Contains the function for generating the simplex matrix for a given number
9 : * of classes.
10 : *
11 : * @copyright
12 : Copyright 2016, G.J.J. van den Burg.
13 :
14 : This file is part of GenSVM.
15 :
16 : GenSVM is free software: you can redistribute it and/or modify
17 : it under the terms of the GNU General Public License as published by
18 : the Free Software Foundation, either version 3 of the License, or
19 : (at your option) any later version.
20 :
21 : GenSVM is distributed in the hope that it will be useful,
22 : but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 : GNU General Public License for more details.
25 :
26 : You should have received a copy of the GNU General Public License
27 : along with GenSVM. If not, see <http://www.gnu.org/licenses/>.
28 :
29 : */
30 :
31 : #include "gensvm_simplex.h"
32 :
33 : /**
34 : * @brief Generate matrix of simplex vertex coordinates
35 : *
36 : * @details
37 : * Generate the simplex matrix. Each row of the created matrix contains the
38 : * coordinate vector of a single vertex of the K-simplex in K-1 dimensions.
39 : * The simplex generated is a special simplex with edges of length 1. The
40 : * simplex matrix U of the GenModel must already have been allocated.
41 : *
42 : * @param[in,out] model a GenModel structure
43 : */
44 13 : void gensvm_simplex(struct GenModel *model)
45 : {
46 13 : long i, j, K = model->K;
47 :
48 56 : for (i=0; i<K; i++) {
49 147 : for (j=0; j<K-1; j++) {
50 104 : if (i <= j) {
51 52 : matrix_set(model->U, K-1, i, j,
52 : -1.0/sqrt(2.0*(j+1)*(j+2)));
53 52 : } else if (i == j+1) {
54 30 : matrix_set(model->U, K-1, i, j,
55 : sqrt((j+1)/(2.0*(j+2))));
56 : } else {
57 22 : matrix_set(model->U, K-1, i, j, 0.0);
58 : }
59 : }
60 : }
61 13 : }
62 :
63 : /**
64 : * @brief Generate the simplex difference matrix
65 : *
66 : * @details
67 : * The simplex difference matrix is a 2D block matrix which is constructed
68 : * as follows. For each class i, we have a block of K rows and K-1 columns.
69 : * Each row in the block for class i contains a row vector with the difference
70 : * of the simplex matrix, U(i, :) - U(j, :).
71 : *
72 : * In the paper the notation @f$\boldsymbol{\delta}_{kj}'@f$ is used for the
73 : * difference vector of @f$\textbf{u}_k' - \textbf{u}_j'@f$, where
74 : * @f$\textbf{u}_k'@f$ corresponds to row k of @f$\textbf{U}@f$. Due to the
75 : * indexing in the paper being 1-based and C indexing is 0 based, the vector
76 : * @f$\boldsymbol{\delta}_{kj}'@f$ corresponds to the row (k-1)*K+(j-1) in the
77 : * UU matrix generated here.
78 : *
79 : * @param[in,out] model the corresponding GenModel
80 : *
81 : */
82 9 : void gensvm_simplex_diff(struct GenModel *model)
83 : {
84 9 : long i, j, l, K = model->K;
85 : double value;
86 :
87 : // UU is a 2D block matrix, where block i has the differences:
88 : // U(i, :) - U(j, :) for all j
89 40 : for (i=0; i<K; i++) {
90 140 : for (j=0; j<K; j++) {
91 391 : for (l=0; l<K-1; l++) {
92 282 : value = matrix_get(model->U, K-1, i, l);
93 282 : value -= matrix_get(model->U, K-1, j, l);
94 282 : matrix_set(model->UU, K-1, i*K+j, l, value);
95 : }
96 : }
97 : }
98 9 : }
|