3 * Interface to GiNaC's ABC. */
6 * GiNaC Copyright (C) 1999-2001 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__
29 // CINT needs <algorithm> to work properly with <vector>
34 #include "assertion.h"
35 #include "registrar.h"
36 /*#include "debugmsg.h"*/
48 typedef std::vector<ex> exvector;
51 /** Function object for map(). */
53 typedef const ex & argument_type;
54 typedef ex result_type;
55 virtual ex operator()(const ex & e) = 0;
59 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
60 * It is responsible for the reference counting. */
63 GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
67 // default ctor, dtor, copy ctor assignment operator and helpers
69 basic() : tinfo_key(TINFO_basic), flags(0), refcount(0)
71 /* debugmsg("basic default ctor", LOGLEVEL_CONSTRUCT); */
73 /** basic dtor, virtual because class ex will delete objects via ptr. */
76 /* debugmsg("basic dtor", LOGLEVEL_DESTRUCT); */
78 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
80 basic(const basic & other);
81 const basic & operator=(const basic & other);
83 /** For use by copy ctor and assignment operator. */
84 void copy(const basic & other)
86 flags = other.flags & ~status_flags::dynallocated;
87 hashvalue = other.hashvalue;
88 tinfo_key = other.tinfo_key;
90 /** For use by dtor and assignment operator. */
91 virtual void destroy(bool call_parent) { }
94 /** ctor with specified tinfo_key */
95 basic(unsigned ti) : tinfo_key(ti), flags(0), refcount(0)
97 /* debugmsg("basic ctor with tinfo_key", LOGLEVEL_CONSTRUCT); */
100 // new virtual functions which can be overridden by derived classes
101 public: // only const functions please (may break reference counting)
102 virtual basic * duplicate() const;
103 virtual void print(const print_context & c, unsigned level = 0) const;
104 virtual void dbgprint(void) const;
105 virtual void dbgprinttree(void) const;
106 virtual unsigned precedence(void) const;
107 virtual bool info(unsigned inf) const;
108 virtual unsigned nops() const;
109 virtual ex op(int i) const;
110 virtual ex & let_op(int i);
111 virtual ex operator[](const ex & index) const;
112 virtual ex operator[](int i) const;
113 virtual ex expand(unsigned options = 0) const;
114 virtual bool has(const ex & other) const;
115 virtual ex map(map_function & f) const;
116 virtual int degree(const ex & s) const;
117 virtual int ldegree(const ex & s) const;
118 virtual ex coeff(const ex & s, int n = 1) const;
119 virtual ex collect(const ex & s, bool distributed = false) const;
120 virtual ex eval(int level = 0) const;
121 virtual ex evalf(int level = 0) const;
122 virtual ex evalm(void) const;
123 virtual ex series(const relational & r, int order, unsigned options = 0) const;
124 virtual bool match(const ex & pattern, lst & repl_lst) const;
125 virtual ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
126 virtual ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
127 virtual ex to_rational(lst &repl_lst) const;
128 virtual numeric integer_content(void) const;
129 virtual ex smod(const numeric &xi) const;
130 virtual numeric max_coefficient(void) const;
131 virtual exvector get_free_indices(void) const;
132 virtual ex eval_indexed(const basic & i) const;
133 virtual ex add_indexed(const ex & self, const ex & other) const;
134 virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
135 virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
136 virtual unsigned return_type(void) const;
137 virtual unsigned return_type_tinfo(void) const;
138 protected: // functions that should be called from class ex only
139 virtual ex derivative(const symbol & s) const;
140 virtual int compare_same_type(const basic & other) const;
141 virtual bool is_equal_same_type(const basic & other) const;
142 virtual bool match_same_type(const basic & other) const;
143 virtual unsigned calchash(void) const;
144 virtual ex simplify_ncmul(const exvector & v) const;
146 // non-virtual functions in this class
148 ex subs(const ex & e, bool no_pattern = false) const;
149 ex diff(const symbol & s, unsigned nth=1) const;
150 int compare(const basic & other) const;
151 bool is_equal(const basic & other) const;
152 const basic & hold(void) const;
153 unsigned gethash(void) const { if (flags & status_flags::hash_calculated) return hashvalue; else return calchash(); }
154 unsigned tinfo(void) const {return tinfo_key;}
156 /** Set some status_flags. */
157 const basic & setflag(unsigned f) const {flags |= f; return *this;}
159 /** Clear some status_flags. */
160 const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
163 void ensure_if_modifiable(void) const;
167 unsigned tinfo_key; ///< typeinfo
168 mutable unsigned flags; ///< of type status_flags
169 mutable unsigned hashvalue; ///< hash value
171 unsigned refcount; ///< Number of reference counts
176 extern int max_recursion_level;
178 // convenience type checker template functions
180 /** Check if obj is a T, including base classes. */
182 inline bool is_a(const basic & obj)
184 return dynamic_cast<const T *>(&obj)!=0;
187 /** Check if obj is a T, not including base classes. This one is just an
188 * inefficient default. It should in all time-critical cases be overridden
189 * by template specializations that don't create a temporary. */
191 inline bool is_exactly_a(const class basic & obj)
193 const T foo; return foo.tinfo()==obj.tinfo();
196 /** Check if ex is a handle to a T, including base classes. */
198 inline bool is_a(const ex & obj)
200 return is_a<T>(*obj.bp);
203 /** Check if ex is a handle to a T, not including base classes. */
205 inline bool is_exactly_a(const ex & obj)
207 return is_exactly_a<T>(*obj.bp);
210 /** Return a reference to the basic-derived class T object embedded in an
211 * expression. This is fast but unsafe: the result is undefined if the
212 * expression does not contain a T object at its top level.
214 * @param e expression
215 * @return reference to pseries object
216 * @see is_exactly_a<class T>() */
218 inline const T &ex_to(const ex &e)
220 GINAC_ASSERT(is_a<T>(e));
221 return static_cast<const T &>(*e.bp);
226 #endif // ndef __GINAC_BASIC_H__