3 * Implementation of GiNaC's symbolic integral. */
6 * GiNaC Copyright (C) 1999-2018 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "registrar.h"
34 #include "operators.h"
35 #include "relational.h"
41 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(integral, basic,
42 print_func<print_dflt>(&integral::do_print).
43 print_func<print_latex>(&integral::do_print_latex))
47 // default constructor
51 : x(dynallocate<symbol>())
60 integral::integral(const ex & x_, const ex & a_, const ex & b_, const ex & f_)
61 : x(x_), a(a_), b(b_), f(f_)
63 if (!is_a<symbol>(x)) {
64 throw(std::invalid_argument("first argument of integral must be of type symbol"));
72 void integral::read_archive(const archive_node& n, lst& sym_lst)
74 inherited::read_archive(n, sym_lst);
75 n.find_ex("x", x, sym_lst);
76 n.find_ex("a", a, sym_lst);
77 n.find_ex("b", b, sym_lst);
78 n.find_ex("f", f, sym_lst);
81 void integral::archive(archive_node & n) const
83 inherited::archive(n);
91 // functions overriding virtual functions from base classes
94 void integral::do_print(const print_context & c, unsigned level) const
107 void integral::do_print_latex(const print_latex & c, unsigned level) const
109 string varname = ex_to<symbol>(x).get_name();
110 if (level > precedence())
117 if (varname.size() > 1)
118 c.s << "\\," << varname << "\\:";
120 c.s << varname << "\\,";
121 f.print(c,precedence());
122 if (level > precedence())
126 int integral::compare_same_type(const basic & other) const
128 GINAC_ASSERT(is_exactly_a<integral>(other));
129 const integral &o = static_cast<const integral &>(other);
131 int cmpval = x.compare(o.x);
134 cmpval = a.compare(o.a);
137 cmpval = b.compare(o.b);
140 return f.compare(o.f);
143 ex integral::eval() const
145 if (flags & status_flags::evaluated)
148 if (!f.has(x) && !haswild(f))
157 ex integral::evalf() const
163 // 12.34 is just an arbitrary number used to check whether a number
164 // results after substituting a number for the integration variable.
165 if (is_exactly_a<numeric>(ea) && is_exactly_a<numeric>(eb) &&
166 is_exactly_a<numeric>(ef.subs(x==12.34).evalf())) {
167 return adaptivesimpson(x, ea, eb, ef);
170 if (are_ex_trivially_equal(a, ea) && are_ex_trivially_equal(b, eb) &&
171 are_ex_trivially_equal(f, ef))
174 return dynallocate<integral>(x, ea, eb, ef);
177 int integral::max_integration_level = 15;
178 ex integral::relative_integration_error = 1e-8;
180 ex subsvalue(const ex & var, const ex & value, const ex & fun)
182 ex result = fun.subs(var==value).evalf();
183 if (is_a<numeric>(result))
185 throw logic_error("integrand does not evaluate to numeric");
188 struct error_and_integral
190 error_and_integral(const ex &err, const ex &integ)
191 :error(err), integral(integ){}
196 struct error_and_integral_is_less
198 bool operator()(const error_and_integral &e1,const error_and_integral &e2) const
200 int c = e1.integral.compare(e2.integral);
205 return ex_is_less()(e1.error, e2.error);
209 typedef map<error_and_integral, ex, error_and_integral_is_less> lookup_map;
211 /** Numeric integration routine based upon the "Adaptive Quadrature" one
212 * in "Numerical Analysis" by Burden and Faires. Parameters are integration
213 * variable, left boundary, right boundary, function to be integrated and
214 * the relative integration error. The function should evalf into a number
215 * after substituting the integration variable by a number. Another thing
216 * to note is that this implementation is no good at integrating functions
217 * with discontinuities. */
218 ex adaptivesimpson(const ex & x, const ex & a_in, const ex & b_in, const ex & f, const ex & error)
220 // Check whether boundaries and error are numbers.
221 ex a = is_exactly_a<numeric>(a_in) ? a_in : a_in.evalf();
222 ex b = is_exactly_a<numeric>(b_in) ? b_in : b_in.evalf();
223 if(!is_exactly_a<numeric>(a) || !is_exactly_a<numeric>(b))
224 throw std::runtime_error("For numerical integration the boundaries of the integral should evalf into numbers.");
225 if(!is_exactly_a<numeric>(error))
226 throw std::runtime_error("For numerical integration the error should be a number.");
228 // Use lookup table to be potentially much faster.
229 static lookup_map lookup;
230 static symbol ivar("ivar");
231 ex lookupex = integral(ivar,a,b,f.subs(x==ivar));
232 auto emi = lookup.find(error_and_integral(error, lookupex));
233 if (emi!=lookup.end())
238 exvector avec(integral::max_integration_level+1);
239 exvector hvec(integral::max_integration_level+1);
240 exvector favec(integral::max_integration_level+1);
241 exvector fbvec(integral::max_integration_level+1);
242 exvector fcvec(integral::max_integration_level+1);
243 exvector svec(integral::max_integration_level+1);
244 exvector errorvec(integral::max_integration_level+1);
245 vector<int> lvec(integral::max_integration_level+1);
249 favec[i] = subsvalue(x, a, f);
250 fcvec[i] = subsvalue(x, a+hvec[i], f);
251 fbvec[i] = subsvalue(x, b, f);
252 svec[i] = hvec[i]*(favec[i]+4*fcvec[i]+fbvec[i])/3;
254 errorvec[i] = error*abs(svec[i]);
257 ex fd = subsvalue(x, avec[i]+hvec[i]/2, f);
258 ex fe = subsvalue(x, avec[i]+3*hvec[i]/2, f);
259 ex s1 = hvec[i]*(favec[i]+4*fd+fcvec[i])/6;
260 ex s2 = hvec[i]*(fcvec[i]+4*fe+fbvec[i])/6;
266 // hopefully prevents a crash if the function is zero sometimes.
267 ex nu6 = max(errorvec[i], abs(s1+s2)*error);
271 if (abs(ex_to<numeric>(s1+s2-nu7)) <= nu6)
274 if (nu8>=integral::max_integration_level)
275 throw runtime_error("max integration level reached");
291 errorvec[i]=errorvec[i-1];
297 lookup[error_and_integral(error, lookupex)]=app;
301 int integral::degree(const ex & s) const
303 return ((b-a)*f).degree(s);
306 int integral::ldegree(const ex & s) const
308 return ((b-a)*f).ldegree(s);
311 ex integral::eval_ncmul(const exvector & v) const
313 return f.eval_ncmul(v);
316 size_t integral::nops() const
321 ex integral::op(size_t i) const
335 throw (std::out_of_range("integral::op() out of range"));
339 ex & integral::let_op(size_t i)
341 ensure_if_modifiable();
352 throw (std::out_of_range("integral::let_op() out of range"));
356 ex integral::expand(unsigned options) const
358 if (options==0 && (flags & status_flags::expanded))
361 ex newa = a.expand(options);
362 ex newb = b.expand(options);
363 ex newf = f.expand(options);
365 if (is_a<add>(newf)) {
367 v.reserve(newf.nops());
368 for (size_t i=0; i<newf.nops(); ++i)
369 v.push_back(integral(x, newa, newb, newf.op(i)).expand(options));
370 return ex(add(v)).expand(options);
373 if (is_a<mul>(newf)) {
376 for (size_t i=0; i<newf.nops(); ++i)
377 if (newf.op(i).has(x))
380 prefactor *= newf.op(i);
382 return (prefactor*integral(x, newa, newb, rest)).expand(options);
385 if (are_ex_trivially_equal(a, newa) && are_ex_trivially_equal(b, newb) &&
386 are_ex_trivially_equal(f, newf)) {
388 this->setflag(status_flags::expanded);
392 const integral & newint = dynallocate<integral>(x, newa, newb, newf);
394 newint.setflag(status_flags::expanded);
398 ex integral::derivative(const symbol & s) const
401 throw(logic_error("differentiation with respect to dummy variable"));
402 return b.diff(s)*f.subs(x==b)-a.diff(s)*f.subs(x==a)+integral(x, a, b, f.diff(s));
405 unsigned integral::return_type() const
407 return f.return_type();
410 return_type_t integral::return_type_tinfo() const
412 return f.return_type_tinfo();
415 ex integral::conjugate() const
417 ex conja = a.conjugate();
418 ex conjb = b.conjugate();
419 ex conjf = f.conjugate().subs(x.conjugate()==x);
421 if (are_ex_trivially_equal(a, conja) && are_ex_trivially_equal(b, conjb) &&
422 are_ex_trivially_equal(f, conjf))
425 return dynallocate<integral>(x, conja, conjb, conjf);
428 ex integral::eval_integ() const
430 if (!(flags & status_flags::expanded))
431 return this->expand().eval_integ();
435 if (is_a<power>(f) && f.op(0)==x) {
438 if (!f.op(1).has(x)) {
439 ex primit = power(x,f.op(1)+1)/(f.op(1)+1);
440 return primit.subs(x==b)-primit.subs(x==a);
447 GINAC_BIND_UNARCHIVER(integral);