1 /** @file fderivative.cpp
3 * Implementation of abstract derivatives of functions. */
6 * GiNaC Copyright (C) 1999-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_csrc>(&fderivative::do_print_csrc).
35 print_func<print_tree>(&fderivative::do_print_tree))
38 // default constructor
41 fderivative::fderivative()
49 fderivative::fderivative(unsigned ser, unsigned param, const exvector & args) : function(ser, args)
51 parameter_set.insert(param);
54 fderivative::fderivative(unsigned ser, const paramset & params, const exvector & args) : function(ser, args), parameter_set(params)
58 fderivative::fderivative(unsigned ser, const paramset & params, std::auto_ptr<exvector> vp) : function(ser, vp), parameter_set(params)
66 fderivative::fderivative(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
71 if (n.find_unsigned("param", u, i))
72 parameter_set.insert(u);
79 void fderivative::archive(archive_node &n) const
81 inherited::archive(n);
82 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
84 n.add_unsigned("param", *i);
89 DEFAULT_UNARCHIVE(fderivative)
92 // functions overriding virtual functions from base classes
95 void fderivative::print(const print_context & c, unsigned level) const
97 // class function overrides print(), but we don't want that
98 basic::print(c, level);
101 void fderivative::do_print(const print_context & c, unsigned level) const
104 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
109 c.s << *i << "](" << registered_functions()[serial].name << ")";
110 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
113 void fderivative::do_print_csrc(const print_csrc & c, unsigned level) const
116 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
120 c.s << *i << "_" << registered_functions()[serial].name;
121 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
124 void fderivative::do_print_tree(const print_tree & c, unsigned level) const
126 c.s << std::string(level, ' ') << class_name() << " "
127 << registered_functions()[serial].name << " @" << this
128 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
129 << ", nops=" << nops()
131 paramset::const_iterator i = parameter_set.begin(), end = parameter_set.end();
135 c.s << *i << std::endl;
136 for (size_t i=0; i<seq.size(); ++i)
137 seq[i].print(c, level + c.delta_indent);
138 c.s << std::string(level + c.delta_indent, ' ') << "=====" << std::endl;
141 ex fderivative::eval(int level) const
144 // first evaluate children, then we will end up here again
145 return fderivative(serial, parameter_set, evalchildren(level));
148 // No parameters specified? Then return the function itself
149 if (parameter_set.empty())
150 return function(serial, seq);
152 // If the function in question actually has a derivative, return it
153 if (registered_functions()[serial].has_derivative() && parameter_set.size() == 1)
154 return pderivative(*(parameter_set.begin()));
159 /** Numeric evaluation falls back to evaluation of arguments.
160 * @see basic::evalf */
161 ex fderivative::evalf(int level) const
163 return basic::evalf(level);
166 /** The series expansion of derivatives falls back to Taylor expansion.
167 * @see basic::series */
168 ex fderivative::series(const relational & r, int order, unsigned options) const
170 return basic::series(r, order, options);
173 ex fderivative::thiscontainer(const exvector & v) const
175 return fderivative(serial, parameter_set, v);
178 ex fderivative::thiscontainer(std::auto_ptr<exvector> vp) const
180 return fderivative(serial, parameter_set, vp);
183 /** Implementation of ex::diff() for derivatives. It applies the chain rule.
185 ex fderivative::derivative(const symbol & s) const
188 for (size_t i=0; i<seq.size(); i++) {
189 ex arg_diff = seq[i].diff(s);
190 if (!arg_diff.is_zero()) {
191 paramset ps = parameter_set;
193 result += arg_diff * fderivative(serial, ps, seq);
199 int fderivative::compare_same_type(const basic & other) const
201 GINAC_ASSERT(is_a<fderivative>(other));
202 const fderivative & o = static_cast<const fderivative &>(other);
204 if (parameter_set != o.parameter_set)
205 return parameter_set < o.parameter_set ? -1 : 1;
207 return inherited::compare_same_type(o);
210 bool fderivative::is_equal_same_type(const basic & other) const
212 GINAC_ASSERT(is_a<fderivative>(other));
213 const fderivative & o = static_cast<const fderivative &>(other);
215 if (parameter_set != o.parameter_set)
218 return inherited::is_equal_same_type(o);
221 bool fderivative::match_same_type(const basic & other) const
223 GINAC_ASSERT(is_a<fderivative>(other));
224 const fderivative & o = static_cast<const fderivative &>(other);
226 return parameter_set == o.parameter_set && inherited::match_same_type(other);