Skip to content

Commit 05f7f43

Browse files
1041176461dyzheng
authored andcommitted
Fix: parse_expression for scientific notation (deepmodeling#5882)
* Fix: parse_expression for scientific notation
1 parent ae6d7a4 commit 05f7f43

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

source/module_io/input_conv.h

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ void parse_expression(const std::string& fn, std::vector<T>& vec)
4747
{
4848
ModuleBase::TITLE("Input_Conv", "parse_expression");
4949
int count = 0;
50-
std::string pattern("([-+]?[0-9]+\\*[-+]?[0-9.]+|[-+]?[0-9,.]+)");
50+
51+
// Update the regex pattern to handle scientific notation
52+
std::string pattern("([-+]?[0-9]+\\*[-+]?[0-9.eE+-]+|[-+]?[0-9,.eE+-]+)");
53+
5154
std::vector<std::string> str;
5255
std::stringstream ss(fn);
5356
std::string section;
57+
58+
// Split the input string into substrings by spaces
5459
while (ss >> section)
5560
{
5661
int index = 0;
@@ -64,24 +69,14 @@ void parse_expression(const std::string& fn, std::vector<T>& vec)
6469
section.erase(0, index);
6570
str.push_back(section);
6671
}
67-
// std::string::size_type pos1, pos2;
68-
// std::string c = " ";
69-
// pos2 = fn.find(c);
70-
// pos1 = 0;
71-
// while (std::string::npos != pos2)
72-
// {
73-
// str.push_back(fn.substr(pos1, pos2 - pos1));
74-
// pos1 = pos2 + c.size();
75-
// pos2 = fn.find(c, pos1);
76-
// }
77-
// if (pos1 != fn.length())
78-
// {
79-
// str.push_back(fn.substr(pos1));
80-
// }
72+
73+
// Compile the regular expression
8174
regex_t reg;
8275
regcomp(&reg, pattern.c_str(), REG_EXTENDED);
8376
regmatch_t pmatch[1];
8477
const size_t nmatch = 1;
78+
79+
// Loop over each section and apply regex to extract numbers
8580
for (size_t i = 0; i < str.size(); ++i)
8681
{
8782
if (str[i] == "")
@@ -90,42 +85,46 @@ void parse_expression(const std::string& fn, std::vector<T>& vec)
9085
}
9186
int status = regexec(&reg, str[i].c_str(), nmatch, pmatch, 0);
9287
std::string sub_str = "";
88+
89+
// Extract the matched substring
9390
for (size_t j = pmatch[0].rm_so; j != pmatch[0].rm_eo; ++j)
9491
{
9592
sub_str += str[i][j];
9693
}
94+
95+
// Check if the substring contains multiplication (e.g., "2*3.14")
9796
std::string sub_pattern("\\*");
9897
regex_t sub_reg;
9998
regcomp(&sub_reg, sub_pattern.c_str(), REG_EXTENDED);
10099
regmatch_t sub_pmatch[1];
101100
const size_t sub_nmatch = 1;
101+
102102
if (regexec(&sub_reg, sub_str.c_str(), sub_nmatch, sub_pmatch, 0) == 0)
103103
{
104104
size_t pos = sub_str.find("*");
105105
int num = stoi(sub_str.substr(0, pos));
106-
assert(num>=0);
106+
assert(num >= 0);
107107
T occ = stof(sub_str.substr(pos + 1, sub_str.size()));
108-
// std::vector<double> ocp_temp(num, occ);
109-
// const std::vector<double>::iterator dest = vec.begin() + count;
110-
// copy(ocp_temp.begin(), ocp_temp.end(), dest);
111-
// count += num;
108+
109+
// Add the value to the vector `num` times
112110
for (size_t k = 0; k != num; k++)
113111
{
114112
vec.emplace_back(occ);
115113
}
116114
}
117115
else
118116
{
119-
// vec[count] = stof(sub_str);
120-
// count += 1;
117+
// Handle scientific notation and convert to T
121118
std::stringstream convert;
122119
convert << sub_str;
123120
T occ;
124121
convert >> occ;
125122
vec.emplace_back(occ);
126123
}
124+
127125
regfree(&sub_reg);
128126
}
127+
129128
regfree(&reg);
130129
}
131130

0 commit comments

Comments
 (0)