Cadabra
Computer algebra system for field theory problems
Loading...
Searching...
No Matches
Storage.hh
Go to the documentation of this file.
1/*
2
3Cadabra: a field-theory motivated computer algebra system.
4Copyright (C) 2001-2014 Kasper Peeters <kasper.peeters@phi-sci.com>
5
6This program is free software: you can redistribute it and/or
7modify it under the terms of the GNU General Public License as
8published by the Free Software Foundation, either version 3 of the
9License, or (at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#pragma once
22
23#include <cstddef>
24#include <iostream>
25#include <gmpxx.h>
26#include <string>
27#include <vector>
28#include <set>
29#include <map>
30#include <stdint.h>
31#include <assert.h>
32#include <initializer_list>
33
34#include "tree.hh"
35
36namespace cadabra {
37
39 typedef std::set<std::string> nset_t;
40 typedef std::set<multiplier_t> rset_t;
42
44 double to_double(multiplier_t);
45 std::string to_string(long);
46
47 extern nset_t name_set;
48 extern rset_t rat_set;
49
55
56 class str_node { // size: 9 bytes (32 bit arch), can be reduced to 5 bytes.
57 public:
59
63
64 str_node(void);
67 str_node(const std::u32string& name, bracket_t btype=b_none, parent_rel_t ptype=p_none);
68
69 bool operator==(const str_node&) const;
70 bool operator<(const str_node&) const;
71
72 nset_t::iterator name;
73 rset_t::iterator multiplier;
74
75#ifdef _WIN32
76 struct flag_t {
77 bool keep_after_eval ;
80 bool line_per_node ;
81 };
82#else
83 struct flag_t { // kept inside 8 bits for speed and size
87 bool line_per_node : 1;
88 };
89#endif
90
92
95 void flip_parent_rel();
96
97 bool is_zero() const;
98 bool is_identity() const;
99 bool is_rational() const;
100 bool is_unsimplified_rational() const;
101 bool is_integer() const;
102 bool is_unsimplified_integer() const;
103 bool is_index() const; // _ or ^ parent_rel (does not query properties)
104 bool is_quoted_string() const;
105 bool is_command() const;
106 bool is_inert_command() const;
107 bool is_name_wildcard() const; // ?
108 bool is_object_wildcard() const; // ??
109 bool is_range_wildcard() const; // #{..}
110 bool is_siblings_wildcard() const; // a...
111 bool is_autodeclare_wildcard() const; // m#
112 bool is_indexstar_wildcard() const; // ?* in sub/super
113 bool is_indexplus_wildcard() const; // ?+ in sub/super
114 bool is_numbered_symbol() const; // [a-zA-Z]+[0-9]+
115
116 nset_t::iterator name_only();
117
118 static bool compare_names_only(const str_node&, const str_node&);
119 static bool compare_name_brack_par(const str_node&, const str_node&);
120 static bool compare_name_inverse_par(const str_node&, const str_node&);
121 };
122
126 void multiply(rset_t::iterator&, multiplier_t);
127 void add(rset_t::iterator&, multiplier_t);
128 void zero(rset_t::iterator&);
129 void one(rset_t::iterator&);
130 void flip_sign(rset_t::iterator&);
131 void half(rset_t::iterator&);
132 void set(rset_t::iterator&, multiplier_t);
133
141
142 class Ex : public std::enable_shared_from_this<Ex>, public tree<str_node> {
143 public:
144 Ex();
146 Ex(tree<str_node>::iterator);
148 Ex(const str_node&);
150 Ex(const Ex&);
152 Ex(const std::string&);
154 Ex(int);
156 Ex(float);
157
158 Ex& operator=(Ex);
159
167
176 result_t state() const;
178 void reset_state();
179
191
192 bool changed_state();
193
196 bool is_rational() const;
198 bool is_integer() const;
199 long to_integer() const;
200 bool is_string() const;
201
203 bool equals(const std::string&) const;
204
206 bool is_empty() const;
207
211 static std::ostream& print_python(std::ostream& str, Ex::iterator it);
212
214 std::ostream& print_entire_tree(std::ostream& str) const;
215 static std::ostream& print_recursive_treeform(std::ostream& str, Ex::iterator it);
216 static std::ostream& print_recursive_treeform(std::ostream& str, Ex::iterator it, unsigned int& number);
217
219 std::ostream& print_repr(std::ostream& str, Ex::iterator it) const;
220
222 iterator named_parent(iterator it, const std::string&) const;
223 iterator erase_expression(iterator it);
224
226 hashval_t calc_hash(iterator it) const;
227
229 static sibling_iterator arg(iterator, unsigned int);
230 static unsigned int arg_size(sibling_iterator);
231
232 multiplier_t arg_to_num(sibling_iterator, unsigned int) const; // shorthand for numerical arguments
233
234 // Like 'child', but using index iterators instead.
235 // sibling_iterator tensor_index(const iterator_base& position, unsigned int) const;
236
237 // Number of \\history nodes in an expression
238 unsigned int number_of_steps(iterator it) const;
239 // Given an iterator pointing to any node in the tree, figure
240 // out to which equation number it belongs.
241 unsigned int number_of_equations() const;
242 unsigned int equation_number(iterator it) const;
243 nset_t::iterator equation_label(iterator it) const;
244 iterator equation_by_number(unsigned int i) const;
245 iterator equation_by_name(nset_t::iterator it) const;
246 iterator equation_by_name(nset_t::iterator it, unsigned int& ) const;
247 iterator equation_by_number_or_name(iterator it, unsigned int last_used_equation) const;
248 iterator equation_by_number_or_name(iterator it, unsigned int last_used_equation,
249 unsigned int&) const;
250 std::string equation_number_or_name(iterator it, unsigned int last_used_equation) const;
251 iterator procedure_by_name(nset_t::iterator it) const;
252
253 // Determine whether a node has an '\ldots' parent (not necessarily direct).
254 bool is_hidden(iterator) const;
255
267 iterator replace_index(iterator position, const iterator& from, bool keep_parent_rel=false);
268
271 iterator move_index(iterator position, const iterator& from);
272
276 void list_wrap_single_element(iterator&);
277 void list_unwrap_single_element(iterator&);
278
283 iterator flatten_and_erase(iterator position);
284
287
288 bool operator==(const Ex& other) const;
289
294 void push_history(const std::vector<Ex::path_t>&);
295
299 std::vector<Ex::path_t> pop_history();
300
303 int history_size() const;
304
305 private:
307
308 std::vector<tree<str_node> > history;
310 std::vector<std::vector<Ex::path_t> > terms;
311 };
312
313
317
319 public:
320 bool operator()(nset_t::iterator first, nset_t::iterator second) const;
321 };
322
323 template <typename T>
324 bool is_in(const T& val, const std::initializer_list<T>& list)
325 {
326 for (const auto& i : list) {
327 if (val == i) {
328 return true;
329 }
330 }
331 return false;
332 }
333
334 }
335
343
344std::ostream& operator<<(std::ostream&, const cadabra::Ex&);
345std::ostream& operator<<(std::ostream&, cadabra::Ex::iterator);
Basic storage class for symbolic mathemematical expressions.
Definition Storage.hh:142
iterator move_index(iterator position, const iterator &from)
As in replace_index, but moves the index rather than making a copy (so that iterators pointing to the...
Definition Storage.cc:648
void list_unwrap_single_element(iterator &)
Definition Storage.cc:670
multiplier_t arg_to_num(sibling_iterator, unsigned int) const
Definition Storage.cc:491
static std::ostream & print_python(std::ostream &str, Ex::iterator it)
Display expression in Python/Cadabra input form.
Definition Storage.cc:205
bool operator==(const Ex &other) const
Compare two Ex objects for exact equality; no dummy equivalence or other things that require property...
Definition Storage.cc:745
std::string equation_number_or_name(iterator it, unsigned int last_used_equation) const
Definition Storage.cc:731
result_t
Keeping track of what algorithms have done to this expression.
Definition Storage.hh:168
@ l_cached
Definition Storage.hh:174
@ l_error
Definition Storage.hh:173
@ l_checkpointed
Definition Storage.hh:169
@ l_no_action
Definition Storage.hh:170
@ l_applied_no_new_dummies
Definition Storage.hh:172
@ l_applied
Definition Storage.hh:171
hashval_t calc_hash(iterator it) const
Calculate the hash value for the subtree starting at 'it'.
Definition Storage.cc:453
void reset_state()
Definition Storage.cc:136
iterator named_parent(iterator it, const std::string &) const
Step up until matching node is found (if current node matches, do nothing)
Definition Storage.cc:433
static sibling_iterator arg(iterator, unsigned int)
Quick access to arguments or argument lists for A(B)(C,D) type nodes.
Definition Storage.cc:476
iterator erase_expression(iterator it)
Definition Storage.cc:446
result_t state() const
Definition Storage.cc:110
std::ostream & print_entire_tree(std::ostream &str) const
Output helpers mainly for debugging purposes.
Definition Storage.cc:341
result_t state_
Definition Storage.hh:306
bool is_integer() const
Definition Storage.cc:166
static unsigned int arg_size(sibling_iterator)
Definition Storage.cc:485
bool is_string() const
Definition Storage.cc:181
void list_wrap_single_element(iterator &)
Make sure that the node pointed to is a \comma object, i.e.
Definition Storage.cc:659
long to_integer() const
Definition Storage.cc:174
iterator equation_by_name(nset_t::iterator it) const
Definition Storage.cc:575
bool changed_state()
A status query method mainly to implement a simple method to apply algorithms until they converge.
Definition Storage.cc:143
unsigned int equation_number(iterator it) const
Definition Storage.cc:499
int history_size() const
Return the size of the history; 0 means no history, just the current expression.
Definition Storage.cc:771
bool is_hidden(iterator) const
Definition Storage.cc:605
Ex()
Definition Storage.cc:57
std::vector< Ex::path_t > pop_history()
Pop the most recent state of the expression off the history stack; returns the set of paths that we a...
Definition Storage.cc:762
void push_history(const std::vector< Ex::path_t > &)
Push a copy of the current state of the expression onto the history stack.
Definition Storage.cc:756
iterator procedure_by_name(nset_t::iterator it) const
Definition Storage.cc:616
unsigned int number_of_steps(iterator it) const
Ex & operator=(Ex)
Definition Storage.cc:78
bool is_empty() const
Test if the expression is empty (no content at all).
Definition Storage.cc:200
bool is_rational() const
Test if the expression is a rational number.
Definition Storage.cc:151
iterator equation_by_number(unsigned int i) const
Definition Storage.cc:557
std::vector< tree< str_node > > history
Definition Storage.hh:308
std::ostream & print_repr(std::ostream &str, Ex::iterator it) const
Print a representation like Python's 'repr'.
Definition Storage.cc:308
bool equals(const std::string &) const
Comparison operators with primitive types.
Definition Storage.cc:193
multiplier_t to_rational() const
Definition Storage.cc:159
iterator flatten_and_erase(iterator position)
Replace the node with the children of the node, useful for e.g.
Definition Storage.cc:680
nset_t::iterator equation_label(iterator it) const
Definition Storage.cc:520
std::vector< std::vector< Ex::path_t > > terms
Patterns which describe how to get from one history step to the next.
Definition Storage.hh:310
iterator replace_index(iterator position, const iterator &from, bool keep_parent_rel=false)
Replace the index-like object (originally intended to replace indices only, but now used also for e....
Definition Storage.cc:636
static std::ostream & print_recursive_treeform(std::ostream &str, Ex::iterator it)
Definition Storage.cc:319
iterator equation_by_number_or_name(iterator it, unsigned int last_used_equation) const
Definition Storage.cc:725
unsigned int number_of_equations() const
Definition Storage.cc:691
void update_state(result_t)
Definition Storage.cc:115
Compare two nset iterators by comparing the strings to which they point.
Definition Storage.hh:318
bool operator()(nset_t::iterator first, nset_t::iterator second) const
Definition Storage.cc:1048
Elementary building block for a mathematical expression.
Definition Storage.hh:56
bool is_indexstar_wildcard() const
Definition Storage.cc:965
bool is_index() const
Definition Storage.cc:879
bool is_unsimplified_integer() const
Definition Storage.cc:869
bool operator<(const str_node &) const
Definition Storage.cc:1100
void flip_parent_rel()
Change the parent relation from sub to super and vice versa (throws error when this is not an index).
Definition Storage.cc:826
bracket_t
Definition Storage.hh:58
@ b_round
Definition Storage.hh:58
@ b_none
Definition Storage.hh:58
@ b_square
Definition Storage.hh:58
@ b_no
Definition Storage.hh:58
@ b_pointy
Definition Storage.hh:58
@ b_curly
Definition Storage.hh:58
@ b_invalid
Definition Storage.hh:58
bool is_numbered_symbol() const
Definition Storage.cc:983
str_node(void)
Definition Storage.cc:776
static bool compare_name_brack_par(const str_node &, const str_node &)
Definition Storage.cc:1031
bool is_quoted_string() const
Definition Storage.cc:887
bool is_unsimplified_rational() const
Definition Storage.cc:859
bool is_siblings_wildcard() const
Definition Storage.cc:948
nset_t::iterator name_only()
Definition Storage.cc:992
bool is_command() const
Definition Storage.cc:895
bool is_object_wildcard() const
Definition Storage.cc:930
parent_rel_t
Child nodes are related to their parent node by a so-called parent relation, which can be one of thes...
Definition Storage.hh:62
@ p_exponent
Definition Storage.hh:62
@ p_sub
Definition Storage.hh:62
@ p_invalid
Definition Storage.hh:62
@ p_property
Definition Storage.hh:62
@ p_none
Definition Storage.hh:62
@ p_super
Definition Storage.hh:62
@ p_components
Definition Storage.hh:62
rset_t::iterator multiplier
Definition Storage.hh:73
static bool compare_name_inverse_par(const str_node &, const str_node &)
Definition Storage.cc:1039
bool is_inert_command() const
Definition Storage.cc:908
bool is_indexplus_wildcard() const
Definition Storage.cc:974
bool is_range_wildcard() const
Definition Storage.cc:939
bool is_identity() const
Definition Storage.cc:839
bool is_name_wildcard() const
Definition Storage.cc:917
bool is_autodeclare_wildcard() const
Definition Storage.cc:957
nset_t::iterator name
Definition Storage.hh:72
static bool compare_names_only(const str_node &, const str_node &)
Definition Storage.cc:1025
bool is_zero() const
Definition Storage.cc:833
flag_t fl
Definition Storage.hh:91
bool operator==(const str_node &) const
Definition Storage.cc:1015
bool is_integer() const
Definition Storage.cc:850
bool is_rational() const
Definition Storage.cc:845
std::ostream & operator<<(std::ostream &, const cadabra::Ex &)
Bare output operator for Ex objects, mainly to provide a simple way to generate debugging output.
Definition Storage.cc:1111
void multiply(rset_t::iterator &num, multiplier_t fac)
Helper functions for manipulation of multipliers.
Definition Storage.cc:1056
Functions to handle the exchange properties of two or more symbols in a product.
Definition Adjform.cc:83
rset_t rat_set
Definition Storage.cc:36
std::set< multiplier_t > rset_t
Definition Storage.hh:40
void flip_sign(rset_t::iterator &num)
Definition Storage.cc:1086
double to_double(multiplier_t mul)
Definition Storage.cc:43
uintptr_t hashval_t
Definition Storage.hh:41
void zero(rset_t::iterator &num)
Definition Storage.cc:1076
bool is_in(const T &val, const std::initializer_list< T > &list)
Definition Storage.hh:324
std::set< std::string > nset_t
Definition Storage.hh:39
void one(rset_t::iterator &num)
Definition Storage.cc:1081
void half(rset_t::iterator &num)
Definition Storage.cc:1093
void set(rset_t::iterator &num, multiplier_t fac)
Definition Storage.cc:1063
void add(rset_t::iterator &num, multiplier_t fac)
Definition Storage.cc:1069
long to_long(multiplier_t mul)
Definition Storage.cc:38
mpq_class multiplier_t
Definition Storage.hh:38
nset_t name_set
Definition Storage.cc:35
std::string to_string(long num)
Definition Storage.cc:48
Definition Storage.hh:83
bracket_t bracket
Definition Storage.hh:85
bool line_per_node
Definition Storage.hh:87
bool keep_after_eval
Definition Storage.hh:84
parent_rel_t parent_rel
Definition Storage.hh:86