Debian Codegraph @

src/thirdparty/VLFeat/mathop.h

raw
1 /** @file mathop.h
2 ** @brief Math operations (@ref mathop)
3 ** @author Andrea Vedaldi, David Novotny
4 **/
5
6 /*
7 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
8 All rights reserved.
9
10 This file is part of the VLFeat library and is made available under
11 the terms of the BSD license (see the COPYING file).
12 */
13
14 #ifndef VL_MATHOP_H
15 #define VL_MATHOP_H
16
17 #include "generic.h"
18 #include <math.h>
19 #include <float.h>
20
21 /** @brief Euler constant*/
22 #define VL_E 2.718281828459045
23
24 /** @brief Logarithm of 2 (math constant)*/
25 #define VL_LOG_OF_2 0.693147180559945
26
27 /** @brief Pi (math constant) */
28 #define VL_PI 3.141592653589793
29
30 /** @brief IEEE single precision epsilon (math constant)
31 **
32 ** <code>1.0F + VL_EPSILON_F</code> is the smallest representable
33 ** single precision number greater than @c 1.0F. Numerically,
34 ** ::VL_EPSILON_F is equal to @f$ 2^{-23} @f$.
35 **
36 **/
37 #define VL_EPSILON_F 1.19209290E-07F
38
39 /** @brief IEEE double precision epsilon (math constant)
40 **
41 ** <code>1.0 + VL_EPSILON_D</code> is the smallest representable
42 ** double precision number greater than @c 1.0. Numerically,
43 ** ::VL_EPSILON_D is equal to @f$ 2^{-52} @f$.
44 **/
45 #define VL_EPSILON_D 2.220446049250313e-16
46
47 /*
48 For the code below: An ANSI C compiler takes the two expressions,
49 LONG_VAR and CHAR_VAR, and implicitly casts them to the type of the
50 first member of the union. Refer to K&R Second Edition Page 148,
51 last paragraph.
52 */
53
54 /** @internal @brief IEEE single precision quiet NaN constant */
55 static union { vl_uint32 raw ; float value ; }
56 const vl_nan_f =
57 { 0x7FC00000UL } ;
58
59 /** @internal @brief IEEE single precision infinity constant */
60 static union { vl_uint32 raw ; float value ; }
61 const vl_infinity_f =
62 { 0x7F800000UL } ;
63
64 /** @internal @brief IEEE double precision quiet NaN constant */
65 static union { vl_uint64 raw ; double value ; }
66 const vl_nan_d =
67 #ifdef VL_COMPILER_MSC
68 { 0x7FF8000000000000ui64 } ;
69 #else
70 { 0x7FF8000000000000ULL } ;
71 #endif
72
73 /** @internal @brief IEEE double precision infinity constant */
74 static union { vl_uint64 raw ; double value ; }
75 const vl_infinity_d =
76 #ifdef VL_COMPILER_MSC
77 { 0x7FF0000000000000ui64 } ;
78 #else
79 { 0x7FF0000000000000ULL } ;
80 #endif
81
82 /** @brief IEEE single precision NaN (not signaling) */
83 #define VL_NAN_F (vl_nan_f.value)
84
85 /** @brief IEEE single precision positive infinity (not signaling) */
86 #define VL_INFINITY_F (vl_infinity_f.value)
87
88 /** @brief IEEE double precision NaN (not signaling) */
89 #define VL_NAN_D (vl_nan_d.value)
90
91 /** @brief IEEE double precision positive infinity (not signaling) */
92 #define VL_INFINITY_D (vl_infinity_d.value)
93
94 /* ---------------------------------------------------------------- */
95
96 /** @brief Fast <code>mod(x, 2 * VL_PI)</code>
97 **
98 ** @param x input value.
99 ** @return <code>mod(x, 2 * VL_PI)</code>
100 **
101 ** The function is optimized for small absolute values of @a x.
102 **
103 ** The result is guaranteed to be not smaller than 0. However, due to
104 ** finite numerical precision and rounding errors, the result can be
105 ** equal to 2 * VL_PI (for instance, if @c x is a very small negative
106 ** number).
107 **/
108
109 VL_INLINE float
110 vl_mod_2pi_f (float x)
111 {
112 while (x > (float)(2 * VL_PI)) x -= (float) (2 * VL_PI) ;
113 while (x < 0.0F) x += (float) (2 * VL_PI);
114 return x ;
115 }
116
117 /** @brief Fast <code>mod(x, 2 * VL_PI)</code>
118 ** @see vl_mod_2pi_f
119 **/
120
121 VL_INLINE double
122 vl_mod_2pi_d (double x)
123 {
124 while (x > 2.0 * VL_PI) x -= 2 * VL_PI ;
125 while (x < 0.0) x += 2 * VL_PI ;
126 return x ;
127 }
128
129 /** @brief Floor and convert to integer
130 ** @param x argument.
131 ** @return Similar to @c (int) floor(x)
132 **/
133
134 VL_INLINE long int
135 vl_floor_f (float x)
136 {
137 long int xi = (long int) x ;
138 if (x >= 0 || (float) xi == x) return xi ;
139 else return xi - 1 ;
140 }
141
142 /** @brief Floor and convert to integer
143 ** @see vl_floor_f
144 **/
145
146 VL_INLINE long int
147 vl_floor_d (double x)
148 {
149 long int xi = (long int) x ;
150 if (x >= 0 || (double) xi == x) return xi ;
151 else return xi - 1 ;
152 }
153
154 /** @brief Ceil and convert to integer
155 ** @param x argument.
156 ** @return @c lceilf(x)
157 **/
158
159 VL_INLINE long int
160 vl_ceil_f (float x)
161 {
162 #ifdef VL_COMPILER_GNUC
163 return (long int) __builtin_ceilf(x) ;
164 #else
165 return (long int) ceilf(x) ;
166 #endif
167 }
168
169 /** @brief Ceil and convert to integer
170 ** @see vl_ceil_f
171 **/
172
173 VL_INLINE long int
174 vl_ceil_d (double x)
175 {
176 #ifdef VL_COMPILER_GNUC
177 return __builtin_ceil(x) ;
178 #else
179 return (long int) ceil(x) ;
180 #endif
181 }
182
183 /** @brief Round
184 ** @param x argument.
185 ** @return @c lroundf(x)
186 ** This function is either the same or similar to C99 @c lroundf().
187 **/
188
189 VL_INLINE long int
190 vl_round_f (float x)
191 {
192 #ifdef VL_COMPILER_GNUC
193 return __builtin_lroundf(x) ;
194 #elif VL_COMPILER_MSC
195 if (x >= 0.0F) {
196 return vl_floor_f(x + 0.5F) ;
197 } else {
198 return vl_ceil_f(x - 0.5F) ;
199 }
200 #else
201 return lroundf(x) ;
202 #endif
203 }
204
205 /** @brief Round
206 ** @param x argument.
207 ** @return @c lround(x)
208 ** This function is either the same or similar to C99 @c lround().
209 **/
210
211 VL_INLINE long int
212 vl_round_d (double x)
213 {
214 #ifdef VL_COMPILER_GNUC
215 return __builtin_lround(x) ;
216 #elif VL_COMPILER_MSC
217 if (x >= 0.0) {
218 return vl_floor_d(x + 0.5) ;
219 } else {
220 return vl_ceil_d(x - 0.5) ;
221 }
222 #else
223 return lround(x) ;
224 #endif
225 }
226
227 /** @brief Fast @c abs(x)
228 ** @param x argument.
229 ** @return @c abs(x)
230 **/
231
232 VL_INLINE float
233 vl_abs_f (float x)
234 {
235 #ifdef VL_COMPILER_GNUC
236 return __builtin_fabsf (x) ;
237 #else
238 return fabsf(x) ;
239 #endif
240 }
241
242 /** @brief Fast @c abs(x)
243 ** @sa vl_abs_f
244 **/
245
246 VL_INLINE double
247 vl_abs_d (double x)
248 {
249 #ifdef VL_COMPILER_GNUC
250 return __builtin_fabs (x) ;
251 #else
252 return fabs(x) ;
253 #endif
254 }
255
256 /** @brief Base-2 logaritghm
257 ** @param x argument.
258 ** @return @c log(x).
259 **/
260
261 VL_INLINE double
262 vl_log2_d (double x)
263 {
264 #ifdef VL_COMPILER_GNUC
265 return __builtin_log2(x) ;
266 #elif VL_COMPILER_MSC
267 return log(x) / 0.693147180559945 ;
268 #else
269 return log2(x) ;
270 #endif
271 }
272
273 /** @copydoc vl_log2_d */
274 VL_INLINE float
275 vl_log2_f (float x)
276 {
277 #ifdef VL_COMPILER_GNUC
278 return __builtin_log2f (x) ;
279 #elif VL_COMPILER_MSC
280 return logf(x) / 0.6931472F ;
281 #else
282 return log2(x) ;
283 #endif
284 }
285
286 /** @brief Square root.
287 ** @param x argument.
288 ** @return @c sqrt(x).
289 **/
290
291 VL_INLINE double
292 vl_sqrt_d (double x)
293 {
294 #ifdef VL_COMPILER_GNUC
295 return __builtin_sqrt(x) ;
296 #else
297 return sqrt(x) ;
298 #endif
299 }
300
301 /** @copydoc vl_sqrt_d */
302 VL_INLINE float
303 vl_sqrt_f (float x)
304 {
305 #ifdef VL_COMPILER_GNUC
306 return __builtin_sqrtf(x) ;
307 #else
308 return sqrtf(x) ;
309 #endif
310 }
311
312
313 /** @brief Check whether a floating point value is NaN
314 ** @param x argument.
315 ** @return true if @a x is NaN.
316 **/
317 VL_INLINE vl_bool
318 vl_is_nan_f (float x)
319 {
320 #ifdef VL_COMPILER_GNUC
321 return __builtin_isnan (x) ;
322 #elif VL_COMPILER_MSC
323 return _isnan(x) ;
324 #else
325 return isnan(x) ;
326 #endif
327 }
328
329 /** @copydoc vl_is_nan_f */
330 VL_INLINE vl_bool
331 vl_is_nan_d (double x)
332 {
333 #ifdef VL_COMPILER_GNUC
334 return __builtin_isnan (x) ;
335 #elif VL_COMPILER_MSC
336 return _isnan(x) ;
337 #else
338 return isnan(x) ;
339 #endif
340 }
341
342 /** @brief Check whether a floating point value is infinity
343 ** @param x argument.
344 ** @return true if @a x is infinity.
345 **/
346 VL_INLINE vl_bool
347 vl_is_inf_f (float x)
348 {
349 #ifdef VL_COMPILER_GNUC
350 return __builtin_isinf (x) ;
351 #elif VL_COMPILER_MSC
352 return ! _finite(x) ;
353 #else
354 return isinf(x) ;
355 #endif
356 }
357
358 /** @copydoc vl_is_inf_f */
359 VL_INLINE vl_bool
360 vl_is_inf_d (double x)
361 {
362 #ifdef VL_COMPILER_GNUC
363 return __builtin_isinf (x) ;
364 #elif VL_COMPILER_MSC
365 return ! _finite(x) ;
366 #else
367 return isinf(x) ;
368 #endif
369 }
370
371 /** ------------------------------------------------------------------
372 ** @brief Fast @c atan2 approximation
373 ** @param y argument.
374 ** @param x argument.
375 **
376 ** The function computes a relatively rough but fast approximation of
377 ** @c atan2(y,x).
378 **
379 ** @par Algorithm
380 **
381 ** The algorithm approximates the function @f$ f(r)=atan((1-r)/(1+r))
382 ** @f$, @f$ r \in [-1,1] @f$ with a third order polynomial @f$
383 ** f(r)=c_0 + c_1 r + c_2 r^2 + c_3 r^3 @f$. To fit the polynomial
384 ** we impose the constraints
385 **
386 ** @f{eqnarray*}
387 ** f(+1) &=& c_0 + c_1 + c_2 + c_3 = atan(0) = 0,\\
388 ** f(-1) &=& c_0 - c_1 + c_2 - c_3 = atan(\infty) = \pi/2,\\
389 ** f(0) &=& c_0 = atan(1) = \pi/4.
390 ** @f}
391 **
392 ** The last degree of freedom is fixed by minimizing the @f$
393 ** l^{\infty} @f$ error, which yields
394 **
395 ** @f[
396 ** c_0=\pi/4, \quad
397 ** c_1=-0.9675, \quad
398 ** c_2=0, \quad
399 ** c_3=0.1821,
400 ** @f]
401 **
402 ** with maximum error of 0.0061 radians at 0.35 degrees.
403 **
404 ** @return Approximation of @c atan2(y,x).
405 **/
406
407 VL_INLINE float
408 vl_fast_atan2_f (float y, float x)
409 {
410 float angle, r ;
411 float const c3 = 0.1821F ;
412 float const c1 = 0.9675F ;
413 float abs_y = vl_abs_f (y) + VL_EPSILON_F ;
414
415 if (x >= 0) {
416 r = (x - abs_y) / (x + abs_y) ;
417 angle = (float) (VL_PI / 4) ;
418 } else {
419 r = (x + abs_y) / (abs_y - x) ;
420 angle = (float) (3 * VL_PI / 4) ;
421 }
422 angle += (c3*r*r - c1) * r ;
423 return (y < 0) ? - angle : angle ;
424 }
425
426 /** @brief Fast @c atan2 approximation
427 ** @sa vl_fast_atan2_f
428 **/
429
430 VL_INLINE double
431 vl_fast_atan2_d (double y, double x)
432 {
433 double angle, r ;
434 double const c3 = 0.1821 ;
435 double const c1 = 0.9675 ;
436 double abs_y = vl_abs_d (y) + VL_EPSILON_D ;
437
438 if (x >= 0) {
439 r = (x - abs_y) / (x + abs_y) ;
440 angle = VL_PI / 4 ;
441 } else {
442 r = (x + abs_y) / (abs_y - x) ;
443 angle = 3 * VL_PI / 4 ;
444 }
445 angle += (c3*r*r - c1) * r ;
446 return (y < 0) ? - angle : angle ;
447 }
448
449 /** ------------------------------------------------------------------
450 ** @brief Fast @c resqrt approximation
451 ** @param x argument.
452 ** @return approximation of @c resqrt(x).
453 **
454 ** The function quickly computes an approximation of @f$ x^{-1/2}
455 ** @f$.
456 **
457 ** @par Algorithm
458 **
459 ** The goal is to compute @f$ y = x^{-1/2} @f$, which we do by
460 ** finding the solution of @f$ 0 = f(y) = y^{-2} - x @f$ by two Newton
461 ** steps. Each Newton iteration is given by
462 **
463 ** @f[
464 ** y \leftarrow
465 ** y - \frac{f(y)}{\frac{df(y)}{dy}} =
466 ** y + \frac{1}{2} (y-xy^3) =
467 ** \frac{y}{2} \left( 3 - xy^2 \right)
468 ** @f]
469 **
470 ** which yields a simple polynomial update rule.
471 **
472 ** The clever bit (attributed to either J. Carmack or G. Tarolli) is
473 ** the way an initial guess @f$ y \approx x^{-1/2} @f$ is chosen.
474 **
475 ** @see <a href="http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf">Inverse Sqare Root</a>.
476 **
477 **/
478
479 VL_INLINE float
480 vl_fast_resqrt_f (float x)
481 {
482 /* 32-bit version */
483 union {
484 float x ;
485 vl_int32 i ;
486 } u ;
487
488 float xhalf = (float) 0.5 * x ;
489
490 /* convert floating point value in RAW integer */
491 u.x = x ;
492
493 /* gives initial guess y0 */
494 u.i = 0x5f3759df - (u.i >> 1);
495 /*u.i = 0xdf59375f - (u.i>>1);*/
496
497 /* two Newton steps */
498 u.x = u.x * ( (float) 1.5 - xhalf*u.x*u.x) ;
499 u.x = u.x * ( (float) 1.5 - xhalf*u.x*u.x) ;
500 return u.x ;
501 }
502
503 /** @brief Fast @c resqrt approximation
504 ** @sa vl_fast_resqrt_d
505 **/
506
507 VL_INLINE double
508 vl_fast_resqrt_d (double x)
509 {
510 /* 64-bit version */
511 union {
512 double x ;
513 vl_int64 i ;
514 } u ;
515
516 double xhalf = (double) 0.5 * x ;
517
518 /* convert floating point value in RAW integer */
519 u.x = x ;
520
521 /* gives initial guess y0 */
522 #ifdef VL_COMPILER_MSC
523 u.i = 0x5fe6ec85e7de30dai64 - (u.i >> 1) ;
524 #else
525 u.i = 0x5fe6ec85e7de30daLL - (u.i >> 1) ;
526 #endif
527
528 /* two Newton steps */
529 u.x = u.x * ( (double) 1.5 - xhalf*u.x*u.x) ;
530 u.x = u.x * ( (double) 1.5 - xhalf*u.x*u.x) ;
531 return u.x ;
532 }
533
534 /** ------------------------------------------------------------------
535 ** @brief Fast @c sqrt approximation
536 ** @param x argument.
537 ** @return approximation of @c sqrt(x).
538 **
539 ** The function uses ::vl_fast_resqrt_f
540 ** (or ::vl_fast_resqrt_d) to compute <code>x *
541 ** vl_fast_resqrt_f(x)</code>.
542 **/
543
544 VL_INLINE float
545 vl_fast_sqrt_f (float x)
546 {
547 return (x < 1e-8) ? 0 : x * vl_fast_resqrt_f (x) ;
548 }
549
550 /** @brief Fast @c sqrt approximation
551 ** @copydoc vl_fast_sqrt_f
552 **/
553
554 VL_INLINE double
555 vl_fast_sqrt_d (float x)
556 {
557 return (x < 1e-8) ? 0 : x * vl_fast_resqrt_d (x) ;
558 }
559
560 /** @brief Fast integer @c sqrt approximation
561 ** @param x non-negative integer.
562 ** @return largest integer $y$ such that $y^2 \leq x$.
563 ** @sa @ref mathop-sqrti "Algorithm"
564 **/
565 VL_INLINE vl_uint64 vl_fast_sqrt_ui64 (vl_uint64 x) ;
566
567 /** @brief Fast @c sqrt approximation
568 ** @copydoc vl_fast_sqrt_ui64 */
569 VL_INLINE vl_uint32 vl_fast_sqrt_ui32 (vl_uint32 x) ;
570
571 /** @brief Fast @c sqrt approximation
572 ** @copydoc vl_fast_sqrt_ui64 */
573 VL_INLINE vl_uint16 vl_fast_sqrt_ui16 (vl_uint16 x) ;
574
575 /** @brief Fast @c sqrt approximation
576 ** @copydoc vl_fast_sqrt_ui64 */
577 VL_INLINE vl_uint8 vl_fast_sqrt_ui8 (vl_uint8 x) ;
578
579 #define VL_FAST_SQRT_UI(T,SFX) \
580 VL_INLINE T \
581 vl_fast_sqrt_ ## SFX (T x) \
582 { \
583 T y = 0 ; \
584 T tmp = 0 ; \
585 int twice_k ; \
586 for (twice_k = 8 * sizeof(T) - 2 ; \
587 twice_k >= 0 ; twice_k -= 2) { \
588 y <<= 1 ; /* y = 2 * y */ \
589 tmp = (2*y + 1) << twice_k ; \
590 if (x >= tmp) { \
591 x -= tmp ; \
592 y += 1 ; \
593 } \
594 } \
595 return y ; \
596 }
597
598 VL_FAST_SQRT_UI(vl_uint64,ui64)
599 VL_FAST_SQRT_UI(vl_uint32,ui32)
600 VL_FAST_SQRT_UI(vl_uint16,ui16)
601 VL_FAST_SQRT_UI(vl_uint8,ui8)
602
603 /* ---------------------------------------------------------------- */
604 /* Vector distances and similarities */
605 /* ---------------------------------------------------------------- */
606
607 /** @typedef VlFloatVectorComparisonFunction
608 ** @brief Pointer to a function to compare vectors of floats
609 **/
610 typedef float (*VlFloatVectorComparisonFunction)(vl_size dimension, float const * X, float const * Y) ;
611
612 /** @typedef VlDoubleVectorComparisonFunction
613 ** @brief Pointer to a function to compare vectors of doubles
614 **/
615 typedef double (*VlDoubleVectorComparisonFunction)(vl_size dimension, double const * X, double const * Y) ;
616
617 /** @typedef VlFloatVector3ComparisonFunction
618 ** @brief Pointer to a function to compare 3 vectors of doubles
619 **/
620 typedef float (*VlFloatVector3ComparisonFunction)(vl_size dimension, float const * X, float const * Y, float const * Z) ;
621
622 /** @typedef VlDoubleVector3ComparisonFunction
623 ** @brief Pointer to a function to compare 3 vectors of doubles
624 **/
625 typedef double (*VlDoubleVector3ComparisonFunction)(vl_size dimension, double const * X, double const * Y, double const * Z) ;
626
627 /** @brief Vector comparison types */
628 enum _VlVectorComparisonType {
629 VlDistanceL1, /**< l1 distance (squared intersection metric) */
630 VlDistanceL2, /**< squared l2 distance */
631 VlDistanceChi2, /**< squared Chi2 distance */
632 VlDistanceHellinger, /**< squared Hellinger's distance */
633 VlDistanceJS, /**< squared Jensen-Shannon distance */
634 VlDistanceMahalanobis, /**< squared mahalanobis distance */
635 VlKernelL1, /**< intersection kernel */
636 VlKernelL2, /**< l2 kernel */
637 VlKernelChi2, /**< Chi2 kernel */
638 VlKernelHellinger, /**< Hellinger's kernel */
639 VlKernelJS /**< Jensen-Shannon kernel */
640 } ;
641
642 /** @brief Vector comparison types */
643 typedef enum _VlVectorComparisonType VlVectorComparisonType ;
644
645 /** @brief Get the symbolic name of a vector comparison type
646 ** @param type vector comparison type.
647 ** @return data symbolic name.
648 **/
649
650 VL_INLINE char const *
651 vl_get_vector_comparison_type_name (int type)
652 {
653 switch (type) {
654 case VlDistanceL1 : return "l1" ;
655 case VlDistanceL2 : return "l2" ;
656 case VlDistanceChi2 : return "chi2" ;
657 case VlDistanceMahalanobis : return "mahalanobis" ;
658 case VlKernelL1 : return "kl1" ;
659 case VlKernelL2 : return "kl2" ;
660 case VlKernelChi2 : return "kchi2" ;
661 default: return NULL ;
662 }
663 }
664
665 VL_EXPORT VlFloatVectorComparisonFunction
666 vl_get_vector_comparison_function_f (VlVectorComparisonType type) ;
667
668 VL_EXPORT VlDoubleVectorComparisonFunction
669 vl_get_vector_comparison_function_d (VlVectorComparisonType type) ;
670
671 VL_EXPORT VlFloatVector3ComparisonFunction
672 vl_get_vector_3_comparison_function_f (VlVectorComparisonType type) ;
673
674 VL_EXPORT VlDoubleVector3ComparisonFunction
675 vl_get_vector_3_comparison_function_d (VlVectorComparisonType type) ;
676
677
678 VL_EXPORT void
679 vl_eval_vector_comparison_on_all_pairs_f (float * result, vl_size dimension,
680 float const * X, vl_size numDataX,
681 float const * Y, vl_size numDataY,
682 VlFloatVectorComparisonFunction function) ;
683
684 VL_EXPORT void
685 vl_eval_vector_comparison_on_all_pairs_d (double * result, vl_size dimension,
686 double const * X, vl_size numDataX,
687 double const * Y, vl_size numDataY,
688 VlDoubleVectorComparisonFunction function) ;
689
690 /* ---------------------------------------------------------------- */
691 /* Numerical analysis */
692 /* ---------------------------------------------------------------- */
693
694 VL_EXPORT void
695 vl_svd2 (double* S, double *U, double *V, double const *M) ;
696
697 VL_EXPORT void
698 vl_lapack_dlasv2 (double *smin,
699 double *smax,
700 double *sv,
701 double *cv,
702 double *su,
703 double *cu,
704 double f,
705 double g,
706 double h) ;
707
708
709 VL_EXPORT int
710 vl_solve_linear_system_3 (double * x, double const * A, double const *b) ;
711
712 VL_EXPORT int
713 vl_solve_linear_system_2 (double * x, double const * A, double const *b) ;
714
715 VL_EXPORT int
716 vl_gaussian_elimination (double * A, vl_size numRows, vl_size numColumns) ;
717
718 /* VL_MATHOP_H */
719 #endif