GiNaC  1.8.0
basic.h
Go to the documentation of this file.
1 
5 /*
6  * GiNaC Copyright (C) 1999-2020 Johannes Gutenberg University Mainz, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifndef GINAC_BASIC_H
24 #define GINAC_BASIC_H
25 
26 #include "flags.h"
27 #include "ptr.h"
28 #include "assertion.h"
29 #include "registrar.h"
30 
31 #include <cstddef> // for size_t
32 #include <map>
33 #include <set>
34 #include <typeinfo> // for typeid
35 #include <vector>
36 #include <utility>
37 
38 namespace GiNaC {
39 
40 class ex;
41 struct ex_is_less;
42 class symbol;
43 class numeric;
44 class relational;
45 class archive_node;
46 class print_context;
47 
48 typedef std::vector<ex> exvector;
49 typedef std::set<ex, ex_is_less> exset;
50 typedef std::map<ex, ex, ex_is_less> exmap;
51 
52 // Define this to enable some statistical output for comparisons and hashing
53 #undef GINAC_COMPARE_STATISTICS
54 
55 #ifdef GINAC_COMPARE_STATISTICS
56 class compare_statistics_t {
57 public:
58  compare_statistics_t()
59  : total_compares(0), nontrivial_compares(0), total_basic_compares(0), compare_same_hashvalue(0), compare_same_type(0),
60  total_is_equals(0), nontrivial_is_equals(0), total_basic_is_equals(0), is_equal_same_hashvalue(0), is_equal_same_type(0),
61  total_gethash(0), gethash_cached(0) {}
62  ~compare_statistics_t();
63 
64  unsigned long total_compares;
65  unsigned long nontrivial_compares;
66  unsigned long total_basic_compares;
67  unsigned long compare_same_hashvalue;
68  unsigned long compare_same_type;
69 
70  unsigned long total_is_equals;
71  unsigned long nontrivial_is_equals;
72  unsigned long total_basic_is_equals;
73  unsigned long is_equal_same_hashvalue;
74  unsigned long is_equal_same_type;
75 
76  unsigned long total_gethash;
77  unsigned long gethash_cached;
78 };
79 
80 extern compare_statistics_t compare_statistics;
81 #endif
82 
83 
85 struct map_function {
86  virtual ~map_function() {}
87  typedef const ex & argument_type;
88  typedef ex result_type;
89  virtual ex operator()(const ex & e) = 0;
90 };
91 
92 
97 class visitor {
98 protected:
99  virtual ~visitor() {}
100 };
101 
102 
104 class basic : public refcounted
105 {
107 
108  friend class ex;
109 
110  // default constructor, destructor, copy constructor and assignment operator
111 protected:
112  basic() : flags(0) {}
113 
114 public:
117  virtual ~basic()
118  {
120  }
121  basic(const basic & other);
122  const basic & operator=(const basic & other);
123 
124 protected:
125  // new virtual functions which can be overridden by derived classes
126 public: // only const functions please (may break reference counting)
127 
131  virtual basic * duplicate() const
132  {
133  basic * bp = new basic(*this);
135  return bp;
136  }
137 
138  // evaluation
139  virtual ex eval() const;
140  virtual ex evalf() const;
141  virtual ex evalm() const;
142  virtual ex eval_integ() const;
143 protected:
144  virtual ex eval_ncmul(const exvector & v) const;
145 public:
146  virtual ex eval_indexed(const basic & i) const;
147 
148  // printing
149  virtual void print(const print_context & c, unsigned level = 0) const;
150  virtual void dbgprint() const;
151  virtual void dbgprinttree() const;
152  virtual unsigned precedence() const;
153 
154  // info
155  virtual bool info(unsigned inf) const;
156 
157  // operand access
158  virtual size_t nops() const;
159  virtual ex op(size_t i) const;
160  virtual ex operator[](const ex & index) const;
161  virtual ex operator[](size_t i) const;
162  virtual ex & let_op(size_t i);
163  virtual ex & operator[](const ex & index);
164  virtual ex & operator[](size_t i);
165 
166  // pattern matching
167  virtual bool has(const ex & other, unsigned options = 0) const;
168  virtual bool match(const ex & pattern, exmap & repls) const;
169 protected:
170  virtual bool match_same_type(const basic & other) const;
171 public:
172 
173  // substitutions
174  virtual ex subs(const exmap & m, unsigned options = 0) const;
175 
176  // function mapping
177  virtual ex map(map_function & f) const;
178 
179  // visitors and tree traversal
180  virtual void accept(GiNaC::visitor & v) const
181  {
182  if (visitor *p = dynamic_cast<visitor *>(&v))
183  p->visit(*this);
184  }
185 
186  // degree/coeff
187  virtual bool is_polynomial(const ex & var) const;
188  virtual int degree(const ex & s) const;
189  virtual int ldegree(const ex & s) const;
190  virtual ex coeff(const ex & s, int n = 1) const;
191 
192  // expand/collect
193  virtual ex expand(unsigned options = 0) const;
194  virtual ex collect(const ex & s, bool distributed = false) const;
195 
196  // differentiation and series expansion
197 protected:
198  virtual ex derivative(const symbol & s) const;
199 public:
200  virtual ex series(const relational & r, int order, unsigned options = 0) const;
201 
202  // rational functions
203  virtual ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const;
204  virtual ex to_rational(exmap & repl) const;
205  virtual ex to_polynomial(exmap & repl) const;
206 
207  // polynomial algorithms
208  virtual numeric integer_content() const;
209  virtual ex smod(const numeric &xi) const;
210  virtual numeric max_coefficient() const;
211 
212  // indexed objects
213  virtual exvector get_free_indices() const;
214  virtual ex add_indexed(const ex & self, const ex & other) const;
215  virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
216  virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
217 
218  // noncommutativity
219  virtual unsigned return_type() const;
220  virtual return_type_t return_type_tinfo() const;
221 
222  // functions for complex expressions
223  virtual ex conjugate() const;
224  virtual ex real_part() const;
225  virtual ex imag_part() const;
226 
227  // functions that should be called from class ex only
228 protected:
229  virtual int compare_same_type(const basic & other) const;
230  virtual bool is_equal_same_type(const basic & other) const;
231 
232  virtual unsigned calchash() const;
233 
234  // non-virtual functions in this class
235 public:
241  template <class T>
242  void print_dispatch(const print_context & c, unsigned level) const
243  {
244  print_dispatch(T::get_class_info_static(), c, level);
245  }
246 
247  void print_dispatch(const registered_class_info & ri, const print_context & c, unsigned level) const;
248 
255  virtual void archive(archive_node& n) const;
264  virtual void read_archive(const archive_node& n, lst& syms); // no const
265 
266  ex subs_one_level(const exmap & m, unsigned options) const;
267  ex diff(const symbol & s, unsigned nth = 1) const;
268  int compare(const basic & other) const;
269  bool is_equal(const basic & other) const;
270  const basic & hold() const;
271 
272  unsigned gethash() const
273  {
274 #ifdef GINAC_COMPARE_STATISTICS
275  compare_statistics.total_gethash++;
276 #endif
278 #ifdef GINAC_COMPARE_STATISTICS
279  compare_statistics.gethash_cached++;
280 #endif
281  return hashvalue;
282  } else {
283  return calchash();
284  }
285  }
286 
288  const basic & setflag(unsigned f) const {flags |= f; return *this;}
289 
291  const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
292 
293 protected:
294  void ensure_if_modifiable() const;
295 
296  void do_print(const print_context & c, unsigned level) const;
297  void do_print_tree(const print_tree & c, unsigned level) const;
298  void do_print_python_repr(const print_python_repr & c, unsigned level) const;
299 
300  // member variables
301 protected:
302  mutable unsigned flags;
303  mutable unsigned hashvalue;
304 };
305 
306 // global variables
307 
308 
309 // convenience type checker template functions
310 
312 template <class T>
313 inline bool is_a(const basic &obj)
314 {
315  return dynamic_cast<const T *>(&obj) != nullptr;
316 }
317 
319 template <class T>
320 inline bool is_exactly_a(const basic & obj)
321 {
322  return typeid(T) == typeid(obj);
323 }
324 
333 template<class B, typename... Args>
334 inline B & dynallocate(Args &&... args)
335 {
336  return const_cast<B &>(static_cast<const B &>((new B(std::forward<Args>(args)...))->setflag(status_flags::dynallocated)));
337 }
344 template<class B>
345 inline B & dynallocate(std::initializer_list<ex> il)
346 {
347  return const_cast<B &>(static_cast<const B &>((new B(il))->setflag(status_flags::dynallocated)));
348 }
349 
350 } // namespace GiNaC
351 
352 #endif // ndef GINAC_BASIC_H
GiNaC::map_function::~map_function
virtual ~map_function()
Definition: basic.h:86
GiNaC::is_a
bool is_a(const basic &obj)
Check if obj is a T, including base classes.
Definition: basic.h:313
GiNaC::print_python_repr
Context for python-parsable output.
Definition: print.h:139
GiNaC::basic::accept
virtual void accept(GiNaC::visitor &v) const
Definition: basic.h:180
GiNaC::basic::subs
virtual ex subs(const exmap &m, unsigned options=0) const
Substitute a set of objects by arbitrary expressions.
Definition: basic.cpp:607
GiNaC::refcounted::get_refcount
unsigned int get_refcount() const noexcept
Definition: ptr.h:41
GiNaC::basic::calchash
virtual unsigned calchash() const
Compute the hash value of an object and if it makes sense to store it in the objects status_flags,...
Definition: basic.cpp:770
r
size_t r
Definition: factor.cpp:770
GiNaC::basic::collect
virtual ex collect(const ex &s, bool distributed=false) const
Sort expanded expression in terms of powers of some object(s).
Definition: basic.cpp:348
GiNaC::basic::return_type
virtual unsigned return_type() const
Definition: basic.cpp:751
GiNaC::basic::ldegree
virtual int ldegree(const ex &s) const
Return degree of lowest power in object s.
Definition: basic.cpp:331
GiNaC::visitor::~visitor
virtual ~visitor()
Definition: basic.h:99
GiNaC::basic::eval_ncmul
virtual ex eval_ncmul(const exvector &v) const
Definition: basic.cpp:686
GiNaC::basic::hashvalue
unsigned hashvalue
hash value
Definition: basic.h:303
GiNaC::map_function::argument_type
const ex & argument_type
Definition: basic.h:87
GiNaC::basic::is_equal_same_type
virtual bool is_equal_same_type(const basic &other) const
Returns true if two objects of same type are equal.
Definition: basic.cpp:729
assertion.h
Assertion macro definition.
GiNaC::basic::operator[]
virtual ex operator[](const ex &index) const
Definition: basic.cpp:250
GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS
#define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername)
Primary macro for inclusion in the declaration of each registered class.
Definition: registrar.h:141
GiNaC::relational
This class holds a relation consisting of two expressions and a logical relation between them.
Definition: relational.h:35
GiNaC::map_function
Function object for map().
Definition: basic.h:85
GiNaC::basic::operator=
const basic & operator=(const basic &other)
basic assignment operator: the other object might be of a derived class.
Definition: basic.cpp:64
GiNaC::print_context
Base class for print_contexts.
Definition: print.h:103
GiNaC::basic::is_polynomial
virtual bool is_polynomial(const ex &var) const
Check whether this is a polynomial in the given variables.
Definition: basic.cpp:319
GiNaC::status_flags::dynallocated
@ dynallocated
heap-allocated (i.e. created by new if we want to be clever and bypass the stack,
Definition: flags.h:202
GiNaC::basic::expand
virtual ex expand(unsigned options=0) const
Expand expression, i.e.
Definition: basic.cpp:796
GiNaC::basic::get_free_indices
virtual exvector get_free_indices() const
Return a vector containing the free indices of an expression.
Definition: basic.cpp:666
GiNaC::basic::evalm
virtual ex evalm() const
Evaluate sums, products and integer powers of matrices.
Definition: basic.cpp:441
GiNaC::basic::archive
virtual void archive(archive_node &n) const
Save (serialize) the object into archive node.
Definition: basic.cpp:100
GiNaC::basic::basic
basic()
Definition: basic.h:112
GiNaC::basic::scalar_mul_indexed
virtual ex scalar_mul_indexed(const ex &self, const numeric &other) const
Multiply an indexed expression with a scalar.
Definition: basic.cpp:493
GiNaC::exvector
std::vector< ex > exvector
Definition: basic.h:46
GiNaC::basic::dbgprinttree
virtual void dbgprinttree() const
Little wrapper around printtree to be called within a debugger.
Definition: basic.cpp:208
GiNaC::visitor
Degenerate base class for visitors.
Definition: basic.h:97
GiNaC::basic::dbgprint
virtual void dbgprint() const
Little wrapper around print to be called within a debugger.
Definition: basic.cpp:199
GiNaC::basic::do_print
void do_print(const print_context &c, unsigned level) const
Default output to stream.
Definition: basic.cpp:169
GiNaC::basic::diff
ex diff(const symbol &s, unsigned nth=1) const
Default interface of nth derivative ex::diff(s, n).
Definition: basic.cpp:646
GiNaC::archive_node
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition: archive.h:49
GiNaC::basic::normal
virtual ex normal(exmap &repl, exmap &rev_lookup, lst &modifier) const
Default implementation of ex::normal().
Definition: normal.cpp:2192
GiNaC::basic::compare
int compare(const basic &other) const
Compare objects syntactically to establish canonical ordering.
Definition: basic.cpp:816
GiNaC::basic::nops
virtual size_t nops() const
Number of operands/members.
Definition: basic.cpp:229
GiNaC::basic::eval
virtual ex eval() const
Perform automatic non-interruptive term rewriting rules.
Definition: basic.cpp:413
GiNaC::refcounted
Base class for reference-counted objects.
Definition: ptr.h:35
options
unsigned options
Definition: factor.cpp:2480
m
mvec m
Definition: factor.cpp:771
GiNaC
Definition: add.cpp:38
syms
exset syms
Definition: factor.cpp:2434
GiNaC::basic::is_equal
bool is_equal(const basic &other) const
Test for syntactic equality.
Definition: basic.cpp:863
GiNaC::basic::print_dispatch
void print_dispatch(const print_context &c, unsigned level) const
Like print(), but dispatch to the specified class.
Definition: basic.h:242
GiNaC::basic::let_op
virtual ex & let_op(size_t i)
Return modifiable operand/member at position i.
Definition: basic.cpp:244
GiNaC::basic::clearflag
const basic & clearflag(unsigned f) const
Clear some status_flags.
Definition: basic.h:291
GiNaC::basic::imag_part
virtual ex imag_part() const
Definition: basic.cpp:681
GiNaC::basic::return_type_tinfo
virtual return_type_t return_type_tinfo() const
Definition: basic.cpp:756
GiNaC::ex
Lightweight wrapper for GiNaC's symbolic objects.
Definition: ex.h:72
GiNaC::basic::info
virtual bool info(unsigned inf) const
Information about the object.
Definition: basic.cpp:222
GiNaC::basic::smod
virtual ex smod(const numeric &xi) const
Apply symmetric modular homomorphism to an expanded multivariate polynomial.
Definition: normal.cpp:1203
GiNaC::basic::compare_same_type
virtual int compare_same_type(const basic &other) const
Returns order relation between two objects of same type.
Definition: basic.cpp:719
GiNaC::basic::eval_integ
virtual ex eval_integ() const
Evaluate integrals, if result is known.
Definition: basic.cpp:455
GiNaC::basic::duplicate
virtual basic * duplicate() const
Create a clone of this object on the heap.
Definition: basic.h:131
GiNaC::basic::do_print_python_repr
void do_print_python_repr(const print_python_repr &c, unsigned level) const
Python parsable output to stream.
Definition: basic.cpp:187
GiNaC::basic::evalf
virtual ex evalf() const
Evaluate object numerically.
Definition: basic.cpp:425
GiNaC::status_flags::hash_calculated
@ hash_calculated
.calchash() has already done its job
Definition: flags.h:205
GiNaC::basic::has
virtual bool has(const ex &other, unsigned options=0) const
Test for occurrence of a pattern.
Definition: basic.cpp:280
GiNaC::basic::hold
const basic & hold() const
Stop further evaluation.
Definition: basic.cpp:887
registrar.h
GiNaC's class registrar (for class basic and all classes derived from it).
GiNaC::basic::derivative
virtual ex derivative(const symbol &s) const
Default implementation of ex::diff().
Definition: basic.cpp:704
GiNaC::basic::coeff
virtual ex coeff(const ex &s, int n=1) const
Return coefficient of degree n in object s.
Definition: basic.cpp:337
GiNaC::basic::~basic
virtual ~basic()
basic destructor, virtual because class ex will delete objects of derived classes via a basic*.
Definition: basic.h:117
GiNaC::basic::subs_one_level
ex subs_one_level(const exmap &m, unsigned options) const
Helper function for subs().
Definition: basic.cpp:585
GiNaC::basic::precedence
virtual unsigned precedence() const
Return relative operator precedence (for parenthezing output).
Definition: basic.cpp:214
GiNaC::return_type_t
To distinguish between different kinds of non-commutative objects.
Definition: registrar.h:44
GiNaC::print_tree
Context for tree-like output for debugging.
Definition: print.h:147
GiNaC::is_exactly_a
bool is_exactly_a(const basic &obj)
Check if obj is a T, not including base classes.
Definition: basic.h:320
GiNaC::exset
std::set< ex, ex_is_less > exset
Definition: basic.h:49
GiNaC::basic::integer_content
virtual numeric integer_content() const
Definition: normal.cpp:323
GiNaC::basic::setflag
const basic & setflag(unsigned f) const
Set some status_flags.
Definition: basic.h:288
GiNaC::map_function::operator()
virtual ex operator()(const ex &e)=0
GiNaC::dynallocate
B & dynallocate(Args &&... args)
Constructs a new (class basic or derived) B object on the heap.
Definition: basic.h:334
GiNaC::container
Wrapper template for making GiNaC classes out of STL containers.
Definition: container.h:73
GiNaC::basic::ensure_if_modifiable
void ensure_if_modifiable() const
Ensure the object may be modified without hurting others, throws if this is not the case.
Definition: basic.cpp:894
GiNaC::basic::to_polynomial
virtual ex to_polynomial(exmap &repl) const
Definition: normal.cpp:2614
GiNaC::basic::conjugate
virtual ex conjugate() const
Definition: basic.cpp:671
c
size_t c
Definition: factor.cpp:770
GiNaC::exmap
std::map< ex, ex, ex_is_less > exmap
Definition: basic.h:50
GiNaC::basic::series
virtual ex series(const relational &r, int order, unsigned options=0) const
Default implementation of ex::series().
Definition: pseries.cpp:612
GiNaC::basic::degree
virtual int degree(const ex &s) const
Return degree of highest power in object s.
Definition: basic.cpp:325
GiNaC::symbol
Basic CAS symbol.
Definition: symbol.h:39
n
size_t n
Definition: factor.cpp:1463
GiNaC::basic::eval_indexed
virtual ex eval_indexed(const basic &i) const
Perform automatic symbolic evaluations on indexed expression that contains this object as the base ex...
Definition: basic.cpp:465
GiNaC::basic::do_print_tree
void do_print_tree(const print_tree &c, unsigned level) const
Tree output to stream.
Definition: basic.cpp:175
GiNaC::map_function::result_type
ex result_type
Definition: basic.h:88
GiNaC::basic::op
virtual ex op(size_t i) const
Return operand/member at position i.
Definition: basic.cpp:238
GiNaC::basic
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition: basic.h:105
GiNaC::class_info
Definition: class_info.h:41
GiNaC::basic::match_same_type
virtual bool match_same_type(const basic &other) const
Returns true if the attributes of two objects are similar enough for a match.
Definition: basic.cpp:744
GiNaC::basic::read_archive
virtual void read_archive(const archive_node &n, lst &syms)
Load (deserialize) the object from an archive node.
Definition: basic.cpp:96
flags.h
Collection of all flags used through the GiNaC framework.
GiNaC::basic::contract_with
virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector &v) const
Try to contract two indexed expressions that appear in the same product.
Definition: basic.cpp:510
GiNaC::basic::add_indexed
virtual ex add_indexed(const ex &self, const ex &other) const
Add two indexed expressions.
Definition: basic.cpp:481
GiNaC::basic::basic
basic(const basic &other)
GiNaC::basic::max_coefficient
virtual numeric max_coefficient() const
Implementation ex::max_coefficient().
Definition: normal.cpp:1161
ptr.h
Reference-counted pointer template.
GiNaC::basic::flags
unsigned flags
of type status_flags
Definition: basic.h:302
GiNaC::basic::print
virtual void print(const print_context &c, unsigned level=0) const
Output to stream.
Definition: basic.cpp:116
GiNaC::basic::map
virtual ex map(map_function &f) const
Construct new expression by applying the specified function to all sub-expressions (one level only,...
Definition: basic.cpp:294
order
int order
Definition: integration_kernel.cpp:248
GiNaC::numeric
This class is a wrapper around CLN-numbers within the GiNaC class hierarchy.
Definition: numeric.h:82
GiNaC::basic::real_part
virtual ex real_part() const
Definition: basic.cpp:676
GINAC_ASSERT
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition: assertion.h:33
GiNaC::basic::to_rational
virtual ex to_rational(exmap &repl) const
Default implementation of ex::to_rational().
Definition: normal.cpp:2609
GiNaC::basic::match
virtual bool match(const ex &pattern, exmap &repls) const
Check whether the expression matches a given pattern.
Definition: basic.cpp:519
GiNaC::basic::gethash
unsigned gethash() const
Definition: basic.h:272

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.