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