1 /** @file fderivative.cpp
3 * Implementation of abstract derivatives of functions. */
6 * GiNaC Copyright (C) 1999-2001 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"
32 GINAC_IMPLEMENT_REGISTERED_CLASS(fderivative, function)
35 // default ctor, dtor, copy ctor, assignment operator and helpers
38 fderivative::fderivative()
40 tinfo_key = TINFO_fderivative;
43 void fderivative::copy(const fderivative & other)
45 inherited::copy(other);
46 parameter_set = other.parameter_set;
49 DEFAULT_DESTROY(fderivative)
55 fderivative::fderivative(unsigned ser, unsigned param, const exvector & args) : function(ser, args)
57 parameter_set.insert(param);
58 tinfo_key = TINFO_fderivative;
61 fderivative::fderivative(unsigned ser, const paramset & params, const exvector & args) : function(ser, args), parameter_set(params)
63 tinfo_key = TINFO_fderivative;
66 fderivative::fderivative(unsigned ser, const paramset & params, exvector * vp) : function(ser, vp), parameter_set(params)
68 tinfo_key = TINFO_fderivative;
75 fderivative::fderivative(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
80 if (n.find_unsigned("param", u, i))
81 parameter_set.insert(u);
88 void fderivative::archive(archive_node &n) const
90 inherited::archive(n);
91 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
93 n.add_unsigned("param", *i);
98 DEFAULT_UNARCHIVE(fderivative)
101 // functions overriding virtual functions from base classes
104 void fderivative::print(const print_context & c, unsigned level) const
106 if (is_a<print_tree>(c)) {
108 c.s << std::string(level, ' ') << class_name() << " "
109 << registered_functions()[serial].name
110 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
111 << ", nops=" << nops()
113 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
117 c.s << *i << std::endl;
118 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
119 for (unsigned i=0; i<seq.size(); ++i)
120 seq[i].print(c, level + delta_indent);
121 c.s << std::string(level + delta_indent, ' ') << "=====" << std::endl;
126 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
130 c.s << *i << "](" << registered_functions()[serial].name << ")";
131 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
135 ex fderivative::eval(int level) const
138 // first evaluate children, then we will end up here again
139 return fderivative(serial, parameter_set, evalchildren(level));
142 // No parameters specified? Then return the function itself
143 if (parameter_set.empty())
144 return function(serial, seq);
146 // If the function in question actually has a derivative, return it
147 if (registered_functions()[serial].has_derivative() && parameter_set.size() == 1)
148 return pderivative(*(parameter_set.begin()));
153 /** Numeric evaluation falls back to evaluation of arguments.
154 * @see basic::evalf */
155 ex fderivative::evalf(int level) const
157 return basic::evalf(level);
160 /** The series expansion of derivatives falls back to Taylor expansion.
161 * @see basic::series */
162 ex fderivative::series(const relational & r, int order, unsigned options) const
164 return basic::series(r, order, options);
167 ex fderivative::thisexprseq(const exvector & v) const
169 return fderivative(serial, parameter_set, v);
172 ex fderivative::thisexprseq(exvector * vp) const
174 return fderivative(serial, parameter_set, vp);
177 /** Implementation of ex::diff() for derivatives. It applies the chain rule.
179 ex fderivative::derivative(const symbol & s) const
182 for (unsigned i=0; i!=seq.size(); i++) {
183 ex arg_diff = seq[i].diff(s);
184 if (!arg_diff.is_zero()) {
185 paramset ps = parameter_set;
187 result += arg_diff * fderivative(serial, ps, seq);
193 int fderivative::compare_same_type(const basic & other) const
195 GINAC_ASSERT(is_a<fderivative>(other));
196 const fderivative & o = static_cast<const fderivative &>(other);
198 if (parameter_set != o.parameter_set)
199 return parameter_set < o.parameter_set ? -1 : 1;
201 return inherited::compare_same_type(o);
204 bool fderivative::is_equal_same_type(const basic & other) const
206 GINAC_ASSERT(is_a<fderivative>(other));
207 const fderivative & o = static_cast<const fderivative &>(other);
209 if (parameter_set != o.parameter_set)
212 return inherited::is_equal_same_type(o);
215 bool fderivative::match_same_type(const basic & other) const
217 GINAC_ASSERT(is_a<fderivative>(other));
218 const fderivative & o = static_cast<const fderivative &>(other);
220 return parameter_set == o.parameter_set;