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