aboutsummaryrefslogtreecommitdiff
path: root/src/gensvm_simplex.c
blob: c209e5908bc09dd20aaebcd465647c67894ddd41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
 * @file gensvm_simplex.c
 * @author G.J.J. van den Burg
 * @date 2016-05-01
 * @brief Function for generating the simplex matrix
 *
 * @details
 * Contains the function for generating the simplex matrix for a given number
 * of classes.
 *
 * @copyright
 Copyright 2016, G.J.J. van den Burg.

 This file is part of GenSVM.

 GenSVM is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 GenSVM is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GenSVM. If not, see <http://www.gnu.org/licenses/>.

 */

#include "gensvm_simplex.h"

/**
 * @brief Generate matrix of simplex vertex coordinates
 *
 * @details
 * Generate the simplex matrix. Each row of the created matrix contains the 
 * coordinate vector of a single vertex of the K-simplex in K-1 dimensions.  
 * The simplex generated is a special simplex with edges of length 1. The 
 * simplex matrix U of the GenModel must already have been allocated.
 *
 * @param[in,out] 	model 	a GenModel structure
 */
void gensvm_simplex(struct GenModel *model)
{
	long i, j, K = model->K;

	for (i=0; i<K; i++) {
		for (j=0; j<K-1; j++) {
			if (i <= j) {
				matrix_set(model->U, K-1, i, j,
					       	-1.0/sqrt(2.0*(j+1)*(j+2)));
			} else if (i == j+1) {
				matrix_set(model->U, K-1, i, j,
					       	sqrt((j+1)/(2.0*(j+2))));
			} else {
				matrix_set(model->U, K-1, i, j, 0.0);
			}
		}
	}
}

/**
 * @brief Generate the simplex difference matrix
 *
 * @details
 * The simplex difference matrix is a 2D block matrix which is constructed
 * as follows. For each class i, we have a block of K rows and K-1 columns.
 * Each row in the block for class i contains a row vector with the difference
 * of the simplex matrix, U(i, :) - U(j, :).
 *
 * In the paper the notation @f$\boldsymbol{\delta}_{kj}'@f$ is used for the
 * difference vector of @f$\textbf{u}_k' - \textbf{u}_j'@f$, where
 * @f$\textbf{u}_k'@f$ corresponds to row k of @f$\textbf{U}@f$. Due to the
 * indexing in the paper being 1-based and C indexing is 0 based, the vector
 * @f$\boldsymbol{\delta}_{kj}'@f$ corresponds to the row (k-1)*K+(j-1) in the
 * UU matrix generated here.
 *
 * @param[in,out] 	model 	the corresponding GenModel
 *
 */
void gensvm_simplex_diff(struct GenModel *model)
{
	long i, j, l, K = model->K;
	double value;

	// UU is a 2D block matrix, where block i has the differences:
	// U(i, :) - U(j, :) for all j
	for (i=0; i<K; i++) {
		for (j=0; j<K; j++) {
			for (l=0; l<K-1; l++) {
				value = matrix_get(model->U, K-1, i, l);
				value -= matrix_get(model->U, K-1, j, l);
				matrix_set(model->UU, K-1, i*K+j, l, value);
			}
		}
	}
}