GiNaC 1.8.10
structure.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_STRUCTURE_H
23#define GINAC_STRUCTURE_H
24
25#include "ex.h"
26#include "ncmul.h"
27#include "numeric.h"
28#include "operators.h"
29#include "print.h"
30
31#include <functional>
32
33namespace GiNaC {
34
36template <class T>
38protected:
39 static bool struct_is_equal(const T * t1, const T * t2) { return true; }
40 static int struct_compare(const T * t1, const T * t2) { return 0; }
41
42 // disallow destruction of structure through a compare_all_equal*
43protected:
45};
46
47
50template <class T>
52protected:
53 static bool struct_is_equal(const T * t1, const T * t2)
54 {
55 return std::equal_to<T>()(*t1, *t2);
56 }
57
58 static int struct_compare(const T * t1, const T * t2)
59 {
60 if (std::less<T>()(*t1, *t2))
61 return -1;
62 else if (std::less<T>()(*t2, *t1))
63 return 1;
64 else
65 return 0;
66 }
67
68 // disallow destruction of structure through a compare_std_less*
69protected:
71};
72
73
75template <class T>
77protected:
78 static bool struct_is_equal(const T * t1, const T * t2)
79 {
80 const char * cp1 = reinterpret_cast<const char *>(t1);
81 const char * cp2 = reinterpret_cast<const char *>(t2);
82
83 return std::equal(cp1, cp1 + sizeof(T), cp2);
84 }
85
86 static int struct_compare(const T * t1, const T * t2)
87 {
88 const unsigned char * cp1 = reinterpret_cast<const unsigned char *>(t1);
89 const unsigned char * cp2 = reinterpret_cast<const unsigned char *>(t2);
90 typedef std::pair<const unsigned char *, const unsigned char *> cppair;
91
92 cppair res = std::mismatch(cp1, cp1 + sizeof(T), cp2);
93
94 if (res.first == cp1 + sizeof(T))
95 return 0;
96 else if (*res.first < *res.second)
97 return -1;
98 else
99 return 1;
100 }
101
102 // disallow destruction of structure through a compare_bitwise*
103protected:
105};
106
107
108// Select default comparison policy
109template <class T, template <class> class ComparisonPolicy = compare_all_equal> class structure;
110
111
113template <class T, template <class> class ComparisonPolicy>
114class structure : public basic, public ComparisonPolicy<T> {
116
117 // helpers
118 static const char *get_class_name() { return "structure"; }
119 // constructors
120public:
122 structure(const T & t) : obj(t) { }
123
124 // functions overriding virtual functions from base classes
125 // All these are just defaults that can be specialized by the user
126public:
127 // evaluation
128 ex eval() const override { return hold(); }
129 ex evalm() const override { return inherited::evalm(); }
130protected:
131 ex eval_ncmul(const exvector & v) const override { return hold_ncmul(v); }
132public:
133 ex eval_indexed(const basic & i) const override { return i.hold(); }
134
135 // printing
136 void print(const print_context & c, unsigned level = 0) const override { inherited::print(c, level); }
137 unsigned precedence() const override { return 70; }
138
139 // info
140 bool info(unsigned inf) const override { return false; }
141
142 // operand access
143 size_t nops() const override { return 0; }
144 ex op(size_t i) const override { return inherited::op(i); }
145 ex operator[](const ex & index) const override { return inherited::operator[](index); }
146 ex operator[](size_t i) const override { return inherited::operator[](i); }
147 ex & let_op(size_t i) override { return inherited::let_op(i); }
148 ex & operator[](const ex & index) override { return inherited::operator[](index); }
149 ex & operator[](size_t i) override { return inherited::operator[](i); }
150
151 // pattern matching
152 bool has(const ex & other, unsigned options = 0) const override { return inherited::has(other, options); }
153 bool match(const ex & pattern, exmap& repl_lst) const override { return inherited::match(pattern, repl_lst); }
154protected:
155 bool match_same_type(const basic & other) const override { return true; }
156public:
157
158 // substitutions
159 ex subs(const exmap & m, unsigned options = 0) const override { return inherited::subs(m, options); }
160
161 // function mapping
162 ex map(map_function & f) const override { return inherited::map(f); }
163
164 // degree/coeff
165 int degree(const ex & s) const override { return inherited::degree(s); }
166 int ldegree(const ex & s) const override { return inherited::ldegree(s); }
167 ex coeff(const ex & s, int n = 1) const override { return inherited::coeff(s, n); }
168
169 // expand/collect
170 ex expand(unsigned options = 0) const override { return inherited::expand(options); }
171 ex collect(const ex & s, bool distributed = false) const override { return inherited::collect(s, distributed); }
172
173 // differentiation and series expansion
174protected:
175 ex derivative(const symbol & s) const override { return inherited::derivative(s); }
176public:
177 ex series(const relational & r, int order, unsigned options = 0) const override { return inherited::series(r, order, options); }
178
179 // rational functions
180 ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const override { return inherited::normal(repl, rev_lookup, modifier); }
181 ex to_rational(exmap & repl) const override { return inherited::to_rational(repl); }
182 ex to_polynomial(exmap & repl) const override { return inherited::to_polynomial(repl); }
183
184 // polynomial algorithms
185 numeric integer_content() const override { return 1; }
186 ex smod(const numeric & xi) const override { return *this; }
187 numeric max_coefficient() const override { return 1; }
188
189 // indexed objects
190 exvector get_free_indices() const override { return exvector(); }
191 ex add_indexed(const ex & self, const ex & other) const override { return self + other; }
192 ex scalar_mul_indexed(const ex & self, const numeric & other) const override { return self * ex(other); }
193 bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const override { return false; }
194
195 // noncommutativity
196 unsigned return_type() const override { return return_types::commutative; }
198 {
200 r.rl = 0;
201 r.tinfo = &typeid(*this);
202 return r;
203 }
204
205protected:
206 bool is_equal_same_type(const basic & other) const override
207 {
208 GINAC_ASSERT(is_a<structure>(other));
209 const structure & o = static_cast<const structure &>(other);
210
211 return this->struct_is_equal(&obj, &o.obj);
212 }
213
214 unsigned calchash() const override { return inherited::calchash(); }
215
216 // non-virtual functions in this class
217public:
218 // access to embedded structure
219 const T *operator->() const { return &obj; }
220 T &get_struct() { return obj; }
221 const T &get_struct() const { return obj; }
222private:
224};
225
226
228template <class T, template <class> class CP>
230
232template <class T, template <class> class CP>
233int structure<T, CP>::compare_same_type(const basic & other) const
234{
235 GINAC_ASSERT(is_a<structure>(other));
236 const structure & o = static_cast<const structure &>(other);
237
238 return this->struct_compare(&obj, &o.obj);
239}
240
241template <class T, template <class> class CP>
242registered_class_info structure<T, CP>::reg_info = registered_class_info(registered_class_options(structure::get_class_name(), "basic", typeid(structure<T, CP>)));
243
244} // namespace GiNaC
245
246#endif // ndef GINAC_STRUCTURE_H
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition assertion.h:32
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition basic.h:104
friend class ex
Definition basic.h:107
const basic & hold() const
Stop further evaluation.
Definition basic.cpp:886
virtual int compare_same_type(const basic &other) const
Returns order relation between two objects of same type.
Definition basic.cpp:718
Comparison policy: all structures of one type are equal.
Definition structure.h:37
static bool struct_is_equal(const T *t1, const T *t2)
Definition structure.h:39
static int struct_compare(const T *t1, const T *t2)
Definition structure.h:40
Comparison policy: use bit-wise comparison to compare structures.
Definition structure.h:76
static bool struct_is_equal(const T *t1, const T *t2)
Definition structure.h:78
static int struct_compare(const T *t1, const T *t2)
Definition structure.h:86
Comparison policy: use std::equal_to/std::less (defaults to operators == and <) to compare structures...
Definition structure.h:51
static bool struct_is_equal(const T *t1, const T *t2)
Definition structure.h:53
static int struct_compare(const T *t1, const T *t2)
Definition structure.h:58
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
This class holds a relation consisting of two expressions and a logical relation between them.
Definition relational.h:34
Wrapper template for making GiNaC classes out of C++ structures.
Definition structure.h:114
size_t nops() const override
Number of operands/members.
Definition structure.h:143
bool info(unsigned inf) const override
Information about the object.
Definition structure.h:140
const T & get_struct() const
Definition structure.h:221
int ldegree(const ex &s) const override
Return degree of lowest power in object s.
Definition structure.h:166
exvector get_free_indices() const override
Return a vector containing the free indices of an expression.
Definition structure.h:190
numeric integer_content() const override
Definition structure.h:185
ex collect(const ex &s, bool distributed=false) const override
Sort expanded expression in terms of powers of some object(s).
Definition structure.h:171
ex eval_indexed(const basic &i) const override
Perform automatic symbolic evaluations on indexed expression that contains this object as the base ex...
Definition structure.h:133
numeric max_coefficient() const override
Implementation ex::max_coefficient().
Definition structure.h:187
ex to_rational(exmap &repl) const override
Default implementation of ex::to_rational().
Definition structure.h:181
ex derivative(const symbol &s) const override
Default implementation of ex::diff().
Definition structure.h:175
ex operator[](size_t i) const override
Definition structure.h:146
ex evalm() const override
Evaluate sums, products and integer powers of matrices.
Definition structure.h:129
static const char * get_class_name()
Definition structure.h:118
ex to_polynomial(exmap &repl) const override
Definition structure.h:182
bool match(const ex &pattern, exmap &repl_lst) const override
Check whether the expression matches a given pattern.
Definition structure.h:153
ex series(const relational &r, int order, unsigned options=0) const override
Default implementation of ex::series().
Definition structure.h:177
ex eval_ncmul(const exvector &v) const override
Definition structure.h:131
unsigned calchash() const override
Compute the hash value of an object and if it makes sense to store it in the objects status_flags,...
Definition structure.h:214
ex expand(unsigned options=0) const override
Expand expression, i.e.
Definition structure.h:170
ex & operator[](size_t i) override
Definition structure.h:149
ex add_indexed(const ex &self, const ex &other) const override
Add two indexed expressions.
Definition structure.h:191
structure(const T &t)
Construct structure as a copy of a given C++ structure.
Definition structure.h:122
bool match_same_type(const basic &other) const override
Returns true if the attributes of two objects are similar enough for a match.
Definition structure.h:155
unsigned return_type() const override
Definition structure.h:196
unsigned precedence() const override
Return relative operator precedence (for parenthezing output).
Definition structure.h:137
ex smod(const numeric &xi) const override
Apply symmetric modular homomorphism to an expanded multivariate polynomial.
Definition structure.h:186
return_type_t return_type_tinfo() const override
Definition structure.h:197
int degree(const ex &s) const override
Return degree of highest power in object s.
Definition structure.h:165
bool contract_with(exvector::iterator self, exvector::iterator other, exvector &v) const override
Try to contract two indexed expressions that appear in the same product.
Definition structure.h:193
ex operator[](const ex &index) const override
Definition structure.h:145
ex & let_op(size_t i) override
Return modifiable operand/member at position i.
Definition structure.h:147
ex scalar_mul_indexed(const ex &self, const numeric &other) const override
Multiply an indexed expression with a scalar.
Definition structure.h:192
ex normal(exmap &repl, exmap &rev_lookup, lst &modifier) const override
Default implementation of ex::normal().
Definition structure.h:180
void print(const print_context &c, unsigned level=0) const override
Output to stream.
Definition structure.h:136
bool is_equal_same_type(const basic &other) const override
Returns true if two objects of same type are equal.
Definition structure.h:206
ex eval() const override
Perform automatic non-interruptive term rewriting rules.
Definition structure.h:128
ex op(size_t i) const override
Return operand/member at position i.
Definition structure.h:144
ex coeff(const ex &s, int n=1) const override
Return coefficient of degree n in object s.
Definition structure.h:167
const T * operator->() const
Definition structure.h:219
ex subs(const exmap &m, unsigned options=0) const override
Substitute a set of objects by arbitrary expressions.
Definition structure.h:159
bool has(const ex &other, unsigned options=0) const override
Test for occurrence of a pattern.
Definition structure.h:152
ex map(map_function &f) const override
Construct new expression by applying the specified function to all sub-expressions (one level only,...
Definition structure.h:162
ex & operator[](const ex &index) override
Definition structure.h:148
Basic CAS symbol.
Definition symbol.h:38
Interface to GiNaC's light-weight expression handles.
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
mvec m
Definition factor.cpp:757
Definition add.cpp:35
ex hold_ncmul(const exvector &v)
Definition ncmul.cpp:612
std::map< ex, ex, ex_is_less > exmap
Definition basic.h:49
class_info< registered_class_options > registered_class_info
Definition registrar.h:124
std::vector< ex > exvector
Definition basic.h:47
Interface to GiNaC's non-commutative products of expressions.
Makes the interface to the underlying bignum package available.
Interface to GiNaC's overloaded operators.
Definition of helper classes for expression output.
#define GINAC_DECLARE_REGISTERED_CLASS(classname, supername)
Macro for inclusion in the declaration of each registered class.
Definition registrar.h:151
Function object for map().
Definition basic.h:84
To distinguish between different kinds of non-commutative objects.
Definition registrar.h:42
unsigned rl
to distinguish between non-commutative objects of the same type.
Definition registrar.h:47

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