1 /** @file fderivative.cpp
3 * Implementation of abstract derivatives of functions. */
6 * GiNaC Copyright (C) 1999-2003 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"
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))
37 // default constructor
40 fderivative::fderivative()
42 tinfo_key = TINFO_fderivative;
49 fderivative::fderivative(unsigned ser, unsigned param, const exvector & args) : function(ser, args)
51 parameter_set.insert(param);
52 tinfo_key = TINFO_fderivative;
55 fderivative::fderivative(unsigned ser, const paramset & params, const exvector & args) : function(ser, args), parameter_set(params)
57 tinfo_key = TINFO_fderivative;
60 fderivative::fderivative(unsigned ser, const paramset & params, exvector * vp) : function(ser, vp), parameter_set(params)
62 tinfo_key = TINFO_fderivative;
69 fderivative::fderivative(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
74 if (n.find_unsigned("param", u, i))
75 parameter_set.insert(u);
82 void fderivative::archive(archive_node &n) const
84 inherited::archive(n);
85 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
87 n.add_unsigned("param", *i);
92 DEFAULT_UNARCHIVE(fderivative)
95 // functions overriding virtual functions from base classes
98 void fderivative::do_print(const print_context & c, unsigned level) const
101 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
105 c.s << *i << "](" << registered_functions()[serial].name << ")";
106 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
109 void fderivative::do_print_tree(const print_tree & c, unsigned level) const
111 c.s << std::string(level, ' ') << class_name() << " "
112 << registered_functions()[serial].name
113 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
114 << ", nops=" << nops()
116 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
120 c.s << *i << std::endl;
121 for (size_t i=0; i<seq.size(); ++i)
122 seq[i].print(c, level + c.delta_indent);
123 c.s << std::string(level + c.delta_indent, ' ') << "=====" << std::endl;
126 ex fderivative::eval(int level) const
129 // first evaluate children, then we will end up here again
130 return fderivative(serial, parameter_set, evalchildren(level));
133 // No parameters specified? Then return the function itself
134 if (parameter_set.empty())
135 return function(serial, seq);
137 // If the function in question actually has a derivative, return it
138 if (registered_functions()[serial].has_derivative() && parameter_set.size() == 1)
139 return pderivative(*(parameter_set.begin()));
144 /** Numeric evaluation falls back to evaluation of arguments.
145 * @see basic::evalf */
146 ex fderivative::evalf(int level) const
148 return basic::evalf(level);
151 /** The series expansion of derivatives falls back to Taylor expansion.
152 * @see basic::series */
153 ex fderivative::series(const relational & r, int order, unsigned options) const
155 return basic::series(r, order, options);
158 ex fderivative::thiscontainer(const exvector & v) const
160 return fderivative(serial, parameter_set, v);
163 ex fderivative::thiscontainer(exvector * vp) const
165 return fderivative(serial, parameter_set, vp);
168 /** Implementation of ex::diff() for derivatives. It applies the chain rule.
170 ex fderivative::derivative(const symbol & s) const
173 for (size_t i=0; i<seq.size(); i++) {
174 ex arg_diff = seq[i].diff(s);
175 if (!arg_diff.is_zero()) {
176 paramset ps = parameter_set;
178 result += arg_diff * fderivative(serial, ps, seq);
184 int fderivative::compare_same_type(const basic & other) const
186 GINAC_ASSERT(is_a<fderivative>(other));
187 const fderivative & o = static_cast<const fderivative &>(other);
189 if (parameter_set != o.parameter_set)
190 return parameter_set < o.parameter_set ? -1 : 1;
192 return inherited::compare_same_type(o);
195 bool fderivative::is_equal_same_type(const basic & other) const
197 GINAC_ASSERT(is_a<fderivative>(other));
198 const fderivative & o = static_cast<const fderivative &>(other);
200 if (parameter_set != o.parameter_set)
203 return inherited::is_equal_same_type(o);
206 bool fderivative::match_same_type(const basic & other) const
208 GINAC_ASSERT(is_a<fderivative>(other));
209 const fderivative & o = static_cast<const fderivative &>(other);
211 return parameter_set == o.parameter_set;