GiNaC 1.8.10
basic.h
Go to the documentation of this file.
1
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}
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 holds archived versions of GiNaC expressions (class ex).
Definition archive.h:254
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
virtual size_t nops() const
Number of operands/members.
Definition basic.cpp:228
unsigned gethash() const
Definition basic.h:271
virtual ex eval_integ() const
Evaluate integrals, if result is known.
Definition basic.cpp:454
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 bool match(const ex &pattern, exmap &repls) const
Check whether the expression matches a given pattern.
Definition basic.cpp:518
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 ex imag_part() const
Definition basic.cpp:680
virtual ex eval() const
Perform automatic non-interruptive term rewriting rules.
Definition basic.cpp:412
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
ex diff(const symbol &s, unsigned nth=1) const
Default interface of nth derivative ex::diff(s, n).
Definition basic.cpp:645
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
virtual bool has(const ex &other, unsigned options=0) const
Test for occurrence of a pattern.
Definition basic.cpp:279
virtual ex evalm() const
Evaluate sums, products and integer powers of matrices.
Definition basic.cpp:440
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 int degree(const ex &s) const
Return degree of highest power in object s.
Definition basic.cpp:324
virtual ex conjugate() const
Definition basic.cpp:670
virtual unsigned precedence() const
Return relative operator precedence (for parenthezing output).
Definition basic.cpp:213
virtual ex op(size_t i) const
Return operand/member at position i.
Definition basic.cpp:237
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 ex series(const relational &r, int order, unsigned options=0) const
Default implementation of ex::series().
Definition pseries.cpp:610
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 ex collect(const ex &s, bool distributed=false) const
Sort expanded expression in terms of powers of some object(s).
Definition basic.cpp:347
virtual ex to_polynomial(exmap &repl) const
Definition normal.cpp:2643
virtual ex subs(const exmap &m, unsigned options=0) const
Substitute a set of objects by arbitrary expressions.
Definition basic.cpp:606
virtual ex real_part() const
Definition basic.cpp:675
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
virtual ex to_rational(exmap &repl) const
Default implementation of ex::to_rational().
Definition normal.cpp:2638
void do_print_tree(const print_tree &c, unsigned level) const
Tree output to stream.
Definition basic.cpp:174
virtual ex coeff(const ex &s, int n=1) const
Return coefficient of degree n in object s.
Definition basic.cpp:336
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
virtual int ldegree(const ex &s) const
Return degree of lowest power in object s.
Definition basic.cpp:330
virtual ex smod(const numeric &xi) const
Apply symmetric modular homomorphism to an expanded multivariate polynomial.
Definition normal.cpp:1202
virtual ex expand(unsigned options=0) const
Expand expression, i.e.
Definition basic.cpp:795
virtual ex normal(exmap &repl, exmap &rev_lookup, lst &modifier) const
Default implementation of ex::normal().
Definition normal.cpp:2217
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 evalf() const
Evaluate object numerically.
Definition basic.cpp:424
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 bool is_polynomial(const ex &var) const
Check whether this is a polynomial in the given variables.
Definition basic.cpp:318
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
Wrapper template for making GiNaC classes out of STL containers.
Definition container.h:72
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
Base class for reference-counted objects.
Definition ptr.h:34
unsigned int get_refcount() const noexcept
Definition ptr.h:40
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
unsigned options
Definition factor.cpp:2473
size_t n
Definition factor.cpp:1431
size_t c
Definition factor.cpp:756
size_t r
Definition factor.cpp:756
exset syms
Definition factor.cpp:2427
mvec m
Definition factor.cpp:757
Collection of all flags used through the GiNaC framework.
Definition add.cpp:35
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
bool is_a(const basic &obj)
Check if obj is a T, including base classes.
Definition basic.h:312
bool is_exactly_a(const basic &obj)
Check if obj is a T, not including base classes.
Definition basic.h:319
std::vector< ex > exvector
Definition basic.h:47
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.