Grace/Cumulus/Real/Analysis/ChainRule.g17

Open in Workbench Download source

Imports

Differentiation

Declarations

Source

1-- The chain rule for real derivatives, and its affine cases x ↦ f(C + x)2-- and x ↦ f(C − x).3-- Part of Cumulus, the Grace17 mathematics library.4-- Copyright (c) 2026 Grant Passmore.5-- Released under the Apache License 2.0. See LICENSE in the repository root.6-- Authors: Grant Passmore78module Grace.Cumulus.Real.Analysis.ChainRule9import Differentiation1011-- The chain rule: (g ∘ f)′(A) = g′(f(A)) f′(A). The difference quotient of12-- g ∘ f at h is the quotient of g over the increment k = f(A + h) − f(A)13-- times the quotient of f, when k ≠ 0, and K times the quotient of f14-- (both 0) when k = 0.15theorem HasDerivativeAt.comp (g f : Set) (D K A : Real)16  requires derivativeF : HasDerivativeAt(f, D, A)17  requires derivativeG : HasDerivativeAt(g, K, f(A))18  shows HasDerivativeAt(compose(g, f, ℝ, ℝ, ℝ), K * D, A)19proof20  have fFunction := HasDerivativeAt.function with derivativeF21  have gFunction := HasDerivativeAt.function with derivativeG22  have aMember : A ∈ ℝ by typecheck23  have faMember := FunctionBetween.apply_closed with fFunction, aMember24  have faReal : Real(f(A)) by exact faMember25  have gfaMember := FunctionBetween.apply_closed with gFunction, faMember26  have gfaReal : Real(g(f(A))) by exact gfaMember27  have composite := compose.function at g, f, ℝ, ℝ, ℝ with fFunction,28    gFunction29  have quotient := differenceQuotient.function30    at compose(g, f, ℝ, ℝ, ℝ), A with composite31  have limitF := HasDerivativeAt.limit with derivativeF32  have limitG := HasDerivativeAt.limit with derivativeG33  have shifted := HasDerivativeAt.shifted_limit with derivativeF34  have translated := translateBy.function at A35  refine ⟨composite, quotient, ?_⟩36  intros e eMember positive37  have eReal : Real(e) by exact eMember38  have scale := limitScale.real at D39  have scalePositive := limitScale.positive at D40  have radiusG := scaledRadius.real at e, limitScale(D)41  have radiusGPositive := scaledRadius.positive with positive, scalePositive42  have absK := abs.real at K43  have absKNonnegative := abs.nonnegative at K44  have boundPositive : 0 < scaledRadius(e, limitScale(D)) + abs(K)45    by linarith46  have radiusFPositive := scaledRadius.positive with positive, boundPositive47  obtain ⟨R, rMember, rPositive, mapsG⟩ := TendsToAt.eventually48    with limitG, radiusGPositive49  obtain ⟨S, sMember, sPositive, mapsShift⟩ := TendsToAt.eventually50    with shifted, rPositive51  obtain ⟨T, tMember, tPositive, mapsF⟩ := TendsToAt.eventually52    with limitF, radiusFPositive53  have sReal : Real(S) by exact sMember54  have tReal : Real(T) by exact tMember55  obtain ⟨d, dMember, dPositive, dS, dT⟩ := positiveRadius.common56    with sPositive, tPositive57  refine ⟨d, dMember, dPositive, ?_⟩58  intros h hMember distinct near59  have hReal : Real(h) by exact hMember60  have nearS := openBall.mono with near, dS61  have nearT := openBall.mono with near, dT62  have quotientNear := mapsF with h, hMember, distinct, nearT63  have movedNear := mapsShift with h, hMember, distinct, nearS64  have shiftValue := compose.apply with translated, fFunction, hMember65  rw [shiftValue, translateBy.value] at movedNear66  have moved : A + h ∈ ℝ by typecheck67  have fMoved := FunctionBetween.apply_closed with fFunction, moved68  have fMovedReal : Real(f(A + h)) by exact fMoved69  have gMoved := FunctionBetween.apply_closed with gFunction, fMoved70  have gMovedReal : Real(g(f(A + h))) by exact gMoved71  have valueF := differenceQuotient.value at f, A, h with fFunction72  have valueH := differenceQuotient.value at compose(g, f, ℝ, ℝ, ℝ), A, h73    with composite74  have composeMoved := compose.apply with fFunction, gFunction, moved75  have composeAt := compose.apply with fFunction, gFunction, aMember76  rw [valueH, composeMoved, composeAt]77  rw valueF at quotientNear78  have inverse := realInvValue.real at h79  have alternatives : f(A + h) - f(A) = 0 ∨ f(A + h) - f(A) ≠ 0 by tauto80  cases alternatives with zero | nonzero81  . have same : f(A + h) = f(A) by linarith82    rw same at quotientNear83    rw same84    have center := openBall.center at K, scaledRadius(e, limitScale(D))85      with radiusGPositive86    have product := openBall.mul_control with positive, center, quotientNear87    have equal : K * ((f(A) - f(A)) / h) = (g(f(A)) - g(f(A))) / h88      rw [realDiv.eq_mul_inv, realDiv.eq_mul_inv]89      ring90    rwa equal at product91  . have increment : Real(f(A + h) - f(A)) by typecheck92    have incrementMember : f(A + h) - f(A) ∈ ℝ by typecheck93    obtain ⟨_, movedDistance⟩ := openBall.membership.forward with movedNear94    have small : dist(f(A + h) - f(A), realZero) < R95      have value : dist(f(A + h) - f(A), realZero) = dist(f(A + h), f(A))96        unfold dist97        have shift : f(A + h) - f(A) - realZero = f(A + h) - f(A) by ring98        rw shift99      rwa value100    have incrementNear := openBall.membership.backward101      with ⟨incrementMember, small⟩102    have quotientG := mapsG with f(A + h) - f(A), incrementMember, nonzero,103      incrementNear104    have valueG := differenceQuotient.value at g, f(A), f(A + h) - f(A)105      with gFunction106    have back : f(A) + (f(A + h) - f(A)) = f(A + h) by ring107    rw [valueG, back] at quotientG108    have product := openBall.mul_control with positive, quotientG,109      quotientNear110    have cancel := realInvValue.mul_left with nonzero111    have incrementInverse := realInvValue.real at f(A + h) - f(A)112    have equal : (g(f(A + h)) - g(f(A))) / (f(A + h) - f(A)) *113        ((f(A + h) - f(A)) / h) = (g(f(A + h)) - g(f(A))) / h114      rw [realDiv.eq_mul_inv, realDiv.eq_mul_inv, realDiv.eq_mul_inv]115      linear_combination (g(f(A + h)) - g(f(A))) * realInvValue(h) * cancel116    rwa equal at product117118theorem translateBy.has_derivative_at (C X : Real)119  shows HasDerivativeAt(translateBy(C), (1:R), X)120proof121  have constant := constantRealFunction.has_derivative_at at C, X122  have identityDerivative := identity.has_derivative_at at X123  have sum := HasDerivativeAt.add with constant, identityDerivative124  have value : (0:R) + (1:R) = (1:R) by ring125  rw value at sum126  unfold translateBy127  exact sum128129-- x ↦ f(C + x) has derivative f′(C + X) at X.130theorem HasDerivativeAt.comp_translate (f : Set) (D C X : Real)131  requires derivative : HasDerivativeAt(f, D, C + X)132  shows HasDerivativeAt(compose(f, translateBy(C), ℝ, ℝ, ℝ), D, X)133proof134  have inner := translateBy.has_derivative_at at C, X135  have xMember : X ∈ ℝ by typecheck136  have value := translateBy.value at C, X137  rw ← value at derivative138  have chain := HasDerivativeAt.comp with inner, derivative139  have unit : D * (1:R) = D by ring140  rwa unit at chain141142-- The reflection x ↦ C − x.143def reflectBy (C : Real) : Set :=144  pointwiseSub(constantRealFunction(C), identity(ℝ))145146theorem reflectBy.function (C : Real)147  shows reflectBy(C) : ℝ → ℝ148proof149  have constant := constantRealFunction.function at C150  have identityFunction := identity.function at ℝ151  unfold reflectBy152  exact pointwiseSub.function with constant, identityFunction153154theorem reflectBy.value (C X : Real)155  shows reflectBy(C)(X) = C - X156proof157  have constant := constantRealFunction.function at C158  have identityFunction := identity.function at ℝ159  have xMember : X ∈ ℝ by typecheck160  have constantValue := constantRealFunction.value at C, X161  have identityValue := identity.apply with xMember162  have constantReal : Real(constantRealFunction(C)(X))163    rw constantValue164    typecheck165  have identityReal : Real(identity(ℝ)(X))166    rw identityValue167    typecheck168  have difference := pointwiseSub.value at constantRealFunction(C),169    identity(ℝ), X with constant, identityFunction, constantReal, identityReal170  unfold reflectBy171  rw [difference, constantValue, identityValue]172173theorem reflectBy.has_derivative_at (C X : Real)174  shows HasDerivativeAt(reflectBy(C), -(1:R), X)175proof176  have constant := constantRealFunction.has_derivative_at at C, X177  have identityDerivative := identity.has_derivative_at at X178  have difference := HasDerivativeAt.sub with constant, identityDerivative179  have value : (0:R) - (1:R) = -(1:R) by ring180  rw value at difference181  unfold reflectBy182  exact difference183184-- x ↦ f(C − x) has derivative −f′(C − X) at X.185theorem HasDerivativeAt.comp_reflect (f : Set) (D C X : Real)186  requires derivative : HasDerivativeAt(f, D, C - X)187  shows HasDerivativeAt(compose(f, reflectBy(C), ℝ, ℝ, ℝ), -D, X)188proof189  have inner := reflectBy.has_derivative_at at C, X190  have value := reflectBy.value at C, X191  rw ← value at derivative192  have chain := HasDerivativeAt.comp with inner, derivative193  have sign : D * -(1:R) = -D by ring194  rwa sign at chain195