]> www.ginac.de Git - ginac.git/blob - ginac/symbol.h
- pseries::coeff() now uses a binary instead of a linear search algorithm
[ginac.git] / ginac / symbol.h
1 /** @file symbol.h
2  *
3  *  Interface to GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 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_SYMBOL_H__
24 #define __GINAC_SYMBOL_H__
25
26 #include <string>
27 #include "basic.h"
28 #include "ex.h"
29
30 #ifndef NO_NAMESPACE_GINAC
31 namespace GiNaC {
32 #endif // ndef NO_NAMESPACE_GINAC
33
34 /** Basic CAS symbol.  It has a name because it must know how to output itself.
35  *  It may be assigned an expression, but this feature is only intended for
36  *  programs like 'ginsh' that want to associate symbols with expressions.
37  *  If you want to replace symbols by expressions in your code, you should
38  *  use ex::subs() or use objects of class ex instead of class symbol in the
39  *  first place. */
40 class symbol : public basic
41 {
42     GINAC_DECLARE_REGISTERED_CLASS(symbol, basic)
43
44 // types
45
46     /** Symbols as keys to expressions. */
47     class assigned_ex_info {
48     public:
49         assigned_ex_info();     //!< Default ctor
50         bool is_assigned;       //!< True if there is an expression assigned
51         ex assigned_expression; //!< The actual expression
52         unsigned refcount;      //!< Yet another refcounter. PLEASE EXPLAIN!
53     };
54     
55 // member functions
56
57     // default constructor, destructor, copy constructor assignment operator and helpers
58 public:
59     symbol();
60     ~symbol();
61     symbol(const symbol & other);
62 protected:
63     void copy(const symbol & other); 
64     void destroy(bool call_parent);
65
66     // other constructors
67 public:
68     explicit symbol(const string & initname);
69
70     // functions overriding virtual functions from base classes
71 public:
72     basic * duplicate() const;
73     void print(ostream & os, unsigned upper_precedence=0) const;
74     void printraw(ostream & os) const;
75     void printtree(ostream & os, unsigned indent) const;
76     void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
77     bool info(unsigned inf) const;
78     ex expand(unsigned options=0) const;
79     bool has(const ex & other) const;
80     int degree(const symbol & s) const;
81     int ldegree(const symbol & s) const;
82     ex coeff(const symbol & s, int n = 1) const;
83     ex eval(int level = 0) const;
84     ex series(const symbol & s, const ex & point, int order) const;
85     ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
86     ex subs(const lst & ls, const lst & lr) const;
87 protected:
88     ex derivative(const symbol & s) const;
89     int compare_same_type(const basic & other) const;
90     bool is_equal_same_type(const basic & other) const;
91     unsigned return_type(void) const;
92     unsigned return_type_tinfo(void) const;
93     unsigned calchash(void) const;
94     
95     // non-virtual functions in this class
96 public:
97     void assign(const ex & value);
98     void unassign(void);
99     void setname(const string & n) {name=n;}
100     string getname(void) const {return name;}
101 private:
102     string & autoname_prefix(void);
103
104 // member variables
105
106 protected:
107     assigned_ex_info * asexinfop;
108     unsigned serial;  //!< unique serial number for comparision
109     string name;
110 private:
111     static unsigned next_serial;
112 };
113
114 // global constants
115
116 extern const symbol some_symbol;
117 extern const type_info & typeid_symbol;
118
119 // utility functions
120 inline const symbol &ex_to_symbol(const ex &e)
121 {
122         return static_cast<const symbol &>(*e.bp);
123 }
124
125 // wrapper functions around member functions
126 inline void unassign(symbol & symarg)
127 { return symarg.unassign(); }
128
129 inline int degree(const symbol & a, const symbol & s)
130 { return a.degree(s); }
131
132 inline int ldegree(const symbol & a, const symbol & s)
133 { return a.ldegree(s); }
134
135 #ifndef NO_NAMESPACE_GINAC
136 } // namespace GiNaC
137 #endif // ndef NO_NAMESPACE_GINAC
138
139 #endif // ndef __GINAC_SYMBOL_H__