summaryrefslogtreecommitdiff
path: root/src/Coxeter_triangulation/test/function_test.cpp
blob: 43dbcb75d9a8baf0ddf700e5839cd0af9edbb951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*    This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
 *    See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
 *    Author(s):       Siargey Kachanovich
 *
 *    Copyright (C) 2019 Inria
 *
 *    Modification(s):
 *      - YYYY/MM Author: Description of the modification
 */

// workaround for the annoying boost message in boost 1.69
#define BOOST_PENDING_INTEGER_LOG2_HPP
#include <boost/integer/integer_log2.hpp>
// end workaround

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "function"
#include <boost/test/unit_test.hpp>
#include <gudhi/Unitary_tests_utils.h>

#include <gudhi/Functions/Function_Sm_in_Rd.h>
#include <gudhi/Functions/Function_affine_plane_in_Rd.h>
#include <gudhi/Functions/Constant_function.h>
#include <gudhi/Functions/Function_chair_in_R3.h>
#include <gudhi/Functions/Function_torus_in_R3.h>
#include <gudhi/Functions/Function_whitney_umbrella_in_R3.h>
#include <gudhi/Functions/Function_lemniscate_revolution_in_R3.h>
#include <gudhi/Functions/Function_iron_in_R3.h>
#include <gudhi/Functions/Function_moment_curve_in_Rd.h>
#include <gudhi/Functions/Embed_in_Rd.h>
#include <gudhi/Functions/Translate.h>
#include <gudhi/Functions/Linear_transformation.h>
#include <gudhi/Functions/Negation.h>
#include <gudhi/Functions/Cartesian_product.h>
#include <gudhi/Functions/PL_approximation.h>

#include <gudhi/Coxeter_triangulation.h>

#include <string>

#include <random>
#include <cstdlib>

using namespace Gudhi::coxeter_triangulation;

template <class Function>
void test_function(const Function& fun) {
  Eigen::VectorXd seed = fun.seed();
  Eigen::VectorXd res_seed = fun(fun.seed());
  BOOST_CHECK(seed.size() == (long int)fun.amb_d());
  BOOST_CHECK(res_seed.size() == (long int)fun.cod_d());
  for (std::size_t i = 0; i < fun.cod_d(); i++) GUDHI_TEST_FLOAT_EQUALITY_CHECK(res_seed(i), 0., 1e-10);
}

BOOST_AUTO_TEST_CASE(function) {
  {
    // the sphere testing part
    std::size_t m = 3, d = 5;
    Eigen::VectorXd center(d);
    center << 2, 1.5, -0.5, 4.5, -1;
    double radius = 5;
    typedef Function_Sm_in_Rd Function_sphere;
    Function_sphere fun_sphere(radius, m, d, center);
    test_function(fun_sphere);
  }
  {
    // the affine plane testing part
    std::size_t m = 0, d = 5;
    Eigen::MatrixXd normal_matrix = Eigen::MatrixXd::Zero(d, d - m);
    for (std::size_t i = 0; i < d - m; ++i) normal_matrix(i, i) = 1;
    typedef Function_affine_plane_in_Rd Function_plane;
    Function_plane fun_plane(normal_matrix);
    test_function(fun_plane);
  }
  {
    // the constant function testing part
    std::size_t k = 2, d = 5;
    auto x = Eigen::VectorXd::Constant(k, 1);
    Constant_function fun_const(d, k, x);
    Eigen::VectorXd res_zero = fun_const(Eigen::VectorXd::Zero(d));
    for (std::size_t i = 0; i < k; ++i) GUDHI_TEST_FLOAT_EQUALITY_CHECK(res_zero(i), x(i), 1e-10);
  }
  {
    // the chair function
    Function_chair_in_R3 fun_chair;
    test_function(fun_chair);
  }
  {
    // the torus function
    Function_torus_in_R3 fun_torus;
    test_function(fun_torus);
  }
  {
    // the whitney umbrella function
    Function_whitney_umbrella_in_R3 fun_umbrella;
    test_function(fun_umbrella);
  }
  {
    // the lemniscate revolution function
    Function_lemniscate_revolution_in_R3 fun_lemniscate;
    test_function(fun_lemniscate);
  }
  {
    // the iron function
    Function_iron_in_R3 fun_iron;
    test_function(fun_iron);
  }
  {
    Function_moment_curve_in_Rd fun_moment_curve(3, 5);
    test_function(fun_moment_curve);
  }
  {
    // function embedding
    Function_iron_in_R3 fun_iron;
    auto fun_embed = make_embedding(fun_iron, 5);
    test_function(fun_iron);

    // function translation
    Eigen::VectorXd off = Eigen::VectorXd::Random(5);
    auto fun_trans = translate(fun_embed, off);
    test_function(fun_trans);

    // function linear transformation
    Eigen::MatrixXd matrix = Eigen::MatrixXd::Random(5, 5);
    BOOST_CHECK(matrix.determinant() != 0.);
    auto fun_lin = make_linear_transformation(fun_trans, matrix);
    test_function(fun_lin);

    // function negative
    auto fun_neg = negation(fun_lin);
    test_function(fun_neg);

    // function product
    typedef Function_Sm_in_Rd Function_sphere;
    Function_sphere fun_sphere(1, 1);
    auto fun_prod = make_product_function(fun_sphere, fun_sphere, fun_sphere);
    test_function(fun_prod);

    // function PL approximation
    Coxeter_triangulation<> cox_tr(6);
    typedef Coxeter_triangulation<>::Vertex_handle Vertex_handle;
    auto fun_pl = make_pl_approximation(fun_prod, cox_tr);
    Vertex_handle v0 = Vertex_handle(cox_tr.dimension(), 0);
    Eigen::VectorXd x0 = cox_tr.cartesian_coordinates(v0);
    Eigen::VectorXd value0 = fun_prod(x0);
    Eigen::VectorXd pl_value0 = fun_pl(x0);
    for (std::size_t i = 0; i < fun_pl.cod_d(); i++) GUDHI_TEST_FLOAT_EQUALITY_CHECK(value0(i), pl_value0(i), 1e-10);
    Vertex_handle v1 = v0;
    v1[0] += 1;
    Eigen::VectorXd x1 = cox_tr.cartesian_coordinates(v1);
    Eigen::VectorXd value1 = fun_prod(x1);
    Eigen::VectorXd pl_value1 = fun_pl(x1);
    for (std::size_t i = 0; i < fun_pl.cod_d(); i++) GUDHI_TEST_FLOAT_EQUALITY_CHECK(value1(i), pl_value1(i), 1e-10);
    Eigen::VectorXd pl_value_mid = fun_pl(0.5 * x0 + 0.5 * x1);
    for (std::size_t i = 0; i < fun_pl.cod_d(); i++)
      GUDHI_TEST_FLOAT_EQUALITY_CHECK(0.5 * value0(i) + 0.5 * value1(i), pl_value_mid(i), 1e-10);
  }
}