You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

719 lines
18 KiB

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