Skip to content

Commit 057fe8f

Browse files
committed
test: add the unit test and comments for math_polyint.h
range: source/module_base/
1 parent 8751d9f commit 057fe8f

File tree

3 files changed

+199
-5
lines changed

3 files changed

+199
-5
lines changed

source/module_base/math_polyint.h

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ class PolyInt
1818
//========================================================
1919
// Polynomial_Interpolation
2020
//========================================================
21+
22+
/**
23+
* @brief Lagrange interpolation
24+
*
25+
* @param table [in] three dimension matrix, the data in 3rd dimension is used to do prediction
26+
* @param dim1 [in] index of 1st dimension of table/y
27+
* @param dim2 [in] index of 2nd dimension of table/y
28+
* @param y [out] three dimension matrix to store the predicted value
29+
* @param dim_y [in] index of 3rd dimension of y to store predicted value
30+
* @param table_length [in] length of 3rd dimension of table
31+
* @param table_interval [in] interval of 3rd dimension of table
32+
* @param x [in] the position in 3rd dimension to be predicted
33+
*/
2134
static void Polynomial_Interpolation
2235
(
2336
const ModuleBase::realArray &table,
@@ -30,41 +43,84 @@ class PolyInt
3043
const double &x
3144
);
3245

46+
/**
47+
* @brief Lagrange interpolation
48+
*
49+
* @param table [in] three dimension matrix, the data in 3rd dimension is used to do prediction
50+
* @param dim1 [in] index of 1st dimension of table
51+
* @param dim2 [in] index of 2nd dimension of table
52+
* @param table_length [in] length of 3rd dimension of table
53+
* @param table_interval [in] interval of 3rd dimension of table
54+
* @param x [in] the position in 3rd dimension to be predicted
55+
* @return double the predicted value
56+
*/
3357
static double Polynomial_Interpolation
3458
(
3559
const ModuleBase::realArray &table,
3660
const int &dim1,
3761
const int &dim2,
3862
const int &table_length,
3963
const double &table_interval,
40-
const double &x // input value
64+
const double &x
4165
);
4266

43-
static double Polynomial_Interpolation // pengfei Li 2018-3-23
67+
/**
68+
* @brief Lagrange interpolation
69+
*
70+
* @param table [in] four dimension matrix, the data in 4th dimension is used to do prediction
71+
* @param dim1 [in] index of 1st dimension of table
72+
* @param dim2 [in] index of 2nd dimension of table
73+
* @param dim3 [in] index of 3rd dimension of table
74+
* @param table_length [in] length of 4th dimension of table
75+
* @param table_interval [in] interval of 4th dimension of table
76+
* @param x [in] the position in 4th dimension to be predicted
77+
* @return double the predicted value
78+
* @author pengfei Li
79+
* @date 2018-3-23
80+
*/
81+
static double Polynomial_Interpolation
4482
(
4583
const ModuleBase::realArray &table,
4684
const int &dim1,
4785
const int &dim2,
4886
const int &dim3,
4987
const int &table_length,
5088
const double &table_interval,
51-
const double &x // input value
89+
const double &x
5290
);
5391

92+
/**
93+
* @brief Lagrange interpolation
94+
*
95+
* @param table [in] the data used to do prediction
96+
* @param table_length [in] length of table
97+
* @param table_interval [in] interval of table
98+
* @param x [in] the position to be predicted
99+
* @return double the predicted value
100+
*/
54101
static double Polynomial_Interpolation
55102
(
56103
const double *table,
57104
const int &table_length,
58105
const double &table_interval,
59-
const double &x // input value
106+
const double &x
60107
);
61108

109+
/**
110+
* @brief Lagrange interpolation
111+
*
112+
* @param xpoint [in] array of postion
113+
* @param ypoint [in] array of data to do prediction
114+
* @param table_length [in] length of xpoint
115+
* @param x [in] position to be predicted
116+
* @return double predicted value
117+
*/
62118
static double Polynomial_Interpolation_xy
63119
(
64120
const double *xpoint,
65121
const double *ypoint,
66122
const int table_length,
67-
const double &x // input value
123+
const double &x
68124
);
69125

