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