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