]> www.ginac.de Git - ginac.git/blob - ginac/fderivative.cpp
[DOC] Update the tutorial for GiNaC::function resolution.
[ginac.git] / ginac / fderivative.cpp
1 /** @file fderivative.cpp
2  *
3  *  Implementation of abstract derivatives of functions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2025 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "fderivative.h"
24 #include "operators.h"
25 #include "archive.h"
26 #include "utils.h"
27
28 namespace GiNaC {
29
30 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(fderivative, function,
31   print_func<print_context>(&fderivative::do_print).
32   print_func<print_latex>(&fderivative::do_print_latex).
33   print_func<print_csrc>(&fderivative::do_print_csrc).
34   print_func<print_tree>(&fderivative::do_print_tree))
35
36 //////////
37 // default constructor
38 //////////
39
40 fderivative::fderivative()
41 {
42 }
43
44 //////////
45 // other constructors
46 //////////
47
48 fderivative::fderivative(unsigned ser, unsigned param, const exvector & args) : function(ser, args)
49 {
50         parameter_set.insert(param);
51 }
52
53 fderivative::fderivative(unsigned ser, const paramset & params, const exvector & args) : function(ser, args), parameter_set(params)
54 {
55 }
56
57 fderivative::fderivative(unsigned ser, const paramset & params, exvector && v) : function(ser, std::move(v)), parameter_set(params)
58 {
59 }
60
61 //////////
62 // archiving
63 //////////
64
65 void fderivative::read_archive(const archive_node& n, lst& sym_lst)
66 {
67         inherited::read_archive(n, sym_lst);
68         unsigned i = 0;
69         while (true) {
70                 unsigned u;
71                 if (n.find_unsigned("param", u, i))
72                         parameter_set.insert(u);
73                 else
74                         break;
75                 ++i;
76         }
77 }
78 GINAC_BIND_UNARCHIVER(fderivative);
79
80 void fderivative::archive(archive_node &n) const
81 {
82         inherited::archive(n);
83         auto i = parameter_set.begin(), end = parameter_set.end();
84         while (i != end) {
85                 n.add_unsigned("param", *i);
86                 ++i;
87         }
88 }
89
90
91 //////////
92 // functions overriding virtual functions from base classes
93 //////////
94
95 void fderivative::print(const print_context & c, unsigned level) const
96 {
97         // class function overrides print(), but we don't want that
98         basic::print(c, level);
99 }
100
101 void fderivative::do_print(const print_context & c, unsigned level) const
102 {
103         c.s << "D[";
104         auto i = parameter_set.begin(), end = parameter_set.end();
105         --end;
106         while (i != end) {
107                 c.s << *i++ << ",";
108         }
109         c.s << *i << "](" << registered_functions()[serial].name << ")";
110         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
111 }
112
113 void fderivative::do_print_latex(const print_context & c, unsigned level) const
114 {
115         int order=1;
116         c.s << "\\partial_{";
117         auto i = parameter_set.begin(), end = parameter_set.end();
118         --end;
119         while (i != end) {
120                 ++order;
121                 c.s << *i++ << ",";
122         }
123         c.s << *i << "}";
124         if (order>1)
125                 c.s << "^{" << order << "}";
126         c.s << "(" << registered_functions()[serial].TeX_name << ")";
127         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
128 }
129
130 void fderivative::do_print_csrc(const print_csrc & c, unsigned level) const
131 {
132         c.s << "D_";
133         auto i = parameter_set.begin(), end = parameter_set.end();
134         --end;
135         while (i != end)
136                 c.s << *i++ << "_";
137         c.s << *i << "_" << registered_functions()[serial].name;
138         printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence());
139 }
140
141 void fderivative::do_print_tree(const print_tree & c, unsigned level) const
142 {
143         c.s << std::string(level, ' ') << class_name() << " "
144             << registered_functions()[serial].name << " @" << this
145             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
146             << ", nops=" << nops()
147             << ", params=";
148         auto i = parameter_set.begin(), end = parameter_set.end();
149         --end;
150         while (i != end)
151                 c.s << *i++ << ",";
152         c.s << *i << std::endl;
153         for (auto & i : seq)
154                 i.print(c, level + c.delta_indent);
155         c.s << std::string(level + c.delta_indent, ' ') << "=====" << std::endl;
156 }
157
158 ex fderivative::eval() const
159 {
160         // No parameters specified? Then return the function itself
161         if (parameter_set.empty())
162                 return function(serial, seq);
163
164         // If the function in question actually has a derivative, return it
165         if (registered_functions()[serial].has_derivative() && parameter_set.size() == 1)
166                 return pderivative(*(parameter_set.begin()));
167
168         return this->hold();
169 }
170
171 /** The series expansion of derivatives falls back to Taylor expansion.
172  *  @see basic::series */
173 ex fderivative::series(const relational & r, int order, unsigned options) const
174 {
175         return basic::series(r, order, options);
176 }
177
178 ex fderivative::thiscontainer(const exvector & v) const
179 {
180         return fderivative(serial, parameter_set, v);
181 }
182
183 ex fderivative::thiscontainer(exvector && v) const
184 {
185         return fderivative(serial, parameter_set, std::move(v));
186 }
187
188 /** Implementation of ex::diff() for derivatives. It applies the chain rule.
189  *  @see ex::diff */
190 ex fderivative::derivative(const symbol & s) const
191 {
192         ex result;
193         for (size_t i=0; i<seq.size(); i++) {
194                 ex arg_diff = seq[i].diff(s);
195                 if (!arg_diff.is_zero()) {
196                         paramset ps = parameter_set;
197                         ps.insert(i);
198                         result += arg_diff * fderivative(serial, ps, seq);
199                 }
200         }
201         return result;
202 }
203
204 int fderivative::compare_same_type(const basic & other) const
205 {
206         GINAC_ASSERT(is_a<fderivative>(other));
207         const fderivative & o = static_cast<const fderivative &>(other);
208
209         if (parameter_set != o.parameter_set)
210                 return parameter_set < o.parameter_set ? -1 : 1;
211         else
212                 return inherited::compare_same_type(o);
213 }
214
215 bool fderivative::is_equal_same_type(const basic & other) const
216 {
217         GINAC_ASSERT(is_a<fderivative>(other));
218         const fderivative & o = static_cast<const fderivative &>(other);
219
220         if (parameter_set != o.parameter_set)
221                 return false;
222         else
223                 return inherited::is_equal_same_type(o);
224 }
225
226 bool fderivative::match_same_type(const basic & other) const
227 {
228         GINAC_ASSERT(is_a<fderivative>(other));
229         const fderivative & o = static_cast<const fderivative &>(other);
230
231         return parameter_set == o.parameter_set && inherited::match_same_type(other);
232 }
233
234 /** Expose this object's derivative structure.
235  *
236  *  Parameter numbers occurring more than once stand for repeated
237  *  differentiation with respect to that parameter. If a symbolic function
238  *  f(x,y) is differentiated with respect to x, this method will return {0}.
239  *  If f(x,y) is differentiated twice with respect to y, it will return {1,1}.
240  *  (This corresponds to the way this object is printed.)
241  *
242  *  @return  multiset of function's parameter numbers that are abstractly
243  *  differentiated. */
244 const paramset& fderivative::derivatives() const
245 {
246         return parameter_set;
247 }
248
249
250 } // namespace GiNaC