3 * Interface to GiNaC's ABC. */
6 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef __GINAC_BASIC_H__
24 #define __GINAC_BASIC_H__
26 #include <cstddef> // for size_t
28 // CINT needs <algorithm> to work properly with <vector>
33 #include "assertion.h"
34 #include "registrar.h"
47 typedef std::vector<ex> exvector;
50 /** Function object for map(). */
52 typedef const ex & argument_type;
53 typedef ex result_type;
54 virtual ex operator()(const ex & e) = 0;
58 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
59 * It is responsible for the reference counting. */
62 GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
66 // default ctor, dtor, copy ctor, assignment operator and helpers
68 basic() : tinfo_key(TINFO_basic), flags(0), refcount(0) {}
69 /** basic dtor, virtual because class ex will delete objects via ptr. */
73 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
75 basic(const basic & other);
76 const basic & operator=(const basic & other);
78 /** For use by copy ctor and assignment operator. */
79 void copy(const basic & other)
81 flags = other.flags & ~status_flags::dynallocated;
82 hashvalue = other.hashvalue;
83 tinfo_key = other.tinfo_key;
85 /** For use by dtor and assignment operator. */
86 virtual void destroy(bool call_parent) { }
89 /** ctor with specified tinfo_key */
90 basic(unsigned ti) : tinfo_key(ti), flags(0), refcount(0) {}
92 // new virtual functions which can be overridden by derived classes
93 public: // only const functions please (may break reference counting)
94 virtual basic * duplicate() const;
95 virtual void print(const print_context & c, unsigned level = 0) const;
96 virtual void dbgprint(void) const;
97 virtual void dbgprinttree(void) const;
98 virtual unsigned precedence(void) const;
99 virtual bool info(unsigned inf) const;
100 virtual size_t nops() const;
101 virtual ex op(size_t i) const;
102 virtual ex operator[](const ex & index) const;
103 virtual ex operator[](size_t i) const;
104 virtual ex & let_op(size_t i);
105 virtual ex & operator[](const ex & index);
106 virtual ex & operator[](size_t i);
107 virtual ex expand(unsigned options = 0) const;
108 virtual bool has(const ex & other) const;
109 virtual ex map(map_function & f) const;
110 virtual int degree(const ex & s) const;
111 virtual int ldegree(const ex & s) const;
112 virtual ex coeff(const ex & s, int n = 1) const;
113 virtual ex collect(const ex & s, bool distributed = false) const;
114 virtual ex eval(int level = 0) const;
115 virtual ex evalf(int level = 0) const;
116 virtual ex evalm(void) const;
117 virtual ex series(const relational & r, int order, unsigned options = 0) const;
118 virtual bool match(const ex & pattern, lst & repl_lst) const;
119 virtual ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
120 virtual ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
121 virtual ex to_rational(lst &repl_lst) const;
122 virtual ex to_polynomial(lst &repl_lst) const;
123 virtual numeric integer_content(void) const;
124 virtual ex smod(const numeric &xi) const;
125 virtual numeric max_coefficient(void) const;
126 virtual exvector get_free_indices(void) const;
127 virtual ex eval_indexed(const basic & i) const;
128 virtual ex add_indexed(const ex & self, const ex & other) const;
129 virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
130 virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
131 virtual unsigned return_type(void) const;
132 virtual unsigned return_type_tinfo(void) const;
133 protected: // functions that should be called from class ex only
134 virtual ex derivative(const symbol & s) const;
135 virtual int compare_same_type(const basic & other) const;
136 virtual bool is_equal_same_type(const basic & other) const;
137 virtual bool match_same_type(const basic & other) const;
138 virtual unsigned calchash(void) const;
139 virtual ex eval_ncmul(const exvector & v) const;
141 // non-virtual functions in this class
143 ex subs(const ex & e, unsigned options = 0) const;
144 ex diff(const symbol & s, unsigned nth = 1) const;
145 int compare(const basic & other) const;
146 bool is_equal(const basic & other) const;
147 const basic & hold(void) const;
148 unsigned gethash(void) const { if (flags & status_flags::hash_calculated) return hashvalue; else return calchash(); }
149 unsigned tinfo(void) const {return tinfo_key;}
151 /** Set some status_flags. */
152 const basic & setflag(unsigned f) const {flags |= f; return *this;}
154 /** Clear some status_flags. */
155 const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
158 void ensure_if_modifiable(void) const;
162 unsigned tinfo_key; ///< typeinfo
163 mutable unsigned flags; ///< of type status_flags
164 mutable unsigned hashvalue; ///< hash value
166 unsigned refcount; ///< Number of reference counts
171 extern int max_recursion_level;
173 // convenience type checker template functions
175 /** Check if obj is a T, including base classes. */
177 inline bool is_a(const basic &obj)
179 return dynamic_cast<const T *>(&obj) != 0;
182 /** Check if obj is a T, not including base classes. This one is just an
183 * inefficient default. It should in all time-critical cases be overridden
184 * by template specializations that don't create a temporary. */
186 inline bool is_exactly_a(const class basic &obj)
188 return obj.tinfo() == T::reg_info.tinfo_key;
193 #endif // ndef __GINAC_BASIC_H__