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