]> www.ginac.de Git - ginac.git/blob - ginac/symbol.h
[nitpick] don't use int instead of std::size_t.
[ginac.git] / ginac / symbol.h
1 /** @file symbol.h
2  *
3  *  Interface to GiNaC's symbolic objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2008 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  */
35 class symbol : public basic
36 {
37         GINAC_DECLARE_REGISTERED_CLASS(symbol, basic)
38
39         friend class realsymbol;
40         friend class possymbol;
41
42 // member functions
43         
44         // other constructors
45 public:
46         explicit symbol(const std::string & initname, unsigned domain = domain::complex);
47         symbol(const std::string & initname, const std::string & texname, unsigned domain = domain::complex);
48         symbol(const std::string & initname, const std::string & texname, unsigned rt, unsigned domain);
49         symbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain = domain::complex);
50         symbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain = domain::complex);
51         
52         // functions overriding virtual functions from base classes
53 public:
54         bool info(unsigned inf) const;
55         ex eval(int level = 0) const { return *this; } // for performance reasons
56         ex evalf(int level = 0) const { return *this; } // overwrites basic::evalf() for performance reasons
57         ex series(const relational & s, int order, unsigned options = 0) const;
58         ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
59         ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
60         ex to_rational(exmap & repl) const;
61         ex to_polynomial(exmap & repl) const;
62         unsigned return_type() const { return ret_type; }
63         tinfo_t return_type_tinfo() const { return ret_type_tinfo; }
64         ex conjugate() const;
65         ex real_part() const;
66         ex imag_part() const;
67         bool is_polynomial(const ex & var) const;
68 protected:
69         ex derivative(const symbol & s) const;
70         bool is_equal_same_type(const basic & other) const;
71         unsigned calchash() const;
72         
73         // non-virtual functions in this class
74 public:
75         void set_name(const std::string & n) { name = n; }
76         std::string get_name() const { return name; }
77         unsigned get_domain() const { return domain; }
78 protected:
79         void do_print(const print_context & c, unsigned level) const;
80         void do_print_latex(const print_latex & c, unsigned level) const;
81         void do_print_tree(const print_tree & c, unsigned level) const;
82         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
83 private:
84         std::string & autoname_prefix();
85         std::string default_TeX_name() const;
86
87 // member variables
88
89 protected:
90         unsigned serial;                 ///< unique serial number for comparison
91         std::string name;                ///< printname of this symbol
92         std::string TeX_name;            ///< LaTeX name of this symbol
93         unsigned domain;                 ///< domain of symbol, complex (default) or real
94         unsigned ret_type;               ///< value returned by return_type()
95         tinfo_t ret_type_tinfo;         ///< value returned by return_type_tinfo()
96 private:
97         static unsigned next_serial;
98 };
99
100
101 /** Specialization of symbol to real domain */
102 class realsymbol : public symbol
103 {
104         // constructors
105 public:
106         realsymbol();
107         explicit realsymbol(const std::string & initname, unsigned domain = domain::real);
108         realsymbol(const std::string & initname, const std::string & texname, unsigned domain = domain::real);
109         realsymbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain = domain::real);
110         realsymbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain = domain::real);
111 };
112
113
114 /** Specialization of symbol to real domain */
115 class possymbol : public symbol
116 {
117         // constructors
118 public:
119         possymbol();
120         explicit possymbol(const std::string & initname, unsigned domain = domain::positive);
121         possymbol(const std::string & initname, const std::string & texname, unsigned domain = domain::positive);
122         possymbol(const std::string & initname, unsigned rt, tinfo_t rtt, unsigned domain = domain::positive);
123         possymbol(const std::string & initname, const std::string & texname, unsigned rt, tinfo_t rtt, unsigned domain = domain::positive);
124 };
125
126
127 // utility functions
128
129 /** Specialization of is_exactly_a<realsymbol>(obj) for realsymbol objects. */
130 template<> inline bool is_exactly_a<realsymbol>(const basic & obj)
131 {
132         if (obj.tinfo() != &symbol::tinfo_static)
133                 return false;
134         unsigned domain = static_cast<const symbol &>(obj).get_domain();
135         return domain==domain::real || domain==domain::positive;
136 }
137
138 /** Specialization of is_exactly_a<possymbol>(obj) for possymbol objects. */
139 template<> inline bool is_exactly_a<possymbol>(const basic & obj)
140 {
141         if (obj.tinfo() != &symbol::tinfo_static)
142                 return false;
143         unsigned domain = static_cast<const symbol &>(obj).get_domain();
144         return domain == domain::positive;
145 }
146
147 } // namespace GiNaC
148
149 #endif // ndef __GINAC_SYMBOL_H__