Skip to content

Commit ebf1afe

Browse files
authored
Merge pull request #758 from hongriTianqi/develop
UT and annotations for inverse_matrix and heapsort
2 parents 3de2b49 + 794495b commit ebf1afe

File tree

4 files changed

+177
-38
lines changed

4 files changed

+177
-38
lines changed

source/module_base/mymath3.cpp

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ void heapAjust(double *r, int *ind, int s, int m)
1111
rc = r[s];
1212
ic = ind[s];
1313

14-
for (j = 2 * s;j <= m;j *= 2)
14+
for (j = 2 * s; j <= m; j *= 2)
1515
{
16-
if (j < m && (r[j] < r[j+1])) j++;
16+
if (j < m && (r[j] < r[j + 1]))
17+
j++;
1718

18-
if (!(rc < r[j])) break;
19+
if (!(rc < r[j]))
20+
break;
1921

2022
r[s] = r[j];
2123

@@ -32,24 +34,24 @@ void heapAjust(double *r, int *ind, int s, int m)
3234

3335
void heapsort(const int n, double *r, int *ind)
3436
{
35-
ModuleBase::timer::tick("mymath","heapsort");
37+
ModuleBase::timer::tick("mymath", "heapsort");
3638
int i, ic;
3739
double rc;
3840

3941
if (ind[0] == 0)
4042
{
41-
for (i = 0;i < n;i++)
43+
for (i = 0; i < n; i++)
4244
{
4345
ind[i] = i;
4446
}
4547
}
4648

47-
for (i = n / 2;i >= 0;i--)
49+
for (i = n / 2; i >= 0; i--)
4850
{
4951
heapAjust(r, ind, i, n - 1);
5052
}
5153

52-
for (i= n - 1;i > 0;i--)
54+
for (i = n - 1; i > 0; i--)
5355
{
5456
rc = r[0];
5557
r[0] = r[i];
@@ -59,7 +61,7 @@ void heapsort(const int n, double *r, int *ind)
5961
ind[i] = ic;
6062
heapAjust(r, ind, 0, i - 1);
6163
}
62-
ModuleBase::timer::tick("mymath","heapsort");
64+
ModuleBase::timer::tick("mymath", "heapsort");
6365
return;
6466
}
6567

@@ -89,82 +91,83 @@ void hpsort(int n, double *ra, int *ind)
8991
int i, ir, j, k, iind;
9092
double rra;
9193

92-
if (ind[1] == 0)
94+
if (ind[0] == 0)
9395
{
94-
for (i = 1;i <= n;i++)
95-
ind[i] = i;
96+
for (i = 1; i <= n; i++)
97+
ind[i - 1] = i;
9698
}
9799

98-
if (n < 2) return; // nothing to order
100+
if (n < 2)
101+
return; // nothing to order
99102

100-
k = n / 2 + 1;
103+
k = n / 2;
101104

102-
ir = n;
105+
ir = n - 1;
103106

104107
while (true)
105108
{
106-
if (k > 1) // still in hiring phase
109+
if (k > 0) // still in hiring phase
107110
{
108-
k = k - 1;
111+
k = k - 1;
109112
rra = ra[k];
110113
iind = ind[k];
111114
}
112-
else // in retirement-promotion phase.
115+
else // in retirement-promotion phase.
113116
{
114-
rra = ra[ir]; // clear a space at the end of the array
115-
iind = ind[ir]; //
116-
ra[ir] = ra[1]; // retire the top of the heap into it
117-
ind[ir] = ind[1]; //
118-
ir = ir - 1; // decrease the size of the corporation
117+
rra = ra[ir]; // clear a space at the end of the array
118+
iind = ind[ir]; //
119+
ra[ir] = ra[0]; // retire the top of the heap into it
120+
ind[ir] = ind[0]; //
121+
ir = ir - 1; // decrease the size of the corporation
119122

120-
if (ir == 1) // done with the last promotion
123+
if (ir == 0) // done with the last promotion
121124
{
122-
ra[1] = rra; // the least competent worker at all //
123-
ind[1] = iind; //
125+
ra[0] = rra; // the least competent worker at all //
126+
ind[0] = iind; //
124127
return;
125128
}
126129
}
127130

128-
i = k; // wheter in hiring or promotion phase, we
131+
i = k; // wheter in hiring or promotion phase, we
129132

130-
j = k + k; // set up to place rra in its proper level
133+
j = k + k + 1; // set up to place rra in its proper level
131134

132135
while (j <= ir)
133136
{
134137
if (j < ir)
135138
{
136-
if (ra[j] < ra[j+1]) // compare to better underling
139+
if (ra[j] < ra[j + 1]) // compare to better underling
137140
{
138141
j = j + 1;
139142
}
140-
else if (ra[j] == ra[j+1])
143+
else if (ra[j] == ra[j + 1])
141144
{
142-
if (ind[j] < ind[j+1])
145+
if (ind[j] < ind[j + 1])
143146
j = j + 1;
144147
}
145148
}
146149

147-
if (rra < ra[j]) // demote rra
150+
if (rra < ra[j]) // demote rra
148151
{
149152
ra[i] = ra[j];
150153
ind[i] = ind[j];
151154
i = j;
152-
j = j + j;
155+
j = j + j + 1;
153156
}
154157
else if (rra == ra[j])
155158
{
156-
if (iind < ind[j]) // demote rra
159+
if (iind < ind[j]) // demote rra
157160
{
158161
ra[i] = ra[j];
159162
ind[i] = ind[j];
160163
i = j;
161-
j = j + j;
164+
j = j + j + 1;
162165
}
163166
else
164-
j = ir + 1; // set j to terminate do-while loop
167+
j = ir + 1; // set j to terminate do-while loop
165168
}
166-
else // this is the right place for rra
167-
j = ir + 1; // set j to terminate do-while loop
169+
else // this is the right place for rra
170+
j = ir + 1; // set j to terminate do-while loop
168171
}
169172

170173
ra[i] = rra;
@@ -173,4 +176,4 @@ void hpsort(int n, double *ra, int *ind)
173176
}
174177
}
175178

