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

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