]> www.ginac.de Git - ginac.git/blob - ginac/symbol.h
input_parser accepts also pow() [A.Sheplyakov]
[ginac.git] / ginac / symbol.h
1 /** @file symbol.h
2  *
3  *  Interface to GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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         friend class realsymbol;
44         friend class possymbol;
45
46 // types
47         
48         /** Symbols as keys to expressions - only for ginsh. */
49         class assigned_ex_info : public refcounted {
50         public:
51                 assigned_ex_info() throw();  ///< Default ctor
52                 bool is_assigned;            ///< True if there is an expression assigned
53                 ex assigned_expression;      ///< The actual expression
54         };
55
56 // member functions
57         
58         // other constructors
59 public:
60         explicit symbol(const std::string & initname, unsigned domain = domain::complex);
61         symbol(const std::string & initname, const std::string & texname, unsigned domain = domain::complex);
62         symbol(const std::string & initname, const std::string & texname, unsigned rt, unsigned domain);
63         symbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain = domain::complex);
64         symbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain = domain::complex);
65         
66         // functions overriding virtual functions from base classes
67 public:
68         bool info(unsigned inf) const;
69         ex eval(int level = 0) const;
70         ex evalf(int level = 0) const { return *this; } // overwrites basic::evalf() for performance reasons
71         ex series(const relational & s, int order, unsigned options = 0) const;
72         ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
73         ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
74         ex to_rational(exmap & repl) const;
75         ex to_polynomial(exmap & repl) const;
76         unsigned return_type() const { return ret_type; }
77         tinfo_t return_type_tinfo() const { return ret_type_tinfo; }
78         ex conjugate() const;
79         ex real_part() const;
80         ex imag_part() const;
81         bool is_polynomial(const ex & var) const;
82 protected:
83         ex derivative(const symbol & s) const;
84         bool is_equal_same_type(const basic & other) const;
85         unsigned calchash() const;
86         
87         // non-virtual functions in this class
88 public:
89         void assign(const ex & value);
90         void unassign();
91         void set_name(const std::string & n) { name = n; }
92         std::string get_name() const { return name; }
93         unsigned get_domain() const { return domain; }
94 protected:
95         void do_print(const print_context & c, unsigned level) const;
96         void do_print_latex(const print_latex & c, unsigned level) const;
97         void do_print_tree(const print_tree & c, unsigned level) const;
98         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
99 private:
100         std::string & autoname_prefix();
101         std::string default_TeX_name() const;
102
103 // member variables
104
105 protected:
106         ptr<assigned_ex_info> asexinfop; ///< assigned expression, only for private use by ginsh
107         unsigned serial;                 ///< unique serial number for comparison
108         std::string name;                ///< printname of this symbol
109         std::string TeX_name;            ///< LaTeX name of this symbol
110         unsigned domain;                 ///< domain of symbol, complex (default) or real
111         unsigned ret_type;               ///< value returned by return_type()
112         tinfo_t ret_type_tinfo;         ///< value returned by return_type_tinfo()
113 private:
114         static unsigned next_serial;
115 };
116
117
118 /** Specialization of symbol to real domain */
119 class realsymbol : public symbol
120 {
121         // constructors
122 public:
123         realsymbol();
124         explicit realsymbol(const std::string & initname, unsigned domain = domain::real);
125         realsymbol(const std::string & initname, const std::string & texname, unsigned domain = domain::real);
126         realsymbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain = domain::real);
127         realsymbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain = domain::real);
128 };
129
130
131 /** Specialization of symbol to real domain */
132 class possymbol : public symbol
133 {
134         // constructors
135 public:
136         possymbol();
137         explicit possymbol(const std::string & initname, unsigned domain = domain::positive);
138         possymbol(const std::string & initname, const std::string & texname, unsigned domain = domain::positive);
139         possymbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain = domain::positive);
140         possymbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain = domain::positive);
141 };
142
143
144 // utility functions
145
146 /** Specialization of is_exactly_a<realsymbol>(obj) for realsymbol objects. */
147 template<> inline bool is_exactly_a<realsymbol>(const basic & obj)
148 {
149         if (obj.tinfo() != &symbol::tinfo_static)
150                 return false;
151         unsigned domain = static_cast<const symbol &>(obj).get_domain();
152         return domain==domain::real || domain==domain::positive;
153 }
154
155 /** Specialization of is_exactly_a<possymbol>(obj) for possymbol objects. */
156 template<> inline bool is_exactly_a<possymbol>(const basic & obj)
157 {
158         if (obj.tinfo() != &symbol::tinfo_static)
159                 return false;
160         unsigned domain = static_cast<const symbol &>(obj).get_domain();
161         return domain == domain::positive;
162 }
163
164 // wrapper functions around member functions
165 inline void unassign(symbol & symarg)
166 { symarg.unassign(); }
167
168 } // namespace GiNaC
169
170 #endif // ndef __GINAC_SYMBOL_H__