]> www.ginac.de Git - ginac.git/blob - ginac/symbol.h
- docs now under automake control
[ginac.git] / ginac / symbol.h
1 /** @file symbol.h
2  *
3  *  Interface to GiNaC's symbolic objects.
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #ifndef __GINAC_SYMBOL_H__
23 #define __GINAC_SYMBOL_H__
24
25 #include <string>
26 #include <ginac/basic.h>
27 #include <ginac/ex.h>
28
29 /** Basic CAS symbol.  It has a name because it must know how to output itself.
30  *  It may be assigned an expression, but this feature is only intended for
31  *  programs like 'ginsh' that want to associate symbols with expressions.
32  *  If you want to replace symbols by expressions in your code, you should
33  *  use ex::subs() or use objects of class ex instead of class symbol in the
34  *  first place. */
35 class symbol : public basic
36 {
37 // types
38     /** Symbols as keys to expressions. */
39     class assigned_ex_info {
40     public:
41         assigned_ex_info();     //!< Default ctor
42         bool is_assigned;       //!< True if there is an expression assigned
43         ex assigned_expression; //!< The actual expression
44         unsigned refcount;      //!< Yet another refcounter. PLEASE EXPLAIN!
45     };
46     
47 // member functions
48
49     // default constructor, destructor, copy constructor assignment operator and helpers
50 public:
51     symbol();
52     ~symbol();
53     symbol(symbol const & other);
54 protected:
55     void copy(symbol const & other); 
56     void destroy(bool call_parent);
57
58     // other constructors
59 public:
60     explicit symbol(string const & initname);
61
62     // functions overriding virtual functions from base classes
63 public:
64     basic * duplicate() const;
65     void printraw(ostream & os) const;
66     void printtree(ostream & os, unsigned indent) const;
67     void print(ostream & os, unsigned upper_precedence=0) const;
68     void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
69     bool info(unsigned inf) const;
70     ex expand(unsigned options=0) const;
71     bool has(ex const & other) const;
72     int degree(symbol const & s) const;
73     int ldegree(symbol const & s) const;
74     ex coeff(symbol const & s, int const n = 1) const;
75     ex eval(int level = 0) const;
76     ex diff(symbol const & s) const;
77     ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
78     ex subs(lst const & ls, lst const & lr) const;
79 protected:
80     int compare_same_type(basic const & other) const;
81     bool is_equal_same_type(basic const & other) const;
82     unsigned return_type(void) const;
83     unsigned return_type_tinfo(void) const;
84     unsigned calchash(void) const;
85     
86     // non-virtual functions in this class
87 public:
88     void assign(ex const & value);
89     void unassign(void);
90     ex diff(symbol const & s, unsigned nth) const;
91     void setname(string const & n) {name=n;}
92     string getname(void) const {return name;}
93 private:
94     string & autoname_prefix(void);
95
96 // member variables
97
98 protected:
99     assigned_ex_info * asexinfop;
100     unsigned serial;  //!< unique serial number for comparision
101     string name;
102 private:
103     static unsigned next_serial;
104 };
105
106 // global constants
107
108 extern const symbol some_symbol;
109 extern type_info const & typeid_symbol;
110
111 // macros
112
113 #define ex_to_symbol(X) static_cast<symbol const &>(*(X).bp)
114
115 // wrapper functions around member functions
116 inline void unassign(symbol & symarg)
117 { return symarg.unassign(); }
118
119 inline int degree(symbol const & a, symbol const & s)
120 { return a.degree(s); }
121
122 inline int ldegree(symbol const & a, symbol const & s)
123 { return a.ldegree(s); }
124
125 #endif // ndef __GINAC_SYMBOL_H__