wedge.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file wedge-product.cc
  3. /// @brief Test generic wedge product with compile-time dimensions.
  4. // (c) Daniel Llorens - 2008-2010, 2015
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include <iostream>
  10. #include "ra/ra.hh"
  11. #include "ra/test.hh"
  12. using std::cout, std::endl, std::flush, ra::TestRecorder;
  13. using fun::Wedge, fun::hodge, fun::hodgex;
  14. using real = double;
  15. using complex = std::complex<double>;
  16. real const GARBAGE(99);
  17. template <class T, ra::dim_t N> using vec = ra::Small<T, N>;
  18. using real1 = vec<real, 1>;
  19. using real2 = vec<real, 2>;
  20. using real3 = vec<real, 3>;
  21. using real4 = vec<real, 4>;
  22. using real6 = vec<real, 6>;
  23. using complex1 = vec<complex, 1>;
  24. using complex2 = vec<complex, 2>;
  25. using complex3 = vec<complex, 3>;
  26. template <class P, class Plist, int w, int s>
  27. struct FindCombinationTester
  28. {
  29. using finder = mp::FindCombination<P, Plist>;
  30. static_assert(finder::where==w && finder::sign==s, "bad");
  31. static void check() {};
  32. };
  33. template <int N, int O>
  34. void test_optimized_hodge_aux(TestRecorder & tr)
  35. {
  36. if constexpr (O<=N) {
  37. tr.section(ra::format("hodge() vs hodgex() with N=", N, " O=", O));
  38. static_assert(N>=O, "bad_N_or_bad_O");
  39. using Va = vec<real, fun::Wedge<N, O, N-O>::Na>;
  40. using Vb = vec<real, fun::Wedge<N, O, N-O>::Nb>;
  41. Va u = ra::iota(u.size(), 1);
  42. Vb w(GARBAGE);
  43. hodge<N, O>(u, w);
  44. cout << "-> " << u << " hodge " << w << endl;
  45. // this is the property that u^(*u) = dot(u, u)*vol form.
  46. if (O==1) {
  47. real S = sum(sqr(u));
  48. // since the volume form and the 1-forms are always ordered lexicographically (0 1 2...) vs (0) (1) (2) ...
  49. tr.info("with O=1, S: ", S, " vs wedge(u, w): ", ra::wedge<N, O, N-O>(u, w))
  50. .test_eq(S, ra::wedge<N, O, N-O>(u, w));
  51. } else if (O+1==N) {
  52. real S = sum(sqr(w));
  53. // compare with the case above, this is the sign of the (anti)commutativity of the exterior product.
  54. S *= odd(O*(N-O)) ? -1 : +1;
  55. tr.info("with O=N-1, S: ", S, " vs wedge(u, w): ", ra::wedge<N, N-O, O>(u, w))
  56. .test_eq(S, ra::wedge<N, N-O, O>(u, w));
  57. }
  58. // test that it does the same as hodgex().
  59. Vb x(GARBAGE);
  60. hodgex<N, O>(u, x);
  61. if (2*O==N) {
  62. tr.info("-> ", u, " hodgex ", x).test_eq(ra::wedge<N, O, N-O>(u, w), ra::wedge<N, O, N-O>(u, x));
  63. }
  64. // test basic duality property, **w = (-1)^{o(n-o)} w.
  65. {
  66. Va b(GARBAGE);
  67. hodgex<N, N-O>(x, b);
  68. tr.info("duality test with hodgex() (N ", N, " O ", O, ") -> ", u, " hodge ", x, " hodge(hodge) ", b)
  69. .test_eq((odd(O*(N-O)) ? -1 : +1)*u, b);
  70. }
  71. {
  72. Va a(GARBAGE);
  73. hodge<N, N-O>(w, a);
  74. tr.info("duality test with hodge() (N ", N, " O ", O, ") -> ", u, " hodge ", w, " hodge(hodge) ", a)
  75. .test_eq((odd(O*(N-O)) ? -1 : +1)*u, a);
  76. }
  77. test_optimized_hodge_aux<N, O+1>(tr);
  78. }
  79. }
  80. template <int N>
  81. void test_optimized_hodge(TestRecorder & tr)
  82. {
  83. static_assert(N>=0, "bad_N");
  84. test_optimized_hodge_aux<N, 0>(tr);
  85. test_optimized_hodge<N-1>(tr);
  86. }
  87. template <>
  88. void test_optimized_hodge<-1>(TestRecorder & tr)
  89. {
  90. }
  91. template <int D, class R, class A, class B>
  92. R test_scalar_case(A const & a, B const & b)
  93. {
  94. R r = ra::wedge<D, 0, 0>(a, b);
  95. cout << "[" << D << "/0/0] " << a << " ^ " << b << " -> " << r << endl;
  96. return r;
  97. }
  98. template <int D, int OA, int OB, class R, class A, class B>
  99. R test_one_one_case(TestRecorder & tr, A const & a, B const & b)
  100. {
  101. R r1(GARBAGE);
  102. Wedge<D, OA, OB>::product(a, b, r1);
  103. cout << "[" << D << "/" << OA << "/" << OB << "] " << a << " ^ " << b << " -> " << r1 << endl;
  104. R r2(ra::wedge<D, OA, OB>(a, b));
  105. cout << "[" << D << "/" << OA << "/" << OB << "] " << a << " ^ " << b << " -> " << r2 << endl;
  106. tr.test_eq(r1, r2);
  107. return r1;
  108. }
  109. template <int D, int OA, int OB, class R, class A, class B>
  110. R test_one_scalar_case(A const & a, B const & b)
  111. {
  112. R r2(ra::wedge<D, OA, OB>(a, b));
  113. cout << "[" << D << "/" << OA << "/" << OB << "] " << a << " ^ " << b << " -> " << r2 << endl;
  114. return r2;
  115. }
  116. int main()
  117. {
  118. TestRecorder tr(std::cout);
  119. static_assert(fun::n_over_p(0, 0)==1, "");
  120. // gcc let a bug pass by https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57891. Cf http://stackoverflow.com/a/24346350.
  121. // TODO We weren't getting the proper diagnostic from clang, probably due to disable_if doing !(integer).
  122. tr.section("MatchPermutationP");
  123. {
  124. using thematch = mp::MatchPermutationP<mp::int_list<1, 0>>::type<mp::int_list<0, 1>>;
  125. cout << "A... " << (thematch::value) << endl;
  126. using index_if = mp::IndexIf<std::tuple<mp::int_list<1, 0>>, mp::MatchPermutationP<mp::int_list<0, 1>>::template type>;
  127. cout << "B... " << index_if::value << endl;
  128. static_assert(index_if::value==0, "bad MatchPermutationP");
  129. }
  130. tr.section("Testing FindCombination");
  131. {
  132. using la = mp::iota<3>;
  133. using ca = mp::combinations<la, 2>;
  134. FindCombinationTester<mp::int_list<0, 1>, ca, 0, +1>::check();
  135. FindCombinationTester<mp::int_list<1, 0>, ca, 0, -1>::check();
  136. FindCombinationTester<mp::int_list<0, 2>, ca, 1, +1>::check();
  137. FindCombinationTester<mp::int_list<2, 0>, ca, 1, -1>::check();
  138. FindCombinationTester<mp::int_list<1, 2>, ca, 2, +1>::check();
  139. FindCombinationTester<mp::int_list<2, 1>, ca, 2, -1>::check();
  140. FindCombinationTester<mp::int_list<0, 0>, ca, -1, 0>::check();
  141. FindCombinationTester<mp::int_list<1, 1>, ca, -1, 0>::check();
  142. FindCombinationTester<mp::int_list<2, 2>, ca, -1, 0>::check();
  143. FindCombinationTester<mp::int_list<3, 0>, ca, -1, 0>::check();
  144. }
  145. tr.section("Testing AntiCombination");
  146. {
  147. using la = mp::iota<3>;
  148. using ca = mp::combinations<la, 1>;
  149. using cc0 = mp::AntiCombination<mp::ref<ca, 0>, 3>::type;
  150. static_assert(mp::check_idx<cc0, 1, 2>::value, "bad");
  151. using cc1 = mp::AntiCombination<mp::ref<ca, 1>, 3>::type;
  152. static_assert(mp::check_idx<cc1, 2, 0>::value, "bad");
  153. using cc2 = mp::AntiCombination<mp::ref<ca, 2>, 3>::type;
  154. static_assert(mp::check_idx<cc2, 0, 1>::value, "bad");
  155. }
  156. tr.section("Testing ChooseComponents");
  157. {
  158. using c1 = mp::ChooseComponents<3, 1>::type;
  159. static_assert(mp::len<c1> == 3, "bad");
  160. static_assert(mp::check_idx<mp::ref<c1, 0>, 0>::value, "bad");
  161. static_assert(mp::check_idx<mp::ref<c1, 1>, 1>::value, "bad");
  162. static_assert(mp::check_idx<mp::ref<c1, 2>, 2>::value, "bad");
  163. using c2 = mp::ChooseComponents<3, 2>::type;
  164. static_assert(mp::len<c2> == 3, "bad");
  165. static_assert(mp::check_idx<mp::ref<c2, 0>, 1, 2>::value, "bad");
  166. static_assert(mp::check_idx<mp::ref<c2, 1>, 2, 0>::value, "bad");
  167. static_assert(mp::check_idx<mp::ref<c2, 2>, 0, 1>::value, "bad");
  168. using c3 = mp::ChooseComponents<3, 3>::type;
  169. static_assert(mp::len<c3> == 1, "bad");
  170. static_assert(mp::check_idx<mp::ref<c3, 0>, 0, 1, 2>::value, "bad");
  171. }
  172. {
  173. using c0 = mp::ChooseComponents<1, 0>::type;
  174. static_assert(mp::len<c0> == 1, "bad");
  175. static_assert(mp::check_idx<mp::ref<c0, 0>>::value, "bad");
  176. using c1 = mp::ChooseComponents<1, 1>::type;
  177. static_assert(mp::len<c1> == 1, "bad");
  178. static_assert(mp::check_idx<mp::ref<c1, 0>, 0>::value, "bad");
  179. }
  180. tr.section("Testing Wedge<>::product()");
  181. {
  182. real1 a(1);
  183. real1 b(3);
  184. real1 r(GARBAGE);
  185. Wedge<1, 0, 0>::product(a, b, r);
  186. tr.info("[1/0/0] ", a, " ^ ", b, " -> ", r).test_eq(3, r[0]);
  187. real1 h(GARBAGE);
  188. hodgex<1, 0>(r, h);
  189. tr.info("thodge-star: ", h).test_eq(3, h[0]);
  190. }
  191. tr.section("change order changes sign");
  192. {
  193. real3 a{1, 0, 0};
  194. real3 b{0, 1, 0};
  195. real3 r(GARBAGE);
  196. Wedge<3, 1, 1>::product(a, b, r);
  197. tr.info("[3/1/1] ", a, " ^ ", b, " -> ", r).test_eq(real3{0, 0, +1}, r); // +1, 0, 0 in lex. order.
  198. real3 h(GARBAGE);
  199. hodgex<3, 2>(r, h);
  200. tr.info("hodge-star: ", h).test_eq(real3{0, 0, 1}, h);
  201. }
  202. {
  203. real3 a{0, 1, 0};
  204. real3 b{1, 0, 0};
  205. real3 r(GARBAGE);
  206. Wedge<3, 1, 1>::product(a, b, r);
  207. tr.info("[3/1/1] ", a, " ^ ", b, " -> ", r).test_eq(real3{0, 0, -1}, r); // -1, 0, 0 in lex order.
  208. real3 h(GARBAGE);
  209. hodgex<3, 2>(r, h);
  210. tr.info("hodge-star: ", h).test_eq(real3{0, 0, -1}, h);
  211. }
  212. tr.section("check type promotion");
  213. {
  214. complex3 a{complex(0, 1), 0, 0};
  215. real3 b{0, 1, 0};
  216. complex3 r(GARBAGE);
  217. Wedge<3, 1, 1>::product(a, b, r);
  218. tr.info("[3/1/1] ", a, " ^ ", b, " -> ", r).test_eq(complex3{0, 0, complex(0, 1)}, r); // +j, 0, 0 in lex. o.
  219. complex3 h(GARBAGE);
  220. hodgex<3, 2>(r, h);
  221. tr.info("hodge-star: ", h).test_eq(complex3{0, 0, complex(0, 1)}, h);
  222. }
  223. tr.section("sign change in going from lexicographic -> our peculiar order");
  224. {
  225. real3 a{1, 0, 0};
  226. real3 b{0, 0, 2};
  227. real3 r(GARBAGE);
  228. Wedge<3, 1, 1>::product(a, b, r);
  229. tr.info("[3/1/1] ", a, " ^ ", b, " -> ", r).test_eq(real3{0, -2, 0}, r); // 0, 2, 0 in lex order.
  230. real3 h(GARBAGE);
  231. hodgex<3, 2>(r, h);
  232. tr.info("hodge-star: ", h).test_eq(real3{0, -2, 0}, h);
  233. }
  234. {
  235. real3 a{1, 0, 2};
  236. real3 b{1, 0, 2};
  237. real3 r(GARBAGE);
  238. Wedge<3, 1, 1>::product(a, b, r);
  239. tr.info("[3/1/1] ", a, " ^ ", b, " -> ", r).test_eq(0., r);
  240. real3 h(GARBAGE);
  241. hodgex<3, 2>(r, h);
  242. tr.info("hodge-star: ", h).test_eq(0., h);
  243. }
  244. {
  245. real3 a{0, 1, 0};
  246. real3 b{0, -1, 0}; // 0, 1, 0 in lex order.
  247. real1 r(GARBAGE);
  248. Wedge<3, 1, 2>::product(a, b, r);
  249. tr.info("[3/1/2] ", a, " ^ ", b, " -> ", r).test_eq(-1, r[0]);
  250. real1 h(GARBAGE);
  251. hodgex<3, 3>(r, h);
  252. tr.info("\thodge-star: ", h).test_eq(-1, h[0]);
  253. // this is not forced for hodgex (depends on vec::ChooseComponents<> as used in fun::Wedge<>) so if you change that, change this too.
  254. real3 c;
  255. hodgex<3, 1>(b, c);
  256. tr.info("hodge<3, 1>(", b, "): ", c).test_eq(real3{0, -1, 0}, b);
  257. hodgex<3, 2>(b, c);
  258. tr.info("hodge<3, 2>(", b, "): ", c).test_eq(real3{0, -1, 0}, b);
  259. }
  260. {
  261. real4 a{1, 0, 0, 0};
  262. real4 b{0, 0, 1, 0};
  263. real6 r(GARBAGE);
  264. Wedge<4, 1, 1>::product(a, b, r);
  265. tr.info("[4/1/1] ", a, " ^ ", b, " -> ", r).test_eq(real6{0, 1, 0, 0, 0, 0}, r);
  266. real6 h(GARBAGE);
  267. hodgex<4, 2>(r, h);
  268. tr.info("hodge-star: ", h).test_eq(real6{0, 0, 0, 0, -1, 0}, h);
  269. r = GARBAGE;
  270. hodgex<4, 2>(h, r);
  271. tr.info("hodge-star(hodge-star()): ", r).test_eq(real6{0, 1, 0, 0, 0, 0}, r);
  272. }
  273. {
  274. real4 a{0, 0, 1, 0};
  275. real4 b{1, 0, 0, 0};
  276. real6 r(GARBAGE);
  277. Wedge<4, 1, 1>::product(a, b, r);
  278. tr.info("[4/1/1] ", a, " ^ ", b, " -> ", r).test_eq(real6{0, -1, 0, 0, 0, 0}, r);
  279. }
  280. {
  281. real6 r{1, 0, 0, 0, 0, 0};
  282. real6 h(GARBAGE);
  283. hodgex<4, 2>(r, h);
  284. tr.info("r: ", r, " -> hodge-star: ", h).test_eq(real6{0, 0, 0, 0, 0, 1}, h);
  285. }
  286. tr.section("important as a case where a^b==b^a");
  287. {
  288. real6 a{1, 0, 0, 0, 0, 0};
  289. real6 b{0, 0, 0, 0, 0, 1};
  290. real1 r(GARBAGE);
  291. Wedge<4, 2, 2>::product(a, b, r);
  292. tr.info("[4/2/2] ", a, " ^ ", b, " -> ", r).test_eq(1, r[0]);
  293. Wedge<4, 2, 2>::product(b, a, r);
  294. tr.info("[4/2/2] ", a, " ^ ", b, " -> ", r).test_eq(1, r[0]);
  295. }
  296. tr.section("important as a case where a^a!=0, see DoCarmo1994, Ch. 1 after Prop. 2.");
  297. {
  298. real6 a{1, 0, 0, 0, 0, 1};
  299. real6 b{1, 0, 0, 0, 0, 1};
  300. real1 r(GARBAGE);
  301. Wedge<4, 2, 2>::product(a, b, r);
  302. tr.info("[4/2/2] ", a, " ^ ", b, " -> ", r).test_eq(2, r[0]);
  303. }
  304. tr.section("important as a case where a^b is not dot(a, b) even though O(a)=D-O(b). This happens when O(a)==O(b), i.e. they have the same components");
  305. {
  306. real2 a{1, 0};
  307. real2 b{0, 1};
  308. real1 r(GARBAGE);
  309. Wedge<2, 1, 1>::product(a, b, r);
  310. tr.info("[2/1/1] ", a, " ^ ", b, " -> ", r).test_eq(1, r[0]);
  311. real2 p{1, 2};
  312. real2 q(GARBAGE);
  313. hodgex<2, 1>(p, q);
  314. tr.info("p: ", p, " -> hodge-star: ", q).test_eq(real2{-2, 1}, q);
  315. }
  316. tr.section("test the specializations in cross(), wedge<>()");
  317. {
  318. real2 a{1, 0};
  319. real2 b{0, 1};
  320. real c(cross(a, b));
  321. tr.info("a cross b: ", c).test_eq(1, c);
  322. c = cross(b, a);
  323. tr.test_eq(-1, c);
  324. // accepts expr arguments.
  325. c = cross(a, b+1.);
  326. tr.test_eq(2, c);
  327. }
  328. tr.section("test the cross product some more");
  329. {
  330. real3 x3{1., 0. ,0.};
  331. real3 y3{0., 1., 0.};
  332. real3 z3{0., 0., 1.};
  333. tr.test_eq(z3, cross(x3, y3));
  334. tr.test_eq(x3, cross(y3, z3));
  335. tr.test_eq(y3, cross(z3, x3));
  336. tr.test_eq(-z3, cross(y3, x3));
  337. tr.test_eq(-x3, cross(z3, y3));
  338. tr.test_eq(-y3, cross(x3, z3));
  339. real2 x2{1., 0.};
  340. real2 y2{0., 1.};
  341. tr.test_eq(1., cross(x2, y2));
  342. tr.test_eq(-1., cross(y2, x2));
  343. complex2 cy2{0., 1.};
  344. tr.test_eq(complex(1., 0.), cross(x2, cy2));
  345. }
  346. tr.section("verify that wedge<>() returns an expression where appropriate");
  347. {
  348. real3 u{1., 2., 3.};
  349. real3 v{3., 2., 1.};
  350. tr.test_eq(10., ra::wedge<3, 1, 2>(u, v));
  351. tr.test_eq(cross(u, v), ra::wedge<3, 1, 1>(u, v));
  352. tr.test_eq(10., ra::wedge<3, 1, 2>(u, v));
  353. }
  354. tr.section("verify that we are allowed to choose our return type to wedge<>(a, b, r)");
  355. {
  356. real a(GARBAGE);
  357. real1 b(GARBAGE);
  358. ra::wedge<2, 1, 1>(real2{1, 0}, real2{0, 1}, a);
  359. ra::wedge<2, 1, 1>(real2{1, 0}, real2{0, 1}, b);
  360. tr.test_eq(1, a);
  361. tr.test_eq(1, b[0]);
  362. }
  363. tr.section("check the optimization of hodgex() that relies on a complementary order of bases in the 2*O>D forms");
  364. {
  365. test_optimized_hodge<6>(tr);
  366. }
  367. tr.section("Test scalar arg cases");
  368. {
  369. tr.test_eq(6, test_scalar_case<0, real>(real1(2), real(3)));
  370. tr.test_eq(6, test_scalar_case<1, real>(real1(2), real(3)));
  371. tr.test_eq(6, test_scalar_case<0, real>(real(2), real(3)));
  372. tr.test_eq(6, test_scalar_case<1, real>(real(2), real(3)));
  373. tr.test_eq(6, test_scalar_case<0, real>(real(2), real1(3)));
  374. tr.test_eq(6, test_scalar_case<1, real>(real(2), real1(3)));
  375. tr.test_eq(6, test_scalar_case<0, real>(real1(2), real1(3)));
  376. tr.test_eq(6, test_scalar_case<1, real>(real1(2), real1(3)));
  377. tr.test_eq(6, test_scalar_case<0, real1>(real(2), real(3)));
  378. tr.test_eq(6, test_scalar_case<1, real1>(real(2), real(3)));
  379. tr.test_eq(6, test_scalar_case<0, real1>(real1(2), real(3)));
  380. tr.test_eq(6, test_scalar_case<1, real1>(real1(2), real(3)));
  381. tr.test_eq(6, test_scalar_case<0, real1>(real(2), real1(3)));
  382. tr.test_eq(6, test_scalar_case<1, real1>(real(2), real1(3)));
  383. tr.test_eq(6, test_scalar_case<0, real1>(real1(2), real1(3)));
  384. tr.test_eq(6, test_scalar_case<1, real1>(real1(2), real1(3)));
  385. }
  386. tr.section("Test scalar x nonscalar arg cases.");
  387. {
  388. tr.test_eq(real2{6, 10}, test_one_one_case<2, 0, 1, real2>(tr, real1(2), real2{3, 5}));
  389. tr.test_eq(real2{6, 10}, test_one_one_case<2, 1, 0, real2>(tr, real2{3, 5}, real1(2)));
  390. tr.test_eq(real3{2, 6, 10}, test_one_one_case<3, 0, 1, real3>(tr, real1(2), real3{1, 3, 5}));
  391. tr.test_eq(real3{2, 6, 10}, test_one_one_case<3, 1, 0, real3>(tr, real3{1, 3, 5}, real1(2)));
  392. }
  393. {
  394. tr.test_eq(real2{6, 10}, test_one_scalar_case<2, 0, 1, real2>(real1(2), real2{3, 5}));
  395. tr.test_eq(real2{6, 10}, test_one_scalar_case<2, 1, 0, real2>(real2{3, 5}, real1(2)));
  396. tr.test_eq(real3{2, 6, 10}, test_one_scalar_case<3, 0, 1, real3>(real1(2), real3{1, 3, 5}));
  397. tr.test_eq(real3{2, 6, 10}, test_one_scalar_case<3, 1, 0, real3>(real3{1, 3, 5}, real1(2)));
  398. }
  399. tr.section("Test scalar x ~scalar arg cases.");
  400. {
  401. tr.test_eq(6., ra::wedge<1, 0, 1>(3., complex1(2.)));
  402. }
  403. return tr.summary();
  404. }