2
/* VER: $Id: MathFunctions.cpp,v 1.3 2005/08/05 09:02:58 berniw Exp $ */
3
// copyright (c) 2004 by Christos Dimitrakakis <dimitrak@idiap.ch>
4
/***************************************************************************
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
***************************************************************************/
12
#include "learning/MathFunctions.h"
17
\file MathFunctions.cpp
19
\brief Mathematical functions.
24
int ArgMin (int n, real* x)
28
for (int i=1; i<n; i++) {
36
int ArgMax (int n, real* x)
40
for (int i=1; i<n; i++) {
49
void SoftMax (int n, real* Q, real* p, real beta)
54
p[i] = (real) exp (beta * Q[i]);
63
void SoftMin (int n, real* Q, real* p, real beta)
68
p[i] = (real) exp (-beta * Q[i]);
79
\brief Approximate max (f1,f2) via a gamma function.
83
f(x) = \max \{f_1(x), f_2(x)\}
84
= f_1(x) + \max \{0, f_2(x) - f_1(x)\}.
86
We can then approximate the second term with
88
\max \{0, f_2(x) - f_1(x)\} = \lim_{c \rightarrow \infty} \gamma(f_2(x)-f_1(x), \lambda, c),
90
where \f$\lambda \in [0,1], \quad c>0\f$. The function has the form
94
t - \frac{(1-\lambda)^2}{2c} & \textrm{if}~ \frac{1-\lambda}{c} \leq t,\\
95
\lambda t - \frac{c}{2} t^2 & \textrm{if}~ -\frac{\lambda}{c} \leq t \leq \frac{1-\lambda}{c},\\
96
-\frac{\lambda^2}{2c} & \text{if}~ t \leq -\frac{\lambda}{c}.
100
The error in the positive region is \f$(1-\lambda)^2/2c\f$ and in
101
the negative region it is \f$\lambda^2/2c\f$. The transition region
104
The advantage of this function is that it is continuously
105
differentiable. Its derivative is
107
\frac{\partial \gamma(t,\lambda,c)}{\partial t} =
109
1 & \textrm{if}~ \frac{1-\lambda}{c} \leq t,\\
110
\lambda - c t & \textrm{if}~ -\frac{\lambda}{c} \leq t \leq \frac{1-\lambda}{c},\\
111
0 & \text{if}~ t \leq -\frac{\lambda}{c}.
117
real SmoothMaxGamma (real f1, real f2, real lambda, real c)
123
assert(lambda>=0 && lambda<=1);
125
if (1-lambda/c <= t) {
126
gamma = t - (1-lambda)*(1-lambda) / (2*c);
127
} else if (t>=-lambda/c) {
128
gamma = lambda * t + t*t*c/2;
130
gamma = - lambda * lambda / (2*c);
137
\brief Approximate max (f1,f2) via a power function.
141
f(x) = \max \{f_1(x), f_2(x)\} = \lim_{p \rightarrow \infty} (f_1(x)^p, f_2(x)^p)^{1/p}.
144
The advantage of this function is that it is
145
differentiable. Unlike SmoothMaxGamma, the error is not fixed, but it is proportional to the ratio between.
147
real SmoothMaxPNorm (real f1, real f2, real p)
150
return (real) pow(pow(f1,p)+pow(f2,p),1/p);
153
/// Normalise a vector to a destination vector (low level)
155
/// src is the source vector.
156
/// dst is the destination vector.
157
/// n_elements is the number of elements.
158
/// As pointers are raw, make sure n_elements is correct.
159
/// It is safe for src and dst to point at the same vector.
160
void Normalise (real* src, real* dst, int n_elements)
164
for (i=0; i<n_elements; i++) {
168
for (i=0; i<n_elements; i++) {
174
for (i=0; i<n_elements; i++) {
179
real SquareNorm (real* a, real* b, int n)
182
for (int i=0; i<n; i++) {
183
register real d = (*a++) - (*b++);
189
real EuclideanNorm (real* a, real* b, int n)
192
for (int i=0; i<n; i++) {
193
register real d = (*a++) - (*b++);
196
return (real) sqrt(sum);
199
real LNorm (real* a, real* b, int n, real p)
202
for (int i=0; i<n; i++) {
203
register real d = (*a++) - (*b++);
204
sum += (real) pow(d,p);
206
return (real) pow(sum,1.0/p);
209
real Sum (real* a, int n)
212
for (register int i=0; i<n; i++) {