Algorithms_in_C
1.0.0
Set of algorithms implemented in C.
|
|
#define | max(a, b) (((a) > (b)) ? (a) : (b)) |
| shorthand for maximum value
|
|
#define | min(a, b) (((a) < (b)) ? (a) : (b)) |
| shorthand for minimum value
|
|
|
double * | kohonen_data_3d (const struct kohonen_array_3d *arr, int x, int y, int z) |
| Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by: More...
|
|
double | _random (double a, double b) |
| Helper function to generate a random number in a given interval. More...
|
|
int | save_2d_data (const char *fname, double **X, int num_points, int num_features) |
| Save a given n-dimensional data martix to file. More...
|
|
int | save_u_matrix (const char *fname, struct kohonen_array_3d *W) |
| Create the distance matrix or U-matrix from the trained weights and save to disk. More...
|
|
void | get_min_2d (double **X, int N, double *val, int *x_idx, int *y_idx) |
| Get minimum value and index of the value in a matrix. More...
|
|
double | kohonen_update_weights (const double *X, struct kohonen_array_3d *W, double **D, int num_out, int num_features, double alpha, int R) |
| Update weights of the SOM using Kohonen algorithm. More...
|
|
void | kohonen_som (double **X, struct kohonen_array_3d *W, int num_samples, int num_features, int num_out, double alpha_min) |
| Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset. More...
|
|
◆ _random()
double _random |
( |
double |
a, |
|
|
double |
b |
|
) |
| |
Helper function to generate a random number in a given interval.
Steps:
r1 = rand() % 100
gets a random number between 0 and 99
r2 = r1 / 100
converts random number to be between 0 and 0.99
- scale and offset the random number to given range of \([a,b)\)
\[ y = (b - a) \times \frac{\text{(random number between 0 and RAND_MAX)} \; \text{mod}\; 100}{100} + a \]
- Parameters
-
[in] | a | lower limit |
[in] | b | upper limit |
- Returns
- random number in the range \([a,b)\)
89 return ((b - a) * (rand() % 100) / 100.f) + a;
◆ get_min_2d()
void get_min_2d |
( |
double ** |
X, |
|
|
int |
N, |
|
|
double * |
val, |
|
|
int * |
x_idx, |
|
|
int * |
y_idx |
|
) |
| |
Get minimum value and index of the value in a matrix.
- Parameters
-
[in] | X | matrix to search |
[in] | N | number of points in the vector |
[out] | val | minimum value found |
[out] | x_idx | x-index where minimum value was found |
[out] | y_idx | y-index where minimum value was found |
208 for (
int i = 0; i < N; i++)
210 for (
int j = 0; j < N; j++)
212 if (X[i][j] < val[0])
◆ kohonen_data_3d()
double* kohonen_data_3d |
( |
const struct kohonen_array_3d * |
arr, |
|
|
int |
x, |
|
|
int |
y, |
|
|
int |
z |
|
) |
| |
Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by:
\[ X_{i,j,k} = i\times M\times N + j\times N + k \]
where \(L\), \(M\) and \(N\) are the 3D matrix dimensions.
- Parameters
-
[in] | arr | pointer to kohonen_array_3d structure |
[in] | x | first index |
[in] | y | second index |
[in] | z | third index |
- Returns
- pointer to (x,y,z)^th location of data
69 int offset = (x * arr->
dim2 * arr->
dim3) + (y * arr->
dim3) + z;
70 return arr->
data + offset;
◆ kohonen_som()
void kohonen_som |
( |
double ** |
X, |
|
|
struct kohonen_array_3d * |
W, |
|
|
int |
num_samples, |
|
|
int |
num_features, |
|
|
int |
num_out, |
|
|
double |
alpha_min |
|
) |
| |
Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset.
- Parameters
-
[in] | X | data set |
[in,out] | W | weights matrix |
[in] | num_samples | number of output points |
[in] | num_features | number of features per input sample |
[in] | num_out | number of output points |
[in] | alpha_min | terminal value of alpha |
317 int R = num_out >> 2, iter = 0;
318 double **D = (
double **)malloc(num_out *
sizeof(
double *));
319 for (
int i = 0; i < num_out; i++)
320 D[i] = (
double *)malloc(num_out *
sizeof(
double));
325 for (
double alpha = 1.f; alpha > alpha_min && dmin > 1e-3;
326 alpha -= 0.001, iter++)
330 for (
int sample = 0; sample < num_samples; sample++)
334 num_features, alpha, R);
338 if (iter % 100 == 0 && R > 1)
342 printf(
"iter: %5d\t alpha: %.4g\t R: %d\td_min: %.4g\r", iter, alpha, R,
347 for (
int i = 0; i < num_out; i++) free(D[i]);
◆ kohonen_update_weights()
double kohonen_update_weights |
( |
const double * |
X, |
|
|
struct kohonen_array_3d * |
W, |
|
|
double ** |
D, |
|
|
int |
num_out, |
|
|
int |
num_features, |
|
|
double |
alpha, |
|
|
int |
R |
|
) |
| |
Update weights of the SOM using Kohonen algorithm.
- Parameters
-
[in] | X | data point |
[in,out] | W | weights matrix |
[in,out] | D | temporary vector to store distances |
[in] | num_out | number of output points |
[in] | num_features | number of features per input sample |
[in] | alpha | learning rate \(0<\alpha\le1\) |
[in] | R | neighborhood range |
- Returns
- minimum distance of sample and trained weights
245 for (x = 0; x < num_out; x++)
247 for (y = 0; y < num_out; y++)
252 for (k = 0; k < num_features; k++)
255 D[x][y] += (w[0] - X[k]) * (w[0] - X[k]);
257 D[x][y] = sqrt(D[x][y]);
263 int d_min_x, d_min_y;
264 get_min_2d(D, num_out, &d_min, &d_min_x, &d_min_y);
267 int from_x =
max(0, d_min_x - R);
268 int to_x =
min(num_out, d_min_x + R + 1);
269 int from_y =
max(0, d_min_y - R);
270 int to_y =
min(num_out, d_min_y + R + 1);
277 for (x = from_x; x < to_x; x++)
279 for (y = from_y; y < to_y; y++)
289 (d_min_x - x) * (d_min_x - x) + (d_min_y - y) * (d_min_y - y);
290 double scale_factor = exp(-d2 / (2.f * alpha * alpha));
292 for (k = 0; k < num_features; k++)
296 w[0] += alpha * scale_factor * (X[k] - w[0]);
◆ save_2d_data()
int save_2d_data |
( |
const char * |
fname, |
|
|
double ** |
X, |
|
|
int |
num_points, |
|
|
int |
num_features |
|
) |
| |
Save a given n-dimensional data martix to file.
- Parameters
-
[in] | fname | filename to save in (gets overwritten without confirmation) |
[in] | X | matrix to save |
[in] | num_points | rows in the matrix = number of points |
[in] | num_features | columns in the matrix = dimensions of points |
- Returns
- 0 if all ok
-
-1 if file creation failed
105 FILE *fp = fopen(fname,
"wt");
109 sprintf(msg,
"File error (%s): ", fname);
114 for (
int i = 0; i < num_points; i++)
116 for (
int j = 0; j < num_features; j++)
118 fprintf(fp,
"%.4g", X[i][j]);
119 if (j < num_features - 1)
122 if (i < num_points - 1)
◆ save_u_matrix()
Create the distance matrix or U-matrix from the trained weights and save to disk.
- Parameters
-
[in] | fname | filename to save in (gets overwriten without confirmation) |
[in] | W | model matrix to save |
- Returns
- 0 if all ok
-
-1 if file creation failed
141 FILE *fp = fopen(fname,
"wt");
145 sprintf(msg,
"File error (%s): ", fname);
152 for (
int i = 0; i < W->
dim1; i++)
154 for (
int j = 0; j < W->
dim2; j++)
156 double distance = 0.f;
159 int from_x =
max(0, i - R);
160 int to_x =
min(W->
dim1, i + R + 1);
161 int from_y =
max(0, j - R);
162 int to_y =
min(W->
dim2, j + R + 1);
165 #pragma omp parallel for reduction(+ : distance)
167 for (l = from_x; l < to_x; l++)
169 for (
int m = from_y; m < to_y; m++)
172 for (k = 0; k < W->
dim3; k++)
176 d += (w1[0] - w2[0]) * (w1[0] - w2[0]);
185 fprintf(fp,
"%.4g", distance);
double * data
pointer to data
Definition: kohonen_som_topology.c:52
int dim2
lengths of second dimension
Definition: kohonen_som_topology.c:50
int dim3
lengths of thirddimension
Definition: kohonen_som_topology.c:51
void get_min_2d(double **X, int N, double *val, int *x_idx, int *y_idx)
Get minimum value and index of the value in a matrix.
Definition: kohonen_som_topology.c:204
#define min(a, b)
shorthand for minimum value
Definition: kohonen_som_topology.c:43
#define max(a, b)
shorthand for maximum value
Definition: kohonen_som_topology.c:39
int dim1
lengths of first dimension
Definition: kohonen_som_topology.c:49
double kohonen_update_weights(const double *X, struct kohonen_array_3d *W, double **D, int num_out, int num_features, double alpha, int R)
Update weights of the SOM using Kohonen algorithm.
Definition: kohonen_som_topology.c:234
double * kohonen_data_3d(const struct kohonen_array_3d *arr, int x, int y, int z)
Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by:
Definition: kohonen_som_topology.c:67