]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
- matrix::pow(): omit last big multiplication if it's not needed.
[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 /*#include "debugmsg.h"*/
37
38 namespace GiNaC {
39
40 class ex;
41 class symbol;
42 class lst;
43 class numeric;
44 class relational;
45 class archive_node;
46 class print_context;
47
48 typedef std::vector<ex> exvector;
49
50 typedef ex (*map_func)(const ex & e);
51
52 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
53  *  It is responsible for the reference counting. */
54 class basic
55 {
56         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
57         
58         friend class ex;
59         
60         // default ctor, dtor, copy ctor assignment operator and helpers
61 public:
62         basic() : tinfo_key(TINFO_basic), flags(0), refcount(0)
63         {
64                 /* debugmsg("basic default ctor", LOGLEVEL_CONSTRUCT); */
65         }
66         /** basic dtor, virtual because class ex will delete objects via ptr. */
67         virtual ~basic()
68         {
69                 /* debugmsg("basic dtor", LOGLEVEL_DESTRUCT); */
70                 destroy(false);
71                 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
72         }
73         basic(const basic & other);
74         const basic & operator=(const basic & other);
75 protected:
76         /** For use by copy ctor and assignment operator. */
77         void copy(const basic & other)
78         {
79                 flags = other.flags & ~status_flags::dynallocated;
80                 hashvalue = other.hashvalue;
81                 tinfo_key = other.tinfo_key;
82         }
83         /** For use by dtor and assignment operator. */
84         virtual void destroy(bool call_parent) { }
85         
86         // other ctors
87         /** ctor with specified tinfo_key */
88         basic(unsigned ti) : tinfo_key(ti), flags(0), refcount(0)
89         {
90                 /* debugmsg("basic ctor with tinfo_key", LOGLEVEL_CONSTRUCT); */
91         }
92         // functions overriding virtual functions from bases classes
93         // none
94         
95         // new virtual functions which can be overridden by derived classes
96 public: // only const functions please (may break reference counting)
97         virtual basic * duplicate() const;
98         virtual void print(const print_context & c, unsigned level = 0) const;
99         virtual void dbgprint(void) const;
100         virtual void dbgprinttree(void) const;
101         virtual unsigned precedence(void) const;
102         virtual bool info(unsigned inf) const;
103         virtual unsigned nops() const;
104         virtual ex op(int i) const;
105         virtual ex & let_op(int i);
106         virtual ex operator[](const ex & index) const;
107         virtual ex operator[](int i) const;
108         virtual bool has(const ex & other) const;
109         virtual ex map(map_func 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, bool no_pattern = false) 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 numeric integer_content(void) const;
123         virtual ex smod(const numeric &xi) const;
124         virtual numeric max_coefficient(void) const;
125         virtual exvector get_free_indices(void) const;
126         virtual ex simplify_ncmul(const exvector & v) 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 protected: // non-const functions should be called from class ex only
132         virtual ex derivative(const symbol & s) const;
133         virtual int compare_same_type(const basic & other) const;
134         virtual bool is_equal_same_type(const basic & other) const;
135         virtual unsigned return_type(void) const;
136         virtual unsigned return_type_tinfo(void) const;
137         virtual unsigned calchash(void) const;
138         virtual ex expand(unsigned options = 0) const;
139         
140         // non-virtual functions in this class
141 public:
142         ex subs(const ex & e, bool no_pattern = false) const;
143         ex diff(const symbol & s, unsigned nth=1) const;
144         int compare(const basic & other) const;
145         bool is_equal(const basic & other) const;
146         const basic & hold(void) const;
147         unsigned gethash(void) const { if (flags & status_flags::hash_calculated) return hashvalue; else return calchash(); }
148         unsigned tinfo(void) const {return tinfo_key;}
149
150         /** Set some status_flags. */
151         const basic & setflag(unsigned f) const {flags |= f; return *this;}
152
153         /** Clear some status_flags. */
154         const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
155
156 protected:
157         void ensure_if_modifiable(void) const;
158         
159         // member variables
160 protected:
161         unsigned tinfo_key;                 ///< typeinfo
162         mutable unsigned flags;             ///< of type status_flags
163         mutable unsigned hashvalue;         ///< hash value
164 private:
165         unsigned refcount;                  ///< Number of reference counts
166 };
167
168 // global variables
169
170 extern int max_recursion_level;
171
172 // convenience macros
173
174 /** Check if OBJ is a TYPE, including base classes. */
175 #define is_of_type(OBJ,TYPE) \
176         (dynamic_cast<const TYPE *>(&OBJ)!=0)
177
178 /** Check if OBJ is a TYPE, not including base classes. */
179 #define is_exactly_of_type(OBJ,TYPE) \
180         ((OBJ).tinfo()==GiNaC::TINFO_##TYPE)
181
182 /** Check if ex is a handle to a TYPE, including base classes. */
183 #define is_ex_of_type(OBJ,TYPE) \
184         (dynamic_cast<const TYPE *>((OBJ).bp)!=0)
185
186 /** Check if ex is a handle to a TYPE, not including base classes. */
187 #define is_ex_exactly_of_type(OBJ,TYPE) \
188         ((*(OBJ).bp).tinfo()==GiNaC::TINFO_##TYPE)
189
190 } // namespace GiNaC
191
192 #endif // ndef __GINAC_BASIC_H__