1 | | ~~Sorry forgot the brackets....~~ |

2 | | |

3 | | |

4 | | ~~{{{~~ |

5 | | ~~def _evalf_(self, f, x, a, b, parent = None): ~~ |

6 | | ~~ """~~ |

7 | | ~~ Returns numerical approximation of the integral~~ |

8 | | ~~ ~~ |

9 | | ~~ EXAMPLES::~~ |

10 | | |

11 | | ~~ sage: from sage.symbolic.integration.integral import definite_integral~~ |

12 | | ~~ sage: h = definite_integral(sin(x)/x^2, x, 1, 2); h~~ |

13 | | ~~ integrate(sin(x)/x^2, x, 1, 2)~~ |

14 | | ~~ sage: h.n() # indirect doctest~~ |

15 | | ~~ 0.4723991772689525...~~ |

16 | | |

17 | | ~~ TESTS:~~ |

18 | | |

19 | | ~~ Check if #3863 is fixed::~~ |

20 | | |

21 | | ~~ sage: integrate(x^2.7 * e^(-2.4*x), x, 0, 3).n()~~ |

22 | | ~~ 0.154572952320790~~ |

23 | | |

24 | | ~~ #Check if #8321 is fixed:~~ |

25 | | |

26 | | ~~ sage: d = definite_integral(sin(x)/x^2, x, 1, 2)~~ |

27 | | ~~ sage: d.n(77)~~ |

28 | | ~~ 0.4723991772689525199904~~ |

29 | | ~~ """~~ |

30 | | ~~ #from sage.gsl.integration import numerical_integral~~ |

31 | | ~~ # The gsl routine returns a tuple, which also contains the error.~~ |

32 | | ~~ # We only return the result.~~ |

33 | | ~~ #return numerical_integral(f, a, b)[0]~~ |

34 | | |

35 | | ~~ #Lets try mpmath instead:~~ |

36 | | |

37 | | ~~ import sage.libs.mpmath.all as mpmath~~ |

38 | | |

39 | | ~~ try:~~ |

40 | | ~~ precision = parent.prec()~~ |

41 | | ~~ mpmath.mp.prec = precision~~ |

42 | | ~~ except AttributeError:~~ |

43 | | ~~ precision = mpmath.mp.prec~~ |

44 | | |

45 | | ~~ mp_f = lambda z: \~~ |

46 | | ~~ f(x = mpmath.mpmath_to_sage(z,precision))~~ |

47 | | |

48 | | ~~ return mpmath.call(mpmath.quad,mp_f,[a,b])~~ |

49 | | ~~}}}~~ |

50 | | |

51 | | ~~Tests:~~ |

52 | | |

53 | | |

54 | | ~~{{{~~ |

55 | | ~~sage: sage: sage: from sage.symbolic.integration.integral import definite_integral~~ |

56 | | ~~sage: sage: h = definite_integral(sin(x)/x^2, x, 1, 2); h~~ |

57 | | ~~integrate(sin(x)/x^2, x, 1, 2)~~ |

58 | | ~~sage: h.n()~~ |

59 | | ~~0.472399177268953~~ |

60 | | ~~sage: h.n(77)~~ |

61 | | ~~0.4723991772689525199904~~ |

62 | | ~~sage: h.n(100)~~ |

63 | | ~~0.47239917726895251999041133798~~ |

64 | | ~~}}}~~ |