]> www.ginac.de Git - ginac.git/blob - ginac/symbol.h
42c6ae3203a421dad3d78dc6d34c8cb07b491626
[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 <typeinfo>
28 #include "basic.h"
29 #include "ex.h"
30 #include "ptr.h"
31
32 namespace GiNaC {
33
34 /** Basic CAS symbol.  It has a name because it must know how to output itself.
35  */
36 class symbol : public basic
37 {
38         GINAC_DECLARE_REGISTERED_CLASS(symbol, basic)
39
40         friend class realsymbol;
41         friend class possymbol;
42
43 // member functions
44         
45         // other constructors
46 public:
47         explicit symbol(const std::string & initname, unsigned domain = domain::complex);
48         symbol(const std::string & initname, const std::string & texname, unsigned domain = domain::complex);
49         
50         // functions overriding virtual functions from base classes
51 public:
52         bool info(unsigned inf) const;
53         ex eval(int level = 0) const { return *this; } // for performance reasons
54         ex evalf(int level = 0) const { return *this; } // overwrites basic::evalf() for performance reasons
55         ex series(const relational & s, int order, unsigned options = 0) const;
56         ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
57         ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
58         ex to_rational(exmap & repl) const;
59         ex to_polynomial(exmap & repl) const;
60         ex conjugate() const;
61         ex real_part() const;
62         ex imag_part() const;
63         bool is_polynomial(const ex & var) const;
64 protected:
65         ex derivative(const symbol & s) const;
66         bool is_equal_same_type(const basic & other) const;
67         unsigned calchash() const;
68         
69         // non-virtual functions in this class
70 public:
71         void set_name(const std::string & n) { name = n; }
72         std::string get_name() const { return name; }
73         unsigned get_domain() const { return domain; }
74 protected:
75         void do_print(const print_context & c, unsigned level) const;
76         void do_print_latex(const print_latex & c, unsigned level) const;
77         void do_print_tree(const print_tree & c, unsigned level) const;
78         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
79
80 // member variables
81
82 protected:
83         unsigned serial;                 ///< unique serial number for comparison
84         std::string name;                ///< printname of this symbol
85         std::string TeX_name;            ///< LaTeX name of this symbol
86         unsigned domain;                 ///< domain of symbol, complex (default) or real
87 private:
88         static unsigned next_serial;
89 };
90
91
92 /** Specialization of symbol to real domain */
93 class realsymbol : public symbol
94 {
95         // constructors
96 public:
97         realsymbol();
98         explicit realsymbol(const std::string & initname, unsigned domain = domain::real);
99         realsymbol(const std::string & initname, const std::string & texname, unsigned domain = domain::real);
100 };
101
102
103 /** Specialization of symbol to real domain */
104 class possymbol : public symbol
105 {
106         // constructors
107 public:
108         possymbol();
109         explicit possymbol(const std::string & initname, unsigned domain = domain::positive);
110         possymbol(const std::string & initname, const std::string & texname, unsigned domain = domain::positive);
111 };
112
113
114 // utility functions
115
116 /** Specialization of is_exactly_a<realsymbol>(obj) for realsymbol objects. */
117 template<> inline bool is_exactly_a<realsymbol>(const basic & obj)
118 {
119         if (!is_a<symbol>(obj))
120                 return false;
121         unsigned domain = static_cast<const symbol &>(obj).get_domain();
122         return domain==domain::real || domain==domain::positive;
123 }
124
125 /** Specialization of is_exactly_a<possymbol>(obj) for possymbol objects. */
126 template<> inline bool is_exactly_a<possymbol>(const basic & obj)
127 {
128         if (!is_a<symbol>(obj))
129                 return false;
130         unsigned domain = static_cast<const symbol &>(obj).get_domain();
131         return domain == domain::positive;
132 }
133
134 } // namespace GiNaC
135
136 #endif // ndef __GINAC_SYMBOL_H__