File: blas_parser.cc

package info (click to toggle)
psicode 3.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 46,400 kB
  • ctags: 18,552
  • sloc: cpp: 291,425; ansic: 12,788; fortran: 10,489; perl: 3,206; sh: 2,702; makefile: 2,207; ruby: 2,178; yacc: 110; lex: 53
file content (215 lines) | stat: -rw-r--r-- 6,511 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <libmoinfo/libmoinfo.h>
#include "blas.h"
#include "debugging.h"
#include <libutil/libutil.h>
#include <algorithm>

namespace psi{ namespace psimrcc{

typedef std::vector<std::string>            strvec;
typedef std::vector<std::pair<int,int> >    intpairvec;

bool is_number(const std::string& str);
double get_number(const std::string& str);
bool is_operation(const std::string& str);

// Parsing Algorithm
int CCBLAS::parse(std::string& str){

  int noperations_added = 0;
  // Split the operation
  strvec split_str = split(str);

  // Define the allowed operations
  strvec allowed_assignments = split("= += >= +>=");

  // Store the A Matrix
  CCMatrix* A_Matrix = get_Matrix(split_str[0],str);
  // Check the assignment operator
  string assignment(split_str[1]);
  strvec::iterator strveciter(find(allowed_assignments.begin(),allowed_assignments.end(),assignment));
  if(strveciter==allowed_assignments.end())
    fprintf(outfile,"\n\nCCBLAS::parse() %s is not a proper assignment\n\nin the expression:\n\t%s\n\n",assignment.c_str(),str.c_str());

  // Eliminate the first two strings and store the rest of the terms
  strvec::iterator iter = split_str.begin();
  iter=split_str.erase(iter,iter+2);
  string    reindexing;
  string    operation;
  CCMatrix* B_Matrix;
  CCMatrix* C_Matrix;

  while(iter!=split_str.end()){
    double factor=1.0;
    C_Matrix = NULL;
    B_Matrix = NULL;
    // Read the reindexing
    if(iter->find("#")!=string::npos){
      reindexing = *iter;
      reindexing = reindexing.substr(1,reindexing.size()-2);
      ++iter;
    }
    // Read a product of numerical factors
    while((iter!=split_str.end()) && get_factor(*iter,factor))
      ++iter;
    operation="add_factor";
    // Read the first matrix
    if(iter!=split_str.end()){
      B_Matrix = get_Matrix(*iter,str);
      ++iter;
      // And eventually an operation and a matrix
      if(iter!=split_str.end()){
        if(is_operation(*iter)){
          // Check operation
          operation = *iter;
          ++iter;
          if(iter!=split_str.end()){
            C_Matrix = get_Matrix(*iter,str);
            ++iter;
          }
        }else{
          operation="plus";
        }
      }else{ // When you reach the end
        operation="plus";
      }
    }
    if(noperations_added && assignment[0]!='+')
      assignment= "+" + assignment;
    CCOperation op(factor,assignment,reindexing,operation,A_Matrix,B_Matrix,C_Matrix,work[0],buffer[0]);
    operations.push_back(op);
    noperations_added++;
    DEBUGGING(5,
      op.print();
    )
  }
  return(noperations_added);
}

bool CCBLAS::get_factor(const std::string& str,double& factor)
{
  if(is_number(str)){
    factor *= get_number(str);
    return true;
  }
  if(str=="-"){
    factor *= -1.0;
    return true;
  }
  if(str=="+"){
    factor *= 1.0;
    return true;
  }
  if(str.substr(0,6)=="factor"){
    factor = get_scalar(str);
    return true;
  }
  return false;
}

bool is_number(const std::string& str){
  // Is the string str a number?
  static const string numbers = "1234567890.-+/e";
  bool numeric = true;
  for(int i=0;i<str.size();i++)
    if(numbers.find(str[i])==string::npos) numeric = false;
  // In case we have only a symbol (ex. "+")
  if((str.size()==1) && !isdigit(str[0])) numeric = false;
  return(numeric);
}

double get_number(const std::string& str){
  double value = 1.0;
  bool fraction = false;
  size_t fraction_sign;
  for(int i=0;i<str.size();i++)
    if(str[i]=='/'){
      fraction = true;
      fraction_sign = i;
    }
  if(fraction){
    string numerator   = str.substr(0,fraction_sign);
    string denominator = str.substr(fraction_sign+1,str.size()-fraction_sign-1);
    string unsigned_numerator = find_and_replace(numerator,"-","");
    if(unsigned_numerator.size() * denominator.size()!=0){
      value=ToDouble(numerator)/ToDouble(denominator);
    }else{
      fprintf(outfile,"\n\nSolve couldn't parse the numerical factor %s\n\n",str.c_str());
      fprintf(outfile,"\n\nCritical Breakdown of the Program. Blame the programmers!!!\n\n");
      fflush(outfile);
      exit(1);
    }

  }else{
    value=ToDouble(str);
  }
  return(value);
}

bool is_operation(const std::string& str){
  // Is the string str a number?
  strvec allowed_operations = split(". @ / * X");
  bool operation = false;
  for(int i=0;i<allowed_operations.size();i++)
    if(str.find(allowed_operations[i])!=string::npos) operation = true;
  return(operation);
}


// 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)
// {
//   size_t opening = str.find_first_of("[");
//   string index_str = str.substr(opening,8);
// 
//   size_t opening_ref = str.find_first_of("{");
//   size_t closing_ref = str.find_first_of("}");
//   string ref_str     = str.substr(opening_ref,closing_ref-opening_ref+1);
// 
//   intpairvec pairs;
//   std::vector<char> labels;
//   int index=0;
//   for(int i=0;i<index_str.size();i++){
//     if(index_str[i]=='o' || index_str[i]=='O' || index_str[i]=='v' || index_str[i]=='V') labels.push_back(index_str[i]);
// 
//     if(index_str[i]=='o') pairs.push_back(make_pair(0,index++));
//     if(index_str[i]=='O') pairs.push_back(make_pair(1,index++));
//     if(index_str[i]=='v') pairs.push_back(make_pair(2,index++));
//     if(index_str[i]=='V') pairs.push_back(make_pair(3,index++));
//   }
//   sort(pairs.begin(),pairs.end());
//   t2_label = "[";
//   for(int i=0;i<index;i++){
//     t2_indices[i]=pairs[i].second;
//     t2_label += labels[t2_indices[i]];
//     if(i==1)
//       t2_label += "][";
//   }
//   t2_label += "]" + ref_str;
// 
//   first_t1_indices[0]=pairs[0].second;
//   first_t1_indices[1]=pairs[2].second;
//   second_t1_indices[0]=pairs[1].second;
//   second_t1_indices[1]=pairs[3].second;
//   first_t1_label = "[";
//   second_t1_label = "[";
//   for(int i=0;i<2;i++){
//     first_t1_label += labels[first_t1_indices[i]];
//     second_t1_label += labels[second_t1_indices[i]];
//     if(i==0){
//       first_t1_label += "][";
//       second_t1_label += "][";
//     }
//   }
//   first_t1_label += "]" + ref_str;
//   second_t1_label += "]" + ref_str;
// 
// //   if(moinfo->get_debug()>7){
// //     printf("\n\nInput string : %s",str.c_str());
// //     printf("\n\nIndex string : %s + %s + %s",t2_label.c_str(),first_t1_label.c_str(),second_t1_label.c_str());
// //   }
// }




}} /* End Namespaces */