wedge.cc 16 KB

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