- minor cleanup
[ginac.git] / ginac / fderivative.cpp
1 /** @file fderivative.cpp
2  *
3  *  Implementation of abstract derivatives of functions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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 #include "fderivative.h"
24 #include "print.h"
25 #include "archive.h"
26 #include "debugmsg.h"
27 #include "utils.h"
28
29 namespace GiNaC {
30
31 GINAC_IMPLEMENT_REGISTERED_CLASS(fderivative, function)
32
33 //////////
34 // default constructor, destructor, copy constructor assignment operator and helpers
35 //////////
36
37 fderivative::fderivative()
38 {
39         debugmsg("fderivative default constructor", LOGLEVEL_CONSTRUCT);
40         tinfo_key = TINFO_fderivative;
41 }
42
43 void fderivative::copy(const fderivative & other)
44 {
45         inherited::copy(other);
46         parameter_set = other.parameter_set;
47 }
48
49 DEFAULT_DESTROY(fderivative)
50
51 //////////
52 // other constructors
53 //////////
54
55 fderivative::fderivative(unsigned ser, unsigned param, const exvector & args) : function(ser, args)
56 {
57         debugmsg("fderivative constructor from unsigned,unsigned,exvector", LOGLEVEL_CONSTRUCT);
58         parameter_set.insert(param);
59         tinfo_key = TINFO_fderivative;
60 }
61
62 fderivative::fderivative(unsigned ser, const paramset & params, const exvector & args) : function(ser, args), parameter_set(params)
63 {
64         debugmsg("fderivative constructor from unsigned,paramset,exvector", LOGLEVEL_CONSTRUCT);
65         tinfo_key = TINFO_fderivative;
66 }
67
68 fderivative::fderivative(unsigned ser, const paramset & params, exvector * vp) : function(ser, vp), parameter_set(params)
69 {
70         debugmsg("fderivative constructor from unsigned,paramset,exvector *", LOGLEVEL_CONSTRUCT);
71         tinfo_key = TINFO_fderivative;
72 }
73
74 //////////
75 // archiving
76 //////////
77
78 fderivative::fderivative(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
79 {
80         debugmsg("fderivative constructor from archive_node", LOGLEVEL_CONSTRUCT);
81         unsigned i = 0;
82         while (true) {
83                 unsigned u;
84                 if (n.find_unsigned("param", u, i))
85                         parameter_set.insert(u);
86                 else
87                         break;
88                 i++;
89         }
90 }
91
92 void fderivative::archive(archive_node &n) const
93 {
94         inherited::archive(n);
95         paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
96         while (i != end) {
97                 n.add_unsigned("param", *i);
98                 ++i;
99         }
100 }
101
102 DEFAULT_UNARCHIVE(fderivative)
103
104 //////////
105 // functions overriding virtual functions from base classes
106 //////////
107
108 void fderivative::print(const print_context & c, unsigned level) const
109 {
110         debugmsg("fderivative print", LOGLEVEL_PRINT);
111
112         if (is_a<print_tree>(c)) {
113
114                 c.s << std::string(level, ' ') << class_name() << " "
115                     << registered_functions()[serial].name
116                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
117                     << ", nops=" << nops()
118                     << ", params=";
119                 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
120                 --end;
121                 while (i != end)
122                         c.s << *i++ << ",";
123                 c.s << *i << std::endl;
124                 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
125                 for (unsigned i=0; i<seq.size(); ++i)
126                         seq[i].print(c, level + delta_indent);
127                 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
128
129         } else {
130
131                 c.s << "D[";
132                 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
133                 --end;
134                 while (i != end)
135                         c.s << *i++ << ",";
136                 c.s << *i << "](" << registered_functions()[serial].name << ")";
137                 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
138         }
139 }
140
141 ex fderivative::eval(int level) const
142 {
143         if (level > 1) {
144                 // first evaluate children, then we will end up here again
145                 return fderivative(serial, parameter_set, evalchildren(level));
146         }
147
148         // No parameters specified? Then return the function itself
149         if (parameter_set.empty())
150                 return function(serial, seq);
151
152         // If the function in question actually has a derivative, return it
153         if (registered_functions()[serial].has_derivative() && parameter_set.size() == 1)
154                 return pderivative(*(parameter_set.begin()));
155
156         return this->hold();
157 }
158
159 /** Numeric evaluation falls back to evaluation of arguments.
160  *  @see basic::evalf */
161 ex fderivative::evalf(int level) const
162 {
163         return basic::evalf(level);
164 }
165
166 /** The series expansion of derivatives falls back to Taylor expansion.
167  *  @see basic::series */
168 ex fderivative::series(const relational & r, int order, unsigned options) const
169 {
170         return basic::series(r, order, options);
171 }
172
173 ex fderivative::thisexprseq(const exvector & v) const
174 {
175         return fderivative(serial, parameter_set, v);
176 }
177
178 ex fderivative::thisexprseq(exvector * vp) const
179 {
180         return fderivative(serial, parameter_set, vp);
181 }
182
183 /** Implementation of ex::diff() for derivatives. It applies the chain rule.
184  *  @see ex::diff */
185 ex fderivative::derivative(const symbol & s) const
186 {
187         ex result;
188         for (unsigned i=0; i!=seq.size(); i++) {
189                 ex arg_diff = seq[i].diff(s);
190                 if (!arg_diff.is_zero()) {
191                         paramset ps = parameter_set;
192                         ps.insert(i);
193                         result += arg_diff * fderivative(serial, ps, seq);
194                 }
195         }
196         return result;
197 }
198
199 int fderivative::compare_same_type(const basic & other) const
200 {
201         GINAC_ASSERT(is_of_type(other, fderivative));
202         const fderivative & o = static_cast<const fderivative &>(other);
203
204         if (parameter_set != o.parameter_set)
205                 return parameter_set < o.parameter_set ? -1 : 1;
206         else
207                 return inherited::compare_same_type(o);
208 }
209
210 bool fderivative::is_equal_same_type(const basic & other) const
211 {
212         GINAC_ASSERT(is_of_type(other, fderivative));
213         const fderivative & o = static_cast<const fderivative &>(other);
214
215         if (parameter_set != o.parameter_set)
216                 return false;
217         else
218                 return inherited::is_equal_same_type(o);
219 }
220
221 bool fderivative::match_same_type(const basic & other) const
222 {
223         GINAC_ASSERT(is_of_type(other, fderivative));
224         const fderivative & o = static_cast<const fderivative &>(other);
225
226         return parameter_set == o.parameter_set;
227 }
228
229 } // namespace GiNaC