GiNaC 1.8.10
basic.h
Go to the documentation of this file.
1
4
5/*
6 * GiNaC Copyright (C) 1999-2026 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef GINAC_BASIC_H
23#define GINAC_BASIC_H
24
25#include "flags.h"
26#include "ptr.h"
27#include "assertion.h"
28#include "registrar.h"
29
30#include <cstddef> // for size_t
31#include <map>
32#include <set>
33#include <typeinfo> // for typeid
34#include <vector>
35#include <utility>
36
37namespace GiNaC {
38
39class ex;
40struct ex_is_less;
41class symbol;
42class numeric;
43class relational;
44class archive_node;
45class print_context;
46
47typedef std::vector<ex> exvector;
48typedef std::set<ex, ex_is_less> exset;
49typedef std::map<ex, ex, ex_is_less> exmap;
50
51// Define this to enable some statistical output for comparisons and hashing
52#undef GINAC_COMPARE_STATISTICS
53
54#ifdef GINAC_COMPARE_STATISTICS
55class compare_statistics_t {
56public:
57 compare_statistics_t()
58 : total_compares(0), nontrivial_compares(0), total_basic_compares(0), compare_same_hashvalue(0), compare_same_type(0),
59 total_is_equals(0), nontrivial_is_equals(0), total_basic_is_equals(0), is_equal_same_hashvalue(0), is_equal_same_type(0),
60 total_gethash(0), gethash_cached(0) {}
61 ~compare_statistics_t();
62
63 unsigned long total_compares;
64 unsigned long nontrivial_compares;
65 unsigned long total_basic_compares;
66 unsigned long compare_same_hashvalue;
67 unsigned long compare_same_type;
68
69 unsigned long total_is_equals;
70 unsigned long nontrivial_is_equals;
71 unsigned long total_basic_is_equals;
72 unsigned long is_equal_same_hashvalue;
73 unsigned long is_equal_same_type;
74
75 unsigned long total_gethash;
76 unsigned long gethash_cached;
77};
78
79extern compare_statistics_t compare_statistics;
80#endif
81
82
85 virtual ~map_function() {}
86 typedef const ex & argument_type;
87 typedef ex result_type;
88 virtual ex operator()(const ex & e) = 0;
89};
90
91
96class visitor {
97protected:
98 virtual ~visitor() {}
99};
100
101
103class basic : public refcounted
104{
106
107 friend class ex;
108
109 // default constructor, destructor, copy constructor and assignment operator
110protected:
111 basic() : flags(0) {}
112
113public:
116 virtual ~basic()
117 {
119 }
120 basic(const basic & other);
121 const basic & operator=(const basic & other);
122
123protected:
124 // new virtual functions which can be overridden by derived classes
125public: // only const functions please (may break reference counting)
126
130 virtual basic * duplicate() const
131 {
132 basic * bp = new basic(*this);
134 return bp;
135 }
136
137 // evaluation
138 virtual ex eval() const;
139 virtual ex evalf() const;
140 virtual ex evalm() const;
141 virtual ex eval_integ() const;
142protected:
143 virtual ex eval_ncmul(const exvector & v) const;
144public:
145 virtual ex eval_indexed(const basic & i) const;
146
147 // printing
148 virtual void print(const print_context & c, unsigned level = 0) const;
149 virtual void dbgprint() const;
150 virtual void dbgprinttree() const;
151 virtual unsigned precedence() const;
152
153 // info
154 virtual bool info(unsigned inf) const;
155
156 // operand access
157 virtual size_t nops() const;
158 virtual ex op(size_t i) const;
159 virtual ex operator[](const ex & index) const;
160 virtual ex operator[](size_t i) const;
161 virtual ex & let_op(size_t i);
162 virtual ex & operator[](const ex & index);
163 virtual ex & operator[](size_t i);
164
165 // pattern matching
166 virtual bool has(const ex & other, unsigned options = 0) const;
167 virtual bool match(const ex & pattern, exmap & repls) const;
168protected:
169 virtual bool match_same_type(const basic & other) const;
170public:
171
172 // substitutions
173 virtual ex subs(const exmap & m, unsigned options = 0) const;
174
175 // function mapping
176 virtual ex map(map_function & f) const;
177
178 // visitors and tree traversal
179 virtual void accept(GiNaC::visitor & v) const
180 {
181 if (visitor *p = dynamic_cast<visitor *>(&v))
182 p->visit(*this);
183 }
184
185 // degree/coeff
186 virtual bool is_polynomial(const ex & var) const;
187 virtual int degree(const ex & s) const;
188 virtual int ldegree(const ex & s) const;
189 virtual ex coeff(const ex & s, int n = 1) const;
190
191 // expand/collect
192 virtual ex expand(unsigned options = 0) const;
193 virtual ex collect(const ex & s, bool distributed = false) const;
194
195 // differentiation and series expansion
196protected:
197 virtual ex derivative(const symbol & s) const;
198public:
199 virtual ex series(const relational & r, int order, unsigned options = 0) const;
200
201 // rational functions
202 virtual ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const;
203 virtual ex to_rational(exmap & repl) const;
204 virtual ex to_polynomial(exmap & repl) const;
205
206 // polynomial algorithms
207 virtual numeric integer_content() const;
208 virtual ex smod(const numeric &xi) const;
209 virtual numeric max_coefficient() const;
210
211 // indexed objects
212 virtual exvector get_free_indices() const;
213 virtual ex add_indexed(const ex & self, const ex & other) const;
214 virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
215 virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
216
217 // noncommutativity
218 virtual unsigned return_type() const;
219 virtual return_type_t return_type_tinfo() const;
220
221 // functions for complex expressions
222 virtual ex conjugate() const;
223 virtual ex real_part() const;
224 virtual ex imag_part() const;
225
226 // functions that should be called from class ex only
227protected:
228 virtual int compare_same_type(const basic & other) const;
229 virtual bool is_equal_same_type(const basic & other) const;
230
231 virtual unsigned calchash() const;
232
233 // non-virtual functions in this class
234public:
240 template <class T>
241 void print_dispatch(const print_context & c, unsigned level) const
242 {
243 print_dispatch(T::get_class_info_static(), c, level);
244 }
245
246 void print_dispatch(const registered_class_info & ri, const print_context & c, unsigned level) const;
247
254 virtual void archive(archive_node& n) const;
263 virtual void read_archive(const archive_node& n, lst& syms); // no const
264
265 ex subs_one_level(const exmap & m, unsigned options) const;
266 ex diff(const symbol & s, unsigned nth = 1) const;
267 int compare(const basic & other) const;
268 bool is_equal(const basic & other) const;
269 const basic & hold() const;
270
271 unsigned gethash() const
272 {
273#ifdef GINAC_COMPARE_STATISTICS
274 compare_statistics.total_gethash++;
275#endif
277#ifdef GINAC_COMPARE_STATISTICS
278 compare_statistics.gethash_cached++;
279#endif
280 return hashvalue;
281 } else {
282 return calchash();
283 }
284 }
285
287 const basic & setflag(unsigned f) const {flags |= f; return *this;}
288
290 const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
291
292protected:
293 void ensure_if_modifiable() const;
294
295 void do_print(const print_context & c, unsigned level) const;
296 void do_print_tree(const print_tree & c, unsigned level) const;
297 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
298
299 // member variables
300protected:
301 mutable unsigned flags;
302 mutable unsigned hashvalue;
303};
304
305// global variables
306
307
308// convenience type checker template functions
309
311template <class T>
312inline bool is_a(const basic &obj)
313{
314 return dynamic_cast<const T *>(&obj) != nullptr;
315}
316
318template <class T>
319inline bool is_exactly_a(const basic & obj)
320{
321 return typeid(T) == typeid(obj);
322}
323
332template<class B, typename... Args>
333inline B & dynallocate(Args &&... args)
334{
335 return const_cast<B &>(static_cast<const B &>((new B(std::forward<Args>(args)...))->setflag(status_flags::dynallocated)));
336}
337
343template<class B>
344inline B & dynallocate(std::initializer_list<ex> il)
345{
346 return const_cast<B &>(static_cast<const B &>((new B(il))->setflag(status_flags::dynallocated)));
347}
348
349} // namespace GiNaC
350
351#endif // ndef GINAC_BASIC_H
Assertion macro definition.
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition assertion.h:32
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition archive.h:48
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition basic.h:104
virtual return_type_t return_type_tinfo() const
Definition basic.cpp:755
unsigned gethash() const
Definition basic.h:271
const basic & clearflag(unsigned f) const
Clear some status_flags.
Definition basic.h:290
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:743
virtual void dbgprinttree() const
Little wrapper around printtree to be called within a debugger.
Definition basic.cpp:207
void print_dispatch(const print_context &c, unsigned level) const
Like print(), but dispatch to the specified class.
Definition basic.h:241
virtual numeric integer_content() const
Definition normal.cpp:322
const basic & setflag(unsigned f) const
Set some status_flags.
Definition basic.h:287
virtual bool info(unsigned inf) const
Information about the object.
Definition basic.cpp:221
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:509
virtual ex scalar_mul_indexed(const ex &self, const numeric &other) const
Multiply an indexed expression with a scalar.
Definition basic.cpp:492
virtual numeric max_coefficient() const
Implementation ex::max_coefficient().
Definition normal.cpp:1160
unsigned hashvalue
hash value
Definition basic.h:302
void ensure_if_modifiable() const
Ensure the object may be modified without hurting others, throws if this is not the case.
Definition basic.cpp:893
virtual bool is_equal_same_type(const basic &other) const
Returns true if two objects of same type are equal.
Definition basic.cpp:728
friend class ex
Definition basic.h:107
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:464
virtual unsigned precedence() const
Return relative operator precedence (for parenthezing output).
Definition basic.cpp:213
unsigned flags
of type status_flags
Definition basic.h:301
virtual ex add_indexed(const ex &self, const ex &other) const
Add two indexed expressions.
Definition basic.cpp:480
const basic & operator=(const basic &other)
basic assignment operator: the other object might be of a derived class.
Definition basic.cpp:63
virtual void print(const print_context &c, unsigned level=0) const
Output to stream.
Definition basic.cpp:115
void do_print(const print_context &c, unsigned level) const
Default output to stream.
Definition basic.cpp:168
virtual void read_archive(const archive_node &n, lst &syms)
Load (deserialize) the object from an archive node.
Definition basic.cpp:95
ex subs_one_level(const exmap &m, unsigned options) const
Helper function for subs().
Definition basic.cpp:584
const basic & hold() const
Stop further evaluation.
Definition basic.cpp:886
virtual void accept(GiNaC::visitor &v) const
Definition basic.h:179
bool is_equal(const basic &other) const
Test for syntactic equality.
Definition basic.cpp:862
virtual int compare_same_type(const basic &other) const
Returns order relation between two objects of same type.
Definition basic.cpp:718
virtual ex & let_op(size_t i)
Return modifiable operand/member at position i.
Definition basic.cpp:243
virtual basic * duplicate() const
Create a clone of this object on the heap.
Definition basic.h:130
virtual void dbgprint() const
Little wrapper around print to be called within a debugger.
Definition basic.cpp:198
void do_print_tree(const print_tree &c, unsigned level) const
Tree output to stream.
Definition basic.cpp:174
virtual unsigned return_type() const
Definition basic.cpp:750
virtual ex eval_ncmul(const exvector &v) const
Definition basic.cpp:685
void do_print_python_repr(const print_python_repr &c, unsigned level) const
Python parsable output to stream.
Definition basic.cpp:186
virtual ex derivative(const symbol &s) const
Default implementation of ex::diff().
Definition basic.cpp:703
basic(const basic &other)
int compare(const basic &other) const
Compare objects syntactically to establish canonical ordering.
Definition basic.cpp:815
virtual ~basic()
basic destructor, virtual because class ex will delete objects of derived classes via a basic*.
Definition basic.h:116
virtual ex operator[](const ex &index) const
Definition basic.cpp:249
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:769
virtual exvector get_free_indices() const
Return a vector containing the free indices of an expression.
Definition basic.cpp:665
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:293
Lightweight wrapper for GiNaC's symbolic objects.
Definition ex.h:72
This class is a wrapper around CLN-numbers within the GiNaC class hierarchy.
Definition numeric.h:81
Base class for print_contexts.
Definition print.h:101
Context for python-parsable output.
Definition print.h:137
Context for tree-like output for debugging.
Definition print.h:145
unsigned int get_refcount() const noexcept
Definition ptr.h:40
refcounted() noexcept
Definition ptr.h:36
This class holds a relation consisting of two expressions and a logical relation between them.
Definition relational.h:34
@ dynallocated
heap-allocated (i.e. created by new if we want to be clever and bypass the stack,
Definition flags.h:201
@ hash_calculated
.calchash() has already done its job
Definition flags.h:204
Basic CAS symbol.
Definition symbol.h:38
Degenerate base class for visitors.
Definition basic.h:96
virtual ~visitor()
Definition basic.h:98
Collection of all flags used through the GiNaC framework.
Definition add.cpp:35
ex real_part(const ex &thisex)
Definition ex.h:736
bool is_polynomial(const ex &thisex, const ex &vars)
Definition ex.h:748
container< std::list > lst
Definition lst.h:31
ex to_rational(const ex &thisex, exmap &repl)
Definition ex.h:772
std::map< ex, ex, ex_is_less > exmap
Definition basic.h:49
std::set< ex, ex_is_less > exset
Definition basic.h:48
B & dynallocate(Args &&... args)
Constructs a new (class basic or derived) B object on the heap.
Definition basic.h:333
class_info< registered_class_options > registered_class_info
Definition registrar.h:124
ex series(const ex &thisex, const ex &r, int order, unsigned options=0)
Definition ex.h:796
ex conjugate(const ex &thisex)
Definition ex.h:733
ex diff(const ex &thisex, const symbol &s, unsigned nth=1)
Definition ex.h:793
ex subs(const ex &thisex, const exmap &m, unsigned options=0)
Definition ex.h:846
const numeric smod(const numeric &a_, const numeric &b_)
Modulus (in symmetric representation).
Definition numeric.cpp:2345
ex eval(const ex &thisex)
Definition ex.h:781
int degree(const ex &thisex, const ex &s)
Definition ex.h:751
bool match(const ex &thisex, const ex &pattern, exmap &repl_lst)
Definition ex.h:799
int ldegree(const ex &thisex, const ex &s)
Definition ex.h:754
bool is_a(const basic &obj)
Check if obj is a T, including base classes.
Definition basic.h:312
ex evalf(const ex &thisex)
Definition ex.h:784
ex normal(const ex &thisex)
Definition ex.h:769
ex op(const ex &thisex, size_t i)
Definition ex.h:826
ex coeff(const ex &thisex, const ex &s, int n=1)
Definition ex.h:757
ex collect(const ex &thisex, const ex &s, bool distributed=false)
Definition ex.h:778
ex eval_integ(const ex &thisex)
Definition ex.h:790
ex evalm(const ex &thisex)
Definition ex.h:787
bool is_exactly_a(const basic &obj)
Check if obj is a T, not including base classes.
Definition basic.h:319
bool has(const ex &thisex, const ex &pattern, unsigned options=0)
Definition ex.h:742
std::vector< ex > exvector
Definition basic.h:47
size_t nops(const ex &thisex)
Definition ex.h:727
ex imag_part(const ex &thisex)
Definition ex.h:739
ex to_polynomial(const ex &thisex, exmap &repl)
Definition ex.h:775
ex expand(const ex &thisex, unsigned options=0)
Definition ex.h:730
Reference-counted pointer template.
GiNaC's class registrar (for class basic and all classes derived from it).
#define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername)
Primary macro for inclusion in the declaration of each registered class.
Definition registrar.h:139
Function object for map().
Definition basic.h:84
virtual ~map_function()
Definition basic.h:85
virtual ex operator()(const ex &e)=0
const ex & argument_type
Definition basic.h:86
To distinguish between different kinds of non-commutative objects.
Definition registrar.h:42

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