GiNaC 1.8.10
fderivative.cpp
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2026 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include "fderivative.h"
23#include "operators.h"
24#include "archive.h"
25#include "utils.h"
26
27namespace GiNaC {
28
31 print_func<print_latex>(&fderivative::do_print_latex).
32 print_func<print_csrc>(&fderivative::do_print_csrc).
33 print_func<print_tree>(&fderivative::do_print_tree))
34
35
36// default constructor
38
40{
41}
42
44// other constructors
46
47fderivative::fderivative(unsigned ser, unsigned param, const exvector & args) : function(ser, args)
48{
49 parameter_set.insert(param);
50}
51
52fderivative::fderivative(unsigned ser, const paramset & params, const exvector & args) : function(ser, args), parameter_set(params)
53{
54}
55
56fderivative::fderivative(unsigned ser, const paramset & params, exvector && v) : function(ser, std::move(v)), parameter_set(params)
57{
58}
59
61// archiving
63
65{
66 inherited::read_archive(n, sym_lst);
67 unsigned i = 0;
68 while (true) {
69 unsigned u;
70 if (n.find_unsigned("param", u, i))
71 parameter_set.insert(u);
72 else
73 break;
74 ++i;
75 }
76}
78
80{
81 inherited::archive(n);
82 auto i = parameter_set.begin(), end = parameter_set.end();
83 while (i != end) {
84 n.add_unsigned("param", *i);
85 ++i;
86 }
87}
88
89
91// functions overriding virtual functions from base classes
93
94void fderivative::print(const print_context & c, unsigned level) const
95{
96 // class function overrides print(), but we don't want that
97 basic::print(c, level);
98}
99
100void fderivative::do_print(const print_context & c, unsigned level) const
101{
102 c.s << "D[";
103 auto i = parameter_set.begin(), end = parameter_set.end();
104 --end;
105 while (i != end) {
106 c.s << *i++ << ",";
107 }
108 c.s << *i << "](" << registered_functions()[serial].name << ")";
110}
111
112void fderivative::do_print_latex(const print_context & c, unsigned level) const
113{
114 int order=1;
115 c.s << "\\partial_{";
116 auto i = parameter_set.begin(), end = parameter_set.end();
117 --end;
118 while (i != end) {
119 ++order;
120 c.s << *i++ << ",";
121 }
122 c.s << *i << "}";
123 if (order>1)
124 c.s << "^{" << order << "}";
125 c.s << "(" << registered_functions()[serial].TeX_name << ")";
127}
128
129void fderivative::do_print_csrc(const print_csrc & c, unsigned level) const
130{
131 c.s << "D_";
132 auto i = parameter_set.begin(), end = parameter_set.end();
133 --end;
134 while (i != end)
135 c.s << *i++ << "_";
136 c.s << *i << "_" << registered_functions()[serial].name;
138}
139
140void fderivative::do_print_tree(const print_tree & c, unsigned level) const
141{
142 c.s << std::string(level, ' ') << class_name() << " "
143 << registered_functions()[serial].name << " @" << this
144 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
145 << ", nops=" << nops()
146 << ", params=";
147 auto i = parameter_set.begin(), end = parameter_set.end();
148 --end;
149 while (i != end)
150 c.s << *i++ << ",";
151 c.s << *i << std::endl;
152 for (auto & i : seq)
153 i.print(c, level + c.delta_indent);
154 c.s << std::string(level + c.delta_indent, ' ') << "=====" << std::endl;
155}
156
158{
159 // No parameters specified? Then return the function itself
160 if (parameter_set.empty())
161 return function(serial, seq);
162
163 // If the function in question actually has a derivative, return it
164 if (registered_functions()[serial].has_derivative() && parameter_set.size() == 1)
165 return pderivative(*(parameter_set.begin()));
166
167 return this->hold();
168}
169
172ex fderivative::series(const relational & r, int order, unsigned options) const
173{
174 return basic::series(r, order, options);
175}
176
178{
179 return fderivative(serial, parameter_set, v);
180}
181
183{
184 return fderivative(serial, parameter_set, std::move(v));
185}
186
190{
191 ex result;
192 for (size_t i=0; i<seq.size(); i++) {
193 ex arg_diff = seq[i].diff(s);
194 if (!arg_diff.is_zero()) {
196 ps.insert(i);
197 result += arg_diff * fderivative(serial, ps, seq);
198 }
199 }
200 return result;
201}
202
203int fderivative::compare_same_type(const basic & other) const
204{
205 GINAC_ASSERT(is_a<fderivative>(other));
206 const fderivative & o = static_cast<const fderivative &>(other);
207
209 return parameter_set < o.parameter_set ? -1 : 1;
210 else
211 return inherited::compare_same_type(o);
212}
213
214bool fderivative::is_equal_same_type(const basic & other) const
215{
216 GINAC_ASSERT(is_a<fderivative>(other));
217 const fderivative & o = static_cast<const fderivative &>(other);
218
220 return false;
221 else
222 return inherited::is_equal_same_type(o);
223}
224
225bool fderivative::match_same_type(const basic & other) const
226{
227 GINAC_ASSERT(is_a<fderivative>(other));
228 const fderivative & o = static_cast<const fderivative &>(other);
229
230 return parameter_set == o.parameter_set && inherited::match_same_type(other);
231}
232
244{
245 return parameter_set;
246}
247
248
249} // namespace GiNaC
Archiving of GiNaC expressions.
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition assertion.h:32
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition archive.h:48
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition basic.h:104
unsigned hashvalue
hash value
Definition basic.h:302
unsigned flags
of type status_flags
Definition basic.h:301
virtual void print(const print_context &c, unsigned level=0) const
Output to stream.
Definition basic.cpp:115
const basic & hold() const
Stop further evaluation.
Definition basic.cpp:886
virtual ex series(const relational &r, int order, unsigned options=0) const
Default implementation of ex::series().
Definition pseries.cpp:610
virtual int compare_same_type(const basic &other) const
Returns order relation between two objects of same type.
Definition basic.cpp:718
Wrapper template for making GiNaC classes out of STL containers.
Definition container.h:72
virtual void printseq(const print_context &c, char openbracket, char delim, char closebracket, unsigned this_precedence, unsigned upper_precedence=0) const
Print sequence of contained elements.
Definition container.h:450
const_iterator end() const
Definition container.h:239
size_t nops() const override
Number of operands/members.
Definition container.h:117
unsigned precedence() const override
Return relative operator precedence (for parenthezing output).
Definition container.h:116
Lightweight wrapper for GiNaC's symbolic objects.
Definition ex.h:72
ex diff(const symbol &s, unsigned nth=1) const
Compute partial derivative of an expression.
Definition ex.cpp:86
bool is_zero() const
Definition ex.h:213
This class represents the (abstract) derivative of a symbolic function.
Definition fderivative.h:37
void do_print_latex(const print_context &c, unsigned level) const
const paramset & derivatives() const
Expose this object's derivative structure.
void do_print(const print_context &c, unsigned level) const
ex derivative(const symbol &s) const override
Implementation of ex::diff() for derivatives.
bool match_same_type(const basic &other) const override
Returns true if the attributes of two objects are similar enough for a match.
ex series(const relational &r, int order, unsigned options=0) const override
The series expansion of derivatives falls back to Taylor expansion.
ex thiscontainer(const exvector &v) const override
fderivative(unsigned ser, unsigned param, const exvector &args)
Construct derivative with respect to one parameter.
void do_print_tree(const print_tree &c, unsigned level) const
ex eval() const override
Perform automatic non-interruptive term rewriting rules.
bool is_equal_same_type(const basic &other) const override
Returns true if two objects of same type are equal.
paramset parameter_set
Set of parameter numbers with respect to which to take the derivative.
Definition fderivative.h:84
void do_print_csrc(const print_csrc &c, unsigned level) const
void print(const print_context &c, unsigned level=0) const override
Output to stream.
void archive(archive_node &n) const override
Archive the object.
void read_archive(const archive_node &n, lst &syms) override
Load (deserialize) the object from an archive node.
The class function is used to implement builtin functions like sin, cos... and user defined functions...
Definition function.h:673
unsigned serial
Definition function.h:750
static std::vector< function_options > & registered_functions()
unsigned precedence() const override
Return relative operator precedence (for parenthezing output).
Definition function.h:706
ex pderivative(unsigned diff_param) const
Base class for print_contexts.
Definition print.h:101
Base context for C source output.
Definition print.h:156
Context for tree-like output for debugging.
Definition print.h:145
This class holds a relation consisting of two expressions and a logical relation between them.
Definition relational.h:34
Basic CAS symbol.
Definition symbol.h:38
unsigned options
Definition factor.cpp:2473
size_t n
Definition factor.cpp:1431
size_t c
Definition factor.cpp:756
size_t r
Definition factor.cpp:756
Interface to abstract derivatives of functions.
Definition add.cpp:35
std::multiset< unsigned > paramset
Definition fderivative.h:31
print_func< print_context >(&varidx::do_print). print_func< print_latex >(&varidx
Definition idx.cpp:43
GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(lst, basic, print_func< print_context >(&lst::do_print). print_func< print_tree >(&lst::do_print_tree)) template<> bool lst GINAC_BIND_UNARCHIVER(lst)
Specialization of container::info() for lst.
Definition lst.cpp:41
std::vector< ex > exvector
Definition basic.h:47
Definition ex.h:987
Interface to GiNaC's overloaded operators.
#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options)
Macro for inclusion in the implementation of each registered class.
Definition registrar.h:183
Interface to several small and furry utilities needed within GiNaC but not of any interest to the use...

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.