Add computaton of persistence diagrams
[libstick.git] / tests / persistence.h
index 55774dd1cfd4f398a989ccd9f51972585c8e5bce..f48c3867c32efdf55603b960cb5c31e07ab94235 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:
@@ -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);
         }
 };