]> www.ginac.de Git - ginac.git/blob - ginac/symbol.h
7ed972ac17de39776df3122d4002d83c74c31ce0
[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 <ginac/basic.h>
28 #include <ginac/ex.h>
29
30 #ifndef NO_GINAC_NAMESPACE
31 namespace GiNaC {
32 #endif // ndef NO_GINAC_NAMESPACE
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 // types
43     /** Symbols as keys to expressions. */
44     class assigned_ex_info {
45     public:
46         assigned_ex_info();     //!< Default ctor
47         bool is_assigned;       //!< True if there is an expression assigned
48         ex assigned_expression; //!< The actual expression
49         unsigned refcount;      //!< Yet another refcounter. PLEASE EXPLAIN!
50     };
51     
52 // member functions
53
54     // default constructor, destructor, copy constructor assignment operator and helpers
55 public:
56     symbol();
57     ~symbol();
58     symbol(symbol const & other);
59 protected:
60     void copy(symbol const & other); 
61     void destroy(bool call_parent);
62
63     // other constructors
64 public:
65     explicit symbol(string const & initname);
66
67     // functions overriding virtual functions from base classes
68 public:
69     basic * duplicate() const;
70     void print(ostream & os, unsigned upper_precedence=0) const;
71     void printraw(ostream & os) const;
72     void printtree(ostream & os, unsigned indent) const;
73     void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
74     bool info(unsigned inf) const;
75     ex expand(unsigned options=0) const;
76     bool has(ex const & other) const;
77     int degree(symbol const & s) const;
78     int ldegree(symbol const & s) const;
79     ex coeff(symbol const & s, int const n = 1) const;
80     ex eval(int level = 0) const;
81     ex diff(symbol const & s) const;
82     ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
83     ex subs(lst const & ls, lst const & lr) const;
84 protected:
85     int compare_same_type(basic const & other) const;
86     bool is_equal_same_type(basic const & other) const;
87     unsigned return_type(void) const;
88     unsigned return_type_tinfo(void) const;
89     unsigned calchash(void) const;
90     
91     // non-virtual functions in this class
92 public:
93     void assign(ex const & value);
94     void unassign(void);
95     ex diff(symbol const & s, unsigned nth) const;
96     void setname(string const & n) {name=n;}
97     string getname(void) const {return name;}
98 private:
99     string & autoname_prefix(void);
100
101 // member variables
102
103 protected:
104     assigned_ex_info * asexinfop;
105     unsigned serial;  //!< unique serial number for comparision
106     string name;
107 private:
108     static unsigned next_serial;
109 };
110
111 // global constants
112
113 extern const symbol some_symbol;
114 extern type_info const & typeid_symbol;
115
116 // utility functions
117 inline const symbol &ex_to_symbol(const ex &e)
118 {
119         return static_cast<const symbol &>(*e.bp);
120 }
121
122 // wrapper functions around member functions
123 inline void unassign(symbol & symarg)
124 { return symarg.unassign(); }
125
126 inline int degree(symbol const & a, symbol const & s)
127 { return a.degree(s); }
128
129 inline int ldegree(symbol const & a, symbol const & s)
130 { return a.ldegree(s); }
131
132 #ifndef NO_GINAC_NAMESPACE
133 } // namespace GiNaC
134 #endif // ndef NO_GINAC_NAMESPACE
135
136 #endif // ndef __GINAC_SYMBOL_H__