1 /** @file fderivative.cpp
3 * Implementation of abstract derivatives of functions. */
6 * GiNaC Copyright (C) 1999-2020 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
23 #include "fderivative.h"
24 #include "operators.h"
32 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(fderivative, function,
33 print_func<print_context>(&fderivative::do_print).
34 print_func<print_latex>(&fderivative::do_print_latex).
35 print_func<print_csrc>(&fderivative::do_print_csrc).
36 print_func<print_tree>(&fderivative::do_print_tree))
39 // default constructor
42 fderivative::fderivative()
50 fderivative::fderivative(unsigned ser, unsigned param, const exvector & args) : function(ser, args)
52 parameter_set.insert(param);
55 fderivative::fderivative(unsigned ser, const paramset & params, const exvector & args) : function(ser, args), parameter_set(params)
59 fderivative::fderivative(unsigned ser, const paramset & params, exvector && v) : function(ser, std::move(v)), parameter_set(params)
67 void fderivative::read_archive(const archive_node& n, lst& sym_lst)
69 inherited::read_archive(n, sym_lst);
73 if (n.find_unsigned("param", u, i))
74 parameter_set.insert(u);
80 GINAC_BIND_UNARCHIVER(fderivative);
82 void fderivative::archive(archive_node &n) const
84 inherited::archive(n);
85 auto i = parameter_set.begin(), end = parameter_set.end();
87 n.add_unsigned("param", *i);
94 // functions overriding virtual functions from base classes
97 void fderivative::print(const print_context & c, unsigned level) const
99 // class function overrides print(), but we don't want that
100 basic::print(c, level);
103 void fderivative::do_print(const print_context & c, unsigned level) const
106 auto i = parameter_set.begin(), end = parameter_set.end();
111 c.s << *i << "](" << registered_functions()[serial].name << ")";
112 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
115 void fderivative::do_print_latex(const print_context & c, unsigned level) const
118 c.s << "\\partial_{";
119 auto i = parameter_set.begin(), end = parameter_set.end();
127 c.s << "^{" << order << "}";
128 c.s << "(" << registered_functions()[serial].TeX_name << ")";
129 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
132 void fderivative::do_print_csrc(const print_csrc & c, unsigned level) const
135 auto i = parameter_set.begin(), end = parameter_set.end();
139 c.s << *i << "_" << registered_functions()[serial].name;
140 printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
143 void fderivative::do_print_tree(const print_tree & c, unsigned level) const
145 c.s << std::string(level, ' ') << class_name() << " "
146 << registered_functions()[serial].name << " @" << this
147 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
148 << ", nops=" << nops()
150 auto i = parameter_set.begin(), end = parameter_set.end();
154 c.s << *i << std::endl;
156 i.print(c, level + c.delta_indent);
157 c.s << std::string(level + c.delta_indent, ' ') << "=====" << std::endl;
160 ex fderivative::eval() const
162 // No parameters specified? Then return the function itself
163 if (parameter_set.empty())
164 return function(serial, seq);
166 // If the function in question actually has a derivative, return it
167 if (registered_functions()[serial].has_derivative() && parameter_set.size() == 1)
168 return pderivative(*(parameter_set.begin()));
173 /** The series expansion of derivatives falls back to Taylor expansion.
174 * @see basic::series */
175 ex fderivative::series(const relational & r, int order, unsigned options) const
177 return basic::series(r, order, options);
180 ex fderivative::thiscontainer(const exvector & v) const
182 return fderivative(serial, parameter_set, v);
185 ex fderivative::thiscontainer(exvector && v) const
187 return fderivative(serial, parameter_set, std::move(v));
190 /** Implementation of ex::diff() for derivatives. It applies the chain rule.
192 ex fderivative::derivative(const symbol & s) const
195 for (size_t i=0; i<seq.size(); i++) {
196 ex arg_diff = seq[i].diff(s);
197 if (!arg_diff.is_zero()) {
198 paramset ps = parameter_set;
200 result += arg_diff * fderivative(serial, ps, seq);
206 int fderivative::compare_same_type(const basic & other) const
208 GINAC_ASSERT(is_a<fderivative>(other));
209 const fderivative & o = static_cast<const fderivative &>(other);
211 if (parameter_set != o.parameter_set)
212 return parameter_set < o.parameter_set ? -1 : 1;
214 return inherited::compare_same_type(o);
217 bool fderivative::is_equal_same_type(const basic & other) const
219 GINAC_ASSERT(is_a<fderivative>(other));
220 const fderivative & o = static_cast<const fderivative &>(other);
222 if (parameter_set != o.parameter_set)
225 return inherited::is_equal_same_type(o);
228 bool fderivative::match_same_type(const basic & other) const
230 GINAC_ASSERT(is_a<fderivative>(other));
231 const fderivative & o = static_cast<const fderivative &>(other);
233 return parameter_set == o.parameter_set && inherited::match_same_type(other);
236 /** Expose this object's derivative structure.
238 * Parameter numbers occurring more than once stand for repeated
239 * differentiation with respect to that parameter. If a symbolic function
240 * f(x,y) is differentiated with respect to x, this method will return {0}.
241 * If f(x,y) is differentiated twice with respect to y, it will return {1,1}.
242 * (This corresponds to the way this object is printed.)
244 * @return multiset of function's parameter numbers that are abstractly
246 const paramset& fderivative::derivatives() const
248 return parameter_set;