ExaTN
tensor_symbol.hpp
1 
33 #ifndef EXATN_TENSOR_SYMBOL_HPP_
34 #define EXATN_TENSOR_SYMBOL_HPP_
35 
36 #include "tensor_basic.hpp"
37 #include "tensor_leg.hpp"
38 
39 #include <string>
40 #include <vector>
41 
42 namespace exatn{
43 
44 //Index label:
45 typedef struct{
46  std::string label; //alphanumeric_ index label
47  LegDirection direction; //index variance (leg direction)
48 } IndexLabel;
49 
50 inline bool is_letter(const char & ch){
51  return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
52 }
53 
54 inline bool is_number(const char & ch){
55  return (ch >= '0' && ch <= '9');
56 }
57 
58 inline bool is_underscore(const char & ch){
59  return (ch == '_');
60 }
61 
62 inline bool is_space(const char & ch){
63  return (ch == ' ');
64 }
65 
66 inline bool is_conjugation_sign(const char & ch){
67  return (ch == '+');
68 }
69 
70 inline bool is_equal_sign(const char & ch){
71  return (ch == '=');
72 }
73 
74 inline bool is_plus_sign(const char & ch){
75  return (ch == '+');
76 }
77 
78 inline bool is_minus_sign(const char & ch){
79  return (ch == '-');
80 }
81 
82 inline bool is_multiply_sign(const char & ch){
83  return (ch == '*');
84 }
85 
87 inline bool is_alphanumeric(const std::string & identifier)
88 {
89  if(identifier.empty()) return false;
90  if(!is_letter(identifier[0])) return false;
91  for(const char & ch: identifier){
92  if(!(is_letter(ch) || is_number(ch) || is_underscore(ch))) return false;
93  }
94  return true;
95 }
96 
98 inline std::pair<int,int> trim_spaces_off(const std::string & str, //in: full string container
99  std::pair<int,int> view) //in: input string view
100 {
101  int sbeg = view.first;
102  int send = view.second;
103  if(sbeg <= send){
104  while(send >= sbeg){ //remove trailing white spaces
105  if(!is_space(str[send])) break;
106  --send;
107  }
108  while(sbeg <= send){ //remove leading white spaces
109  if(!is_space(str[sbeg])) break;
110  ++sbeg;
111  }
112  }
113  return std::make_pair(sbeg,send);
114 }
115 
118 bool parse_tensor(const std::string & tensor, //in: tensor as a string
119  std::string & tensor_name, //out: tensor name
120  std::vector<IndexLabel> & indices, //out: tensor indices (labels)
121  bool & complex_conjugated); //out: whether or not tensor appears complex conjugated
122 
126 bool parse_tensor_network(const std::string & network, //in: tensor network as a string
127  std::vector<std::string> & tensors); //out: parsed (symbolic) tensors
128 
138 bool generate_contraction_pattern(const std::vector<numerics::TensorLeg> & pattern,
139  unsigned int left_tensor_rank,
140  unsigned int right_tensor_rank,
141  std::string & symb_pattern);
142 
143 } //namespace exatn
144 
145 #endif //EXATN_TENSOR_SYMBOL_HPP_
exatn
Definition: DriverClient.hpp:10
exatn::parse_tensor
bool parse_tensor(const std::string &tensor, std::string &tensor_name, std::vector< IndexLabel > &indices, bool &complex_conjugated)
Definition: tensor_symbol.cpp:13
exatn::is_alphanumeric
bool is_alphanumeric(const std::string &identifier)
Definition: tensor_symbol.hpp:87
exatn::parse_tensor_network
bool parse_tensor_network(const std::string &network, std::vector< std::string > &tensors)
Definition: tensor_symbol.cpp:92
exatn::IndexLabel
Definition: tensor_symbol.hpp:45
exatn::generate_contraction_pattern
bool generate_contraction_pattern(const std::vector< numerics::TensorLeg > &pattern, unsigned int left_tensor_rank, unsigned int right_tensor_rank, std::string &symb_pattern)
Definition: tensor_symbol.cpp:134
exatn::trim_spaces_off
std::pair< int, int > trim_spaces_off(const std::string &str, std::pair< int, int > view)
Definition: tensor_symbol.hpp:98