/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import analysis.calculus.mean_value /-! # L'HΓ΄pital's rule for 0/0 indeterminate forms In this file, we prove several forms of "L'Hopital's rule" for computing 0/0 indeterminate forms. The proof of `has_deriv_at.lhopital_zero_right_on_Ioo` is based on the one given in the corresponding [Wikibooks](https://en.wikibooks.org/wiki/Calculus/L%27H%C3%B4pital%27s_Rule) chapter, and all other statements are derived from this one by composing by carefully chosen functions. Note that the filter `f'/g'` tends to isn't required to be one of `𝓝 a`, `at_top` or `at_bot`. In fact, we give a slightly stronger statement by allowing it to be any filter on `ℝ`. Each statement is available in a `has_deriv_at` form and a `deriv` form, which is denoted by each statement being in either the `has_deriv_at` or the `deriv` namespace. ## Tags L'HΓ΄pital's rule, L'Hopital's rule -/ open filter set open_locale filter topological_space pointwise variables {a b : ℝ} (hab : a < b) {l : filter ℝ} {f f' g g' : ℝ β†’ ℝ} /-! ## Interval-based versions We start by proving statements where all conditions (derivability, `g' β‰  0`) have to be satisfied on an explicitly-provided interval. -/ namespace has_deriv_at include hab theorem lhopital_zero_right_on_Ioo (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hg' : βˆ€ x ∈ Ioo a b, g' x β‰  0) (hfa : tendsto f (𝓝[>] a) (𝓝 0)) (hga : tendsto g (𝓝[>] a) (𝓝 0)) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (𝓝[>] a) l) : tendsto (Ξ» x, (f x) / (g x)) (𝓝[>] a) l := begin have sub : βˆ€ x ∈ Ioo a b, Ioo a x βŠ† Ioo a b := Ξ» x hx, Ioo_subset_Ioo (le_refl a) (le_of_lt hx.2), have hg : βˆ€ x ∈ (Ioo a b), g x β‰  0, { intros x hx h, have : tendsto g (𝓝[<] x) (𝓝 0), { rw [← h, ← nhds_within_Ioo_eq_nhds_within_Iio hx.1], exact ((hgg' x hx).continuous_at.continuous_within_at.mono $ sub x hx).tendsto }, obtain ⟨y, hyx, hy⟩ : βˆƒ c ∈ Ioo a x, g' c = 0, from exists_has_deriv_at_eq_zero' hx.1 hga this (Ξ» y hy, hgg' y $ sub x hx hy), exact hg' y (sub x hx hyx) hy }, have : βˆ€ x ∈ Ioo a b, βˆƒ c ∈ Ioo a x, (f x) * (g' c) = (g x) * (f' c), { intros x hx, rw [← sub_zero (f x), ← sub_zero (g x)], exact exists_ratio_has_deriv_at_eq_ratio_slope' g g' hx.1 f f' (Ξ» y hy, hgg' y $ sub x hx hy) (Ξ» y hy, hff' y $ sub x hx hy) hga hfa (tendsto_nhds_within_of_tendsto_nhds (hgg' x hx).continuous_at.tendsto) (tendsto_nhds_within_of_tendsto_nhds (hff' x hx).continuous_at.tendsto) }, choose! c hc using this, have : βˆ€ x ∈ Ioo a b, ((Ξ» x', (f' x') / (g' x')) ∘ c) x = f x / g x, { intros x hx, rcases hc x hx with ⟨h₁, hβ‚‚βŸ©, field_simp [hg x hx, hg' (c x) ((sub x hx) h₁)], simp only [hβ‚‚], rwa mul_comm }, have cmp : βˆ€ x ∈ Ioo a b, a < c x ∧ c x < x, from Ξ» x hx, (hc x hx).1, rw ← nhds_within_Ioo_eq_nhds_within_Ioi hab, apply tendsto_nhds_within_congr this, simp only, apply hdiv.comp, refine tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds (tendsto_nhds_within_of_tendsto_nhds tendsto_id) _ _) _, all_goals { apply eventually_nhds_within_of_forall, intros x hx, have := cmp x hx, try {simp}, linarith [this] } end theorem lhopital_zero_right_on_Ico (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hcf : continuous_on f (Ico a b)) (hcg : continuous_on g (Ico a b)) (hg' : βˆ€ x ∈ Ioo a b, g' x β‰  0) (hfa : f a = 0) (hga : g a = 0) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (nhds_within a (Ioi a)) l) : tendsto (Ξ» x, (f x) / (g x)) (nhds_within a (Ioi a)) l := begin refine lhopital_zero_right_on_Ioo hab hff' hgg' hg' _ _ hdiv, { rw [← hfa, ← nhds_within_Ioo_eq_nhds_within_Ioi hab], exact ((hcf a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto }, { rw [← hga, ← nhds_within_Ioo_eq_nhds_within_Ioi hab], exact ((hcg a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto }, end theorem lhopital_zero_left_on_Ioo (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hg' : βˆ€ x ∈ Ioo a b, g' x β‰  0) (hfb : tendsto f (nhds_within b (Iio b)) (𝓝 0)) (hgb : tendsto g (nhds_within b (Iio b)) (𝓝 0)) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (nhds_within b (Iio b)) l) : tendsto (Ξ» x, (f x) / (g x)) (nhds_within b (Iio b)) l := begin -- Here, we essentially compose by `has_neg.neg`. The following is mostly technical details. have hdnf : βˆ€ x ∈ -Ioo a b, has_deriv_at (f ∘ has_neg.neg) (f' (-x) * (-1)) x, from Ξ» x hx, comp x (hff' (-x) hx) (has_deriv_at_neg x), have hdng : βˆ€ x ∈ -Ioo a b, has_deriv_at (g ∘ has_neg.neg) (g' (-x) * (-1)) x, from Ξ» x hx, comp x (hgg' (-x) hx) (has_deriv_at_neg x), rw preimage_neg_Ioo at hdnf, rw preimage_neg_Ioo at hdng, have := lhopital_zero_right_on_Ioo (neg_lt_neg hab) hdnf hdng (by { intros x hx h, apply hg' _ (by {rw ← preimage_neg_Ioo at hx, exact hx}), rwa [mul_comm, ← neg_eq_neg_one_mul, neg_eq_zero] at h }) (hfb.comp tendsto_neg_nhds_within_Ioi_neg) (hgb.comp tendsto_neg_nhds_within_Ioi_neg) (by { simp only [neg_div_neg_eq, mul_one, mul_neg], exact (tendsto_congr $ Ξ» x, rfl).mp (hdiv.comp tendsto_neg_nhds_within_Ioi_neg) }), have := this.comp tendsto_neg_nhds_within_Iio, unfold function.comp at this, simpa only [neg_neg] end theorem lhopital_zero_left_on_Ioc (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hcf : continuous_on f (Ioc a b)) (hcg : continuous_on g (Ioc a b)) (hg' : βˆ€ x ∈ Ioo a b, g' x β‰  0) (hfb : f b = 0) (hgb : g b = 0) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (nhds_within b (Iio b)) l) : tendsto (Ξ» x, (f x) / (g x)) (nhds_within b (Iio b)) l := begin refine lhopital_zero_left_on_Ioo hab hff' hgg' hg' _ _ hdiv, { rw [← hfb, ← nhds_within_Ioo_eq_nhds_within_Iio hab], exact ((hcf b $ right_mem_Ioc.mpr hab).mono Ioo_subset_Ioc_self).tendsto }, { rw [← hgb, ← nhds_within_Ioo_eq_nhds_within_Iio hab], exact ((hcg b $ right_mem_Ioc.mpr hab).mono Ioo_subset_Ioc_self).tendsto }, end omit hab theorem lhopital_zero_at_top_on_Ioi (hff' : βˆ€ x ∈ Ioi a, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioi a, has_deriv_at g (g' x) x) (hg' : βˆ€ x ∈ Ioi a, g' x β‰  0) (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0)) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) at_top l) : tendsto (Ξ» x, (f x) / (g x)) at_top l := begin obtain ⟨ a', haa', ha'⟩ : βˆƒ a', a < a' ∧ 0 < a' := ⟨1 + max a 0, ⟨lt_of_le_of_lt (le_max_left a 0) (lt_one_add _), lt_of_le_of_lt (le_max_right a 0) (lt_one_add _)⟩⟩, have fact1 : βˆ€ (x:ℝ), x ∈ Ioo 0 a'⁻¹ β†’ x β‰  0 := Ξ» _ hx, (ne_of_lt hx.1).symm, have fact2 : βˆ€ x ∈ Ioo 0 a'⁻¹, a < x⁻¹, from Ξ» _ hx, lt_trans haa' ((lt_inv ha' hx.1).mpr hx.2), have hdnf : βˆ€ x ∈ Ioo 0 a'⁻¹, has_deriv_at (f ∘ has_inv.inv) (f' (x⁻¹) * (-(x^2)⁻¹)) x, from Ξ» x hx, comp x (hff' (x⁻¹) $ fact2 x hx) (has_deriv_at_inv $ fact1 x hx), have hdng : βˆ€ x ∈ Ioo 0 a'⁻¹, has_deriv_at (g ∘ has_inv.inv) (g' (x⁻¹) * (-(x^2)⁻¹)) x, from Ξ» x hx, comp x (hgg' (x⁻¹) $ fact2 x hx) (has_deriv_at_inv $ fact1 x hx), have := lhopital_zero_right_on_Ioo (inv_pos.mpr ha') hdnf hdng (by { intros x hx, refine mul_ne_zero _ (neg_ne_zero.mpr $ inv_ne_zero $ pow_ne_zero _ $ fact1 x hx), exact hg' _ (fact2 x hx) }) (hftop.comp tendsto_inv_zero_at_top) (hgtop.comp tendsto_inv_zero_at_top) (by { refine (tendsto_congr' _).mp (hdiv.comp tendsto_inv_zero_at_top), rw eventually_eq_iff_exists_mem, use [Ioi 0, self_mem_nhds_within], intros x hx, unfold function.comp, erw mul_div_mul_right, refine neg_ne_zero.mpr (inv_ne_zero $ pow_ne_zero _ $ ne_of_gt hx) }), have := this.comp tendsto_inv_at_top_zero', unfold function.comp at this, simpa only [inv_inv], end theorem lhopital_zero_at_bot_on_Iio (hff' : βˆ€ x ∈ Iio a, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Iio a, has_deriv_at g (g' x) x) (hg' : βˆ€ x ∈ Iio a, g' x β‰  0) (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0)) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) at_bot l) : tendsto (Ξ» x, (f x) / (g x)) at_bot l := begin -- Here, we essentially compose by `has_neg.neg`. The following is mostly technical details. have hdnf : βˆ€ x ∈ -Iio a, has_deriv_at (f ∘ has_neg.neg) (f' (-x) * (-1)) x, from Ξ» x hx, comp x (hff' (-x) hx) (has_deriv_at_neg x), have hdng : βˆ€ x ∈ -Iio a, has_deriv_at (g ∘ has_neg.neg) (g' (-x) * (-1)) x, from Ξ» x hx, comp x (hgg' (-x) hx) (has_deriv_at_neg x), rw preimage_neg_Iio at hdnf, rw preimage_neg_Iio at hdng, have := lhopital_zero_at_top_on_Ioi hdnf hdng (by { intros x hx h, apply hg' _ (by {rw ← preimage_neg_Iio at hx, exact hx}), rwa [mul_comm, ← neg_eq_neg_one_mul, neg_eq_zero] at h }) (hfbot.comp tendsto_neg_at_top_at_bot) (hgbot.comp tendsto_neg_at_top_at_bot) (by { simp only [mul_one, mul_neg, neg_div_neg_eq], exact (tendsto_congr $ Ξ» x, rfl).mp (hdiv.comp tendsto_neg_at_top_at_bot) }), have := this.comp tendsto_neg_at_bot_at_top, unfold function.comp at this, simpa only [neg_neg], end end has_deriv_at namespace deriv include hab theorem lhopital_zero_right_on_Ioo (hdf : differentiable_on ℝ f (Ioo a b)) (hg' : βˆ€ x ∈ Ioo a b, deriv g x β‰  0) (hfa : tendsto f (𝓝[>] a) (𝓝 0)) (hga : tendsto g (𝓝[>] a) (𝓝 0)) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (𝓝[>] a) l) : tendsto (Ξ» x, (f x) / (g x)) (𝓝[>] a) l := begin have hdf : βˆ€ x ∈ Ioo a b, differentiable_at ℝ f x, from Ξ» x hx, (hdf x hx).differentiable_at (Ioo_mem_nhds hx.1 hx.2), have hdg : βˆ€ x ∈ Ioo a b, differentiable_at ℝ g x, from Ξ» x hx, classical.by_contradiction (Ξ» h, hg' x hx (deriv_zero_of_not_differentiable_at h)), exact has_deriv_at.lhopital_zero_right_on_Ioo hab (Ξ» x hx, (hdf x hx).has_deriv_at) (Ξ» x hx, (hdg x hx).has_deriv_at) hg' hfa hga hdiv end theorem lhopital_zero_right_on_Ico (hdf : differentiable_on ℝ f (Ioo a b)) (hcf : continuous_on f (Ico a b)) (hcg : continuous_on g (Ico a b)) (hg' : βˆ€ x ∈ (Ioo a b), (deriv g) x β‰  0) (hfa : f a = 0) (hga : g a = 0) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (nhds_within a (Ioi a)) l) : tendsto (Ξ» x, (f x) / (g x)) (nhds_within a (Ioi a)) l := begin refine lhopital_zero_right_on_Ioo hab hdf hg' _ _ hdiv, { rw [← hfa, ← nhds_within_Ioo_eq_nhds_within_Ioi hab], exact ((hcf a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto }, { rw [← hga, ← nhds_within_Ioo_eq_nhds_within_Ioi hab], exact ((hcg a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto }, end theorem lhopital_zero_left_on_Ioo (hdf : differentiable_on ℝ f (Ioo a b)) (hg' : βˆ€ x ∈ (Ioo a b), (deriv g) x β‰  0) (hfb : tendsto f (nhds_within b (Iio b)) (𝓝 0)) (hgb : tendsto g (nhds_within b (Iio b)) (𝓝 0)) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (nhds_within b (Iio b)) l) : tendsto (Ξ» x, (f x) / (g x)) (nhds_within b (Iio b)) l := begin have hdf : βˆ€ x ∈ Ioo a b, differentiable_at ℝ f x, from Ξ» x hx, (hdf x hx).differentiable_at (Ioo_mem_nhds hx.1 hx.2), have hdg : βˆ€ x ∈ Ioo a b, differentiable_at ℝ g x, from Ξ» x hx, classical.by_contradiction (Ξ» h, hg' x hx (deriv_zero_of_not_differentiable_at h)), exact has_deriv_at.lhopital_zero_left_on_Ioo hab (Ξ» x hx, (hdf x hx).has_deriv_at) (Ξ» x hx, (hdg x hx).has_deriv_at) hg' hfb hgb hdiv end omit hab theorem lhopital_zero_at_top_on_Ioi (hdf : differentiable_on ℝ f (Ioi a)) (hg' : βˆ€ x ∈ (Ioi a), (deriv g) x β‰  0) (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0)) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) at_top l) : tendsto (Ξ» x, (f x) / (g x)) at_top l := begin have hdf : βˆ€ x ∈ Ioi a, differentiable_at ℝ f x, from Ξ» x hx, (hdf x hx).differentiable_at (Ioi_mem_nhds hx), have hdg : βˆ€ x ∈ Ioi a, differentiable_at ℝ g x, from Ξ» x hx, classical.by_contradiction (Ξ» h, hg' x hx (deriv_zero_of_not_differentiable_at h)), exact has_deriv_at.lhopital_zero_at_top_on_Ioi (Ξ» x hx, (hdf x hx).has_deriv_at) (Ξ» x hx, (hdg x hx).has_deriv_at) hg' hftop hgtop hdiv, end theorem lhopital_zero_at_bot_on_Iio (hdf : differentiable_on ℝ f (Iio a)) (hg' : βˆ€ x ∈ (Iio a), (deriv g) x β‰  0) (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0)) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) at_bot l) : tendsto (Ξ» x, (f x) / (g x)) at_bot l := begin have hdf : βˆ€ x ∈ Iio a, differentiable_at ℝ f x, from Ξ» x hx, (hdf x hx).differentiable_at (Iio_mem_nhds hx), have hdg : βˆ€ x ∈ Iio a, differentiable_at ℝ g x, from Ξ» x hx, classical.by_contradiction (Ξ» h, hg' x hx (deriv_zero_of_not_differentiable_at h)), exact has_deriv_at.lhopital_zero_at_bot_on_Iio (Ξ» x hx, (hdf x hx).has_deriv_at) (Ξ» x hx, (hdg x hx).has_deriv_at) hg' hfbot hgbot hdiv, end end deriv /-! ## Generic versions The following statements no longer any explicit interval, as they only require conditions holding eventually. -/ namespace has_deriv_at /-- L'HΓ΄pital's rule for approaching a real from the right, `has_deriv_at` version -/ theorem lhopital_zero_nhds_right (hff' : βˆ€αΆ  x in 𝓝[>] a, has_deriv_at f (f' x) x) (hgg' : βˆ€αΆ  x in 𝓝[>] a, has_deriv_at g (g' x) x) (hg' : βˆ€αΆ  x in 𝓝[>] a, g' x β‰  0) (hfa : tendsto f (𝓝[>] a) (𝓝 0)) (hga : tendsto g (𝓝[>] a) (𝓝 0)) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (𝓝[>] a) l) : tendsto (Ξ» x, (f x) / (g x)) (𝓝[>] a) l := begin rw eventually_iff_exists_mem at *, rcases hff' with ⟨s₁, hs₁, hff'⟩, rcases hgg' with ⟨sβ‚‚, hsβ‚‚, hgg'⟩, rcases hg' with ⟨s₃, hs₃, hg'⟩, let s := s₁ ∩ sβ‚‚ ∩ s₃, have hs : s ∈ 𝓝[>] a := inter_mem (inter_mem hs₁ hsβ‚‚) hs₃, rw mem_nhds_within_Ioi_iff_exists_Ioo_subset at hs, rcases hs with ⟨u, hau, hu⟩, refine lhopital_zero_right_on_Ioo hau _ _ _ hfa hga hdiv; intros x hx; apply_assumption; exact (hu hx).1.1 <|> exact (hu hx).1.2 <|> exact (hu hx).2 end /-- L'HΓ΄pital's rule for approaching a real from the left, `has_deriv_at` version -/ theorem lhopital_zero_nhds_left (hff' : βˆ€αΆ  x in 𝓝[<] a, has_deriv_at f (f' x) x) (hgg' : βˆ€αΆ  x in 𝓝[<] a, has_deriv_at g (g' x) x) (hg' : βˆ€αΆ  x in 𝓝[<] a, g' x β‰  0) (hfa : tendsto f (𝓝[<] a) (𝓝 0)) (hga : tendsto g (𝓝[<] a) (𝓝 0)) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (𝓝[<] a) l) : tendsto (Ξ» x, (f x) / (g x)) (𝓝[<] a) l := begin rw eventually_iff_exists_mem at *, rcases hff' with ⟨s₁, hs₁, hff'⟩, rcases hgg' with ⟨sβ‚‚, hsβ‚‚, hgg'⟩, rcases hg' with ⟨s₃, hs₃, hg'⟩, let s := s₁ ∩ sβ‚‚ ∩ s₃, have hs : s ∈ 𝓝[<] a := inter_mem (inter_mem hs₁ hsβ‚‚) hs₃, rw mem_nhds_within_Iio_iff_exists_Ioo_subset at hs, rcases hs with ⟨l, hal, hl⟩, refine lhopital_zero_left_on_Ioo hal _ _ _ hfa hga hdiv; intros x hx; apply_assumption; exact (hl hx).1.1 <|> exact (hl hx).1.2 <|> exact (hl hx).2 end /-- L'HΓ΄pital's rule for approaching a real, `has_deriv_at` version. This does not require anything about the situation at `a` -/ theorem lhopital_zero_nhds' (hff' : βˆ€αΆ  x in 𝓝[univ \ {a}] a, has_deriv_at f (f' x) x) (hgg' : βˆ€αΆ  x in 𝓝[univ \ {a}] a, has_deriv_at g (g' x) x) (hg' : βˆ€αΆ  x in 𝓝[univ \ {a}] a, g' x β‰  0) (hfa : tendsto f (𝓝[univ \ {a}] a) (𝓝 0)) (hga : tendsto g (𝓝[univ \ {a}] a) (𝓝 0)) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) (𝓝[univ \ {a}] a) l) : tendsto (Ξ» x, (f x) / (g x)) (𝓝[univ \ {a}] a) l := begin have : univ \ {a} = Iio a βˆͺ Ioi a, { ext, rw [mem_diff_singleton, eq_true_intro $ mem_univ x, true_and, ne_iff_lt_or_gt], refl }, simp only [this, nhds_within_union, tendsto_sup, eventually_sup] at *, exact ⟨lhopital_zero_nhds_left hff'.1 hgg'.1 hg'.1 hfa.1 hga.1 hdiv.1, lhopital_zero_nhds_right hff'.2 hgg'.2 hg'.2 hfa.2 hga.2 hdiv.2⟩ end /-- **L'HΓ΄pital's rule** for approaching a real, `has_deriv_at` version -/ theorem lhopital_zero_nhds (hff' : βˆ€αΆ  x in 𝓝 a, has_deriv_at f (f' x) x) (hgg' : βˆ€αΆ  x in 𝓝 a, has_deriv_at g (g' x) x) (hg' : βˆ€αΆ  x in 𝓝 a, g' x β‰  0) (hfa : tendsto f (𝓝 a) (𝓝 0)) (hga : tendsto g (𝓝 a) (𝓝 0)) (hdiv : tendsto (Ξ» x, f' x / g' x) (𝓝 a) l) : tendsto (Ξ» x, f x / g x) (𝓝[univ \ {a}] a) l := begin apply @lhopital_zero_nhds' _ _ _ f' _ g'; apply eventually_nhds_within_of_eventually_nhds <|> apply tendsto_nhds_within_of_tendsto_nhds; assumption end /-- L'HΓ΄pital's rule for approaching +∞, `has_deriv_at` version -/ theorem lhopital_zero_at_top (hff' : βˆ€αΆ  x in at_top, has_deriv_at f (f' x) x) (hgg' : βˆ€αΆ  x in at_top, has_deriv_at g (g' x) x) (hg' : βˆ€αΆ  x in at_top, g' x β‰  0) (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0)) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) at_top l) : tendsto (Ξ» x, (f x) / (g x)) at_top l := begin rw eventually_iff_exists_mem at *, rcases hff' with ⟨s₁, hs₁, hff'⟩, rcases hgg' with ⟨sβ‚‚, hsβ‚‚, hgg'⟩, rcases hg' with ⟨s₃, hs₃, hg'⟩, let s := s₁ ∩ sβ‚‚ ∩ s₃, have hs : s ∈ at_top := inter_mem (inter_mem hs₁ hsβ‚‚) hs₃, rw mem_at_top_sets at hs, rcases hs with ⟨l, hl⟩, have hl' : Ioi l βŠ† s := Ξ» x hx, hl x (le_of_lt hx), refine lhopital_zero_at_top_on_Ioi _ _ (Ξ» x hx, hg' x $ (hl' hx).2) hftop hgtop hdiv; intros x hx; apply_assumption; exact (hl' hx).1.1 <|> exact (hl' hx).1.2 end /-- L'HΓ΄pital's rule for approaching -∞, `has_deriv_at` version -/ theorem lhopital_zero_at_bot (hff' : βˆ€αΆ  x in at_bot, has_deriv_at f (f' x) x) (hgg' : βˆ€αΆ  x in at_bot, has_deriv_at g (g' x) x) (hg' : βˆ€αΆ  x in at_bot, g' x β‰  0) (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0)) (hdiv : tendsto (Ξ» x, (f' x) / (g' x)) at_bot l) : tendsto (Ξ» x, (f x) / (g x)) at_bot l := begin rw eventually_iff_exists_mem at *, rcases hff' with ⟨s₁, hs₁, hff'⟩, rcases hgg' with ⟨sβ‚‚, hsβ‚‚, hgg'⟩, rcases hg' with ⟨s₃, hs₃, hg'⟩, let s := s₁ ∩ sβ‚‚ ∩ s₃, have hs : s ∈ at_bot := inter_mem (inter_mem hs₁ hsβ‚‚) hs₃, rw mem_at_bot_sets at hs, rcases hs with ⟨l, hl⟩, have hl' : Iio l βŠ† s := Ξ» x hx, hl x (le_of_lt hx), refine lhopital_zero_at_bot_on_Iio _ _ (Ξ» x hx, hg' x $ (hl' hx).2) hfbot hgbot hdiv; intros x hx; apply_assumption; exact (hl' hx).1.1 <|> exact (hl' hx).1.2 end end has_deriv_at namespace deriv /-- **L'HΓ΄pital's rule** for approaching a real from the right, `deriv` version -/ theorem lhopital_zero_nhds_right (hdf : βˆ€αΆ  x in 𝓝[>] a, differentiable_at ℝ f x) (hg' : βˆ€αΆ  x in 𝓝[>] a, deriv g x β‰  0) (hfa : tendsto f (𝓝[>] a) (𝓝 0)) (hga : tendsto g (𝓝[>] a) (𝓝 0)) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (𝓝[>] a) l) : tendsto (Ξ» x, (f x) / (g x)) (𝓝[>] a) l := begin have hdg : βˆ€αΆ  x in 𝓝[>] a, differentiable_at ℝ g x, from hg'.mp (eventually_of_forall $ Ξ» _ hg', classical.by_contradiction (Ξ» h, hg' (deriv_zero_of_not_differentiable_at h))), have hdf' : βˆ€αΆ  x in 𝓝[>] a, has_deriv_at f (deriv f x) x, from hdf.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at), have hdg' : βˆ€αΆ  x in 𝓝[>] a, has_deriv_at g (deriv g x) x, from hdg.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at), exact has_deriv_at.lhopital_zero_nhds_right hdf' hdg' hg' hfa hga hdiv end /-- **L'HΓ΄pital's rule** for approaching a real from the left, `deriv` version -/ theorem lhopital_zero_nhds_left (hdf : βˆ€αΆ  x in 𝓝[<] a, differentiable_at ℝ f x) (hg' : βˆ€αΆ  x in 𝓝[<] a, deriv g x β‰  0) (hfa : tendsto f (𝓝[<] a) (𝓝 0)) (hga : tendsto g (𝓝[<] a) (𝓝 0)) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (𝓝[<] a) l) : tendsto (Ξ» x, (f x) / (g x)) (𝓝[<] a) l := begin have hdg : βˆ€αΆ  x in 𝓝[<] a, differentiable_at ℝ g x, from hg'.mp (eventually_of_forall $ Ξ» _ hg', classical.by_contradiction (Ξ» h, hg' (deriv_zero_of_not_differentiable_at h))), have hdf' : βˆ€αΆ  x in 𝓝[<] a, has_deriv_at f (deriv f x) x, from hdf.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at), have hdg' : βˆ€αΆ  x in 𝓝[<] a, has_deriv_at g (deriv g x) x, from hdg.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at), exact has_deriv_at.lhopital_zero_nhds_left hdf' hdg' hg' hfa hga hdiv end /-- **L'HΓ΄pital's rule** for approaching a real, `deriv` version. This does not require anything about the situation at `a` -/ theorem lhopital_zero_nhds' (hdf : βˆ€αΆ  x in 𝓝[univ \ {a}] a, differentiable_at ℝ f x) (hg' : βˆ€αΆ  x in 𝓝[univ \ {a}] a, deriv g x β‰  0) (hfa : tendsto f (𝓝[univ \ {a}] a) (𝓝 0)) (hga : tendsto g (𝓝[univ \ {a}] a) (𝓝 0)) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (𝓝[univ \ {a}] a) l) : tendsto (Ξ» x, (f x) / (g x)) (𝓝[univ \ {a}] a) l := begin have : univ \ {a} = Iio a βˆͺ Ioi a, { ext, rw [mem_diff_singleton, eq_true_intro $ mem_univ x, true_and, ne_iff_lt_or_gt], refl }, simp only [this, nhds_within_union, tendsto_sup, eventually_sup] at *, exact ⟨lhopital_zero_nhds_left hdf.1 hg'.1 hfa.1 hga.1 hdiv.1, lhopital_zero_nhds_right hdf.2 hg'.2 hfa.2 hga.2 hdiv.2⟩, end /-- **L'HΓ΄pital's rule** for approaching a real, `deriv` version -/ theorem lhopital_zero_nhds (hdf : βˆ€αΆ  x in 𝓝 a, differentiable_at ℝ f x) (hg' : βˆ€αΆ  x in 𝓝 a, deriv g x β‰  0) (hfa : tendsto f (𝓝 a) (𝓝 0)) (hga : tendsto g (𝓝 a) (𝓝 0)) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) (𝓝 a) l) : tendsto (Ξ» x, (f x) / (g x)) (𝓝[univ \ {a}] a) l := begin apply lhopital_zero_nhds'; apply eventually_nhds_within_of_eventually_nhds <|> apply tendsto_nhds_within_of_tendsto_nhds; assumption end /-- **L'HΓ΄pital's rule** for approaching +∞, `deriv` version -/ theorem lhopital_zero_at_top (hdf : βˆ€αΆ  (x : ℝ) in at_top, differentiable_at ℝ f x) (hg' : βˆ€αΆ  (x : ℝ) in at_top, deriv g x β‰  0) (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0)) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) at_top l) : tendsto (Ξ» x, (f x) / (g x)) at_top l := begin have hdg : βˆ€αΆ  x in at_top, differentiable_at ℝ g x, from hg'.mp (eventually_of_forall $ Ξ» _ hg', classical.by_contradiction (Ξ» h, hg' (deriv_zero_of_not_differentiable_at h))), have hdf' : βˆ€αΆ  x in at_top, has_deriv_at f (deriv f x) x, from hdf.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at), have hdg' : βˆ€αΆ  x in at_top, has_deriv_at g (deriv g x) x, from hdg.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at), exact has_deriv_at.lhopital_zero_at_top hdf' hdg' hg' hftop hgtop hdiv end /-- **L'HΓ΄pital's rule** for approaching -∞, `deriv` version -/ theorem lhopital_zero_at_bot (hdf : βˆ€αΆ  (x : ℝ) in at_bot, differentiable_at ℝ f x) (hg' : βˆ€αΆ  (x : ℝ) in at_bot, deriv g x β‰  0) (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0)) (hdiv : tendsto (Ξ» x, ((deriv f) x) / ((deriv g) x)) at_bot l) : tendsto (Ξ» x, (f x) / (g x)) at_bot l := begin have hdg : βˆ€αΆ  x in at_bot, differentiable_at ℝ g x, from hg'.mp (eventually_of_forall $ Ξ» _ hg', classical.by_contradiction (Ξ» h, hg' (deriv_zero_of_not_differentiable_at h))), have hdf' : βˆ€αΆ  x in at_bot, has_deriv_at f (deriv f x) x, from hdf.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at), have hdg' : βˆ€αΆ  x in at_bot, has_deriv_at g (deriv g x) x, from hdg.mp (eventually_of_forall $ Ξ» _, differentiable_at.has_deriv_at), exact has_deriv_at.lhopital_zero_at_bot hdf' hdg' hg' hfbot hgbot hdiv end end deriv