Renaming Simplex to simplex
[libstick.git] / tests / persistence.h
index 55774dd1cfd4f398a989ccd9f51972585c8e5bce..1a6600c50139cbcf7641602990bfb9452eb2bee5 100644 (file)
@@ -29,6 +29,7 @@ class persistence_TestSuite: public Test::Suite {
             otorus(torus)
             {
             TEST_ADD(persistence_TestSuite::test_matrix_reduction);
+            TEST_ADD(persistence_TestSuite::test_betti_numbers);
         }
 
     protected:
@@ -59,7 +60,7 @@ class persistence_TestSuite: public Test::Suite {
             //                      \ /
             //                       9
 
-            scomplex::Simplex ssring[] = {
+            scomplex::simplex ssring[] = {
                 // dimension, faces, value...
                 /*  1 */ {0, {},           1},
                 /*  2 */ {0, {},           2},
@@ -86,11 +87,11 @@ class persistence_TestSuite: public Test::Suite {
                 /* 23 */ {2, {12, 13, 22}, 6.1201},
                 /* 24 */ {2, {8, 16, 22},  6.1202}
             };
-            const size_t cntssring = sizeof(ssring)/sizeof(scomplex::Simplex);
+            const size_t cntssring = sizeof(ssring)/sizeof(scomplex::simplex);
             ring.add_simplices(ssring, cntssring);
             oring.reset();
 
-            scomplex::Simplex sstorus[] = {
+            scomplex::simplex sstorus[] = {
                 // dimension, faces, value...
                 /* 25 */ {0, {},           7},
                 /* 26 */ {0, {},           8},
@@ -123,12 +124,12 @@ class persistence_TestSuite: public Test::Suite {
                 /* 53 */ {2, {10, 46, 44}, 9.1505},
                 /* 54 */ {2, {43, 46, 28}, 9.1506},
             };
-            const size_t cntsstorus = sizeof(sstorus)/sizeof(scomplex::Simplex);
+            const size_t cntsstorus = sizeof(sstorus)/sizeof(scomplex::simplex);
             torus = ring;
             torus.add_simplices(sstorus, cntsstorus);
             otorus.reset();
 
-            scomplex::Simplex ssball[] = {
+            scomplex::simplex ssball[] = {
                 // dimension, faces, value...
                 {0, {},                1},
                 {0, {},                2},
@@ -154,7 +155,7 @@ class persistence_TestSuite: public Test::Suite {
                 {3, {21, 14, 15, 19}, 22},
                 {3, {21, 16, 17, 20}, 23},
             };
-            const size_t cntssball = sizeof(ssball)/sizeof(scomplex::Simplex);
+            const size_t cntssball = sizeof(ssball)/sizeof(scomplex::simplex);
             ball.add_simplices(ssball, cntssball);
             oball.reset();
         }
@@ -164,12 +165,14 @@ class persistence_TestSuite: public Test::Suite {
 
         void test_matrix_reduction() {
             pers ringp(oring);
+            ringp.compute_matrices();
             const bm &ringbm = ringp.get_boundary_matrix();
             const bm &ringrbm = ringp.get_reduced_boundary_matrix();
             const tm &ringtm = ringp.get_transformation_matrix();
             TEST_ASSERT(ringrbm == ringbm * ringtm);
 
             pers torusp(otorus);
+            torusp.compute_matrices();
             const bm &torusbm = torusp.get_boundary_matrix();
             const bm &torusrbm = torusp.get_reduced_boundary_matrix();
             const tm &torustm = torusp.get_transformation_matrix();
@@ -193,6 +196,7 @@ class persistence_TestSuite: public Test::Suite {
 
 
             pers ballp(oball);
+            ballp.compute_matrices();
             const bm &ballbm = ballp.get_boundary_matrix();
             const bm &ballrbm = ballp.get_reduced_boundary_matrix();
             const tm &balltm = ballp.get_transformation_matrix();
@@ -211,6 +215,73 @@ class persistence_TestSuite: public Test::Suite {
 
             //std::cout << std::endl;
             //ballp.interpret_reduction(std::cout);
+            //torusp.compute_diagrams();
+            //torusp.interpret_persistence(std::cout);
+            //std::cout << std::endl;
+            //std::cout << std::endl;
+        }
+
+        void test_betti_numbers() {
+            pers ringp(oring);
+            ringp.compute_diagrams();
+
+            TEST_ASSERT(ringp.get_persistence_diagram(-1).betti() == 0);
+            TEST_ASSERT(ringp.get_persistence_diagram(0).betti() == 0);
+            TEST_ASSERT(ringp.get_persistence_diagram(1).betti() == 1);
+            TEST_ASSERT(ringp.get_persistence_diagram(2).betti() == 0);
+            TEST_ASSERT(ringp.get_persistence_diagram(3).betti() == 0);
+            for (unsigned d=0; d < 3; ++d)
+                TEST_ASSERT(ringp.get_persistence_diagram(d).persistent_betti(oring.size()-1, oring.size()-1) ==
+                        ringp.get_persistence_diagram(d).betti());
+
+
+            pers torusp(otorus);
+            torusp.compute_diagrams();
+
+            TEST_ASSERT(torusp.get_persistence_diagram(-1).betti() == 0);
+            TEST_ASSERT(torusp.get_persistence_diagram(0).betti() == 0);
+            TEST_ASSERT(torusp.get_persistence_diagram(1).betti() == 2);
+            TEST_ASSERT(torusp.get_persistence_diagram(2).betti() == 1);
+            TEST_ASSERT(torusp.get_persistence_diagram(3).betti() == 0);
+            for (unsigned d=0; d < 3; ++d)
+                TEST_ASSERT(torusp.get_persistence_diagram(d).persistent_betti(otorus.size()-1, otorus.size()-1) ==
+                        torusp.get_persistence_diagram(d).betti());
+
+            // Torus was made from ring. Hence, persistent betti numbers must correspond to betti numbers of ring.
+            for (unsigned d=0; d < 3; ++d)
+                TEST_ASSERT(torusp.get_persistence_diagram(d).persistent_betti(oring.size()-1, oring.size()-1) ==
+                        ringp.get_persistence_diagram(d).betti());
+
+
+            pers ballp(oball);
+            ballp.compute_diagrams();
+
+            //std::cout << std::endl;
+            //ballp.interpret_reduction(std::cout);
+            //std::cout << std::endl;
+
+            TEST_ASSERT(ballp.get_persistence_diagram(-1).betti() == 0);
+            TEST_ASSERT(ballp.get_persistence_diagram(0).betti() == 0);
+            TEST_ASSERT(ballp.get_persistence_diagram(1).betti() == 0);
+            TEST_ASSERT(ballp.get_persistence_diagram(2).betti() == 0);
+            TEST_ASSERT(ballp.get_persistence_diagram(3).betti() == 0);
+            for (unsigned d=0; d < 3; ++d)
+                TEST_ASSERT(ballp.get_persistence_diagram(d).persistent_betti(oball.size()-1, oball.size()-1) ==
+                        ballp.get_persistence_diagram(d).betti());
+
+            // Before we inserted the two tetrahedra and the interior triangle, we had a sphere.
+            TEST_ASSERT(ballp.get_persistence_diagram(-1).persistent_betti(oball.size()-4, oball.size()-4) == 0);
+            TEST_ASSERT(ballp.get_persistence_diagram(0).persistent_betti(oball.size()-4, oball.size()-4) == 0);
+            TEST_ASSERT(ballp.get_persistence_diagram(1).persistent_betti(oball.size()-4, oball.size()-4) == 0);
+            TEST_ASSERT(ballp.get_persistence_diagram(2).persistent_betti(oball.size()-4, oball.size()-4) == 1);
+            TEST_ASSERT(ballp.get_persistence_diagram(3).persistent_betti(oball.size()-4, oball.size()-4) == 0);
+
+            // Before we inserted the two tetrahedra, we had a double-sphere.
+            TEST_ASSERT(ballp.get_persistence_diagram(-1).persistent_betti(oball.size()-3, oball.size()-3) == 0);
+            TEST_ASSERT(ballp.get_persistence_diagram(0).persistent_betti(oball.size()-3, oball.size()-3) == 0);
+            TEST_ASSERT(ballp.get_persistence_diagram(1).persistent_betti(oball.size()-3, oball.size()-3) == 0);
+            TEST_ASSERT(ballp.get_persistence_diagram(2).persistent_betti(oball.size()-3, oball.size()-3) == 2);
+            TEST_ASSERT(ballp.get_persistence_diagram(3).persistent_betti(oball.size()-3, oball.size()-3) == 0);
         }
 };