]> www.ginac.de Git - ginac.git/blob - ginac/symbol.h
documentation update
[ginac.git] / ginac / symbol.h
1 /** @file symbol.h
2  *
3  *  Interface to GiNaC's symbolic objects. */
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_SYMBOL_H__
24 #define __GINAC_SYMBOL_H__
25
26 #include <string>
27 #include "basic.h"
28 #include "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         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(symbol, basic)
41         
42 // types
43         
44         /** Symbols as keys to expressions - this is deprecated. */
45         class assigned_ex_info {
46         public:
47                 assigned_ex_info();     ///< Default ctor
48                 bool is_assigned;       ///< True if there is an expression assigned
49                 ex assigned_expression; ///< The actual expression
50                 unsigned refcount;      ///< Reference counter
51         };
52         
53 // member functions
54         
55         // default ctor, dtor, copy ctor assignment operator and helpers
56 public:
57         symbol();
58         ~symbol()
59         {
60                 /*debugmsg("symbol dtor", LOGLEVEL_DESTRUCT);*/
61                 destroy(false);
62         }
63         symbol(const symbol & other);
64 protected:
65         void copy(const symbol & other); 
66         void destroy(bool call_parent);
67         
68         // other ctors
69 public:
70         explicit symbol(const std::string & initname);
71         
72         // functions overriding virtual functions from base classes
73 public:
74         basic * duplicate() const;
75         void print(std::ostream & os, unsigned upper_precedence = 0) const;
76         void printraw(std::ostream & os) const;
77         void printtree(std::ostream & os, unsigned indent) const;
78         void printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence = 0) const;
79         bool info(unsigned inf) const;
80         ex expand(unsigned options = 0) const;
81         bool has(const ex & other) const;
82         int degree(const symbol & s) const;
83         int ldegree(const symbol & s) const;
84         ex coeff(const symbol & s, int n = 1) const;
85         ex eval(int level = 0) const;
86         ex series(const relational & s, int order, unsigned options = 0) const;
87         ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
88         ex to_rational(lst &repl_lst) const;
89         ex subs(const lst & ls, const lst & lr) const;
90 protected:
91         ex derivative(const symbol & s) const;
92         int compare_same_type(const basic & other) const;
93         bool is_equal_same_type(const basic & other) const;
94         unsigned return_type(void) const;
95         unsigned return_type_tinfo(void) const;
96         unsigned calchash(void) const;
97         
98         // non-virtual functions in this class
99 public:
100         void assign(const ex & value);
101         void unassign(void);
102         void setname(const std::string & n) { name = n; }
103         std::string getname(void) const { return name; }
104 private:
105         std::string & autoname_prefix(void);
106
107 // member variables
108
109 protected:
110         assigned_ex_info * asexinfop;   ///< ptr to assigned expression, deprecated
111         unsigned serial;    ///< unique serial number for comparison
112         std::string name;   ///< printname of this symbol
113 private:
114         static unsigned next_serial;
115 };
116
117
118 // utility functions
119 inline const symbol &ex_to_symbol(const ex &e)
120 {
121         return static_cast<const symbol &>(*e.bp);
122 }
123
124
125 // wrapper functions around member functions
126 inline void unassign(symbol & symarg)
127 { 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 } // namespace GiNaC
136
137 #endif // ndef __GINAC_SYMBOL_H__