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.

1032 lines
28 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file mathop.c
  2. ** @brief Math operations - Definition
  3. ** @author Andrea Vedaldi, David Novotny
  4. **/
  5. /*
  6. Copyright (C) 2014 Andrea Vedaldi.
  7. Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
  8. All rights reserved.
  9. This file is part of the VLFeat library and is made available under
  10. the terms of the BSD license (see the COPYING file).
  11. */
  12. /**
  13. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  14. @page mathop Mathematical operations
  15. @author Andrea Vedaldi
  16. @author Brian Fulkerson
  17. @tableofcontents
  18. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  19. VLFeat include several low-level routines to speedup common
  20. mathematical operations used throughout the library. Most are
  21. collected in the @ref mathop.h module.
  22. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  23. @section mathop-usage-vector-comparison Comparing vectors
  24. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  25. @ref mathop.h includes a number of functions to quickly compute
  26. distances or similarity of pairs of vector. Applications include
  27. clustering and evaluation of SVM-like classifiers.
  28. Use ::vl_get_vector_comparison_function_f or
  29. ::vl_get_vector_comparison_function_d obtain an approprite function
  30. to comprare vectors of floats or doubles, respectively. Such
  31. functions are usually optimized (for instance, on X86 platforms they
  32. use the SSE vector extension) and are several times faster than a
  33. naive implementation. ::vl_eval_vector_comparison_on_all_pairs_f and
  34. ::vl_eval_vector_comparison_on_all_pairs_d can be used to evaluate
  35. the comparison function on all pairs of one or two sequences of
  36. vectors.
  37. Let @f$ \mathbf{x} = (x_1,\dots,x_d) @f$ and @f$ \mathbf{y} =
  38. (y_1,\dots,y_d) @f$ be two vectors. The following comparison
  39. functions are supported:
  40. <table>
  41. <tr>
  42. <td>@f$ l^1 @f$</td>
  43. <td>::VlDistanceL1</td>
  44. <td>@f$ \sum_{i=1}^d |x_i - y_i| @f$</td>
  45. <td>l1 distance (squared intersection metric)</td>
  46. </tr>
  47. <tr>
  48. <td>@f$ l^2 @f$</td>
  49. <td>::VlDistanceL2</td>
  50. <td>@f$\sum_{i=1}^d (x_i - y_i)^2@f$</td>
  51. <td>Squared Euclidean disance</td>
  52. </tr>
  53. <tr>
  54. <td>@f$ \chi^2 @f$</td>
  55. <td>::VlDistanceChi2</td>
  56. <td>@f$\sum_{i=1}^d \frac{(x_i - y_i)^2}{x_i + y_i}@f$</td>
  57. <td>Squared chi-square distance</td>
  58. </tr>
  59. <tr>
  60. <td>-</td>
  61. <td>::VlDistanceHellinger</td>
  62. <td>@f$\sum_{i=1}^d (\sqrt{x_i} - \sqrt{y_i})^2@f$</td>
  63. <td>Squared Hellinger's distance</td>
  64. </tr>
  65. <tr>
  66. <td>-</td>
  67. <td>::VlDistanceJS</td>
  68. <td>@f$
  69. \sum_{i=1}^d
  70. \left(
  71. x_i \log\frac{2x_i}{x_i+y_i}
  72. + y_i \log\frac{2y_i}{x_i+y_i}
  73. \right)
  74. @f$
  75. </td>
  76. <td>Squared Jensen-Shannon distance</td>
  77. </tr>
  78. <tr>
  79. <td>@f$ l^1 @f$</td>
  80. <td>::VlKernelL1</td>
  81. <td>@f$ \sum_{i=1}^d \min\{ x_i, y_i \} @f$</td>
  82. <td>intersection kernel</td>
  83. </tr>
  84. <tr>
  85. <td>@f$ l^2 @f$</td>
  86. <td>::VlKernelL2</td>
  87. <td>@f$\sum_{i=1}^d x_iy_i @f$</td>
  88. <td>linear kernel</td>
  89. </tr>
  90. <tr>
  91. <td>@f$ \chi^2 @f$</td>
  92. <td>::VlKernelChi2</td>
  93. <td>@f$\sum_{i=1}^d 2 \frac{x_iy_i}{x_i + y_i}@f$</td>
  94. <td>chi-square kernel</td>
  95. </tr>
  96. <tr>
  97. <td>-</td>
  98. <td>::VlKernelHellinger</td>
  99. <td>@f$\sum_{i=1}^d 2 \sqrt{x_i y_i}@f$</td>
  100. <td>Hellinger's kernel (Bhattacharya coefficient)</td>
  101. </tr>
  102. <tr>
  103. <td>-</td>
  104. <td>::VlKernelJS</td>
  105. <td>@f$
  106. \sum_{i=1}^d
  107. \left(
  108. \frac{x_i}{2} \log_2\frac{x_i+y_i}{x_i}
  109. + \frac{y_i}{2} \log_2\frac{x_i+y_i}{y_i}
  110. \right)
  111. @f$
  112. </td>
  113. <td>Jensen-Shannon kernel</td>
  114. </tr>
  115. </table>
  116. @remark The definitions have been choosen so that corresponding kernels and
  117. distances are related by the equation:
  118. @f[
  119. d^2(\mathbf{x},\mathbf{y})
  120. =
  121. k(\mathbf{x},\mathbf{x})
  122. +k(\mathbf{y},\mathbf{y})
  123. -k(\mathbf{x},\mathbf{y})
  124. -k(\mathbf{y},\mathbf{x})
  125. @f]
  126. This means that each of these distances can be interpreted as a
  127. squared distance or metric in the corresponding reproducing kernel
  128. Hilbert space. Notice in particular that the @f$ l^1 @f$ or Manhattan
  129. distance is also a <em>squared</em> distance in this sense.
  130. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  131. @section mathop-integer-ops Fast basic functions operations
  132. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  133. In certain algorithm it is useful to quickly compute integer
  134. approximation of certain mathematical operations. Presently, VLFeat
  135. includes and implementations of:
  136. - Fast single precision atan2: ::vl_fast_sqrt_f.
  137. - Fast inverse square root: ::vl_fast_resqrt_f, ::vl_fast_resqrt_d.
  138. - Fast square root: ::vl_fast_sqrt_f, ::vl_fast_sqrt_d.
  139. - Fast integer square root: ::vl_fast_sqrt_ui16, ::vl_fast_sqrt_ui32,
  140. ::vl_fast_sqrt_ui64 (see also @subpage mathop-sqrti).
  141. **/
  142. /** @fn vl_get_vector_comparison_function_f(VlVectorComparisonType)
  143. **
  144. ** @brief Get vector comparison function from comparison type
  145. ** @param type vector comparison type.
  146. ** @return comparison function.
  147. **/
  148. /** @fn vl_get_vector_comparison_function_d(VlVectorComparisonType)
  149. ** @brief Get vector comparison function from comparison type
  150. ** @sa vl_get_vector_comparison_function_f
  151. **/
  152. /** @fn vl_eval_vector_comparison_on_all_pairs_f(float*,vl_size,
  153. ** float const*,vl_size,float const*,vl_size,VlFloatVectorComparisonFunction)
  154. **
  155. ** @brief Evaluate vector comparison function on all vector pairs
  156. ** @param result comparison matrix (output).
  157. ** @param dimension number of vector components (rows of @a X and @a Y).
  158. ** @param X data matrix X.
  159. ** @param Y data matrix Y.
  160. ** @param numDataX number of vectors in @a X (columns of @a X)
  161. ** @param numDataY number of vectros in @a Y (columns of @a Y)
  162. ** @param function vector comparison function.
  163. **
  164. ** The function evaluates @a function on all pairs of columns
  165. ** from matrices @a X and @a Y, filling a @a numDataX by @a numDataY
  166. ** matrix.
  167. **
  168. ** If @a Y is a null pointer the function compares all columns from
  169. ** @a X with themselves.
  170. **/
  171. /** @fn vl_eval_vector_comparison_on_all_pairs_d(double*,vl_size,
  172. ** double const*,vl_size,double const*,vl_size,VlDoubleVectorComparisonFunction)
  173. ** @brief Evaluate vector comparison function on all vector pairs
  174. ** @sa vl_eval_vector_comparison_on_all_pairs_f
  175. **/
  176. /**
  177. @page mathop-sqrti Fast integer square root algorithm
  178. @tableofcontents
  179. This section describes the fast integer square root algorithm used by
  180. vl_fast_sqrt_ui8, ::vl_fast_sqrt_ui16, ::vl_fast_sqrt_ui32,
  181. ::vl_fast_sqrt_ui64.
  182. Given a non-negative integer $x \in \mathbb{Z}_+$, the goal of this
  183. algorithm is to quickly compute the integer approximation of the
  184. square root of an integer number:
  185. \[
  186. y = \max_{\bar y\in\mathbb{Z}} \bar y, \qquad \text{such that}\ \bar y^2 \leq x.
  187. \]
  188. Consider determining the k-th bit of $y$. To this end, decompose $y$
  189. in three parts:
  190. \[
  191. y = y_{k+1} + q 2^k + r,
  192. \qquad \text{where}\ y_{k+1} \geq 2^{k+1}, r < 2^k,
  193. \]
  194. and $q\in\{0,1\}$ is the bit to be determined. Here $y_{k+1}$ is a part
  195. of the result $y$ that has already been determined, while the bit $q$
  196. and the remainder $r$ are still unknown. Recall that the goal is to
  197. find the largest $y^2$ such that $y^2 \leq x$. Expanding $y^2$ this
  198. condition becomes
  199. \[
  200. q (2^{2k} + 2 y_{k+1} 2^k) + r(r + 2q 2^k + 2 y_{k+1}) \leq x - y_{k+1}^2.
  201. \]
  202. We can now determine if $q=1$ or $q=0$ based on the value of the
  203. residual $x - y_{k+1}^2$. Specifically, $q=1$ requires that:
  204. \[
  205. \boxed{
  206. 2^{2k} + 2a2^k \leq x - y_{k+1}^2.
  207. }
  208. \]
  209. On the other hand, if this equation is satisfied, then setting $r=0$
  210. shows that there exists at least one $y$ such that $q=1$ and $y^2 \leq
  211. x$. In particular, greedily choosing $q=1$ in $x=y_{k+1} + 2^k q + r$ is
  212. optimal because $2^k > r$. This yields the algorithm:
  213. 1. Note that if $x$ is stored in $n$ bits and $n$ is even, then the
  214. integer square root $y$ does not require more than $m = n / 2$ bit
  215. to be stored. Thus the first bit to be determined is $k \leftarrow
  216. m - 1 = n/2 - 1$ and $y_{n/2}=0$.
  217. 2. The algorithm stores and updates $y_k/2^{k}$ and $x - y_{k}^2$ for
  218. convenience.
  219. 3. During iteration $k$, $y_k$ is determined. On entering the
  220. iteration, the first step is to compute $y_{k+1}/2^k = 2
  221. y_{k+1}/2^{k+1}$.
  222. 4. Then the bound $t = (2^{2k} + 2 y_{k+1})2^k = 2^{2k}(1 + 2
  223. y_{k+1}/2^k)$.
  224. 5. If $t \geq x - y_{k+1}$, the $k$-th bit of $y_k$ is set to
  225. one. This means applying the update $\hat y_{k}/2^k \leftarrow
  226. y_{k+1}/2^k + 1$. This also requires computing $x - y_{k}^2
  227. \leftarrow x - y_{k+1}^2 - t$.
  228. 6. Decrement $k \leftarrow k -1$ and, if $k\geq 0$, continue from 3.
  229. **/
  230. /* ---------------------------------------------------------------- */
  231. #ifndef VL_MATHOP_INSTANTIATING
  232. #include "mathop.h"
  233. #include "mathop_sse2.h"
  234. #include "mathop_avx.h"
  235. #include <math.h>
  236. #undef FLT
  237. #define FLT VL_TYPE_FLOAT
  238. #define VL_MATHOP_INSTANTIATING
  239. #include "mathop.c"
  240. #undef FLT
  241. #define FLT VL_TYPE_DOUBLE
  242. #define VL_MATHOP_INSTANTIATING
  243. #include "mathop.c"
  244. #endif
  245. /* ---------------------------------------------------------------- */
  246. #ifdef VL_MATHOP_INSTANTIATING
  247. #include "float.th"
  248. #undef COMPARISONFUNCTION_TYPE
  249. #undef COMPARISONFUNCTION3_TYPE
  250. #if (FLT == VL_TYPE_FLOAT)
  251. # define COMPARISONFUNCTION_TYPE VlFloatVectorComparisonFunction
  252. # define COMPARISONFUNCTION3_TYPE VlFloatVector3ComparisonFunction
  253. #else
  254. # define COMPARISONFUNCTION_TYPE VlDoubleVectorComparisonFunction
  255. # define COMPARISONFUNCTION3_TYPE VlDoubleVector3ComparisonFunction
  256. #endif
  257. /* ---------------------------------------------------------------- */
  258. VL_EXPORT T
  259. VL_XCAT(_vl_distance_l2_, SFX)
  260. (vl_size dimension, T const * X, T const * Y)
  261. {
  262. T const * X_end = X + dimension ;
  263. T acc = 0.0 ;
  264. while (X < X_end) {
  265. T d = *X++ - *Y++ ;
  266. acc += d * d ;
  267. }
  268. return acc ;
  269. }
  270. VL_EXPORT T
  271. VL_XCAT(_vl_distance_l1_, SFX)
  272. (vl_size dimension, T const * X, T const * Y)
  273. {
  274. T const * X_end = X + dimension ;
  275. T acc = 0.0 ;
  276. while (X < X_end) {
  277. T d = *X++ - *Y++ ;
  278. acc += VL_MAX(d, -d) ;
  279. }
  280. return acc ;
  281. }
  282. VL_EXPORT T
  283. VL_XCAT(_vl_distance_chi2_, SFX)
  284. (vl_size dimension, T const * X, T const * Y)
  285. {
  286. T const * X_end = X + dimension ;
  287. T acc = 0.0 ;
  288. while (X < X_end) {
  289. T a = *X++ ;
  290. T b = *Y++ ;
  291. T delta = a - b ;
  292. T denom = (a + b) ;
  293. T numer = delta * delta ;
  294. if (denom) {
  295. T ratio = numer / denom ;
  296. acc += ratio ;
  297. }
  298. }
  299. return acc ;
  300. }
  301. VL_EXPORT T
  302. VL_XCAT(_vl_distance_hellinger_, SFX)
  303. (vl_size dimension, T const * X, T const * Y)
  304. {
  305. T const * X_end = X + dimension ;
  306. T acc = 0.0 ;
  307. while (X < X_end) {
  308. T a = *X++ ;
  309. T b = *Y++ ;
  310. #if (FLT == VL_TYPE_FLOAT)
  311. acc += a + b - 2.0 * sqrtf (a*b) ;
  312. #else
  313. acc += a + b - 2.0 * sqrt (a*b) ;
  314. #endif
  315. }
  316. return acc ;
  317. }
  318. VL_EXPORT T
  319. VL_XCAT(_vl_distance_js_, SFX)
  320. (vl_size dimension, T const * X, T const * Y)
  321. {
  322. T const * X_end = X + dimension ;
  323. T acc = 0.0 ;
  324. while (X < X_end) {
  325. T x = *X++ ;
  326. T y = *Y++ ;
  327. if (x) acc += x - x * VL_XCAT(vl_log2_,SFX)(1 + y/x) ;
  328. if (y) acc += y - y * VL_XCAT(vl_log2_,SFX)(1 + x/y) ;
  329. }
  330. return acc ;
  331. }
  332. VL_EXPORT T
  333. VL_XCAT(_vl_kernel_l2_, SFX)
  334. (vl_size dimension, T const * X, T const * Y)
  335. {
  336. T const * X_end = X + dimension ;
  337. T acc = 0.0 ;
  338. while (X < X_end) {
  339. T a = *X++ ;
  340. T b = *Y++ ;
  341. acc += a * b ;
  342. }
  343. return acc ;
  344. }
  345. VL_EXPORT T
  346. VL_XCAT(_vl_kernel_l1_, SFX)
  347. (vl_size dimension, T const * X, T const * Y)
  348. {
  349. T const * X_end = X + dimension ;
  350. T acc = 0.0 ;
  351. while (X < X_end) {
  352. T a = *X++ ;
  353. T b = *Y++ ;
  354. T a_ = VL_XCAT(vl_abs_, SFX) (a) ;
  355. T b_ = VL_XCAT(vl_abs_, SFX) (b) ;
  356. acc += a_ + b_ - VL_XCAT(vl_abs_, SFX) (a - b) ;
  357. }
  358. return acc / ((T)2) ;
  359. }
  360. VL_EXPORT T
  361. VL_XCAT(_vl_kernel_chi2_, SFX)
  362. (vl_size dimension, T const * X, T const * Y)
  363. {
  364. T const * X_end = X + dimension ;
  365. T acc = 0.0 ;
  366. while (X < X_end) {
  367. T a = *X++ ;
  368. T b = *Y++ ;
  369. T denom = (a + b) ;
  370. if (denom) {
  371. T numer = 2 * a * b ;
  372. T ratio = numer / denom ;
  373. acc += ratio ;
  374. }
  375. }
  376. return acc ;
  377. }
  378. VL_EXPORT T
  379. VL_XCAT(_vl_kernel_hellinger_, SFX)
  380. (vl_size dimension, T const * X, T const * Y)
  381. {
  382. T const * X_end = X + dimension ;
  383. T acc = 0.0 ;
  384. while (X < X_end) {
  385. T a = *X++ ;
  386. T b = *Y++ ;
  387. #if (FLT == VL_TYPE_FLOAT)
  388. acc += sqrtf (a*b) ;
  389. #else
  390. acc += sqrt (a*b) ;
  391. #endif
  392. }
  393. return acc ;
  394. }
  395. VL_EXPORT T
  396. VL_XCAT(_vl_kernel_js_, SFX)
  397. (vl_size dimension, T const * X, T const * Y)
  398. {
  399. T const * X_end = X + dimension ;
  400. T acc = 0.0 ;
  401. while (X < X_end) {
  402. T x = *X++ ;
  403. T y = *Y++ ;
  404. if (x) acc += x * VL_XCAT(vl_log2_,SFX)(1 + y/x) ;
  405. if (y) acc += y * VL_XCAT(vl_log2_,SFX)(1 + x/y) ;
  406. }
  407. return (T)0.5 * acc ;
  408. }
  409. VL_EXPORT T
  410. VL_XCAT(_vl_distance_mahalanobis_sq_, SFX)
  411. (vl_size dimension, T const * X, T const * MU, T const * S)
  412. {
  413. T const * X_end = X + dimension ;
  414. T acc = 0.0 ;
  415. while (X < X_end) {
  416. T d = *X++ - *MU++ ;
  417. acc += d * d * (*S++) ;
  418. }
  419. return acc ;
  420. }
  421. /* ---------------------------------------------------------------- */
  422. VL_EXPORT COMPARISONFUNCTION_TYPE
  423. VL_XCAT(vl_get_vector_comparison_function_, SFX)(VlVectorComparisonType type)
  424. {
  425. COMPARISONFUNCTION_TYPE function = 0 ;
  426. switch (type) {
  427. case VlDistanceL2 : function = VL_XCAT(_vl_distance_l2_, SFX) ; break ;
  428. case VlDistanceL1 : function = VL_XCAT(_vl_distance_l1_, SFX) ; break ;
  429. case VlDistanceChi2 : function = VL_XCAT(_vl_distance_chi2_, SFX) ; break ;
  430. case VlDistanceHellinger : function = VL_XCAT(_vl_distance_hellinger_, SFX) ; break ;
  431. case VlDistanceJS : function = VL_XCAT(_vl_distance_js_, SFX) ; break ;
  432. case VlKernelL2 : function = VL_XCAT(_vl_kernel_l2_, SFX) ; break ;
  433. case VlKernelL1 : function = VL_XCAT(_vl_kernel_l1_, SFX) ; break ;
  434. case VlKernelChi2 : function = VL_XCAT(_vl_kernel_chi2_, SFX) ; break ;
  435. case VlKernelHellinger : function = VL_XCAT(_vl_kernel_hellinger_, SFX) ; break ;
  436. case VlKernelJS : function = VL_XCAT(_vl_kernel_js_, SFX) ; break ;
  437. default: abort() ;
  438. }
  439. #ifndef VL_DISABLE_SSE2
  440. /* if a SSE2 implementation is available, use it */
  441. if (vl_cpu_has_sse2() && vl_get_simd_enabled()) {
  442. switch (type) {
  443. case VlDistanceL2 : function = VL_XCAT(_vl_distance_l2_sse2_, SFX) ; break ;
  444. case VlDistanceL1 : function = VL_XCAT(_vl_distance_l1_sse2_, SFX) ; break ;
  445. case VlDistanceChi2 : function = VL_XCAT(_vl_distance_chi2_sse2_, SFX) ; break ;
  446. case VlKernelL2 : function = VL_XCAT(_vl_kernel_l2_sse2_, SFX) ; break ;
  447. case VlKernelL1 : function = VL_XCAT(_vl_kernel_l1_sse2_, SFX) ; break ;
  448. case VlKernelChi2 : function = VL_XCAT(_vl_kernel_chi2_sse2_, SFX) ; break ;
  449. default: break ;
  450. }
  451. }
  452. #endif
  453. #ifndef VL_DISABLE_AVX
  454. /* if an AVX implementation is available, use it */
  455. if (vl_cpu_has_avx() && vl_get_simd_enabled()) {
  456. switch (type) {
  457. case VlDistanceL2 : function = VL_XCAT(_vl_distance_l2_avx_, SFX) ; break ;
  458. default: break ;
  459. }
  460. }
  461. #endif
  462. return function ;
  463. }
  464. /* ---------------------------------------------------------------- */
  465. VL_EXPORT COMPARISONFUNCTION3_TYPE
  466. VL_XCAT(vl_get_vector_3_comparison_function_, SFX)(VlVectorComparisonType type)
  467. {
  468. COMPARISONFUNCTION3_TYPE function = 0 ;
  469. switch (type) {
  470. case VlDistanceMahalanobis : function = VL_XCAT(_vl_distance_mahalanobis_sq_, SFX) ; break ;
  471. default: abort() ;
  472. }
  473. #ifndef VL_DISABLE_SSE2
  474. /* if a SSE2 implementation is available, use it */
  475. if (vl_cpu_has_sse2() && vl_get_simd_enabled()) {
  476. switch (type) {
  477. case VlDistanceMahalanobis : function = VL_XCAT(_vl_distance_mahalanobis_sq_sse2_, SFX) ; break ;
  478. default: break ;
  479. }
  480. }
  481. #endif
  482. #ifndef VL_DISABLE_AVX
  483. /* if an AVX implementation is available, use it */
  484. if (vl_cpu_has_avx() && vl_get_simd_enabled()) {
  485. switch (type) {
  486. case VlDistanceMahalanobis : function = VL_XCAT(_vl_distance_mahalanobis_sq_avx_, SFX) ; break ;
  487. default: break ;
  488. }
  489. }
  490. #endif
  491. return function ;
  492. }
  493. /* ---------------------------------------------------------------- */
  494. VL_EXPORT void
  495. VL_XCAT(vl_eval_vector_comparison_on_all_pairs_, SFX)
  496. (T * result, vl_size dimension,
  497. T const * X, vl_size numDataX,
  498. T const * Y, vl_size numDataY,
  499. COMPARISONFUNCTION_TYPE function)
  500. {
  501. vl_uindex xi ;
  502. vl_uindex yi ;
  503. if (dimension == 0) return ;
  504. if (numDataX == 0) return ;
  505. assert (X) ;
  506. if (Y) {
  507. if (numDataY == 0) return ;
  508. for (yi = 0 ; yi < numDataY ; ++ yi) {
  509. for (xi = 0 ; xi < numDataX ; ++ xi) {
  510. *result++ = (*function)(dimension, X, Y) ;
  511. X += dimension ;
  512. }
  513. X -= dimension * numDataX ;
  514. Y += dimension ;
  515. }
  516. } else {
  517. T * resultTransp = result ;
  518. Y = X ;
  519. for (yi = 0 ; yi < numDataX ; ++ yi) {
  520. for (xi = 0 ; xi <= yi ; ++ xi) {
  521. T z = (*function)(dimension, X, Y) ;
  522. X += dimension ;
  523. *result = z ;
  524. *resultTransp = z ;
  525. result += 1 ;
  526. resultTransp += numDataX ;
  527. }
  528. X -= dimension * (yi + 1) ;
  529. Y += dimension ;
  530. result += numDataX - (yi + 1) ;
  531. resultTransp += 1 - (yi + 1) * numDataX ;
  532. }
  533. }
  534. }
  535. /* VL_MATHOP_INSTANTIATING */
  536. #endif
  537. /* ---------------------------------------------------------------- */
  538. /* Numerical analysis */
  539. /* ---------------------------------------------------------------- */
  540. #ifndef VL_MATHOP_INSTANTIATING
  541. /** @brief SVD of a 2x2 real matrix
  542. ** @param S 2x2 real diagonal matrix of the singular values (out).
  543. ** @param U first 2x2 real orthonormal matrix (out).
  544. ** @param V second 2x2 real orthonormal matrix (out).
  545. ** @param M 2x2 matrix.
  546. **
  547. ** The function comptues the SVD decomposition of the 2x2
  548. ** real matrix @f$ M @f$:
  549. ** @f[
  550. ** M = U \operatorname S V^\top
  551. ** @f]
  552. ** where @f$ U @f$ and @f$ V @f$ are real orthonormal matrices
  553. ** and @f$ S @f$ is the diagonal matrix of the singular values
  554. ** in decreasing order.
  555. **
  556. ** @par Algorithm
  557. **
  558. ** The fist step is to find rotation matrices @f$ U_1 @f$ and
  559. ** @f$ V_1 @f$ such taht
  560. ** @f[
  561. ** M = U_1 R V_1^\top, \quad
  562. ** U_1 = \begin{barray} c_{u1} & - s_{u1} \\ s_{u1} & c_{u1} \end{barray}, \quad
  563. ** V_1 = \begin{barray} c_{v1} & - s_{v1} \\ s_{v1} & c_{v1} \end{barray}, \quad
  564. ** R = \begin{barray} f & g \\ 0 & h \end{barray}.
  565. ** @f]
  566. ** Gives a 2x2 triangular matrix. The second step is to call
  567. ** ::vl_lapack_dlasv2 on the matrix @f$ R @f$ obtaining
  568. ** @f[
  569. ** M = U_1 (U_2 S V_2^\top) V_2.
  570. ** @f]
  571. **/
  572. void
  573. vl_svd2 (double* S, double *U, double *V, double const *M)
  574. {
  575. double m11 = M[0] ;
  576. double m21 = M[1] ;
  577. double m12 = M[2] ;
  578. double m22 = M[3] ;
  579. double cu1 = m11 ;
  580. double su1 = m21 ;
  581. double norm = sqrt(cu1*cu1 + su1*su1) ;
  582. double cu2, su2, cv2, sv2 ;
  583. double f, g, h ;
  584. double smin, smax ;
  585. cu1 /= norm ;
  586. su1 /= norm ;
  587. f = cu1 * m11 + su1 * m21 ;
  588. g = cu1 * m12 + su1 * m22 ;
  589. h = - su1 * m12 + cu1 * m22 ;
  590. vl_lapack_dlasv2 (&smin, &smax,
  591. &sv2, &cv2,
  592. &su2, &cu2,
  593. f, g, h) ;
  594. assert(S) ;
  595. S[0] = smax ;
  596. S[1] = 0 ;
  597. S[2] = 0 ;
  598. S[3] = smin ;
  599. if (U) {
  600. U[0] = cu2*cu1 - su2*su1 ;
  601. U[1] = su2*cu1 + cu2*su1 ;
  602. U[2] = - cu2*su1 - su2*cu1 ;
  603. U[3] = - su2*su1 + cu2*cu1 ;
  604. }
  605. if (V) {
  606. V[0] = cv2 ;
  607. V[1] = sv2 ;
  608. V[2] = - sv2 ;
  609. V[3] = cv2 ;
  610. }
  611. }
  612. /** @brief SVD of a 2x2 upper triangular matrix (LAPACK @c dlasv2 equivalent)
  613. ** @param smin smallest (in modulus) singular value (out).
  614. ** @param smax largest (in modulus) singuarl value (out).
  615. ** @param sv second component of the right singular vector of @c smax (out).
  616. ** @param cv first component of the right singular vector of @c smax (out).
  617. ** @param su second component of the left singular vector of @c smax (out).
  618. ** @param cu first component of the left singular vector of @c smax (out).
  619. ** @param f first entry of the upper triangular matrix.
  620. ** @param g second entry of the upper triangular matrix.
  621. ** @param h third entry of the upper triangular matrix.
  622. **
  623. ** @f[
  624. ** \begin{bmatrix} f & g \\ 0 & h \end{bmatrix}
  625. ** =
  626. ** \begin{bmatrix} cv & - sv \\ sv & cv \end{bmatrix}
  627. ** \begon{bmatrix} smax & 0 \\ 0 & smin \end{bmatrix}
  628. ** \begin{bmatrix} cv & - sv \\ sv & cv \end{bmatrix}
  629. ** @f]
  630. **
  631. ** Z.Bai and J.Demmel,
  632. ** "Computing the Generalized Singular Value Decomposition",
  633. ** SIAM J. Sci. Comput., Vol. 14, No. 6, pp. 1464-1486, November 1993
  634. **/
  635. #define isign(i) ((i)<0 ? (-1) : (+1)) /* integer sign function */
  636. #define sign(x) ((x)<0.0 ? (-1) : (+1)) /* double sign function */
  637. void
  638. vl_lapack_dlasv2 (double *smin,
  639. double *smax,
  640. double *sv,
  641. double *cv,
  642. double *su,
  643. double *cu,
  644. double f,
  645. double g,
  646. double h)
  647. {
  648. double svt, cvt, sut, cut; /* temporary sv, cv, su, and cu */
  649. double ft = f, gt = g, ht = h; /* temporary f, g, h */
  650. double fa = fabs(f), ga = fabs(g), ha = fabs(h); /* |f|, |g|, and |h| */
  651. int pmax = 1 ; /* pointer to max abs entry */
  652. int swap = 0 ; /* is swapped */
  653. int glarge = 0 ; /* is g very large */
  654. int tsign ; /* tmp sign */
  655. double fmh ; /* |f| -|h| */
  656. double d ; /* (|f| -|h|)/|f| */
  657. double dd ; /* d*d */
  658. double q ; /* g/f */
  659. double qq ; /* q*q */
  660. double s ; /* (|f| + |h|)/|f| */
  661. double ss ; /* s*s */
  662. double spq ; /* sqrt(ss + qq) */
  663. double dpq ; /* sqrt(dd + qq) */
  664. double a ; /* (spq + dpq)/2 */
  665. double tmp ; /* temporaries */
  666. double tt;
  667. /* make fa >= ha */
  668. if (fa < ha) {
  669. pmax = 3 ;
  670. tmp =ft ; ft = ht ; ht = tmp ; /* swap ft and ht */
  671. tmp =fa ; fa = ha ; ha = tmp ; /* swap fa and ha */
  672. swap = 1 ;
  673. }
  674. if (ga == 0.0) { /* diagonal */
  675. *smin = ha ;
  676. *smax = fa ;
  677. /* identity matrix */
  678. cut = 1.0 ; sut = 0.0 ;
  679. cvt = 1.0 ; svt = 0.0 ;
  680. }
  681. else { /* not diagonal */
  682. if (ga > fa) { /* g is the largest entry */
  683. pmax = 2 ;
  684. if ((fa / ga) < VL_EPSILON_D) { /* g is very large */
  685. glarge = 1 ;
  686. *smax = ga ; /* 1 ulp */
  687. if (ha > 1.0) {
  688. *smin = fa / (ga / ha) ; /* 2 ulps */
  689. } else {
  690. *smin = (fa / ga) * ha ; /* 2 ulps */
  691. }
  692. cut = 1.0 ; sut = ht / gt ;
  693. cvt = 1.0 ; svt = ft / gt ;
  694. }
  695. }
  696. if (glarge == 0) { /* normal case */
  697. fmh = fa - ha ; /* 1ulp */
  698. if (fmh == fa) { /* cope with infinite f or h */
  699. d = 1.0 ;
  700. } else {
  701. d = fmh / fa ; /* note 0<=d<=1.0, 2 ulps */
  702. }
  703. q = gt / ft ; /* note |q|<1/EPS, 1 ulp */
  704. s = 2.0 - d ; /* note s>=1.0, 3 ulps */
  705. dd = d*d ;
  706. qq = q*q ;
  707. ss = s*s ;
  708. spq = sqrt(ss + qq) ; /* note 1<=spq<=1+1/EPS, 5 ulps */
  709. if (d == 0.0) {
  710. dpq = fabs(q) ; /* 0 ulp */
  711. } else {
  712. dpq = sqrt(dd + qq) ; /* note 0<=dpq<=1+1/EPS, 3.5 ulps */
  713. }
  714. a = 0.5 * (spq + dpq) ; /* note 1<=a<=1 + |q|, 6 ulps */
  715. *smin = ha / a; /* 7 ulps */
  716. *smax = fa * a; /* 7 ulps */
  717. if (qq==0.0) { /* qq underflow */
  718. if (d==0.0) {
  719. tmp = sign(ft)*2*sign(gt); /* 0ulp */
  720. }
  721. else {
  722. tmp = gt/(sign(ft)*fmh) + q/s; /* 6 ulps */
  723. }
  724. } else {
  725. tmp = (q/(spq + s) + q/(dpq + d))*(1.0 + a); /* 17 ulps */
  726. }
  727. /* if qq */
  728. tt = sqrt(tmp*tmp + 4.0) ; /* 18.5 ulps */
  729. cvt = 2.0 / tt ; /* 19.5 ulps */
  730. svt = tmp / tt ; /* 36.5 ulps */
  731. cut = (cvt + svt*q) / a ; /* 46.5 ulps */
  732. sut = (ht / ft) * svt / a ; /* 45.5 ulps */
  733. } /* if g not large */
  734. } /* if ga */
  735. if (swap == 1) {
  736. *cu = svt ; *su = cvt ;
  737. *cv = sut ; *sv = cut ;
  738. } else {
  739. *cu = cut ; *su = sut ;
  740. *cv = cvt ; *sv = svt ;
  741. }
  742. /* correct the signs of smax and smin */
  743. if (pmax==1) { tsign = sign(*cv) * sign(*cu) * sign(f) ; }
  744. if (pmax==2) { tsign = sign(*sv) * sign(*cu) * sign(g) ; }
  745. if (pmax==3) { tsign = sign(*sv) * sign(*su) * sign(h) ; }
  746. *smax = isign(tsign) * (*smax);
  747. *smin = isign(tsign * sign(f) * sign(h)) * (*smin) ;
  748. }
  749. /** @brief Solve a 3x3 linear system
  750. ** @param x result.
  751. ** @param A system matrix.
  752. ** @param b coefficients.
  753. **
  754. ** The function computes a solution to @f$ Ax =b @f$ for a 3x3
  755. ** matrix.
  756. **/
  757. VL_EXPORT int
  758. vl_solve_linear_system_3 (double * x, double const * A, double const *b)
  759. {
  760. int err ;
  761. double M[3*4] ;
  762. M[0] = A[0] ;
  763. M[1] = A[1] ;
  764. M[2] = A[2] ;
  765. M[3] = A[3] ;
  766. M[4] = A[4] ;
  767. M[5] = A[5] ;
  768. M[6] = A[6] ;
  769. M[7] = A[7] ;
  770. M[8] = A[8] ;
  771. M[9] = b[0] ;
  772. M[10] = b[1] ;
  773. M[11] = b[2] ;
  774. err = vl_gaussian_elimination(M,3,4) ;
  775. x[0] = M[9] ;
  776. x[1] = M[10] ;
  777. x[2] = M[11] ;
  778. return err ;
  779. }
  780. /** @brief Solve a 2x2 linear system
  781. ** @param x result.
  782. ** @param A system matrix.
  783. ** @param b coefficients.
  784. **
  785. ** The function computes a solution to @f$ Ax =b @f$ for a 2x2
  786. ** matrix.
  787. **/
  788. VL_EXPORT int
  789. vl_solve_linear_system_2 (double * x, double const * A, double const *b)
  790. {
  791. int err ;
  792. double M[2*3] ;
  793. M[0] = A[0] ;
  794. M[1] = A[1] ;
  795. M[2] = A[2] ;
  796. M[3] = A[3] ;
  797. M[4] = b[0];
  798. M[5] = b[1] ;
  799. err = vl_gaussian_elimination(M,2,3) ;
  800. x[0] = M[4] ;
  801. x[1] = M[5] ;
  802. return err ;
  803. }
  804. /** @brief Gaussian elimination
  805. ** @param M matrix.
  806. ** @param numRows number of rows of @c M.
  807. ** @param numColumns number of columns of @c M.
  808. **
  809. ** The function runs Gaussian elimination with pivoting
  810. ** on the matrix @a M in place.
  811. ** @c numRows must be not larger than @c numColumns.
  812. **
  813. ** Let @f$ M = [A, b] @f$ to obtain the solution to the linear
  814. ** system @f$ Ax=b @f$ (as the last column of @c M after
  815. ** elimination).
  816. **
  817. ** Let @f$ M = [A, I] @f$ to compute the inverse of @c A in
  818. ** a similar manner.
  819. **/
  820. VL_EXPORT vl_bool
  821. vl_gaussian_elimination (double * A, vl_size numRows, vl_size numColumns)
  822. {
  823. vl_index i, j, ii, jj ;
  824. assert(A) ;
  825. assert(numRows <= numColumns) ;
  826. #define Aat(i,j) A[(i) + (j)*numRows]
  827. /* Gauss elimination */
  828. for(j = 0 ; j < (signed)numRows ; ++j) {
  829. double maxa = 0 ;
  830. double maxabsa = 0 ;
  831. vl_index maxi = -1 ;
  832. double tmp ;
  833. #if 0
  834. {
  835. vl_index iii, jjj ;
  836. for (iii = 0 ; iii < 2 ; ++iii) {
  837. for (jjj = 0 ; jjj < 3 ; ++jjj) {
  838. VL_PRINTF("%5.2g ", Aat(iii,jjj)) ;
  839. }
  840. VL_PRINTF("\n") ;
  841. }
  842. VL_PRINTF("\n") ;
  843. }
  844. #endif
  845. /* look for the maximally stable pivot */
  846. for (i = j ; i < (signed)numRows ; ++i) {
  847. double a = Aat(i,j) ;
  848. double absa = vl_abs_d (a) ;
  849. if (absa > maxabsa) {
  850. maxa = a ;
  851. maxabsa = absa ;
  852. maxi = i ;
  853. }
  854. }
  855. i = maxi ;
  856. /* if singular give up */
  857. if (maxabsa < 1e-10) return VL_ERR_OVERFLOW ;
  858. /* swap j-th row with i-th row and normalize j-th row */
  859. for(jj = j ; jj < (signed)numColumns ; ++jj) {
  860. tmp = Aat(i,jj) ; Aat(i,jj) = Aat(j,jj) ; Aat(j,jj) = tmp ;
  861. Aat(j,jj) /= maxa ;
  862. }
  863. #if 0
  864. {
  865. vl_index iii, jjj ;
  866. VL_PRINTF("after swap %d %d\n", j, i);
  867. for (iii = 0 ; iii < 2 ; ++iii) {
  868. for (jjj = 0 ; jjj < 3 ; ++jjj) {
  869. VL_PRINTF("%5.2g ", Aat(iii,jjj)) ;
  870. }
  871. VL_PRINTF("\n") ;
  872. }
  873. VL_PRINTF("\n") ;
  874. }
  875. #endif
  876. /* elimination */
  877. for (ii = j+1 ; ii < (signed)numRows ; ++ii) {
  878. double x = Aat(ii,j) ;
  879. for (jj = j ; jj < (signed)numColumns ; ++jj) {
  880. Aat(ii,jj) -= x * Aat(j,jj) ;
  881. }
  882. }
  883. #if 0
  884. {
  885. VL_PRINTF("after elimination\n");
  886. vl_index iii, jjj ;
  887. for (iii = 0 ; iii < 2 ; ++iii) {
  888. for (jjj = 0 ; jjj < 3 ; ++jjj) {
  889. VL_PRINTF("%5.2g ", Aat(iii,jjj)) ;
  890. }
  891. VL_PRINTF("\n") ;
  892. }
  893. VL_PRINTF("\n") ;
  894. }
  895. #endif
  896. }
  897. /* backward substitution */
  898. for (i = numRows - 1 ; i > 0 ; --i) {
  899. /* substitute in all rows above */
  900. for (ii = i - 1 ; ii >= 0 ; --ii) {
  901. double x = Aat(ii,i) ;
  902. /* j = numRows */
  903. for (j = numRows ; j < (signed)numColumns ; ++j) {
  904. Aat(ii,j) -= x * Aat(i,j) ;
  905. }
  906. }
  907. }
  908. #if 0
  909. {
  910. VL_PRINTF("after substitution\n");
  911. vl_index iii, jjj ;
  912. for (iii = 0 ; iii < 2 ; ++iii) {
  913. for (jjj = 0 ; jjj < 3 ; ++jjj) {
  914. VL_PRINTF("%5.2g ", Aat(iii,jjj)) ;
  915. }
  916. VL_PRINTF("\n") ;
  917. }
  918. VL_PRINTF("\n") ;
  919. }
  920. #endif
  921. return VL_ERR_OK ;
  922. }
  923. /* VL_MATHOP_INSTANTIATING */
  924. #endif
  925. #undef VL_MATHOP_INSTANTIATING