norms.qbk 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. [/
  2. Copyright 2018 Nick Thompson
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:norms Norms]
  8. [heading Synopsis]
  9. ``
  10. #include <boost/math/tools/norms.hpp>
  11. namespace boost{ namespace math{ namespace tools {
  12. template<class Container>
  13. auto l0_pseudo_norm(Container const & c);
  14. template<class ForwardIterator>
  15. auto l0_pseudo_norm(ForwardIterator first, ForwardIterator last);
  16. template<class ForwardIterator>
  17. size_t hamming_distance(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2);
  18. template<class Container>
  19. size_t hamming_distance(Container const & u, Container const & v);
  20. template<class Container>
  21. auto l1_norm(Container const & c);
  22. template<class ForwardIterator>
  23. auto l1_norm(ForwardIterator first, ForwardIterator last);
  24. template<class Container>
  25. auto l1_distance(Container const & v1, Container const & v2);
  26. template<class ForwardIterator>
  27. auto l1_distance(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2);
  28. template<class Container>
  29. auto l2_norm(Container const & c);
  30. template<class ForwardIterator>
  31. auto l2_norm(ForwardIterator first, ForwardIterator last);
  32. template<class Container>
  33. auto l2_distance(Container const & v1, Container const & v2);
  34. template<class ForwardIterator>
  35. auto l2_distance(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2);
  36. template<class Container>
  37. auto sup_norm(Container const & c);
  38. template<class ForwardIterator>
  39. auto sup_norm(ForwardIterator first, ForwardIterator last);
  40. template<class Container>
  41. auto sup_distance(Container const & v1, Container const & v2);
  42. template<class ForwardIterator>
  43. auto sup_distance(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2);
  44. template<class Container>
  45. auto lp_norm(Container const & c, unsigned p);
  46. template<class ForwardIterator>
  47. auto lp_norm(ForwardIterator first, ForwardIterator last, unsigned p);
  48. template<class Container>
  49. auto lp_distance(Container const & v1, Container const & v2, unsigned p);
  50. template<class ForwardIterator>
  51. auto lp_distance(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2, unsigned p);
  52. template<class Container>
  53. auto total_variation(Container const & c);
  54. template<class ForwardIterator>
  55. auto total_variation(ForwardIterator first, ForwardIterator last);
  56. }}}
  57. ``
  58. [heading Description]
  59. The file `boost/math/tools/norms.hpp` is a set of facilities for computing scalar values traditionally useful in numerical analysis from vectors.
  60. Our examples use `std::vector<double>` to hold the data, but this not required.
  61. In general, you can store your data in an Eigen array, an Armadillo vector, `std::array`, and for many of the routines, a `std::forward_list`.
  62. These routines are usable in float, double, long double, and Boost.Multiprecision precision, as well as their complex extensions whenever the computation is well-defined.
  63. Integral datatypes are supported for most routines.
  64. [heading \u2113[super \u221E] norm]
  65. Computes the supremum norm of a dataset:
  66. std::vector<double> v{-3, 2, 1};
  67. double sup = boost::math::tools::sup_norm(v.cbegin(), v.cend());
  68. // sup = 3
  69. std::vector<std::complex<double>> v{{0, -8}, {1,1}, {-3,2}};
  70. // Range call:
  71. double sup = boost::math::tools::sup_norm(v);
  72. // sup = 8
  73. Supports real, integral, and complex arithmetic.
  74. Container must be forward iterable and is not modified.
  75. [heading \u2113[super \u221E] distance]
  76. Computes the supremum norm distance between two vectors:
  77. std::vector<double> v{-3, 2, 1};
  78. std::vector<double> w{6, -2, 1};
  79. double sup = boost::math::tools::sup_distance(w, v);
  80. // sup = 9
  81. Supports real, integral, and complex arithmetic.
  82. Container must be forward iterable and is not modified.
  83. If the input it integral, the output is a double precision float.
  84. [heading \u2113[super /p/] norm]
  85. std::vector<double> v{-8, 0, 0};
  86. double sup = boost::math::tools::lp_norm(v.cbegin(), v.cend(), 7);
  87. // sup = 8
  88. std::vector<std::complex<double>> v{{1, 0}, {0,1}, {0,-1}};
  89. double sup = boost::math::tools::lp_norm(v.cbegin(), v.cend(), 3);
  90. // sup = cbrt(3)
  91. Supports both real, integral, and complex arithmetic.
  92. If the input is integral, the output is a double precision float.
  93. The container must be forward iterable and the contents are not modified.
  94. Only supports integral /p/ for two reasons: The computation is much slower for real /p/, and the non-integral \u2113[super /p/] norm is rarely used.
  95. [heading \u2113[super /p/] distance]
  96. std::vector<double> v{-8, 0, 0};
  97. std::vector<double> w{8, 0, 0};
  98. double dist = boost::math::tools::lp_distance(v, w, 7);
  99. // dist = 16
  100. std::vector<std::complex<double>> v{{1, 0}, {0,1}, {0,-1}};
  101. double dist = boost::math::tools::lp_distance(v, v, 3);
  102. // dist = 0
  103. Supports both real, integral, and complex arithmetic.
  104. If the input is integral, the output is a double precision float.
  105. The container must be forward iterable and the contents are not modified.
  106. Only supports integer /p/.
  107. [heading \u2113[super 0] pseudo-norm]
  108. Counts the number of non-zero elements in a container.
  109. std::vector<double> v{0,0,1};
  110. size_t count = boost::math::tools::l0_pseudo_norm(v.begin(), v.end());
  111. // count = 1
  112. Supports real, integral, and complex numbers.
  113. The container must be forward iterable and the contents are not modified.
  114. Note that this measure is not robust against numerical noise and is therefore not as useful as (say) the Hoyer sparsity in numerical applications.
  115. Works with real, complex, and integral inputs.
  116. [heading Hamming Distance]
  117. Compute the number of non-equal elements between two vectors /w/ and /v/:
  118. std::vector<double> v{0,0,1};
  119. std::vector<double> w{1,0,0};
  120. size_t count = boost::math::tools::hamming_distance(w, v);
  121. // count = 2
  122. Works for any datatype for which the operator `!=` is defined.
  123. [heading \u2113[super 1] norm]
  124. The \u2113[super 1] norm is a special case of the \u2113[super /p/] norm, but is much faster:
  125. std::vector<double> v{1,1,1};
  126. double l1 = boost::math::tools::l1_norm(v.begin(), v.end());
  127. // l1 = 3
  128. Requires a forward iterable input, does not modify input data, and works with real, integral, and complex numbers.
  129. [heading \u2113[super 1] distance]
  130. Computes the \u2113[super 1] distance between two vectors:
  131. std::vector<double> v{1,1,1};
  132. std::vector<double> w{1,1,1};
  133. double dist = boost::math::tools::l1_distance(w, v);
  134. // dist = 0
  135. Requires a forward iterable inputs, does not modify input data, and works with real, integral, and complex numbers.
  136. If the input type is integral, the output is a double precision float.
  137. [heading \u2113[super 2] norm]
  138. The \u2113[super 2] norm is again a special case of the \u2113[super /p/] norm, but is much faster:
  139. std::vector<double> v{1,1,1};
  140. double l2 = boost::math::tools::l2_norm(v.begin(), v.end());
  141. // l2 = sqrt(3)
  142. Requires a forward iterable input, does not modify input data, and works with real, complex and integral data.
  143. If the input is integral, the output is a double precision float.
  144. [heading \u2113[super 2] distance]
  145. Compute the \u2113[super 2] distance between two vectors /w/ and /v/:
  146. std::vector<double> v{1,1,1};
  147. std::vector<double> w{1,2,1};
  148. double dist = boost::math::tools::l2_distance(w, v);
  149. // dist = 1
  150. Requires a forward iterable input, does not modify input data, and works with real, complex numbers, and integral data.
  151. If the input type is integral, the output is a double precision float.
  152. [heading Total Variation]
  153. std::vector<double> v{1,1,1};
  154. double tv = boost::math::tools::total_variation(v.begin(), v.end());
  155. // no variation in v, so tv = 0.
  156. v = {0,1};
  157. double tv = boost::math::tools::total_variation(v.begin(), v.end());
  158. // variation is 1, so tv = 1.
  159. std::vector<int> v{1,1,1};
  160. double tv = boost::math::tools::total_variation(v);
  161. The total variation only supports real numbers and integers.
  162. If the input is integral, the output is a double precision float.
  163. All the constituent operations to compute the total variation are well-defined for complex numbers,
  164. but the computed result is not meaningful; a 2D total variation is more appropriate.
  165. The container must be forward iterable, and the contents are not modified.
  166. As an aside, the total variation is not technically a norm, since /TV(v) = 0/ does not imply /v = 0/.
  167. However, it satisfies the triangle inequality and is absolutely 1-homogeneous, so it is a seminorm, and hence is grouped with the other norms here.
  168. [heading References]
  169. * Higham, Nicholas J. ['Accuracy and stability of numerical algorithms.] Vol. 80. Siam, 2002.
  170. * Mallat, Stephane. ['A wavelet tour of signal processing: the sparse way.] Academic press, 2008.
  171. * Hurley, Niall, and Scott Rickard. ['Comparing measures of sparsity.] IEEE Transactions on Information Theory 55.10 (2009): 4723-4741.
  172. [endsect]
  173. [/section:norms Norms]