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
28 #include "expairseq.h"
35 #include "relational.h"
39 /** Default implementation of ex::diff(). It prints and error message and returns a fail object.
41 ex basic::diff(symbol const & s) const
43 throw(std::logic_error("differentiation not supported by this type"));
47 /** Implementation of ex::diff() for a numeric. It always returns 0.
50 ex numeric::diff(symbol const & s) const
56 /** Implementation of ex::diff() for single differentiation of a symbol.
60 ex symbol::diff(symbol const & s) const
62 if (compare_same_type(s)) {
69 /** Implementation of ex::diff() for a constant. It always returns 0.
72 ex constant::diff(symbol const & s) const
77 /** Implementation of ex::diff() for multiple differentiation of a symbol.
78 * It returns the symbol, 1 or 0.
80 * @param nth order of differentiation
82 ex symbol::diff(symbol const & s, unsigned nth) const
84 if (compare_same_type(s)) {
101 /** Implementation of ex::diff() for an indexed object. It always returns 0.
103 ex indexed::diff(symbol const & s) const
109 /** Implementation of ex::diff() for an expairseq. It differentiates all elements of the sequence.
111 ex expairseq::diff(symbol const & s) const
113 return thisexpairseq(diffchildren(s),overall_coeff);
117 /** Implementation of ex::diff() for a sum. It differentiates each term.
119 ex add::diff(symbol const & s) const
121 // D(a+b+c)=D(a)+D(b)+D(c)
122 return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
126 /** Implementation of ex::diff() for a product. It applies the product rule.
128 ex mul::diff(symbol const & s) const
131 new_seq.reserve(seq.size());
133 // D(a*b*c)=D(a)*b*c+a*D(b)*c+a*b*D(c)
134 for (unsigned i=0; i!=seq.size(); i++) {
135 epvector sub_seq=seq;
136 sub_seq[i] = split_ex_to_pair(sub_seq[i].coeff*
137 power(sub_seq[i].rest,sub_seq[i].coeff-1)*
138 sub_seq[i].rest.diff(s));
139 new_seq.push_back((new mul(sub_seq,overall_coeff))->setflag(status_flags::dynallocated));
141 return (new add(new_seq))->setflag(status_flags::dynallocated);
145 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
147 ex ncmul::diff(symbol const & s) const
153 /** Implementation of ex::diff() for a power.
155 ex power::diff(symbol const & s) const
157 if (exponent.info(info_flags::real)) {
158 // D(b^r) = r * b^(r-1) * D(b) (faster than the formula below)
159 return mul(mul(exponent, power(basis, exponent - exONE())), basis.diff(s));
161 // D(b^e) = b^e * (D(e)*ln(b) + e*D(b)/b)
162 return mul(power(basis, exponent),
163 add(mul(exponent.diff(s), log(basis)),
164 mul(mul(exponent, basis.diff(s)), power(basis, -1))));
169 /** Implementation of ex::diff() for functions. It applies the chain rule,
170 * except for the Order term function.
172 ex function::diff(symbol const & s) const
176 if (serial == function_index_Order) {
178 // Order Term function only differentiates the argument
179 return Order(seq[0].diff(s));
184 for (unsigned i=0; i!=seq.size(); i++) {
185 new_seq.push_back(mul(pdiff(i), seq[i].diff(s)));
192 /** Implementation of ex::diff() for a power-series. It treats the series as a polynomial.
194 ex series::diff(symbol const & s) const
198 epvector::const_iterator it = seq.begin(), itend = seq.end();
200 //!! coeff might depend on var
201 while (it != itend) {
202 if (is_order_function(it->rest)) {
203 new_seq.push_back(expair(it->rest, it->coeff - 1));
205 ex c = it->rest * it->coeff;
207 new_seq.push_back(expair(c, it->coeff - 1));
211 return series(var, point, new_seq);
218 /** Compute partial derivative of an expression.
220 * @param s symbol by which the expression is derived
221 * @param nth order of derivative (default 1)
222 * @return partial derivative as a new expression */
224 ex ex::diff(symbol const & s, unsigned nth) const
232 ex ndiff = bp->diff(s);
234 ndiff = ndiff.diff(s);