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#include <variant>
34#include <memory_resource>
35
36#include "tree.hh"
37#include "Multiplier.hh"
38
39namespace cadabra {
40
41 class NTensor;
42 class NInterpolatingFunction;
43
45 typedef std::set<std::string> nset_t;
46 typedef std::set<multiplier_t> rset_t;
48
50 double to_double(multiplier_t);
51 std::string to_string(long);
52
53 extern nset_t name_set;
54 extern rset_t rat_set;
55
61
62 class str_node { // size: 9 bytes (32 bit arch), can be reduced to 5 bytes.
63 public:
65
69
70 str_node(void);
71// str_node(const str_node&);
72// str_node(str_node&&);
75 str_node(const std::u32string& name, bracket_t btype=b_none, parent_rel_t ptype=p_none);
76
77 bool operator==(const str_node&) const;
78 bool operator<(const str_node&) const;
79
80 nset_t::iterator name;
81 rset_t::iterator multiplier;
82
83 // Special content which does not fit the AST structure, but still needs to
84 // sit in the tree, is stored in the form of a shared pointer to data outside
85 // the tree. If there is nothing, the std::monostate is set.
86 std::variant<std::monostate,
87 std::shared_ptr<NTensor>,
88 std::shared_ptr<NInterpolatingFunction>
90
91#ifdef _WIN32
92 struct flag_t {
93 bool keep_after_eval ;
96 bool line_per_node ;
97 };
98#else
99 struct flag_t { // kept inside 8 bits for speed and size
104 };
105#endif
106
108
111 void flip_parent_rel();
112
116 bool is_standard() const;
117
118 bool is_zero() const;
119 bool is_identity() const;
120 bool is_rational() const;
121 bool is_double() const;
122 bool is_unsimplified_rational() const;
123 bool is_unsimplified_double() const;
124 bool is_integer() const;
125 bool is_unsimplified_integer() const;
126 bool is_index() const; // _ or ^ parent_rel (does not query properties)
127 bool is_quoted_string() const;
128 bool is_command() const;
129 bool is_inert_command() const;
130 bool is_name_wildcard() const; // ?
131 bool is_object_wildcard() const; // ??
132 bool is_range_wildcard() const; // #{..}
133 bool is_siblings_wildcard() const; // a...
134 bool is_autodeclare_wildcard() const; // m#
135 bool is_indexstar_wildcard() const; // ?* in sub/super
136 bool is_indexplus_wildcard() const; // ?+ in sub/super
137 bool is_numbered_symbol() const; // [a-zA-Z]+[0-9]+
138
139 nset_t::iterator name_only();
140
141 static bool compare_names_only(const str_node&, const str_node&);
142 static bool compare_name_brack_par(const str_node&, const str_node&);
143 static bool compare_name_inverse_par(const str_node&, const str_node&);
144 };
145
149 void multiply(rset_t::iterator&, multiplier_t);
150 void divide(rset_t::iterator&, multiplier_t);
151 void add(rset_t::iterator&, multiplier_t);
152 void zero(rset_t::iterator&);
153 void one(rset_t::iterator&);
154 void flip_sign(rset_t::iterator&);
155 void half(rset_t::iterator&);
156 void set(rset_t::iterator&, multiplier_t);
157
158
165
166// typedef tree<str_node, std::pmr::polymorphic_allocator<tree_node_<str_node>>> cdb_tree;
167// extern std::pmr::polymorphic_allocator<tree_node_<str_node>> alloc;
169
170 class Ex : public std::enable_shared_from_this<Ex>, public cdb_tree {
171 public:
172 Ex();
174 explicit Ex(cdb_tree::iterator);
176 explicit Ex(const str_node&);
178 Ex(const Ex&);
180 // explicit Ex(Ex);
182 explicit Ex(const std::string&);
184 explicit Ex(int);
186 explicit Ex(double);
188 explicit Ex(int, int);
189
190 Ex& operator=(Ex);
191
199
208 result_t state() const;
210 void reset_state();
211
223
224 bool changed_state();
225
228 bool is_rational() const;
230 bool is_integer() const;
231 long to_integer() const;
232 bool is_string() const;
233
235 bool equals(const std::string&) const;
236
238 bool is_empty() const;
239
243 static std::ostream& print_python(std::ostream& str, Ex::iterator it);
244
246 std::ostream& print_entire_tree(std::ostream& str) const;
247 static std::ostream& print_recursive_treeform(std::ostream& str, Ex::iterator it);
248 static std::ostream& print_recursive_treeform(std::ostream& str, Ex::iterator it, unsigned int& number);
249
251 std::ostream& print_repr(std::ostream& str, Ex::iterator it) const;
252
254 iterator named_parent(iterator it, const std::string&) const;
255 iterator erase_expression(iterator it);
256
258 hashval_t calc_hash(iterator it) const;
259
261 static sibling_iterator arg(iterator, unsigned int);
262 static unsigned int arg_size(sibling_iterator);
263
264 multiplier_t arg_to_num(sibling_iterator, unsigned int) const; // shorthand for numerical arguments
265
266 // Like 'child', but using index iterators instead.
267 // sibling_iterator tensor_index(const iterator_base& position, unsigned int) const;
268
269 // Number of \\history nodes in an expression
270 unsigned int number_of_steps(iterator it) const;
271 // Given an iterator pointing to any node in the tree, figure
272 // out to which equation number it belongs.
273// unsigned int number_of_equations() const;
274// unsigned int equation_number(iterator it) const;
275// nset_t::iterator equation_label(iterator it) const;
276// iterator equation_by_number(unsigned int i) const;
277// iterator equation_by_name(nset_t::iterator it) const;
278// iterator equation_by_name(nset_t::iterator it, unsigned int& ) const;
279// iterator equation_by_number_or_name(iterator it, unsigned int last_used_equation) const;
280// iterator equation_by_number_or_name(iterator it, unsigned int last_used_equation,
281// unsigned int&) const;
282// std::string equation_number_or_name(iterator it, unsigned int last_used_equation) const;
283// iterator procedure_by_name(nset_t::iterator it) const;
284
285 // Determine whether a node has an '\ldots' parent (not necessarily direct).
286 bool is_hidden(iterator) const;
287
299 iterator replace_index(iterator position, const iterator& from, bool keep_parent_rel=false);
300
303 iterator move_index(iterator position, const iterator& from);
304
308 void list_wrap_single_element(iterator&);
309 void list_unwrap_single_element(iterator&);
310
315 iterator flatten_and_erase(iterator position);
316
319
320 bool operator==(const Ex& other) const;
321
326 void push_history(const std::vector<Ex::path_t>&);
327
331 std::vector<Ex::path_t> pop_history();
332
335 int history_size() const;
336
337 private:
339
340 std::vector<cdb_tree> history;
342 std::vector<std::vector<Ex::path_t> > terms;
343 };
344
345
349
351 public:
352 bool operator()(nset_t::iterator first, nset_t::iterator second) const;
353 };
354
355 template <typename T>
356 bool is_in(const T& val, const std::initializer_list<T>& list)
357 {
358 for (const auto& i : list) {
359 if (val == i) {
360 return true;
361 }
362 }
363 return false;
364 }
365
366 }
367
375
376std::ostream& operator<<(std::ostream&, const cadabra::Ex&);
377std::ostream& operator<<(std::ostream&, cadabra::Ex::iterator);
Definition Storage.hh:170
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:689
void list_unwrap_single_element(iterator &)
Definition Storage.cc:711
multiplier_t arg_to_num(sibling_iterator, unsigned int) const
Definition Storage.cc:532
static std::ostream & print_python(std::ostream &str, Ex::iterator it)
Display expression in Python/Cadabra input form.
Definition Storage.cc:239
std::vector< cdb_tree > history
Definition Storage.hh:340
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:786
result_t
Keeping track of what algorithms have done to this expression.
Definition Storage.hh:200
@ l_cached
Definition Storage.hh:206
@ l_error
Definition Storage.hh:205
@ l_checkpointed
Definition Storage.hh:201
@ l_no_action
Definition Storage.hh:202
@ l_applied_no_new_dummies
Definition Storage.hh:204
@ l_applied
Definition Storage.hh:203
hashval_t calc_hash(iterator it) const
Calculate the hash value for the subtree starting at 'it'.
Definition Storage.cc:494
void reset_state()
Definition Storage.cc:170
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:474
static sibling_iterator arg(iterator, unsigned int)
Quick access to arguments or argument lists for A(B)(C,D) type nodes.
Definition Storage.cc:517
iterator erase_expression(iterator it)
Definition Storage.cc:487
result_t state() const
Definition Storage.cc:144
std::ostream & print_entire_tree(std::ostream &str) const
Output helpers mainly for debugging purposes.
Definition Storage.cc:376
result_t state_
Definition Storage.hh:338
bool is_integer() const
Definition Storage.cc:200
static unsigned int arg_size(sibling_iterator)
Definition Storage.cc:526
bool is_string() const
Definition Storage.cc:215
void list_wrap_single_element(iterator &)
Make sure that the node pointed to is a \comma object, i.e.
Definition Storage.cc:700
long to_integer() const
Definition Storage.cc:208
bool changed_state()
A status query method mainly to implement a simple method to apply algorithms until they converge.
Definition Storage.cc:177
int history_size() const
Return the size of the history; 0 means no history, just the current expression.
Definition Storage.cc:812
bool is_hidden(iterator) const
Definition Storage.cc:646
Ex()
Definition Storage.cc:73
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:803
void push_history(const std::vector< Ex::path_t > &)
Push a copy of the current Storstate of the expression onto the history stack.
Definition Storage.cc:797
unsigned int number_of_steps(iterator it) const
Ex & operator=(Ex)
Definition Storage.cc:99
bool is_empty() const
Test if the expression is empty (no content at all).
Definition Storage.cc:234
bool is_rational() const
Test if the expression is a rational number.
Definition Storage.cc:185
std::ostream & print_repr(std::ostream &str, Ex::iterator it) const
Print a representation like Python's 'repr'.
Definition Storage.cc:343
bool equals(const std::string &) const
Comparison operators with primitive types.
Definition Storage.cc:227
multiplier_t to_rational() const
Definition Storage.cc:193
iterator flatten_and_erase(iterator position)
Replace the node with the children of the node, useful for e.g.
Definition Storage.cc:721
std::vector< std::vector< Ex::path_t > > terms
Patterns which describe how to get from one history step to the next.
Definition Storage.hh:342
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:677
static std::ostream & print_recursive_treeform(std::ostream &str, Ex::iterator it)
Definition Storage.cc:354
void update_state(result_t)
Definition Storage.cc:149
Definition Multiplier.hh:9
Compare two nset iterators by comparing the strings to which they point.
Definition Storage.hh:350
bool operator()(nset_t::iterator first, nset_t::iterator second) const
Definition Storage.cc:1156
Elementary building block for a mathematical expression.
Definition Storage.hh:62
bool is_indexstar_wildcard() const
Definition Storage.cc:1067
bool is_index() const
Definition Storage.cc:969
bool is_unsimplified_integer() const
Definition Storage.cc:959
bool operator<(const str_node &) const
Definition Storage.cc:1224
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:891
bracket_t
Definition Storage.hh:64
@ b_round
Definition Storage.hh:64
@ b_none
Definition Storage.hh:64
@ b_square
Definition Storage.hh:64
@ b_no
Definition Storage.hh:64
@ b_pointy
Definition Storage.hh:64
@ b_curly
Definition Storage.hh:64
@ b_invalid
Definition Storage.hh:64
bool is_numbered_symbol() const
Definition Storage.cc:1089
str_node(void)
Definition Storage.cc:817
static bool compare_name_brack_par(const str_node &, const str_node &)
Definition Storage.cc:1139
bool is_quoted_string() const
Definition Storage.cc:975
bool is_unsimplified_rational() const
Definition Storage.cc:937
bool is_siblings_wildcard() const
Definition Storage.cc:1046
std::variant< std::monostate, std::shared_ptr< NTensor >, std::shared_ptr< NInterpolatingFunction > > content
Definition Storage.hh:89
nset_t::iterator name_only()
Definition Storage.cc:1100
bool is_command() const
Definition Storage.cc:983
bool is_object_wildcard() const
Definition Storage.cc:1024
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:68
@ p_exponent
Definition Storage.hh:68
@ p_sub
Definition Storage.hh:68
@ p_invalid
Definition Storage.hh:68
@ p_property
Definition Storage.hh:68
@ p_none
Definition Storage.hh:68
@ p_super
Definition Storage.hh:68
@ p_components
Definition Storage.hh:68
rset_t::iterator multiplier
Definition Storage.hh:81
static bool compare_name_inverse_par(const str_node &, const str_node &)
Definition Storage.cc:1147
bool is_inert_command() const
Definition Storage.cc:998
bool is_indexplus_wildcard() const
Definition Storage.cc:1078
bool is_range_wildcard() const
Definition Storage.cc:1035
bool is_standard() const
Standard nodes are nodes which do not hold special content in the content variable.
Definition Storage.cc:898
bool is_identity() const
Definition Storage.cc:909
bool is_name_wildcard() const
Definition Storage.cc:1009
bool is_autodeclare_wildcard() const
Definition Storage.cc:1057
bool is_double() const
Definition Storage.cc:920
bool is_unsimplified_double() const
Definition Storage.cc:947
nset_t::iterator name
Definition Storage.hh:80
static bool compare_names_only(const str_node &, const str_node &)
Definition Storage.cc:1133
bool is_zero() const
Definition Storage.cc:903
flag_t fl
Definition Storage.hh:107
bool operator==(const str_node &) const
Definition Storage.cc:1123
bool is_integer() const
Definition Storage.cc:925
bool is_rational() const
Definition Storage.cc:915
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:1235
void multiply(rset_t::iterator &num, multiplier_t fac)
Helper functions for manipulation of multipliers.
Definition Storage.cc:1164
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:43
tree< str_node, std::pmr::polymorphic_allocator< tree_node_< str_node > > > cdb_tree
Basic storage class for symbolic mathemematical expressions.
Definition Storage.hh:168
std::set< multiplier_t > rset_t
Definition Storage.hh:46
void flip_sign(rset_t::iterator &num)
Definition Storage.cc:1210
double to_double(multiplier_t mul)
Definition Storage.cc:56
Multiplier multiplier_t
Definition Storage.hh:44
uintptr_t hashval_t
Definition Storage.hh:47
void zero(rset_t::iterator &num)
Definition Storage.cc:1192
bool is_in(const T &val, const std::initializer_list< T > &list)
Definition Storage.hh:356
std::set< std::string > nset_t
Definition Storage.hh:45
void one(rset_t::iterator &num)
Definition Storage.cc:1201
void divide(rset_t::iterator &num, multiplier_t fac)
Definition Storage.cc:1171
void half(rset_t::iterator &num)
Definition Storage.cc:1217
void set(rset_t::iterator &num, multiplier_t fac)
Definition Storage.cc:1179
void add(rset_t::iterator &num, multiplier_t fac)
Definition Storage.cc:1185
long to_long(multiplier_t mul)
Definition Storage.cc:48
nset_t name_set
Definition Storage.cc:42
std::string to_string(long num)
Definition Storage.cc:64
Definition Storage.hh:99
bracket_t bracket
Definition Storage.hh:101
bool line_per_node
Definition Storage.hh:103
bool keep_after_eval
Definition Storage.hh:100
parent_rel_t parent_rel
Definition Storage.hh:102