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