~ubuntu-branches/ubuntu/quantal/psicode/quantal

« back to all changes in this revision

Viewing changes to src/bin/psimrcc/blas_parser.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck, Michael Banck, Daniel Leidert
  • Date: 2009-02-23 00:12:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090223001202-rutldoy3dimfpesc
Tags: 3.4.0-1
* New upstream release.

[ Michael Banck ]
* debian/patches/01_DESTDIR.dpatch: Refreshed.
* debian/patches/02_FHS.dpatch: Removed, applied upstream.
* debian/patches/03_debian_docdir: Likewise.
* debian/patches/04_man.dpatch: Likewise.
* debian/patches/06_466828_fix_gcc_43_ftbfs.dpatch: Likewise.
* debian/patches/07_464867_move_executables: Fixed and refreshed.
* debian/patches/00list: Adjusted.
* debian/control: Improved description.
* debian/patches-held: Removed.
* debian/rules (install/psi3): Do not ship the ruby bindings for now.

[ Daniel Leidert ]
* debian/rules: Fix txtdir via DEB_MAKE_INSTALL_TARGET.
* debian/patches/01_DESTDIR.dpatch: Refreshed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <libmoinfo/libmoinfo.h>
 
2
#include "blas.h"
 
3
#include "debugging.h"
 
4
#include <libutil/libutil.h>
 
5
#include <algorithm>
 
6
 
 
7
namespace psi{ namespace psimrcc{
 
8
 
 
9
typedef std::vector<std::string>            strvec;
 
10
typedef std::vector<std::pair<int,int> >    intpairvec;
 
11
 
 
12
bool is_number(const std::string& str);
 
13
double get_number(const std::string& str);
 
14
bool is_operation(const std::string& str);
 
15
 
 
16
// Parsing Algorithm
 
17
int CCBLAS::parse(std::string& str){
 
18
 
 
19
  int noperations_added = 0;
 
20
  // Split the operation
 
21
  strvec split_str = split(str);
 
22
 
 
23
  // Define the allowed operations
 
24
  strvec allowed_assignments = split("= += >= +>=");
 
25
 
 
26
  // Store the A Matrix
 
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());
 
33
 
 
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);
 
37
  string    reindexing;
 
38
  string    operation;
 
39
  CCMatrix* B_Matrix;
 
40
  CCMatrix* C_Matrix;
 
41
 
 
42
  while(iter!=split_str.end()){
 
43
    double factor=1.0;
 
44
    C_Matrix = NULL;
 
45
    B_Matrix = NULL;
 
46
    // Read the reindexing
 
47
    if(iter->find("#")!=string::npos){
 
48
      reindexing = *iter;
 
49
      reindexing = reindexing.substr(1,reindexing.size()-2);
 
50
      ++iter;
 
51
    }
 
52
    // Read a product of numerical factors
 
53
    while((iter!=split_str.end()) && get_factor(*iter,factor))
 
54
      ++iter;
 
55
    operation="add_factor";
 
56
    // Read the first matrix
 
57
    if(iter!=split_str.end()){
 
58
      B_Matrix = get_Matrix(*iter,str);
 
59
      ++iter;
 
60
      // And eventually an operation and a matrix
 
61
      if(iter!=split_str.end()){
 
62
        if(is_operation(*iter)){
 
63
          // Check operation
 
64
          operation = *iter;
 
65
          ++iter;
 
66
          if(iter!=split_str.end()){
 
67
            C_Matrix = get_Matrix(*iter,str);
 
68
            ++iter;
 
69
          }
 
70
        }else{
 
71
          operation="plus";
 
72
        }
 
73
      }else{ // When you reach the end
 
74
        operation="plus";
 
75
      }
 
76
    }
 
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);
 
81
    noperations_added++;
 
82
    DEBUGGING(5,
 
83
      op.print();
 
84
    )
 
85
  }
 
86
  return(noperations_added);
 
87
}
 
88
 
 
89
bool CCBLAS::get_factor(const std::string& str,double& factor)
 
90
{
 
91
  if(is_number(str)){
 
92
    factor *= get_number(str);
 
93
    return true;
 
94
  }
 
95
  if(str=="-"){
 
96
    factor *= -1.0;
 
97
    return true;
 
98
  }
 
99
  if(str=="+"){
 
100
    factor *= 1.0;
 
101
    return true;
 
102
  }
 
103
  if(str.substr(0,6)=="factor"){
 
104
    factor = get_scalar(str);
 
105
    return true;
 
106
  }
 
107
  return false;
 
108
}
 
109
 
 
110
bool is_number(const std::string& str){
 
111
  // Is the string str a number?
 
112
  static const string numbers = "1234567890.-+/e";
 
113
  bool numeric = true;
 
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;
 
118
  return(numeric);
 
119
}
 
120
 
 
121
double get_number(const std::string& str){
 
122
  double value = 1.0;
 
123
  bool fraction = false;
 
124
  size_t fraction_sign;
 
125
  for(int i=0;i<str.size();i++)
 
126
    if(str[i]=='/'){
 
127
      fraction = true;
 
128
      fraction_sign = i;
 
129
    }
 
130
  if(fraction){
 
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);
 
136
    }else{
 
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");
 
139
      fflush(outfile);
 
140
      exit(1);
 
141
    }
 
142
 
 
143
  }else{
 
144
    value=ToDouble(str);
 
145
  }
 
146
  return(value);
 
147
}
 
148
 
 
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;
 
155
  return(operation);
 
156
}
 
157
 
 
158
 
 
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)
 
160
// {
 
161
//   size_t opening = str.find_first_of("[");
 
162
//   string index_str = str.substr(opening,8);
 
163
// 
 
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);
 
167
// 
 
168
//   intpairvec pairs;
 
169
//   std::vector<char> labels;
 
170
//   int index=0;
 
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]);
 
173
// 
 
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++));
 
178
//   }
 
179
//   sort(pairs.begin(),pairs.end());
 
180
//   t2_label = "[";
 
181
//   for(int i=0;i<index;i++){
 
182
//     t2_indices[i]=pairs[i].second;
 
183
//     t2_label += labels[t2_indices[i]];
 
184
//     if(i==1)
 
185
//       t2_label += "][";
 
186
//   }
 
187
//   t2_label += "]" + ref_str;
 
188
// 
 
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]];
 
198
//     if(i==0){
 
199
//       first_t1_label += "][";
 
200
//       second_t1_label += "][";
 
201
//     }
 
202
//   }
 
203
//   first_t1_label += "]" + ref_str;
 
204
//   second_t1_label += "]" + ref_str;
 
205
// 
 
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());
 
209
// //   }
 
210
// }
 
211
 
 
212
 
 
213
 
 
214
 
 
215
}} /* End Namespaces */