]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
e5c3069d2a09f5cc40fe43b7a7a47a394d5acfec
[ginac.git] / ginac / basic.h
1 /** @file basic.h
2  *
3  *  Interface to GiNaC's ABC. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #ifndef __GINAC_BASIC_H__
24 #define __GINAC_BASIC_H__
25
26 #include <iostream>
27 #include <vector>
28
29 // CINT needs <algorithm> to work properly with <vector>
30 #include <algorithm>
31
32 #include "flags.h"
33 #include "tinfos.h"
34 #include "assertion.h"
35 #include "registrar.h"
36
37 namespace GiNaC {
38
39 class ex;
40 class symbol;
41 class lst;
42 class numeric;
43 class relational;
44 class archive_node;
45 class print_context;
46
47 typedef std::vector<ex> exvector;
48
49
50 /** Function object for map(). */
51 struct map_function {
52         typedef const ex & argument_type;
53         typedef ex result_type;
54         virtual ex operator()(const ex & e) = 0;
55 };
56
57
58 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
59  *  It is responsible for the reference counting. */
60 class basic
61 {
62         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
63         
64         friend class ex;
65         
66         // default ctor, dtor, copy ctor, assignment operator and helpers
67 public:
68         basic() : tinfo_key(TINFO_basic), flags(0), refcount(0) {}
69         /** basic dtor, virtual because class ex will delete objects via ptr. */
70         virtual ~basic()
71         {
72                 destroy(false);
73                 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
74         }
75         basic(const basic & other);
76         const basic & operator=(const basic & other);
77 protected:
78         /** For use by copy ctor and assignment operator. */
79         void copy(const basic & other)
80         {
81                 flags = other.flags & ~status_flags::dynallocated;
82                 hashvalue = other.hashvalue;
83                 tinfo_key = other.tinfo_key;
84         }
85         /** For use by dtor and assignment operator. */
86         virtual void destroy(bool call_parent) { }
87         
88         // other ctors
89         /** ctor with specified tinfo_key */
90         basic(unsigned ti) : tinfo_key(ti), flags(0), refcount(0) {}
91         
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 unsigned nops() const;
101         virtual ex op(int i) const;
102         virtual ex & let_op(int i);
103         virtual ex operator[](const ex & index) const;
104         virtual ex operator[](int i) const;
105         virtual ex expand(unsigned options = 0) const;
106         virtual bool has(const ex & other) const;
107         virtual ex map(map_function & f) const;
108         virtual int degree(const ex & s) const;
109         virtual int ldegree(const ex & s) const;
110         virtual ex coeff(const ex & s, int n = 1) const;
111         virtual ex collect(const ex & s, bool distributed = false) const;
112         virtual ex eval(int level = 0) const;
113         virtual ex evalf(int level = 0) const;
114         virtual ex evalm(void) const;
115         virtual ex series(const relational & r, int order, unsigned options = 0) const;
116         virtual bool match(const ex & pattern, lst & repl_lst) const;
117         virtual ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
118         virtual ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
119         virtual ex to_rational(lst &repl_lst) const;
120         virtual numeric integer_content(void) const;
121         virtual ex smod(const numeric &xi) const;
122         virtual numeric max_coefficient(void) const;
123         virtual exvector get_free_indices(void) const;
124         virtual ex eval_indexed(const basic & i) const;
125         virtual ex add_indexed(const ex & self, const ex & other) const;
126         virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
127         virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
128         virtual unsigned return_type(void) const;
129         virtual unsigned return_type_tinfo(void) const;
130 protected: // functions that should be called from class ex only
131         virtual ex derivative(const symbol & s) const;
132         virtual int compare_same_type(const basic & other) const;
133         virtual bool is_equal_same_type(const basic & other) const;
134         virtual bool match_same_type(const basic & other) const;
135         virtual unsigned calchash(void) const;
136         virtual ex simplify_ncmul(const exvector & v) const;
137         
138         // non-virtual functions in this class
139 public:
140         ex subs(const ex & e, bool no_pattern = false) const;
141         ex diff(const symbol & s, unsigned nth=1) const;
142         int compare(const basic & other) const;
143         bool is_equal(const basic & other) const;
144         const basic & hold(void) const;
145         unsigned gethash(void) const { if (flags & status_flags::hash_calculated) return hashvalue; else return calchash(); }
146         unsigned tinfo(void) const {return tinfo_key;}
147
148         /** Set some status_flags. */
149         const basic & setflag(unsigned f) const {flags |= f; return *this;}
150
151         /** Clear some status_flags. */
152         const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
153
154 protected:
155         void ensure_if_modifiable(void) const;
156         
157         // member variables
158 protected:
159         unsigned tinfo_key;                 ///< typeinfo
160         mutable unsigned flags;             ///< of type status_flags
161         mutable unsigned hashvalue;         ///< hash value
162 private:
163         unsigned refcount;                  ///< Number of reference counts
164 };
165
166 // global variables
167
168 extern int max_recursion_level;
169
170 // convenience type checker template functions
171
172 /** Check if obj is a T, including base classes. */
173 template <class T>
174 inline bool is_a(const basic &obj)
175 {
176         return dynamic_cast<const T *>(&obj)!=0;
177 }
178
179 /** Check if obj is a T, not including base classes.  This one is just an
180  *  inefficient default.  It should in all time-critical cases be overridden
181  *  by template specializations that don't create a temporary. */
182 template <class T>
183 inline bool is_exactly_a(const class basic &obj)
184 {
185         const T foo; return foo.tinfo()==obj.tinfo();
186 }
187
188 /** Check if ex is a handle to a T, including base classes. */
189 template <class T>
190 inline bool is_a(const ex &obj)
191 {
192         return is_a<T>(*obj.bp);
193 }
194
195 /** Check if ex is a handle to a T, not including base classes. */
196 template <class T>
197 inline bool is_exactly_a(const ex &obj)
198 {
199         return is_exactly_a<T>(*obj.bp);
200 }
201
202 /** Return a reference to the basic-derived class T object embedded in an
203  *  expression.  This is fast but unsafe: the result is undefined if the
204  *  expression does not contain a T object at its top level.  Hence, you
205  *  should generally check the type of e first.
206  *
207  *  @param e expression
208  *  @return reference to pseries object
209  *  @see is_exactly_a<class T>() */
210 template <class T>
211 inline const T &ex_to(const ex &e)
212 {
213         GINAC_ASSERT(is_a<T>(e));
214         return static_cast<const T &>(*e.bp);
215 }
216
217 } // namespace GiNaC
218
219 #endif // ndef __GINAC_BASIC_H__