]> www.ginac.de Git - ginac.git/blob - ginac/symbol.h
synced to 1.1
[ginac.git] / ginac / symbol.h
1 /** @file symbol.h
2  *
3  *  Interface to GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 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 #include "ptr.h"
30
31 namespace GiNaC {
32
33 /** Basic CAS symbol.  It has a name because it must know how to output itself.
34  *  It may be assigned an expression, but this feature is only intended for
35  *  programs like 'ginsh' that want to associate symbols with expressions.
36  *  If you want to replace symbols by expressions in your code, you should
37  *  use ex::subs() or use objects of class ex instead of class symbol in the
38  *  first place. */
39 class symbol : public basic
40 {
41         GINAC_DECLARE_REGISTERED_CLASS(symbol, basic)
42
43 // types
44         
45         /** Symbols as keys to expressions - only for ginsh. */
46         class assigned_ex_info {
47                 friend class ptr<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         private:
53                 size_t refcount;        ///< Reference counter, managed by ptr<assigned_ex_info>
54         };
55
56 // member functions
57         
58         // other constructors
59 public:
60         explicit symbol(const std::string & initname);
61         explicit symbol(const std::string & initname, const std::string & texname);
62         
63         // functions overriding virtual functions from base classes
64 public:
65         bool info(unsigned inf) const;
66         ex eval(int level = 0) const;
67         ex evalf(int level = 0) const { return *this; } // overwrites basic::evalf() for performance reasons
68         ex series(const relational & s, int order, unsigned options = 0) const;
69         ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
70         ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
71         ex to_rational(lst &repl_lst) const;
72         ex to_polynomial(lst &repl_lst) const;
73 protected:
74         ex derivative(const symbol & s) const;
75         bool is_equal_same_type(const basic & other) const;
76         unsigned calchash() const;
77         
78         // non-virtual functions in this class
79 public:
80         void assign(const ex & value);
81         void unassign();
82         void set_name(const std::string & n) { name = n; }
83         std::string get_name() const { return name; }
84 protected:
85         void do_print(const print_context & c, unsigned level) const;
86         void do_print_latex(const print_latex & c, unsigned level) const;
87         void do_print_tree(const print_tree & c, unsigned level) const;
88         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
89 private:
90         std::string & autoname_prefix();
91         std::string default_TeX_name() const;
92
93 // member variables
94
95 protected:
96         ptr<assigned_ex_info> asexinfop; ///< assigned expression, only for private use by ginsh
97         unsigned serial;                 ///< unique serial number for comparison
98         std::string name;                ///< printname of this symbol
99         std::string TeX_name;            ///< LaTeX name of this symbol
100 private:
101         static unsigned next_serial;
102 };
103
104
105 // utility functions
106
107 /** Specialization of is_exactly_a<symbol>(obj) for symbol objects. */
108 template<> inline bool is_exactly_a<symbol>(const basic & obj)
109 {
110         return obj.tinfo()==TINFO_symbol;
111 }
112
113 // wrapper functions around member functions
114 inline void unassign(symbol & symarg)
115 { symarg.unassign(); }
116
117 inline int degree(const symbol & a, const ex & s)
118 { return a.degree(s); }
119
120 inline int ldegree(const symbol & a, const ex & s)
121 { return a.ldegree(s); }
122
123 } // namespace GiNaC
124
125 #endif // ndef __GINAC_SYMBOL_H__