e9cabf45e4808ab6914b3bbe07dc4d0cafe6fca0
[svrt.git] / misc.h
1 /*
2  *  svrt is the ``Synthetic Visual Reasoning Test'', an image
3  *  generator for evaluating classification performance of machine
4  *  learning systems, humans and primates.
5  *
6  *  Copyright (c) 2009 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of svrt.
10  *
11  *  svrt is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  svrt is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with selector.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #ifndef MISC_H
26 #define MISC_H
27
28 #include <iostream>
29 #include <cmath>
30 #include <fstream>
31 #include <cfloat>
32 #include <stdlib.h>
33 #include <string.h>
34
35 using namespace std;
36
37 typedef double scalar_t;
38 // typedef float scalar_t;
39
40 const int buffer_size = 1024;
41
42 using namespace std;
43
44 #ifdef DEBUG
45 #define ASSERT(x) if(!(x)) { \
46   std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
47   abort(); \
48 }
49 #else
50 #define ASSERT(x)
51 #endif
52
53 template<class T>
54 T **allocate_array(int a, int b) {
55   T *tmp = new T[a * b];
56   T **array = new T *[a];
57   for(int k = 0; k < a; k++) {
58     array[k] = tmp;
59     tmp += b;
60   }
61   return array;
62 }
63
64 template<class T>
65 void deallocate_array(T **array) {
66   delete[] array[0];
67   delete[] array;
68 }
69
70 template<class T>
71 T smooth_min(T x, T y) {
72   T z = exp(x - y);
73   return 0.5 * (x + y - (x - y)/(1 + 1/z) - (y - x)/(1 + z));
74 }
75
76 template <class T>
77 void write_var(ostream *os, const T *x) { os->write((char *) x, sizeof(T)); }
78
79 template <class T>
80 void read_var(istream *is, T *x) { is->read((char *) x, sizeof(T)); }
81
82 template <class T>
83 void grow(int *nb_max, int nb, T** current, int factor) {
84   ASSERT(*nb_max > 0);
85   if(nb == *nb_max) {
86     T *tmp = new T[*nb_max * factor];
87     memcpy(tmp, *current, *nb_max * sizeof(T));
88     delete[] *current;
89     *current = tmp;
90     *nb_max *= factor;
91   }
92 }
93
94 template <class T>
95 inline T sq(T x) {
96   return x * x;
97 }
98
99 inline scalar_t log2(scalar_t x) {
100   return log(x)/log(2.0);
101 }
102
103 inline scalar_t xi(scalar_t x) {
104   if(x <= 0.0) return 0.0;
105   else         return - x * log(x)/log(2.0);
106 }
107
108 scalar_t discrete_entropy(int *n, int nb);
109
110 char *next_word(char *buffer, char *r, int buffer_size);
111
112 void random_permutation(int *val, int nb);
113 void tag_subset(bool *val, int nb_total, int nb_to_tag);
114
115 struct Couple {
116   int index;
117   scalar_t value;
118 };
119
120 int compare_couple(const void *a, const void *b);
121
122 #endif