# Package for limits

**URL:** <https://discourse.julialang.org/t/package-for-limits/49286>\
**Category:** General Usage\
**Tags:** package, math, calculus\
**Created:** [October 30, 2020, 4:58am UTC](https://discourse.julialang.org/t/package-for-limits/49286 "2020-10-30T04:58:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 30, 2020, 4:58am UTC](https://discourse.julialang.org/t/package-for-limits/49286/1 "2020-10-30T04:58:29Z")

</div>

What is a package that can be used to find the limit of a function? I thought ForwardDiff.jl might have this feature but I couldn’t find it in the documentation.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [October 30, 2020, 5:38am UTC](https://discourse.julialang.org/t/package-for-limits/49286/2 "2020-10-30T05:38:54Z")

</div>

Here is a “poor man’s” way: stick `Inf` or `-Inf` in.

eg,

```julia
julia> exp(Inf)
Inf

julia> exp(-Inf)
0.0

```

EDIT: Sorry, I guess I just assumed you meant limits as the input approached `+/- Inf`, and that could not be the case. To get more to the point of what you are after, I’m not sure such a thing exists. If the function is continuous, it could be possible.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 30, 2020, 5:49am UTC](https://discourse.julialang.org/t/package-for-limits/49286/3 "2020-10-30T05:49:16Z")

</div>

Can you give an example of what you’re looking for?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 30, 2020, 5:59am UTC](https://discourse.julialang.org/t/package-for-limits/49286/4 "2020-10-30T05:59:39Z")

</div>

Something like this? [https://github.com/JuliaMath/Richardson.jl](https://github.com/JuliaMath/Richardson.jl)

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 30, 2020, 3:40pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/5 "2020-10-30T15:40:23Z")

</div>

It would be for continuous functions yes, I meant for a tool that substitutes a variable, and rearanges a term such as 1/x or does a limit numerically from each side. Reduce.jl has such a function (but it’s a weird package that won’t load right). I’m thinking a calculus or symbolic algebra package would have these.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 30, 2020, 6:01pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/6 "2020-10-30T18:01:47Z")

</div>

> [@DNF](#):
>
> Something like this? [https://github.com/JuliaMath/Richardson.jl](https://github.com/JuliaMath/Richardson.jl)

Yes, if you want a _numerical_ limit calculation, then Richardson extrapolation would be the first thing that I would try. The Richardson.jl package can handle [x → ∞ limits](https://github.com/JuliaMath/Richardson.jl#infinite-limits) too.

If you want a symbolic limit evaluation, that is a matter for a symbolic-algebra package (e.g. you could try SymPy.jl to call sympy from Julia).

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [October 30, 2020, 7:00pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/7 "2020-10-30T19:00:21Z")

</div>

As pointed out, `SymPy.jl` has the `limit` function for taking limits. It has a special method for a univariate function. For an example usage, here is an innocuous looking function from a colleague which won’t have a numeric limit:

```julia
julia> g(x) = x^2 + 1 + log(abs( 11*x-15 ))/99
g (generic function with 1 method)

julia> limit(g, 15//11)
-∞

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 30, 2020, 9:22pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/8 "2020-10-30T21:22:03Z")

</div>

I’m not sure I know how to read this. What does 15 and 11 mean?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 30, 2020, 9:23pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/9 "2020-10-30T21:23:59Z")

</div>

15//11 is a rational type to represent exactly the fraction 15/11, see [Complex and Rational Numbers · The Julia Language](https://docs.julialang.org/en/v1/manual/complex-and-rational-numbers/#Rational-Numbers)

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 30, 2020, 9:34pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/10 "2020-10-30T21:34:18Z")

</div>

ok thanks.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 30, 2020, 9:55pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/11 "2020-10-30T21:55:11Z")

</div>

That would work great, but I do feel silly, I though that subs in SymEngine just changede the value of x, but if I do something like

```julia
subs(1x/2x,x=>0)

```

sure enough, I get 1/2. I’m trying to think of a function with removable discontinuity to test.  
I can aslo do one sided limits, using infintesimals

```julia
δ=ε=1 * 10^-308 #the smallest number a 64 bit processor can handle
subs(1/x,x=>δ)

```

and I get a crazy big number, that I can include is infinity.

sub(1/x,x=Inf)

gives me 0.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [October 30, 2020, 10:07pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/12 "2020-10-30T22:07:48Z")

</div>

I was curious, so I gave it a try:

```julia
julia> using IntervalArithmetic, Richardson

julia> g(x) = x^2 + 1 + log(abs( 11*x-15 ))/99
g (generic function with 1 method)

julia> extrapolate(g, 15/11)
(1.0273540424380285, 1.4376019930040229e-8)

julia> extrapolate(g, 15//11)
(1.0273540424380285, 1.4376019930040229e-8)

julia> extrapolate(g, big(15)/11)
(-Inf, Inf)

julia> extrapolate(g, interval(15)/11)
([0.846292, ∞], [0, ∞])

julia> extrapolate(g, interval(big(15))/11)
([1.04933, ∞]₂₅₆, [0, ∞]₂₅₆)

julia> g(15/11)
2.5164312852875934

julia> g(15//11)
-Inf

julia> g(big(15)/11)
-Inf

julia> g(interval(15)/11)
[-∞, 2.51644]

```

Some of them got it, but not all, and the error estimate of `Inf` from Richardson does not inspire confidence 😄

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 30, 2020, 10:18pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/13 "2020-10-30T22:18:50Z")

</div>

Which is from IntervalArithmatic, and which are from Richardson, I like the idea of extrapolate being able to find range, but there is a problem if it makes errors.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [October 30, 2020, 10:20pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/14 "2020-10-30T22:20:01Z")

</div>

`extrapolate` is from Richardson and `interval` is from IntervalArithmetic

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [October 30, 2020, 11:06pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/15 "2020-10-30T23:06:02Z")

</div>

It is a tricky function as the asymptote is really sharp. But +oo and not -oo?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 31, 2020, 12:02am UTC](https://discourse.julialang.org/t/package-for-limits/49286/16 "2020-10-31T00:02:39Z")

</div>

> [@ericphanson](#):
>
> Some of them got it, but not all, and the error estimate of `Inf` from Richardson does not inspire confidence

First, you’re using `Richardson.extrapolate` incorrectly: The second argument is not the point to extrapolate to, it is the initial step away from the limit point `x0` (which defaults to zero).

Second, Richardson extrapolation is based on polynomial extrapolation, so it isn’t going to work if the limit diverges.

Third, this particular extrapolation problem is going to be very difficult to do numerically (as opposed to symbolically) in any case. Because 11/15 is not representable in binary floating point, and the singularity is only logarithmic, in double precision `g(11/15)` gives a quite small magnitude result of `≈ 1.5573367747276683`. (And calling `extrapolate(g, 0.1, x0=11/15)` “correctly” gives this answer.)

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [October 31, 2020, 12:10am UTC](https://discourse.julialang.org/t/package-for-limits/49286/17 "2020-10-31T00:10:07Z")

</div>

> [@stevengj](#):
>
> First, you’re using `Richardson.extrapolate` incorrectly: The second argument is not the point to extrapolate to, it is the initial step away from the limit point `x0` (which defaults to zero).

Ah, thanks for the correction!

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 31, 2020, 1:08am UTC](https://discourse.julialang.org/t/package-for-limits/49286/18 "2020-10-31T01:08:52Z")

</div>

It is good to have range and domain of many functions. And, it would be good to do limits, I was able to use the subs by an using an infentesimal, which I definite as 10e-302 , or the smallest number a 64 bit machine would do. I’m not sure what is a safe number to put in Julia.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [October 31, 2020, 2:27am UTC](https://discourse.julialang.org/t/package-for-limits/49286/19 "2020-10-31T02:27:16Z")

</div>

If the limit does not exist, will it give a null or an error?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 31, 2020, 12:18pm UTC](https://discourse.julialang.org/t/package-for-limits/49286/20 "2020-10-31T12:18:45Z")

</div>

> [@brett\_knoss](#):
>
> If the limit does not exist, will it give a null or an error?

It will typically halt after a few iterations and give a large error estimate (the second return value):

```julia
julia> extrapolate(x -> 1/x, 0.1, x0=0)
(90.0, 80.0)

```

but log divergences are more difficult to detect with polynomial extrapolation so the error estimates are smaller (but still much larger than the typical error estimates when it succeeds):

```julia
julia> extrapolate(log, 0.1, x0=0)
(-744.7275688945767, 1.7346317053750226)

```

Note that (by design) it computes the one-sided limit (you specify the side using the initial step):

```julia
julia> extrapolate(x -> sqrt(x^2 + x^3)/x, 0.1, x0=0)
(1.0000000000000002, 1.3793410857942945e-11)

julia> extrapolate(x -> sqrt(x^2 + x^3)/x, -0.1, x0=0)
(-0.9999999999999998, 1.6191714635738208e-11)

```

[Next page](https://discourse.julialang.org/t/package-for-limits/49286.md?page=2)
