3 * Implementation of symbolic differentiation in all of GiNaC's classes.
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /** Default implementation of ex::diff(). It prints and error message and returns a fail object.
28 ex basic::diff(symbol const & s) const
30 throw(std::logic_error("differentiation not supported by this type"));
34 /** Implementation of ex::diff() for a numeric. It always returns 0.
37 ex numeric::diff(symbol const & s) const
43 /** Implementation of ex::diff() for single differentiation of a symbol.
47 ex symbol::diff(symbol const & s) const
49 if (compare_same_type(s)) {
56 /** Implementation of ex::diff() for a constant. It always returns 0.
59 ex constant::diff(symbol const & s) const
64 /** Implementation of ex::diff() for multiple differentiation of a symbol.
65 * It returns the symbol, 1 or 0.
67 * @param nth order of differentiation
69 ex symbol::diff(symbol const & s, unsigned nth) const
71 if (compare_same_type(s)) {
88 /** Implementation of ex::diff() for an indexed object. It always returns 0.
90 ex indexed::diff(symbol const & s) const
96 /** Implementation of ex::diff() for an expairseq. It differentiates all elements of the sequence.
98 ex expairseq::diff(symbol const & s) const
100 return thisexpairseq(diffchildren(s),overall_coeff);
104 /** Implementation of ex::diff() for a sum. It differentiates each term.
106 ex add::diff(symbol const & s) const
108 // D(a+b+c)=D(a)+D(b)+D(c)
109 return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
113 /** Implementation of ex::diff() for a product. It applies the product rule.
115 ex mul::diff(symbol const & s) const
118 new_seq.reserve(seq.size());
120 // D(a*b*c)=D(a)*b*c+a*D(b)*c+a*b*D(c)
121 for (unsigned i=0; i!=seq.size(); i++) {
122 epvector sub_seq=seq;
123 sub_seq[i] = split_ex_to_pair(sub_seq[i].coeff*
124 power(sub_seq[i].rest,sub_seq[i].coeff-1)*
125 sub_seq[i].rest.diff(s));
126 new_seq.push_back((new mul(sub_seq,overall_coeff))->setflag(status_flags::dynallocated));
128 return (new add(new_seq))->setflag(status_flags::dynallocated);
132 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
134 ex ncmul::diff(symbol const & s) const
140 /** Implementation of ex::diff() for a power.
142 ex power::diff(symbol const & s) const
144 if (exponent.info(info_flags::real)) {
145 // D(b^r) = r * b^(r-1) * D(b) (faster than the formula below)
146 return mul(mul(exponent, power(basis, exponent - exONE())), basis.diff(s));
148 // D(b^e) = b^e * (D(e)*ln(b) + e*D(b)/b)
149 return mul(power(basis, exponent),
150 add(mul(exponent.diff(s), log(basis)),
151 mul(mul(exponent, basis.diff(s)), power(basis, -1))));
156 /** Implementation of ex::diff() for functions. It applies the chain rule,
157 * except for the Order term function.
159 ex function::diff(symbol const & s) const
163 if (serial == function_index_Order) {
165 // Order Term function only differentiates the argument
166 return Order(seq[0].diff(s));
171 for (unsigned i=0; i!=seq.size(); i++) {
172 new_seq.push_back(mul(pdiff(i), seq[i].diff(s)));
179 /** Implementation of ex::diff() for a power-series. It treats the series as a polynomial.
181 ex series::diff(symbol const & s) const
185 epvector::const_iterator it = seq.begin(), itend = seq.end();
187 //!! coeff might depend on var
188 while (it != itend) {
189 if (is_order_function(it->rest)) {
190 new_seq.push_back(expair(it->rest, it->coeff - 1));
192 ex c = it->rest * it->coeff;
194 new_seq.push_back(expair(c, it->coeff - 1));
198 return series(var, point, new_seq);
205 /** Compute partial derivative of an expression.
207 * @param s symbol by which the expression is derived
208 * @param nth order of derivative (default 1)
209 * @return partial derivative as a new expression */
211 ex ex::diff(symbol const & s, unsigned nth) const
219 ex ndiff = bp->diff(s);
221 ndiff = ndiff.diff(s);