1 /** @file fderivative.cpp
3 * Implementation of abstract derivatives of functions. */
6 * GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
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.
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.
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
25 #include "fderivative.h"
26 #include "operators.h"
33 GINAC_IMPLEMENT_REGISTERED_CLASS(fderivative, function)
36 // default ctor, dtor, copy ctor, assignment operator and helpers
39 fderivative::fderivative()
41 tinfo_key = TINFO_fderivative;
44 void fderivative::copy(const fderivative & other)
46 inherited::copy(other);
47 parameter_set = other.parameter_set;
50 DEFAULT_DESTROY(fderivative)
56 fderivative::fderivative(unsigned ser, unsigned param, const exvector & args) : function(ser, args)
58 parameter_set.insert(param);
59 tinfo_key = TINFO_fderivative;
62 fderivative::fderivative(unsigned ser, const paramset & params, const exvector & args) : function(ser, args), parameter_set(params)
64 tinfo_key = TINFO_fderivative;
67 fderivative::fderivative(unsigned ser, const paramset & params, exvector * vp) : function(ser, vp), parameter_set(params)
69 tinfo_key = TINFO_fderivative;
76 fderivative::fderivative(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
81 if (n.find_unsigned("param", u, i))
82 parameter_set.insert(u);
89 void fderivative::archive(archive_node &n) const
91 inherited::archive(n);
92 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
94 n.add_unsigned("param", *i);
99 DEFAULT_UNARCHIVE(fderivative)
102 // functions overriding virtual functions from base classes
105 void fderivative::print(const print_context & c, unsigned level) const
107 if (is_a<print_tree>(c)) {
109 c.s << std::string(level, ' ') << class_name() << " "
110 << registered_functions()[serial].name
111 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
112 << ", nops=" << nops()
114 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
118 c.s << *i << std::endl;
119 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
120 for (unsigned i=0; i<seq.size(); ++i)
121 seq[i].print(c, level + delta_indent);
122 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
127 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
131 c.s << *i << "](" << registered_functions()[serial].name << ")";
132 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
136 ex fderivative::eval(int level) const
139 // first evaluate children, then we will end up here again
140 return fderivative(serial, parameter_set, evalchildren(level));
143 // No parameters specified? Then return the function itself
144 if (parameter_set.empty())
145 return function(serial, seq);
147 // If the function in question actually has a derivative, return it
148 if (registered_functions()[serial].has_derivative() && parameter_set.size() == 1)
149 return pderivative(*(parameter_set.begin()));
154 /** Numeric evaluation falls back to evaluation of arguments.
155 * @see basic::evalf */
156 ex fderivative::evalf(int level) const
158 return basic::evalf(level);
161 /** The series expansion of derivatives falls back to Taylor expansion.
162 * @see basic::series */
163 ex fderivative::series(const relational & r, int order, unsigned options) const
165 return basic::series(r, order, options);
168 ex fderivative::thisexprseq(const exvector & v) const
170 return fderivative(serial, parameter_set, v);
173 ex fderivative::thisexprseq(exvector * vp) const
175 return fderivative(serial, parameter_set, vp);
178 /** Implementation of ex::diff() for derivatives. It applies the chain rule.
180 ex fderivative::derivative(const symbol & s) const
183 for (unsigned i=0; i!=seq.size(); i++) {
184 ex arg_diff = seq[i].diff(s);
185 if (!arg_diff.is_zero()) {
186 paramset ps = parameter_set;
188 result += arg_diff * fderivative(serial, ps, seq);
194 int fderivative::compare_same_type(const basic & other) const
196 GINAC_ASSERT(is_a<fderivative>(other));
197 const fderivative & o = static_cast<const fderivative &>(other);
199 if (parameter_set != o.parameter_set)
200 return parameter_set < o.parameter_set ? -1 : 1;
202 return inherited::compare_same_type(o);
205 bool fderivative::is_equal_same_type(const basic & other) const
207 GINAC_ASSERT(is_a<fderivative>(other));
208 const fderivative & o = static_cast<const fderivative &>(other);
210 if (parameter_set != o.parameter_set)
213 return inherited::is_equal_same_type(o);
216 bool fderivative::match_same_type(const basic & other) const
218 GINAC_ASSERT(is_a<fderivative>(other));
219 const fderivative & o = static_cast<const fderivative &>(other);
221 return parameter_set == o.parameter_set;