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"
42 /** Default implementation of ex::diff(). It prints and error message and returns a fail object.
44 ex basic::diff(symbol const & s) const
46 throw(std::logic_error("differentiation not supported by this type"));
50 /** Implementation of ex::diff() for a numeric. It always returns 0.
53 ex numeric::diff(symbol const & s) const
59 /** Implementation of ex::diff() for single differentiation of a symbol.
63 ex symbol::diff(symbol const & s) const
65 if (compare_same_type(s)) {
72 /** Implementation of ex::diff() for a constant. It always returns 0.
75 ex constant::diff(symbol const & s) const
80 /** Implementation of ex::diff() for multiple differentiation of a symbol.
81 * It returns the symbol, 1 or 0.
83 * @param nth order of differentiation
85 ex symbol::diff(symbol const & s, unsigned nth) const
87 if (compare_same_type(s)) {
104 /** Implementation of ex::diff() for an indexed object. It always returns 0.
106 ex indexed::diff(symbol const & s) const
112 /** Implementation of ex::diff() for an expairseq. It differentiates all elements of the sequence.
114 ex expairseq::diff(symbol const & s) const
116 return thisexpairseq(diffchildren(s),overall_coeff);
120 /** Implementation of ex::diff() for a sum. It differentiates each term.
122 ex add::diff(symbol const & s) const
124 // D(a+b+c)=D(a)+D(b)+D(c)
125 return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
129 /** Implementation of ex::diff() for a product. It applies the product rule.
131 ex mul::diff(symbol const & s) const
134 new_seq.reserve(seq.size());
136 // D(a*b*c)=D(a)*b*c+a*D(b)*c+a*b*D(c)
137 for (unsigned i=0; i!=seq.size(); i++) {
138 epvector sub_seq=seq;
139 sub_seq[i] = split_ex_to_pair(sub_seq[i].coeff*
140 power(sub_seq[i].rest,sub_seq[i].coeff-1)*
141 sub_seq[i].rest.diff(s));
142 new_seq.push_back((new mul(sub_seq,overall_coeff))->setflag(status_flags::dynallocated));
144 return (new add(new_seq))->setflag(status_flags::dynallocated);
148 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
150 ex ncmul::diff(symbol const & s) const
156 /** Implementation of ex::diff() for a power.
158 ex power::diff(symbol const & s) const
160 if (exponent.info(info_flags::real)) {
161 // D(b^r) = r * b^(r-1) * D(b) (faster than the formula below)
162 return mul(mul(exponent, power(basis, exponent - exONE())), basis.diff(s));
164 // D(b^e) = b^e * (D(e)*ln(b) + e*D(b)/b)
165 return mul(power(basis, exponent),
166 add(mul(exponent.diff(s), log(basis)),
167 mul(mul(exponent, basis.diff(s)), power(basis, -1))));
172 /** Implementation of ex::diff() for functions. It applies the chain rule,
173 * except for the Order term function.
175 ex function::diff(symbol const & s) const
179 if (serial == function_index_Order) {
181 // Order Term function only differentiates the argument
182 return Order(seq[0].diff(s));
187 for (unsigned i=0; i!=seq.size(); i++) {
188 new_seq.push_back(mul(pdiff(i), seq[i].diff(s)));
195 /** Implementation of ex::diff() for a power-series. It treats the series as a polynomial.
197 ex series::diff(symbol const & s) const
201 epvector::const_iterator it = seq.begin(), itend = seq.end();
203 // FIXME: coeff might depend on var
204 while (it != itend) {
205 if (is_order_function(it->rest)) {
206 new_seq.push_back(expair(it->rest, it->coeff - 1));
208 ex c = it->rest * it->coeff;
210 new_seq.push_back(expair(c, it->coeff - 1));
214 return series(var, point, new_seq);
221 /** Compute partial derivative of an expression.
223 * @param s symbol by which the expression is derived
224 * @param nth order of derivative (default 1)
225 * @return partial derivative as a new expression */
227 ex ex::diff(symbol const & s, unsigned nth) const
235 ex ndiff = bp->diff(s);
237 ndiff = ndiff.diff(s);