70126
};

source/module_base/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,8 @@ AddTest(
6767
TARGET base_mathzone
6868
LIBS ${math_libs}
6969
SOURCES mathzone_test.cpp ../mathzone.cpp ../matrix3.cpp ../matrix.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../memory.cpp ../timer.cpp
70+
)
71+
AddTest(
72+
TARGET base_math_polyint
73+
SOURCES math_polyint_test.cpp ../math_polyint.cpp ../realarray.cpp ../timer.cpp
7074
)
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include"../math_polyint.h"
2+
#include"gtest/gtest.h"
3+
#include"../realarray.h"
4+
#include<math.h>
5+
6+
#define doublethreshold 1e-9
7+
8+
/************************************************
9+
* unit test of class PolyInt
10+
***********************************************/
11+
12+
/**
13+
* This unit test is to verify the accuracy of
14+
* interpolation method on the function sin(x)/x
15+
* with a interval of 0.01.
16+
* sin(x)/x is one of the solution of spherical bessel
17+
* function when l=0.
18+
*
19+
* - Tested function:
20+
* - 4 types of Polynomial_Interpolation
21+
* - Polynomial_Interpolation_xy
22+
*/
23+
24+
25+
class bessell0 : public testing::Test
26+
{
27+
protected:
28+
29+
int TableLength = 400;
30+
double interval = 0.01;
31+
ModuleBase::realArray table3,table4;
32+
ModuleBase::realArray y3;
33+
double *tablex = new double[TableLength];
34+
double *tabley = new double[TableLength];
35+
36+
double Func(double x) {return sin(x)/x;}
37+
38+
void SetUp()
39+
{
40+
table3.create(1,1,TableLength);
41+
table4.create(1,1,1,TableLength);
42+
y3.create(1,1,TableLength);
43+
44+
for(int i=1;i<TableLength;++i)
45+
{
46+
table3(0,0,i) = Func(i * interval);
47+
table4(0,0,0,i) = Func(i * interval);
48+
tablex[i] = i * interval;
49+
tabley[i] = Func(i * interval);
50+
}
51+
}
52+
53+
void TearDown()
54+
{
55+
delete [] tablex;
56+
delete [] tabley;
57+
}
58+
};
59+
60+
61+
TEST_F(bessell0,PolynomialInterpolationThreeDimensionY)
62+
{
63+
ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,y3,1,TableLength,interval,0.1);
64+
ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,y3,2,TableLength,interval,1.005);
65+
ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,y3,3,TableLength,interval,2.005);
66+
ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,y3,4,TableLength,interval,3.005);
67+
ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,y3,5,TableLength,interval,3.505);
68+
69+
EXPECT_NEAR(y3(0,0,1),Func(0.1),doublethreshold);
70+
EXPECT_NEAR(y3(0,0,2),Func(1.005),doublethreshold);
71+
EXPECT_NEAR(y3(0,0,3),Func(2.005),doublethreshold);
72+
EXPECT_NEAR(y3(0,0,4),Func(3.005),doublethreshold);
73+
EXPECT_NEAR(y3(0,0,5),Func(3.505),doublethreshold);
74+
}
75+
76+
TEST_F(bessell0,PolynomialInterpolationThreeDimension)
77+
{
78+
double y1 = ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,TableLength,interval,0.1);
79+
double y2 = ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,TableLength,interval,1.005);
80+
double y3 = ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,TableLength,interval,2.005);
81+
double y4 = ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,TableLength,interval,3.005);
82+
double y5 = ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,TableLength,interval,3.505);
83+
84+
EXPECT_NEAR(y1,Func(0.1),doublethreshold);
85+
EXPECT_NEAR(y2,Func(1.005),doublethreshold);
86+
EXPECT_NEAR(y3,Func(2.005),doublethreshold);
87+
EXPECT_NEAR(y4,Func(3.005),doublethreshold);
88+
EXPECT_NEAR(y5,Func(3.505),doublethreshold);
89+
}
90+
91+
TEST_F(bessell0,PolynomialInterpolationFourDimension)
92+
{
93+
double y1 = ModuleBase::PolyInt::Polynomial_Interpolation(table4,0,0,0,TableLength,interval,0.1);
94+
double y2 = ModuleBase::PolyInt::Polynomial_Interpolation(table4,0,0,0,TableLength,interval,1.005);
95+
double y3 = ModuleBase::PolyInt::Polynomial_Interpolation(table4,0,0,0,TableLength,interval,2.005);
96+
double y4 = ModuleBase::PolyInt::Polynomial_Interpolation(table4,0,0,0,TableLength,interval,3.005);
97+
double y5 = ModuleBase::PolyInt::Polynomial_Interpolation(table4,0,0,0,TableLength,interval,3.505);
98+
99+
EXPECT_NEAR(y1,Func(0.1),doublethreshold);
100+
EXPECT_NEAR(y2,Func(1.005),doublethreshold);
101+
EXPECT_NEAR(y3,Func(2.005),doublethreshold);
102+
EXPECT_NEAR(y4,Func(3.005),doublethreshold);
103+
EXPECT_NEAR(y5,Func(3.505),doublethreshold);
104+
}
105+
106+
TEST_F(bessell0,PolynomialInterpolation)
107+
{
108+
double y1 = ModuleBase::PolyInt::Polynomial_Interpolation(tabley,TableLength,interval,0.1);
109+
double y2 = ModuleBase::PolyInt::Polynomial_Interpolation(tabley,TableLength,interval,1.005);
110+
double y3 = ModuleBase::PolyInt::Polynomial_Interpolation(tabley,TableLength,interval,2.005);
111+
double y4 = ModuleBase::PolyInt::Polynomial_Interpolation(tabley,TableLength,interval,3.005);
112+
double y5 = ModuleBase::PolyInt::Polynomial_Interpolation(tabley,TableLength,interval,3.505);
113+
114+
EXPECT_NEAR(y1,Func(0.1),doublethreshold);
115+
EXPECT_NEAR(y2,Func(1.005),doublethreshold);
116+
EXPECT_NEAR(y3,Func(2.005),doublethreshold);
117+
EXPECT_NEAR(y4,Func(3.005),doublethreshold);
118+
EXPECT_NEAR(y5,Func(3.505),doublethreshold);
119+
}
120+
121+
TEST_F(bessell0,PolynomialInterpolationXY)
122+
{
123+
double y1 = ModuleBase::PolyInt::Polynomial_Interpolation_xy(tablex,tabley,TableLength,0.1);
124+
double y2 = ModuleBase::PolyInt::Polynomial_Interpolation_xy(tablex,tabley,TableLength,1.005);
125+
double y3 = ModuleBase::PolyInt::Polynomial_Interpolation_xy(tablex,tabley,TableLength,2.005);
126+
double y4 = ModuleBase::PolyInt::Polynomial_Interpolation_xy(tablex,tabley,TableLength,3.005);
127+
double y5 = ModuleBase::PolyInt::Polynomial_Interpolation_xy(tablex,tabley,TableLength,3.505);
128+
129+
EXPECT_NEAR(y1,Func(0.1),doublethreshold);
130+
EXPECT_NEAR(y2,Func(1.005),doublethreshold);
131+
EXPECT_NEAR(y3,Func(2.005),doublethreshold);
132+
EXPECT_NEAR(y4,Func(3.005),doublethreshold);
133+
EXPECT_NEAR(y5,Func(3.505),doublethreshold);
134+
}

0 commit comments

Comments
 (0)