176-
}
179+
} // namespace ModuleBase

source/module_base/test/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,12 @@ AddTest(
8686
TARGET base_math_bspline
8787
SOURCES math_bspline_test.cpp ../math_bspline.cpp
8888
)
89+
AddTest(
90+
TARGET base_inverse_matrix
91+
LIBS ${math_libs}
92+
SOURCES inverse_matrix_test.cpp ../inverse_matrix.cpp ../complexmatrix.cpp ../matrix.cpp ../timer.cpp
93+
)
94+
AddTest(
95+
TARGET base_mymath
96+
SOURCES mymath_test.cpp ../mymath3.cpp ../timer.cpp
97+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "../inverse_matrix.h"
2+
#include "gtest/gtest.h"
3+
#include <iostream>
4+
5+
/************************************************
6+
* unit test of class Inverse_Matrix_Complex
7+
***********************************************/
8+
9+
/**
10+
* - Tested Functions:
11+
* - Inverse
12+
* - use Inverse_Matrix_Complex to inverse a Hermite matrix
13+
* - functions: init and using_zheev
14+
*
15+
*/
16+
17+
//a mock function of WARNING_QUIT, to avoid the uncorrected call by matrix.cpp at line 37.
18+
namespace ModuleBase
19+
{
20+
void WARNING_QUIT(const std::string &file,const std::string &description) {return ;}
21+
}
22+
23+
TEST(InverseMatrixComplexTest,Inverse)
24+
{
25+
int dim = 10;
26+
ModuleBase::ComplexMatrix B(dim,dim);
27+
ModuleBase::ComplexMatrix C(dim,dim);
28+
ModuleBase::ComplexMatrix D(dim,dim);
29+
double a;
30+
double b;
31+
double c;
32+
// construct a Hermite matrix
33+
for(int j=0;j<dim;j++)
34+
{
35+
for(int i=0;i<=j;i++)
36+
{
37+
if (i==j)
38+
{
39+
c = std::rand();
40+
B(i,j) = std::complex<double> (c,0.0);
41+
}
42+
else
43+
{
44+
a = std::rand();
45+
b = std::rand();
46+
B(i,j) = std::complex<double> (a,b);
47+
B(j,i) = conj(B(i,j));
48+
}
49+
}
50+
}
51+
ModuleBase::Inverse_Matrix_Complex IMC;
52+
IMC.init(dim);
53+
IMC.using_zheev(B,C);
54+
D = B * C;
55+
for(int i=0;i<dim;i++)
56+
{
57+
EXPECT_NEAR(D(i,i).real(),1.0,1e-14);
58+
EXPECT_NEAR(D(i,i).imag(),0.0,1e-14);
59+
//std::cout << D(i,i).real() << " " << D(i,i).imag() << std::endl;
60+
}
61+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "../mymath.h"
2+
#include "gtest/gtest.h"
3+
#include <iostream>
4+
#include <algorithm>
5+
6+
/************************************************
7+
* unit test of heap sort functions
8+
***********************************************/
9+
10+
/**
11+
* - Tested Functions:
12+
* - Heapsort
13+
* - heapsort function in mymath3.cpp
14+
* - Hpsort
15+
* - hpsort function in mymath3.cpp
16+
*
17+
*/
18+
19+
class MymathTest : public testing::Test
20+
{
21+
protected:
22+
int number;
23+
};
24+
25+
TEST_F(MymathTest,Heapsort)
26+
{
27+
number = 5;
28+
double rr[number];
29+
int index[number];
30+
rr[0] = 10;
31+
rr[1] = 9;
32+
rr[2] = 8;
33+
rr[3] = 7;
34+
rr[4] = 6;
35+
index[0] = 0;
36+
//for (int i=0;i<number;i++)
37+
// std::cout << i << " " << rr[i] << std::endl;
38+
ModuleBase::heapsort(number,rr,index);
39+
for (int i=0;i<number-1;i++)
40+
{
41+
//std::cout << i << " " << rr[i] << std::endl;
42+
EXPECT_LT(rr[i],rr[i+1]);
43+
}
44+
}
45+
46+
TEST_F(MymathTest,Hpsort)
47+
{
48+
number = 5;
49+
double rr[number];
50+
int index[number];
51+
rr[0] = 10;
52+
rr[1] = 9;
53+
rr[2] = 8;
54+
rr[3] = 7;
55+
rr[4] = 6;
56+
index[0] = 0;
57+
//for (int i=0;i<number;i++)
58+
// std::cout << i << " " << rr[i] << std::endl;
59+
ModuleBase::hpsort(number,rr,index);
60+
//std::sort(rr,rr+number);
61+
for (int i=0;i<number-1;i++)
62+
{
63+
//std::cout << i << " " << rr[i] << std::endl;
64+
EXPECT_LT(rr[i],rr[i+1]);
65+
}
66+
}

0 commit comments

Comments
 (0)