Zulip Chat Archive

Stream: new members

Topic: How to use deriv.comp?


Adomas Baliuka (Jan 13 2024 at 03:06):

How does one use the chain rule with deriv? Is there a trick to get the _ ∘ _ to unify? Can one do this example nicely? (Yes, one can use deriv.log here but I want to see how to use deriv.comp)

import Mathlib
open Real

@[simp] lemma deriv_one_minus (x : ) : deriv (fun (y : )  1 - y) x = -1 := by
  have onem (y : ) : 1 - y = -(y + -1) := by ring
  simp_rw [onem]
  simp

@[simp] lemma diff_1_minusp (p : ) : DifferentiableAt  (fun p => 1 - p) p := by
  have (p : ) : 1 - p = -(p - 1) := by ring
  simp_rw [this]
  apply differentiableAt_neg_iff.mpr
  apply DifferentiableAt.add_const
  simp

lemma deriv_log_one_sub (x : ) (hh : x  1): deriv (fun p  log (1 - p)) x = -(1-x)⁻¹ := by
  have (x : ) : log (1 - x) = ((fun x  log x)  (1 - ·)) x := by simp
  simp_rw [this]
  rw [deriv.comp]  -- How to apply chain rule without this ugly hack?
  · simp
  · simp
    exact sub_ne_zero.mpr hh.symm
  · simp

Ruben Van de Velde (Jan 13 2024 at 07:19):

You might rewrite with Function.comp_def first. Or maybe we need a deriv.comp' with the lambda version


Last updated: May 02 2025 at 03:31 UTC