3 * Implementation of symbolic differentiation in all of GiNaC's classes. */
6 * GiNaC Copyright (C) 1999 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
29 #include "expairseq.h"
36 #include "relational.h"
40 /** Default implementation of ex::diff(). It prints and error message and returns a fail object.
42 ex basic::diff(symbol const & s) const
44 throw(std::logic_error("differentiation not supported by this type"));
48 /** Implementation of ex::diff() for a numeric. It always returns 0.
51 ex numeric::diff(symbol const & s) const
57 /** Implementation of ex::diff() for single differentiation of a symbol.
61 ex symbol::diff(symbol const & s) const
63 if (compare_same_type(s)) {
70 /** Implementation of ex::diff() for a constant. It always returns 0.
73 ex constant::diff(symbol const & s) const
78 /** Implementation of ex::diff() for multiple differentiation of a symbol.
79 * It returns the symbol, 1 or 0.
81 * @param nth order of differentiation
83 ex symbol::diff(symbol const & s, unsigned nth) const
85 if (compare_same_type(s)) {
102 /** Implementation of ex::diff() for an indexed object. It always returns 0.
104 ex indexed::diff(symbol const & s) const
110 /** Implementation of ex::diff() for an expairseq. It differentiates all elements of the sequence.
112 ex expairseq::diff(symbol const & s) const
114 return thisexpairseq(diffchildren(s),overall_coeff);
118 /** Implementation of ex::diff() for a sum. It differentiates each term.
120 ex add::diff(symbol const & s) const
122 // D(a+b+c)=D(a)+D(b)+D(c)
123 return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
127 /** Implementation of ex::diff() for a product. It applies the product rule.
129 ex mul::diff(symbol const & s) const
132 new_seq.reserve(seq.size());
134 // D(a*b*c)=D(a)*b*c+a*D(b)*c+a*b*D(c)
135 for (unsigned i=0; i!=seq.size(); i++) {
136 epvector sub_seq=seq;
137 sub_seq[i] = split_ex_to_pair(sub_seq[i].coeff*
138 power(sub_seq[i].rest,sub_seq[i].coeff-1)*
139 sub_seq[i].rest.diff(s));
140 new_seq.push_back((new mul(sub_seq,overall_coeff))->setflag(status_flags::dynallocated));
142 return (new add(new_seq))->setflag(status_flags::dynallocated);
146 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
148 ex ncmul::diff(symbol const & s) const
154 /** Implementation of ex::diff() for a power.
156 ex power::diff(symbol const & s) const
158 if (exponent.info(info_flags::real)) {
159 // D(b^r) = r * b^(r-1) * D(b) (faster than the formula below)
160 return mul(mul(exponent, power(basis, exponent - exONE())), basis.diff(s));
162 // D(b^e) = b^e * (D(e)*ln(b) + e*D(b)/b)
163 return mul(power(basis, exponent),
164 add(mul(exponent.diff(s), log(basis)),
165 mul(mul(exponent, basis.diff(s)), power(basis, -1))));
170 /** Implementation of ex::diff() for functions. It applies the chain rule,
171 * except for the Order term function.
173 ex function::diff(symbol const & s) const
177 if (serial == function_index_Order) {
179 // Order Term function only differentiates the argument
180 return Order(seq[0].diff(s));
185 for (unsigned i=0; i!=seq.size(); i++) {
186 new_seq.push_back(mul(pdiff(i), seq[i].diff(s)));
193 /** Implementation of ex::diff() for a power-series. It treats the series as a polynomial.
195 ex series::diff(symbol const & s) const
199 epvector::const_iterator it = seq.begin(), itend = seq.end();
201 //!! coeff might depend on var
202 while (it != itend) {
203 if (is_order_function(it->rest)) {
204 new_seq.push_back(expair(it->rest, it->coeff - 1));
206 ex c = it->rest * it->coeff;
208 new_seq.push_back(expair(c, it->coeff - 1));
212 return series(var, point, new_seq);
219 /** Compute partial derivative of an expression.
221 * @param s symbol by which the expression is derived
222 * @param nth order of derivative (default 1)
223 * @return partial derivative as a new expression */
225 ex ex::diff(symbol const & s, unsigned nth) const
233 ex ndiff = bp->diff(s);
235 ndiff = ndiff.diff(s);