]> www.ginac.de Git - ginac.git/blob - ginac/symbol.h
- switched to automake build environment
[ginac.git] / ginac / symbol.h
1 /** @file symbol.h
2  *
3  *  Interface to GiNaC's symbolic objects. */
4
5 #ifndef _SYMBOL_H_
6 #define _SYMBOL_H_
7
8 #include <string>
9
10 class symbol;
11
12 #include "basic.h"
13 #include "ex.h"
14
15 /** Basic CAS symbol.  It has a name because it must know how to output itself.
16  *  It may be assigned an expression, but this feature is only intended for
17  *  programs like 'ginsh' that want to associate symbols with expressions.
18  *  If you want to replace symbols by expressions in your code, you should
19  *  use ex::subs() or use objects of class ex instead of class symbol in the
20  *  first place. */
21 class symbol : public basic
22 {
23 // types
24     /** Symbols as keys to expressions. */
25     class assigned_ex_info {
26     public:
27         assigned_ex_info();     //!< Default ctor
28         bool is_assigned;       //!< True if there is an expression assigned
29         ex assigned_expression; //!< The actual expression
30         unsigned refcount;      //!< Yet another refcounter. PLEASE EXPLAIN!
31     };
32     
33 // member functions
34
35     // default constructor, destructor, copy constructor assignment operator and helpers
36 public:
37     symbol();
38     ~symbol();
39     symbol(symbol const & other);
40 protected:
41     void copy(symbol const & other); 
42     void destroy(bool call_parent);
43
44     // other constructors
45 public:
46     explicit symbol(string const & initname);
47
48     // functions overriding virtual functions from base classes
49 public:
50     basic * duplicate() const;
51     void printraw(ostream & os) const;
52     void printtree(ostream & os, unsigned indent) const;
53     void print(ostream & os, unsigned upper_precedence=0) const;
54     void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const;
55     bool info(unsigned inf) const;
56     ex expand(unsigned options=0) const;
57     bool has(ex const & other) const;
58     int degree(symbol const & s) const;
59     int ldegree(symbol const & s) const;
60     ex coeff(symbol const & s, int const n = 1) const;
61     ex eval(int level = 0) const;
62     ex diff(symbol const & s) const;
63     ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
64     ex subs(lst const & ls, lst const & lr) const;
65 protected:
66     int compare_same_type(basic const & other) const;
67     bool is_equal_same_type(basic const & other) const;
68     unsigned return_type(void) const;
69     unsigned return_type_tinfo(void) const;
70     unsigned calchash(void) const;
71     
72     // non-virtual functions in this class
73 public:
74     void assign(ex const & value);
75     void unassign(void);
76     ex diff(symbol const & s, unsigned nth) const;
77     void setname(string const & n) {name=n;}
78     string getname(void) const {return name;}
79 private:
80     string & autoname_prefix(void);
81
82 // member variables
83
84 protected:
85     assigned_ex_info * asexinfop;
86     unsigned serial;  //!< unique serial number for comparision
87     string name;
88 private:
89     static unsigned next_serial;
90 };
91
92 // global constants
93
94 extern const symbol some_symbol;
95 extern type_info const & typeid_symbol;
96
97 // macros
98
99 #define ex_to_symbol(X) static_cast<symbol const &>(*(X).bp)
100
101 // wrapper functions around member functions
102 inline void unassign(symbol & symarg)
103 { return symarg.unassign(); }
104
105 inline int degree(symbol const & a, symbol const & s)
106 { return a.degree(s); }
107
108 inline int ldegree(symbol const & a, symbol const & s)
109 { return a.ldegree(s); }
110
111 #endif // ndef _SYMBOL_H_