1
#include <libmoinfo/libmoinfo.h>
4
#include <libutil/libutil.h>
7
namespace psi{ namespace psimrcc{
9
typedef std::vector<std::string> strvec;
10
typedef std::vector<std::pair<int,int> > intpairvec;
12
bool is_number(const std::string& str);
13
double get_number(const std::string& str);
14
bool is_operation(const std::string& str);
17
int CCBLAS::parse(std::string& str){
19
int noperations_added = 0;
20
// Split the operation
21
strvec split_str = split(str);
23
// Define the allowed operations
24
strvec allowed_assignments = split("= += >= +>=");
27
CCMatrix* A_Matrix = get_Matrix(split_str[0],str);
28
// Check the assigment operator
29
string assignment(split_str[1]);
30
strvec::iterator strveciter(find(allowed_assignments.begin(),allowed_assignments.end(),assignment));
31
if(strveciter==allowed_assignments.end())
32
fprintf(outfile,"\n\nCCBLAS::parse() %s is not a proper assigment\n\nin the expression:\n\t%s\n\n",assignment.c_str(),str.c_str());
34
// Eliminate the first two strings and store the rest of the terms
35
strvec::iterator iter = split_str.begin();
36
iter=split_str.erase(iter,iter+2);
42
while(iter!=split_str.end()){
46
// Read the reindexing
47
if(iter->find("#")!=string::npos){
49
reindexing = reindexing.substr(1,reindexing.size()-2);
52
// Read a product of numerical factors
53
while((iter!=split_str.end()) && get_factor(*iter,factor))
55
operation="add_factor";
56
// Read the first matrix
57
if(iter!=split_str.end()){
58
B_Matrix = get_Matrix(*iter,str);
60
// And eventually an operation and a matrix
61
if(iter!=split_str.end()){
62
if(is_operation(*iter)){
66
if(iter!=split_str.end()){
67
C_Matrix = get_Matrix(*iter,str);
73
}else{ // When you reach the end
77
if(noperations_added && assignment[0]!='+')
78
assignment= "+" + assignment;
79
CCOperation op(factor,assignment,reindexing,operation,A_Matrix,B_Matrix,C_Matrix,work[0],buffer[0]);
80
operations.push_back(op);
86
return(noperations_added);
89
bool CCBLAS::get_factor(const std::string& str,double& factor)
92
factor *= get_number(str);
103
if(str.substr(0,6)=="factor"){
104
factor = get_scalar(str);
110
bool is_number(const std::string& str){
111
// Is the string str a number?
112
static const string numbers = "1234567890.-+/e";
114
for(int i=0;i<str.size();i++)
115
if(numbers.find(str[i])==string::npos) numeric = false;
116
// In case we have only a symbol (ex. "+")
117
if((str.size()==1) && !isdigit(str[0])) numeric = false;
121
double get_number(const std::string& str){
123
bool fraction = false;
124
size_t fraction_sign;
125
for(int i=0;i<str.size();i++)
131
string numerator = str.substr(0,fraction_sign);
132
string denominator = str.substr(fraction_sign+1,str.size()-fraction_sign-1);
133
string unsigned_numerator = find_and_replace(numerator,"-","");
134
if(unsigned_numerator.size() * denominator.size()!=0){
135
value=ToDouble(numerator)/ToDouble(denominator);
137
fprintf(outfile,"\n\nSolve couldn't parse the numerical factor %s\n\n",str.c_str());
138
fprintf(outfile,"\n\nCritical Breakdown of the Program. Blame the programmers!!!\n\n");
149
bool is_operation(const std::string& str){
150
// Is the string str a number?
151
strvec allowed_operations = split(". @ / * X");
152
bool operation = false;
153
for(int i=0;i<allowed_operations.size();i++)
154
if(str.find(allowed_operations[i])!=string::npos) operation = true;
159
// void split_index(const std::string& str,std::string& t2_label, int*& t2_indices,std::string& first_t1_label, int*& first_t1_indices,std::string& second_t1_label, int*& second_t1_indices)
161
// size_t opening = str.find_first_of("[");
162
// string index_str = str.substr(opening,8);
164
// size_t opening_ref = str.find_first_of("{");
165
// size_t closing_ref = str.find_first_of("}");
166
// string ref_str = str.substr(opening_ref,closing_ref-opening_ref+1);
169
// std::vector<char> labels;
171
// for(int i=0;i<index_str.size();i++){
172
// if(index_str[i]=='o' || index_str[i]=='O' || index_str[i]=='v' || index_str[i]=='V') labels.push_back(index_str[i]);
174
// if(index_str[i]=='o') pairs.push_back(make_pair(0,index++));
175
// if(index_str[i]=='O') pairs.push_back(make_pair(1,index++));
176
// if(index_str[i]=='v') pairs.push_back(make_pair(2,index++));
177
// if(index_str[i]=='V') pairs.push_back(make_pair(3,index++));
179
// sort(pairs.begin(),pairs.end());
181
// for(int i=0;i<index;i++){
182
// t2_indices[i]=pairs[i].second;
183
// t2_label += labels[t2_indices[i]];
187
// t2_label += "]" + ref_str;
189
// first_t1_indices[0]=pairs[0].second;
190
// first_t1_indices[1]=pairs[2].second;
191
// second_t1_indices[0]=pairs[1].second;
192
// second_t1_indices[1]=pairs[3].second;
193
// first_t1_label = "[";
194
// second_t1_label = "[";
195
// for(int i=0;i<2;i++){
196
// first_t1_label += labels[first_t1_indices[i]];
197
// second_t1_label += labels[second_t1_indices[i]];
199
// first_t1_label += "][";
200
// second_t1_label += "][";
203
// first_t1_label += "]" + ref_str;
204
// second_t1_label += "]" + ref_str;
206
// // if(moinfo->get_debug()>7){
207
// // printf("\n\nInput string : %s",str.c_str());
208
// // printf("\n\nIndex string : %s + %s + %s",t2_label.c_str(),first_t1_label.c_str(),second_t1_label.c_str());
215
}} /* End Namespaces */