norms.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. // (C) Copyright Nick Thompson 2018.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_TOOLS_NORMS_HPP
  6. #define BOOST_MATH_TOOLS_NORMS_HPP
  7. #include <algorithm>
  8. #include <iterator>
  9. #include <complex>
  10. #include <cmath>
  11. #include <boost/math/tools/assert.hpp>
  12. #include <boost/math/tools/complex.hpp>
  13. #include <boost/math/tools/is_standalone.hpp>
  14. #ifndef BOOST_MATH_STANDALONE
  15. #include <boost/config.hpp>
  16. #ifdef BOOST_MATH_NO_CXX17_IF_CONSTEXPR
  17. #error "The header <boost/math/norms.hpp> can only be used in C++17 and later."
  18. #endif
  19. #endif
  20. namespace boost::math::tools {
  21. // Mallat, "A Wavelet Tour of Signal Processing", equation 2.60:
  22. template<class ForwardIterator>
  23. auto total_variation(ForwardIterator first, ForwardIterator last)
  24. {
  25. using T = typename std::iterator_traits<ForwardIterator>::value_type;
  26. using std::abs;
  27. BOOST_MATH_ASSERT_MSG(first != last && std::next(first) != last, "At least two samples are required to compute the total variation.");
  28. auto it = first;
  29. if constexpr (std::is_unsigned<T>::value)
  30. {
  31. T tmp = *it;
  32. double tv = 0;
  33. while (++it != last)
  34. {
  35. if (*it > tmp)
  36. {
  37. tv += *it - tmp;
  38. }
  39. else
  40. {
  41. tv += tmp - *it;
  42. }
  43. tmp = *it;
  44. }
  45. return tv;
  46. }
  47. else if constexpr (std::is_integral<T>::value)
  48. {
  49. double tv = 0;
  50. double tmp = *it;
  51. while(++it != last)
  52. {
  53. double tmp2 = *it;
  54. tv += abs(tmp2 - tmp);
  55. tmp = *it;
  56. }
  57. return tv;
  58. }
  59. else
  60. {
  61. T tmp = *it;
  62. T tv = 0;
  63. while (++it != last)
  64. {
  65. tv += abs(*it - tmp);
  66. tmp = *it;
  67. }
  68. return tv;
  69. }
  70. }
  71. template<class Container>
  72. inline auto total_variation(Container const & v)
  73. {
  74. return total_variation(v.cbegin(), v.cend());
  75. }
  76. template<class ForwardIterator>
  77. auto sup_norm(ForwardIterator first, ForwardIterator last)
  78. {
  79. BOOST_MATH_ASSERT_MSG(first != last, "At least one value is required to compute the sup norm.");
  80. using T = typename std::iterator_traits<ForwardIterator>::value_type;
  81. using std::abs;
  82. if constexpr (boost::math::tools::is_complex_type<T>::value)
  83. {
  84. auto it = std::max_element(first, last, [](T a, T b) { return abs(b) > abs(a); });
  85. return abs(*it);
  86. }
  87. else if constexpr (std::is_unsigned<T>::value)
  88. {
  89. return *std::max_element(first, last);
  90. }
  91. else
  92. {
  93. auto pair = std::minmax_element(first, last);
  94. if (abs(*pair.first) > abs(*pair.second))
  95. {
  96. return abs(*pair.first);
  97. }
  98. else
  99. {
  100. return abs(*pair.second);
  101. }
  102. }
  103. }
  104. template<class Container>
  105. inline auto sup_norm(Container const & v)
  106. {
  107. return sup_norm(v.cbegin(), v.cend());
  108. }
  109. template<class ForwardIterator>
  110. auto l1_norm(ForwardIterator first, ForwardIterator last)
  111. {
  112. using T = typename std::iterator_traits<ForwardIterator>::value_type;
  113. using std::abs;
  114. if constexpr (std::is_unsigned<T>::value)
  115. {
  116. double l1 = 0;
  117. for (auto it = first; it != last; ++it)
  118. {
  119. l1 += *it;
  120. }
  121. return l1;
  122. }
  123. else if constexpr (std::is_integral<T>::value)
  124. {
  125. double l1 = 0;
  126. for (auto it = first; it != last; ++it)
  127. {
  128. double tmp = *it;
  129. l1 += abs(tmp);
  130. }
  131. return l1;
  132. }
  133. else
  134. {
  135. decltype(abs(*first)) l1 = 0;
  136. for (auto it = first; it != last; ++it)
  137. {
  138. l1 += abs(*it);
  139. }
  140. return l1;
  141. }
  142. }
  143. template<class Container>
  144. inline auto l1_norm(Container const & v)
  145. {
  146. return l1_norm(v.cbegin(), v.cend());
  147. }
  148. template<class ForwardIterator>
  149. auto l2_norm(ForwardIterator first, ForwardIterator last)
  150. {
  151. using T = typename std::iterator_traits<ForwardIterator>::value_type;
  152. using std::abs;
  153. using std::norm;
  154. using std::sqrt;
  155. using std::is_floating_point;
  156. using std::isfinite;
  157. if constexpr (boost::math::tools::is_complex_type<T>::value)
  158. {
  159. typedef typename T::value_type Real;
  160. Real l2 = 0;
  161. for (auto it = first; it != last; ++it)
  162. {
  163. l2 += norm(*it);
  164. }
  165. Real result = sqrt(l2);
  166. if (!isfinite(result))
  167. {
  168. Real a = sup_norm(first, last);
  169. l2 = 0;
  170. for (auto it = first; it != last; ++it)
  171. {
  172. l2 += norm(*it/a);
  173. }
  174. return a*sqrt(l2);
  175. }
  176. return result;
  177. }
  178. else if constexpr (is_floating_point<T>::value ||
  179. std::numeric_limits<T>::max_exponent)
  180. {
  181. T l2 = 0;
  182. for (auto it = first; it != last; ++it)
  183. {
  184. l2 += (*it)*(*it);
  185. }
  186. T result = sqrt(l2);
  187. // Higham, Accuracy and Stability of Numerical Algorithms,
  188. // Problem 27.5 presents a different algorithm to deal with overflow.
  189. // The algorithm used here takes 3 passes *if* there is overflow.
  190. // Higham's algorithm is 1 pass, but more requires operations than the no overflow case.
  191. // I'm operating under the assumption that overflow is rare since the dynamic range of floating point numbers is huge.
  192. if (!isfinite(result))
  193. {
  194. T a = sup_norm(first, last);
  195. l2 = 0;
  196. for (auto it = first; it != last; ++it)
  197. {
  198. T tmp = *it/a;
  199. l2 += tmp*tmp;
  200. }
  201. return a*sqrt(l2);
  202. }
  203. return result;
  204. }
  205. else
  206. {
  207. double l2 = 0;
  208. for (auto it = first; it != last; ++it)
  209. {
  210. double tmp = *it;
  211. l2 += tmp*tmp;
  212. }
  213. return sqrt(l2);
  214. }
  215. }
  216. template<class Container>
  217. inline auto l2_norm(Container const & v)
  218. {
  219. return l2_norm(v.cbegin(), v.cend());
  220. }
  221. template<class ForwardIterator>
  222. size_t l0_pseudo_norm(ForwardIterator first, ForwardIterator last)
  223. {
  224. using RealOrComplex = typename std::iterator_traits<ForwardIterator>::value_type;
  225. size_t count = 0;
  226. for (auto it = first; it != last; ++it)
  227. {
  228. if (*it != RealOrComplex(0))
  229. {
  230. ++count;
  231. }
  232. }
  233. return count;
  234. }
  235. template<class Container>
  236. inline size_t l0_pseudo_norm(Container const & v)
  237. {
  238. return l0_pseudo_norm(v.cbegin(), v.cend());
  239. }
  240. template<class ForwardIterator>
  241. size_t hamming_distance(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2)
  242. {
  243. size_t count = 0;
  244. auto it1 = first1;
  245. auto it2 = first2;
  246. while (it1 != last1)
  247. {
  248. if (*it1++ != *it2++)
  249. {
  250. ++count;
  251. }
  252. }
  253. return count;
  254. }
  255. template<class Container>
  256. inline size_t hamming_distance(Container const & v, Container const & w)
  257. {
  258. return hamming_distance(v.cbegin(), v.cend(), w.cbegin());
  259. }
  260. template<class ForwardIterator>
  261. auto lp_norm(ForwardIterator first, ForwardIterator last, unsigned p)
  262. {
  263. using std::abs;
  264. using std::pow;
  265. using std::is_floating_point;
  266. using std::isfinite;
  267. using RealOrComplex = typename std::iterator_traits<ForwardIterator>::value_type;
  268. if constexpr (boost::math::tools::is_complex_type<RealOrComplex>::value)
  269. {
  270. using std::norm;
  271. using Real = typename RealOrComplex::value_type;
  272. Real lp = 0;
  273. for (auto it = first; it != last; ++it)
  274. {
  275. lp += pow(abs(*it), p);
  276. }
  277. auto result = pow(lp, Real(1)/Real(p));
  278. if (!isfinite(result))
  279. {
  280. auto a = boost::math::tools::sup_norm(first, last);
  281. Real lp = 0;
  282. for (auto it = first; it != last; ++it)
  283. {
  284. lp += pow(abs(*it)/a, p);
  285. }
  286. result = a*pow(lp, Real(1)/Real(p));
  287. }
  288. return result;
  289. }
  290. else if constexpr (is_floating_point<RealOrComplex>::value || std::numeric_limits<RealOrComplex>::max_exponent)
  291. {
  292. BOOST_MATH_ASSERT_MSG(p >= 0, "For p < 0, the lp norm is not a norm");
  293. RealOrComplex lp = 0;
  294. for (auto it = first; it != last; ++it)
  295. {
  296. lp += pow(abs(*it), p);
  297. }
  298. RealOrComplex result = pow(lp, RealOrComplex(1)/RealOrComplex(p));
  299. if (!isfinite(result))
  300. {
  301. RealOrComplex a = boost::math::tools::sup_norm(first, last);
  302. lp = 0;
  303. for (auto it = first; it != last; ++it)
  304. {
  305. lp += pow(abs(*it)/a, p);
  306. }
  307. result = a*pow(lp, RealOrComplex(1)/RealOrComplex(p));
  308. }
  309. return result;
  310. }
  311. else
  312. {
  313. double lp = 0;
  314. for (auto it = first; it != last; ++it)
  315. {
  316. double tmp = *it;
  317. lp += pow(abs(tmp), p);
  318. }
  319. double result = pow(lp, 1.0/static_cast<double>(p));
  320. if (!isfinite(result))
  321. {
  322. double a = boost::math::tools::sup_norm(first, last);
  323. lp = 0;
  324. for (auto it = first; it != last; ++it)
  325. {
  326. double tmp = *it;
  327. lp += pow(abs(tmp)/a, p);
  328. }
  329. result = a*pow(lp, static_cast<double>(1)/static_cast<double>(p));
  330. }
  331. return result;
  332. }
  333. }
  334. template<class Container>
  335. inline auto lp_norm(Container const & v, unsigned p)
  336. {
  337. return lp_norm(v.cbegin(), v.cend(), p);
  338. }
  339. template<class ForwardIterator>
  340. auto lp_distance(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2, unsigned p)
  341. {
  342. using std::pow;
  343. using std::abs;
  344. using std::is_floating_point;
  345. using std::isfinite;
  346. using RealOrComplex = typename std::iterator_traits<ForwardIterator>::value_type;
  347. auto it1 = first1;
  348. auto it2 = first2;
  349. if constexpr (boost::math::tools::is_complex_type<RealOrComplex>::value)
  350. {
  351. using Real = typename RealOrComplex::value_type;
  352. using std::norm;
  353. Real dist = 0;
  354. while(it1 != last1)
  355. {
  356. auto tmp = *it1++ - *it2++;
  357. dist += pow(abs(tmp), p);
  358. }
  359. return pow(dist, Real(1)/Real(p));
  360. }
  361. else if constexpr (is_floating_point<RealOrComplex>::value || std::numeric_limits<RealOrComplex>::max_exponent)
  362. {
  363. RealOrComplex dist = 0;
  364. while(it1 != last1)
  365. {
  366. auto tmp = *it1++ - *it2++;
  367. dist += pow(abs(tmp), p);
  368. }
  369. return pow(dist, RealOrComplex(1)/RealOrComplex(p));
  370. }
  371. else
  372. {
  373. double dist = 0;
  374. while(it1 != last1)
  375. {
  376. double tmp1 = *it1++;
  377. double tmp2 = *it2++;
  378. // Naively you'd expect the integer subtraction to be faster,
  379. // but this can overflow or wraparound:
  380. //double tmp = *it1++ - *it2++;
  381. dist += pow(abs(tmp1 - tmp2), p);
  382. }
  383. return pow(dist, 1.0/static_cast<double>(p));
  384. }
  385. }
  386. template<class Container>
  387. inline auto lp_distance(Container const & v, Container const & w, unsigned p)
  388. {
  389. return lp_distance(v.cbegin(), v.cend(), w.cbegin(), p);
  390. }
  391. template<class ForwardIterator>
  392. auto l1_distance(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2)
  393. {
  394. using std::abs;
  395. using std::is_floating_point;
  396. using std::isfinite;
  397. using T = typename std::iterator_traits<ForwardIterator>::value_type;
  398. auto it1 = first1;
  399. auto it2 = first2;
  400. if constexpr (boost::math::tools::is_complex_type<T>::value)
  401. {
  402. using Real = typename T::value_type;
  403. Real sum = 0;
  404. while (it1 != last1) {
  405. sum += abs(*it1++ - *it2++);
  406. }
  407. return sum;
  408. }
  409. else if constexpr (is_floating_point<T>::value || std::numeric_limits<T>::max_exponent)
  410. {
  411. T sum = 0;
  412. while (it1 != last1)
  413. {
  414. sum += abs(*it1++ - *it2++);
  415. }
  416. return sum;
  417. }
  418. else if constexpr (std::is_unsigned<T>::value)
  419. {
  420. double sum = 0;
  421. while(it1 != last1)
  422. {
  423. T x1 = *it1++;
  424. T x2 = *it2++;
  425. if (x1 > x2)
  426. {
  427. sum += (x1 - x2);
  428. }
  429. else
  430. {
  431. sum += (x2 - x1);
  432. }
  433. }
  434. return sum;
  435. }
  436. else if constexpr (std::is_integral<T>::value)
  437. {
  438. double sum = 0;
  439. while(it1 != last1)
  440. {
  441. double x1 = *it1++;
  442. double x2 = *it2++;
  443. sum += abs(x1-x2);
  444. }
  445. return sum;
  446. }
  447. else
  448. {
  449. BOOST_MATH_ASSERT_MSG(false, "Could not recognize type.");
  450. }
  451. }
  452. template<class Container>
  453. auto l1_distance(Container const & v, Container const & w)
  454. {
  455. using std::size;
  456. BOOST_MATH_ASSERT_MSG(size(v) == size(w),
  457. "L1 distance requires both containers to have the same number of elements");
  458. return l1_distance(v.cbegin(), v.cend(), w.begin());
  459. }
  460. template<class ForwardIterator>
  461. auto l2_distance(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2)
  462. {
  463. using std::abs;
  464. using std::norm;
  465. using std::sqrt;
  466. using std::is_floating_point;
  467. using std::isfinite;
  468. using T = typename std::iterator_traits<ForwardIterator>::value_type;
  469. auto it1 = first1;
  470. auto it2 = first2;
  471. if constexpr (boost::math::tools::is_complex_type<T>::value)
  472. {
  473. using Real = typename T::value_type;
  474. Real sum = 0;
  475. while (it1 != last1) {
  476. sum += norm(*it1++ - *it2++);
  477. }
  478. return sqrt(sum);
  479. }
  480. else if constexpr (is_floating_point<T>::value || std::numeric_limits<T>::max_exponent)
  481. {
  482. T sum = 0;
  483. while (it1 != last1)
  484. {
  485. T tmp = *it1++ - *it2++;
  486. sum += tmp*tmp;
  487. }
  488. return sqrt(sum);
  489. }
  490. else if constexpr (std::is_unsigned<T>::value)
  491. {
  492. double sum = 0;
  493. while(it1 != last1)
  494. {
  495. T x1 = *it1++;
  496. T x2 = *it2++;
  497. if (x1 > x2)
  498. {
  499. double tmp = x1-x2;
  500. sum += tmp*tmp;
  501. }
  502. else
  503. {
  504. double tmp = x2 - x1;
  505. sum += tmp*tmp;
  506. }
  507. }
  508. return sqrt(sum);
  509. }
  510. else
  511. {
  512. double sum = 0;
  513. while(it1 != last1)
  514. {
  515. double x1 = *it1++;
  516. double x2 = *it2++;
  517. double tmp = x1-x2;
  518. sum += tmp*tmp;
  519. }
  520. return sqrt(sum);
  521. }
  522. }
  523. template<class Container>
  524. auto l2_distance(Container const & v, Container const & w)
  525. {
  526. using std::size;
  527. BOOST_MATH_ASSERT_MSG(size(v) == size(w),
  528. "L2 distance requires both containers to have the same number of elements");
  529. return l2_distance(v.cbegin(), v.cend(), w.begin());
  530. }
  531. template<class ForwardIterator>
  532. auto sup_distance(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2)
  533. {
  534. using std::abs;
  535. using std::norm;
  536. using std::sqrt;
  537. using std::is_floating_point;
  538. using std::isfinite;
  539. using T = typename std::iterator_traits<ForwardIterator>::value_type;
  540. auto it1 = first1;
  541. auto it2 = first2;
  542. if constexpr (boost::math::tools::is_complex_type<T>::value)
  543. {
  544. using Real = typename T::value_type;
  545. Real sup_sq = 0;
  546. while (it1 != last1) {
  547. Real tmp = norm(*it1++ - *it2++);
  548. if (tmp > sup_sq) {
  549. sup_sq = tmp;
  550. }
  551. }
  552. return sqrt(sup_sq);
  553. }
  554. else if constexpr (is_floating_point<T>::value || std::numeric_limits<T>::max_exponent)
  555. {
  556. T sup = 0;
  557. while (it1 != last1)
  558. {
  559. T tmp = *it1++ - *it2++;
  560. if (sup < abs(tmp))
  561. {
  562. sup = abs(tmp);
  563. }
  564. }
  565. return sup;
  566. }
  567. else // integral values:
  568. {
  569. double sup = 0;
  570. while(it1 != last1)
  571. {
  572. T x1 = *it1++;
  573. T x2 = *it2++;
  574. double tmp;
  575. if (x1 > x2)
  576. {
  577. tmp = x1-x2;
  578. }
  579. else
  580. {
  581. tmp = x2 - x1;
  582. }
  583. if (sup < tmp) {
  584. sup = tmp;
  585. }
  586. }
  587. return sup;
  588. }
  589. }
  590. template<class Container>
  591. auto sup_distance(Container const & v, Container const & w)
  592. {
  593. using std::size;
  594. BOOST_MATH_ASSERT_MSG(size(v) == size(w),
  595. "sup distance requires both containers to have the same number of elements");
  596. return sup_distance(v.cbegin(), v.cend(), w.begin());
  597. }
  598. }
  599. #endif