-- The chain rule for real derivatives, and its affine cases x ↦ f(C + x) -- and x ↦ f(C − x). -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.Real.Analysis.ChainRule import Differentiation -- The chain rule: (g ∘ f)′(A) = g′(f(A)) f′(A). The difference quotient of -- g ∘ f at h is the quotient of g over the increment k = f(A + h) − f(A) -- times the quotient of f, when k ≠ 0, and K times the quotient of f -- (both 0) when k = 0. theorem HasDerivativeAt.comp (g f : Set) (D K A : Real) requires derivativeF : HasDerivativeAt(f, D, A) requires derivativeG : HasDerivativeAt(g, K, f(A)) shows HasDerivativeAt(compose(g, f, ℝ, ℝ, ℝ), K * D, A) proof have fFunction := HasDerivativeAt.function with derivativeF have gFunction := HasDerivativeAt.function with derivativeG have aMember : A ∈ ℝ by typecheck have faMember := FunctionBetween.apply_closed with fFunction, aMember have faReal : Real(f(A)) by exact faMember have gfaMember := FunctionBetween.apply_closed with gFunction, faMember have gfaReal : Real(g(f(A))) by exact gfaMember have composite := compose.function at g, f, ℝ, ℝ, ℝ with fFunction, gFunction have quotient := differenceQuotient.function at compose(g, f, ℝ, ℝ, ℝ), A with composite have limitF := HasDerivativeAt.limit with derivativeF have limitG := HasDerivativeAt.limit with derivativeG have shifted := HasDerivativeAt.shifted_limit with derivativeF have translated := translateBy.function at A refine ⟨composite, quotient, ?_⟩ intros e eMember positive have eReal : Real(e) by exact eMember have scale := limitScale.real at D have scalePositive := limitScale.positive at D have radiusG := scaledRadius.real at e, limitScale(D) have radiusGPositive := scaledRadius.positive with positive, scalePositive have absK := abs.real at K have absKNonnegative := abs.nonnegative at K have boundPositive : 0 < scaledRadius(e, limitScale(D)) + abs(K) by linarith have radiusFPositive := scaledRadius.positive with positive, boundPositive obtain ⟨R, rMember, rPositive, mapsG⟩ := TendsToAt.eventually with limitG, radiusGPositive obtain ⟨S, sMember, sPositive, mapsShift⟩ := TendsToAt.eventually with shifted, rPositive obtain ⟨T, tMember, tPositive, mapsF⟩ := TendsToAt.eventually with limitF, radiusFPositive have sReal : Real(S) by exact sMember have tReal : Real(T) by exact tMember obtain ⟨d, dMember, dPositive, dS, dT⟩ := positiveRadius.common with sPositive, tPositive refine ⟨d, dMember, dPositive, ?_⟩ intros h hMember distinct near have hReal : Real(h) by exact hMember have nearS := openBall.mono with near, dS have nearT := openBall.mono with near, dT have quotientNear := mapsF with h, hMember, distinct, nearT have movedNear := mapsShift with h, hMember, distinct, nearS have shiftValue := compose.apply with translated, fFunction, hMember rw [shiftValue, translateBy.value] at movedNear have moved : A + h ∈ ℝ by typecheck have fMoved := FunctionBetween.apply_closed with fFunction, moved have fMovedReal : Real(f(A + h)) by exact fMoved have gMoved := FunctionBetween.apply_closed with gFunction, fMoved have gMovedReal : Real(g(f(A + h))) by exact gMoved have valueF := differenceQuotient.value at f, A, h with fFunction have valueH := differenceQuotient.value at compose(g, f, ℝ, ℝ, ℝ), A, h with composite have composeMoved := compose.apply with fFunction, gFunction, moved have composeAt := compose.apply with fFunction, gFunction, aMember rw [valueH, composeMoved, composeAt] rw valueF at quotientNear have inverse := realInvValue.real at h have alternatives : f(A + h) - f(A) = 0 ∨ f(A + h) - f(A) ≠ 0 by tauto cases alternatives with zero | nonzero . have same : f(A + h) = f(A) by linarith rw same at quotientNear rw same have center := openBall.center at K, scaledRadius(e, limitScale(D)) with radiusGPositive have product := openBall.mul_control with positive, center, quotientNear have equal : K * ((f(A) - f(A)) / h) = (g(f(A)) - g(f(A))) / h rw [realDiv.eq_mul_inv, realDiv.eq_mul_inv] ring rwa equal at product . have increment : Real(f(A + h) - f(A)) by typecheck have incrementMember : f(A + h) - f(A) ∈ ℝ by typecheck obtain ⟨_, movedDistance⟩ := openBall.membership.forward with movedNear have small : dist(f(A + h) - f(A), realZero) < R have value : dist(f(A + h) - f(A), realZero) = dist(f(A + h), f(A)) unfold dist have shift : f(A + h) - f(A) - realZero = f(A + h) - f(A) by ring rw shift rwa value have incrementNear := openBall.membership.backward with ⟨incrementMember, small⟩ have quotientG := mapsG with f(A + h) - f(A), incrementMember, nonzero, incrementNear have valueG := differenceQuotient.value at g, f(A), f(A + h) - f(A) with gFunction have back : f(A) + (f(A + h) - f(A)) = f(A + h) by ring rw [valueG, back] at quotientG have product := openBall.mul_control with positive, quotientG, quotientNear have cancel := realInvValue.mul_left with nonzero have incrementInverse := realInvValue.real at f(A + h) - f(A) have equal : (g(f(A + h)) - g(f(A))) / (f(A + h) - f(A)) * ((f(A + h) - f(A)) / h) = (g(f(A + h)) - g(f(A))) / h rw [realDiv.eq_mul_inv, realDiv.eq_mul_inv, realDiv.eq_mul_inv] linear_combination (g(f(A + h)) - g(f(A))) * realInvValue(h) * cancel rwa equal at product theorem translateBy.has_derivative_at (C X : Real) shows HasDerivativeAt(translateBy(C), (1:R), X) proof have constant := constantRealFunction.has_derivative_at at C, X have identityDerivative := identity.has_derivative_at at X have sum := HasDerivativeAt.add with constant, identityDerivative have value : (0:R) + (1:R) = (1:R) by ring rw value at sum unfold translateBy exact sum -- x ↦ f(C + x) has derivative f′(C + X) at X. theorem HasDerivativeAt.comp_translate (f : Set) (D C X : Real) requires derivative : HasDerivativeAt(f, D, C + X) shows HasDerivativeAt(compose(f, translateBy(C), ℝ, ℝ, ℝ), D, X) proof have inner := translateBy.has_derivative_at at C, X have xMember : X ∈ ℝ by typecheck have value := translateBy.value at C, X rw ← value at derivative have chain := HasDerivativeAt.comp with inner, derivative have unit : D * (1:R) = D by ring rwa unit at chain -- The reflection x ↦ C − x. def reflectBy (C : Real) : Set := pointwiseSub(constantRealFunction(C), identity(ℝ)) theorem reflectBy.function (C : Real) shows reflectBy(C) : ℝ → ℝ proof have constant := constantRealFunction.function at C have identityFunction := identity.function at ℝ unfold reflectBy exact pointwiseSub.function with constant, identityFunction theorem reflectBy.value (C X : Real) shows reflectBy(C)(X) = C - X proof have constant := constantRealFunction.function at C have identityFunction := identity.function at ℝ have xMember : X ∈ ℝ by typecheck have constantValue := constantRealFunction.value at C, X have identityValue := identity.apply with xMember have constantReal : Real(constantRealFunction(C)(X)) rw constantValue typecheck have identityReal : Real(identity(ℝ)(X)) rw identityValue typecheck have difference := pointwiseSub.value at constantRealFunction(C), identity(ℝ), X with constant, identityFunction, constantReal, identityReal unfold reflectBy rw [difference, constantValue, identityValue] theorem reflectBy.has_derivative_at (C X : Real) shows HasDerivativeAt(reflectBy(C), -(1:R), X) proof have constant := constantRealFunction.has_derivative_at at C, X have identityDerivative := identity.has_derivative_at at X have difference := HasDerivativeAt.sub with constant, identityDerivative have value : (0:R) - (1:R) = -(1:R) by ring rw value at difference unfold reflectBy exact difference -- x ↦ f(C − x) has derivative −f′(C − X) at X. theorem HasDerivativeAt.comp_reflect (f : Set) (D C X : Real) requires derivative : HasDerivativeAt(f, D, C - X) shows HasDerivativeAt(compose(f, reflectBy(C), ℝ, ℝ, ℝ), -D, X) proof have inner := reflectBy.has_derivative_at at C, X have value := reflectBy.value at C, X rw ← value at derivative have chain := HasDerivativeAt.comp with inner, derivative have sign : D * -(1:R) = -D by ring rwa sign at chain