Grace/Cumulus/Algebra/Polynomial/ChainRule.g17

Open in Workbench Download source

Imports

Grace.Cumulus.Algebra.Polynomial.Derivative · Grace.Cumulus.Algebra.Ring.IdentitySum · Grace.Cumulus.Algebra.Polynomial.Algebra

Declarations

Source

1-- The chain rule for an algebra homomorphism φ : R[n] → R[J] between2-- polynomial algebras with a numeral index set: the derivative of φ(f) in3-- Xⱼ is the sum over i < n of φ(∂f/∂Xᵢ) times the derivative of φ(Xᵢ)4-- in Xⱼ, by generation.5-- Part of Cumulus, the Grace17 mathematics library.6-- Copyright (c) 2026 Grant Passmore.7-- Released under the Apache License 2.0. See LICENSE in the repository root.8-- Authors: Grant Passmore910module Grace.Cumulus.Algebra.Polynomial.ChainRule11import Grace.Cumulus.Algebra.Polynomial.Derivative12import Grace.Cumulus.Algebra.Ring.IdentitySum13import Grace.Cumulus.Algebra.Polynomial.Algebra1415-- The chain rule for an algebra homomorphism φ : R[n] → R[J]: the16-- derivative of φ(f) in Xⱼ is the sum over i < n of φ(∂f/∂Xᵢ) times17-- the derivative of φ(Xᵢ) in Xⱼ. The terms of the sum, the rule for f,18-- and the polynomials satisfying it.19def chainTerms (R J φ f j : Set, n : Nat) : Set :=20  { t ∈ n × Ring.carrier(polynomialRing(R, J)) |21    snd(t) = call2(Ring.mul(polynomialRing(R, J)),22      φ(partialDerivative(R, n, fst(t))(f)),23      partialDerivative(R, J, j)(φ(X(R, n, fst(t))))) }2425predicate ChainRule (R J φ f j : Set, n : Nat) :=26  partialDerivative(R, J, j)(φ(f)) =27    indexedRingSum(identity(Ring.carrier(polynomialRing(R, J))),28      chainTerms(R, J, φ, f, j, n), Ring.add(polynomialRing(R, J)),29      Ring.zero(polynomialRing(R, J)), n)3031def chainPolynomials (R J φ j : Set, n : Nat) : Set :=32  { f ∈ polynomials(n, Ring.carrier(R), Ring.zero(R)) |33    ChainRule(R, J, φ, f, j, n) }3435-- An algebra homomorphism between polynomial algebras is a ring36-- homomorphism between the polynomial rings fixing the constants.37theorem AlgebraHom.polynomial_ring_hom (R I J φ : Set)38  requires hom : φ : polynomialAlgebra(R, I) →ₐ polynomialAlgebra(R, J)39  shows φ : polynomialRing(R, I) →+* polynomialRing(R, J)40proof41  have ringHom := AlgebraHom.ring_hom with hom42  rw [polynomialAlgebra.ring, polynomialAlgebra.ring] at ringHom43  exact ringHom4445theorem AlgebraHom.polynomial_constant (R I J φ a : Set)46  requires hom : φ : polynomialAlgebra(R, I) →ₐ polynomialAlgebra(R, J)47  requires scalar : a ∈ Ring.carrier(R)48  shows φ(polynomialConstant(R, I)(a)) = polynomialConstant(R, J)(a)49proof50  have baseIn : a ∈ Ring.carrier(Algebra.base(polynomialAlgebra(R, I)))51    rw polynomialAlgebra.base52    exact scalar53  have mapped := AlgebraHom.map_base with hom, baseIn54  rw [polynomialAlgebra.map, polynomialAlgebra.map] at mapped55  exact mapped5657-- The chain terms as a function on n, and their values.58section ChainTerms59variable (R J φ f j : Set, n : Nat)60hypothesis ring : CommRing(R)61hypothesis hom : φ : polynomialAlgebra(R, n) →ₐ polynomialAlgebra(R, J)62hypothesis member : f ∈ Ring.carrier(polynomialRing(R, n))63hypothesis index : j ∈ J6465theorem chainTerms.value_mem (k : Set)66  requires position : k ∈ n67  shows call2(Ring.mul(polynomialRing(R, J)), φ(partialDerivative(R, n, k)(f)),68    partialDerivative(R, J, j)(φ(X(R, n, k)))) ∈69    Ring.carrier(polynomialRing(R, J))70proof71  have ringN := polynomialRing.comm_ring at R, n with ring72  have ringJ := polynomialRing.comm_ring at R, J with ring73  have ringHom := AlgebraHom.polynomial_ring_hom at R, n, J, φ with hom74  have derivative := partialDerivative.map_mem at R, n, k, f with *75  have mapped : φ(partialDerivative(R, n, k)(f)) ∈ polynomialRing(R, J) :=76    RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J),77      partialDerivative(R, n, k)(f) with *78  have variable : X(R, n, k) ∈ polynomialRing(R, n) := X.mem at R, n, k with *79  have mappedVariable : φ(X(R, n, k)) ∈ polynomialRing(R, J) :=80    RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J),81      X(R, n, k) with *82  have derivativeJ :=83    partialDerivative.map_mem at R, J, j, φ(X(R, n, k)) with *84  exact Ring.mul_mem with *8586theorem chainTerms.function87  shows chainTerms(R, J, φ, f, j, n) : n → Ring.carrier(polynomialRing(R, J))88proof89  constructor90  . intros e he91    exact he.left92  constructor93  . intros x y relatedY z relatedZ94    obtain ⟨_, shapeY⟩ := relatedY95    obtain ⟨_, shapeZ⟩ := relatedZ96    simp [fst.pair, snd.pair] at shapeY, shapeZ97    cc98  . intros k hk99    have valueIn := chainTerms.value_mem at R, J, φ, f, j, n, k with *100    refine ⟨call2(Ring.mul(polynomialRing(R, J)),101      φ(partialDerivative(R, n, k)(f)),102      partialDerivative(R, J, j)(φ(X(R, n, k)))), valueIn, _⟩103    apply chainTerms.membership.backward104    refine ⟨(product.pair with hk, valueIn), _⟩105    simp [fst.pair, snd.pair]106107theorem chainTerms.value (k : Set)108  requires position : k ∈ n109  shows chainTerms(R, J, φ, f, j, n)(k) =110    call2(Ring.mul(polynomialRing(R, J)), φ(partialDerivative(R, n, k)(f)),111      partialDerivative(R, J, j)(φ(X(R, n, k))))112proof113  have function := chainTerms.function at R, J, φ, f, j, n with *114  have valueIn := chainTerms.value_mem at R, J, φ, f, j, n, k with *115  have related : relates(chainTerms(R, J, φ, f, j, n), k,116      call2(Ring.mul(polynomialRing(R, J)), φ(partialDerivative(R, n, k)(f)),117        partialDerivative(R, J, j)(φ(X(R, n, k)))))118    apply chainTerms.membership.backward119    refine ⟨(product.pair with position, valueIn), _⟩120    simp [fst.pair, snd.pair]121  exact FunctionBetween.apply_value with function, position, related122123theorem chainTerms.values124  shows ∀ k ∈ n,125    chainTerms(R, J, φ, f, j, n)(k) ∈ Ring.carrier(polynomialRing(R, J))126proof127  intros k hk128  have function := chainTerms.function at R, J, φ, f, j, n with *129  exact FunctionBetween.apply_closed with function, hk130131end ChainTerms132133-- The chain rule, by generation: the polynomials satisfying it contain134-- the constants (both sides vanish) and the variables (a single term135-- survives), and are closed under sums and products (the sums add and136-- combine bilinearly termwise).137section ChainRule138variable (R J : Set, n : Nat)139hypothesis ring : CommRing(R)140141theorem polynomial_chain_rule (φ f j : Set)142  requires hom : φ : polynomialAlgebra(R, n) →ₐ polynomialAlgebra(R, J)143  requires member : f ∈ Ring.carrier(polynomialRing(R, n))144  requires index : j ∈ J145  shows partialDerivative(R, J, j)(φ(f)) =146    indexedRingSum(identity(Ring.carrier(polynomialRing(R, J))),147      chainTerms(R, J, φ, f, j, n), Ring.add(polynomialRing(R, J)),148      Ring.zero(polynomialRing(R, J)), n)149proof150  have parts := CommRing.parts with ring151  have ringN := polynomialRing.comm_ring at R, n with ring152  have ringJ := polynomialRing.comm_ring at R, J with ring153  have ringHom := AlgebraHom.polynomial_ring_hom at R, n, J, φ with hom154  have mappedZero := RingHom.map_zero at φ, polynomialRing(R, n),155    polynomialRing(R, J) with ringHom156  have mappedOne := RingHom.map_one at φ, polynomialRing(R, n),157    polynomialRing(R, J) with ringHom158  have fParts := polynomialRing.member_parts at R, n, f with *159  have variableTerms : ∀ k ∈ n, φ(X(R, n, k)) ∈ polynomialRing(R, J) ∧160      partialDerivative(R, J, j)(φ(X(R, n, k))) ∈ polynomialRing(R, J)161    intros k hk162    have variable : X(R, n, k) ∈ polynomialRing(R, n) :=163      X.mem at R, n, k with *164    have mappedVariable : φ(X(R, n, k)) ∈ polynomialRing(R, J) :=165      RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J),166        X(R, n, k) with *167    have derivativeJ :=168      partialDerivative.map_mem at R, J, j, φ(X(R, n, k)) with *169    exact ⟨mappedVariable, derivativeJ⟩170  have constants : ∀ a ∈ R.carrier,171      polynomialConstant(n, R.carrier, R.zero, a) ∈172        chainPolynomials(R, J, φ, j, n)173    intros a ha174    have value := polynomialConstant.apply at R, n, a with *175    have constantN : polynomialConstant(R, n)(a) ∈ polynomialRing(R, n) :=176      polynomialConstant.mem at R, n, a with *177    have constantParts := polynomialRing.member_parts at R, n,178      polynomialConstant(R, n)(a) with *179    rw ← value180    apply chainPolynomials.membership.backward181    refine ⟨constantParts, _⟩182    unfold ChainRule183    have mapped := AlgebraHom.polynomial_constant at R, n, J, φ, a with *184    have constantJ : polynomialConstant(R, J)(a) ∈ polynomialRing(R, J) :=185      polynomialConstant.mem at R, J, a with *186    have vanishes := partialDerivative.constant at R, J, j, a with *187    rw [mapped, vanishes]188    have terms : ∀ k ∈ n,189        chainTerms(R, J, φ, polynomialConstant(R, n)(a), j, n)(k) =190        Ring.zero(polynomialRing(R, J))191      intros k hk192      have termValue := chainTerms.value at R, J, φ,193        polynomialConstant(R, n)(a), j, n, k with *194      have derivativeZero := partialDerivative.constant at R, n, k, a with *195      have pieces := variableTerms with k, hk196      have derivativeJ := pieces.right197      rw [termValue, derivativeZero, mappedZero]198      ring199    have vanishing := indexedRingSum.identity_zero at polynomialRing(R, J),200      chainTerms(R, J, φ, polynomialConstant(R, n)(a), j, n), n with *, terms201    rw vanishing202    rfl203  have variables : ∀ m ∈ n,204      polynomialVariable(n, R.carrier, R.zero, R.one, m) ∈205        chainPolynomials(R, J, φ, j, n)206    intros m hm207    have variableN : X(R, n, m) ∈ polynomialRing(R, n) :=208      X.mem at R, n, m with *209    have variableParts := polynomialRing.member_parts at R, n, X(R, n, m) with *210    rw ← X.value211    apply chainPolynomials.membership.backward212    refine ⟨variableParts, _⟩213    unfold ChainRule214    have piecesM := variableTerms with m, hm215    have derivativeJ := piecesM.right216    have others : ∀ k ∈ n, k ≠ m →217        chainTerms(R, J, φ, X(R, n, m), j, n)(k) =218        Ring.zero(polynomialRing(R, J))219      intros k hk different220      have reverse : m ≠ k by cc221      have termValue := chainTerms.value at R, J, φ, X(R, n, m), j, n, k with *222      have derivativeZero := partialDerivative.X_other at R, n, k, m with *223      have pieces := variableTerms with k, hk224      have derivativeJK := pieces.right225      rw [termValue, derivativeZero, mappedZero]226      ring227    have same : chainTerms(R, J, φ, X(R, n, m), j, n)(m) =228        partialDerivative(R, J, j)(φ(X(R, n, m)))229      have termValue := chainTerms.value at R, J, φ, X(R, n, m), j, n, m with *230      have derivativeOne := partialDerivative.X_self at R, n, m with *231      rw [termValue, derivativeOne, mappedOne]232      ring233    have single := indexedRingSum.identity_single at polynomialRing(R, J),234      chainTerms(R, J, φ, X(R, n, m), j, n), m,235      partialDerivative(R, J, j)(φ(X(R, n, m))), n236      with ringJ, derivativeJ, hm, others, same237    rw single238    rfl239  have sums : ∀ u ∈ chainPolynomials(R, J, φ, j, n),240      ∀ v ∈ chainPolynomials(R, J, φ, j, n),241      coefficientAdd(monomials(n), R.carrier, R.add, R.zero, R.neg, R.mul,242        R.one, u, v) ∈ chainPolynomials(R, J, φ, j, n)243    intros u hu v hv244    have dataU := chainPolynomials.membership.forward with hu245    have dataV := chainPolynomials.membership.forward with hv246    have uParts := dataU.left247    have uIn := polynomialRing.member_of_parts at R, n, u with *248    have vParts := dataV.left249    have vIn := polynomialRing.member_of_parts at R, n, v with *250    have ruleU := dataU.right251    have ruleV := dataV.right252    unfold ChainRule at ruleU253    unfold ChainRule at ruleV254    have sumForm := polynomialRing.add_apply at R, n, u, v with *255    have sumIn : u + v ∈ polynomialRing(R, n) := Ring.add_mem with *256    have sumParts := polynomialRing.member_parts at R, n, u + v with *257    rw ← sumForm258    apply chainPolynomials.membership.backward259    refine ⟨sumParts, _⟩260    unfold ChainRule261    have mappedU : φ(u) ∈ polynomialRing(R, J) :=262      RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J), u263        with *264    have mappedV : φ(v) ∈ polynomialRing(R, J) :=265      RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J), v266        with *267    have mappedSum := RingHom.map_add at φ, polynomialRing(R, n),268      polynomialRing(R, J), u, v with *269    have additive := partialDerivative.add at R, J, j, φ(u), φ(v) with *270    rw [mappedSum, additive, ruleU, ruleV]271    have valuesU := chainTerms.values at R, J, φ, u, j, n with *272    have valuesV := chainTerms.values at R, J, φ, v, j, n with *273    have equations : ∀ k ∈ n, chainTerms(R, J, φ, u + v, j, n)(k) =274        call2(Ring.add(polynomialRing(R, J)), chainTerms(R, J, φ, u, j, n)(k),275          chainTerms(R, J, φ, v, j, n)(k))276      intros k hk277      have termSum := chainTerms.value at R, J, φ, u + v, j, n, k with *278      have termU := chainTerms.value at R, J, φ, u, j, n, k with *279      have termV := chainTerms.value at R, J, φ, v, j, n, k with *280      have derivativeSum := partialDerivative.add at R, n, k, u, v with *281      have derivativeU :282          partialDerivative(R, n, k)(u) ∈ polynomialRing(R, n) :=283        partialDerivative.map_mem at R, n, k, u with *284      have derivativeV :285          partialDerivative(R, n, k)(v) ∈ polynomialRing(R, n) :=286        partialDerivative.map_mem at R, n, k, v with *287      have mappedDerivativeU :288          φ(partialDerivative(R, n, k)(u)) ∈ polynomialRing(R, J) :=289        RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J),290          partialDerivative(R, n, k)(u) with *291      have mappedDerivativeV :292          φ(partialDerivative(R, n, k)(v)) ∈ polynomialRing(R, J) :=293        RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J),294          partialDerivative(R, n, k)(v) with *295      have mappedDerivativeSum := RingHom.map_add at φ, polynomialRing(R, n),296        polynomialRing(R, J), partialDerivative(R, n, k)(u),297        partialDerivative(R, n, k)(v) with *298      have pieces := variableTerms with k, hk299      have derivativeJK := pieces.right300      rw [termSum, termU, termV, derivativeSum, mappedDerivativeSum]301      ring302    have combined := indexedRingSum.identity_add at polynomialRing(R, J),303      chainTerms(R, J, φ, u, j, n), chainTerms(R, J, φ, v, j, n),304      chainTerms(R, J, φ, u + v, j, n), n305      with ringJ, valuesU, valuesV, equations306    rw combined307    rfl308  have products : ∀ u ∈ chainPolynomials(R, J, φ, j, n),309      ∀ v ∈ chainPolynomials(R, J, φ, j, n),310      polynomialProduct(n, R.carrier, R.add, R.zero, R.neg, R.mul, R.one, u, v)311        ∈ chainPolynomials(R, J, φ, j, n)312    intros u hu v hv313    have dataU := chainPolynomials.membership.forward with hu314    have dataV := chainPolynomials.membership.forward with hv315    have uParts := dataU.left316    have uIn := polynomialRing.member_of_parts at R, n, u with *317    have vParts := dataV.left318    have vIn := polynomialRing.member_of_parts at R, n, v with *319    have ruleU := dataU.right320    have ruleV := dataV.right321    unfold ChainRule at ruleU322    unfold ChainRule at ruleV323    have productForm := polynomialRing.mul_apply at R, n, u, v with *324    have productIn : u * v ∈ polynomialRing(R, n) := Ring.mul_mem with *325    have productParts := polynomialRing.member_parts at R, n, u * v with *326    rw ← productForm327    apply chainPolynomials.membership.backward328    refine ⟨productParts, _⟩329    unfold ChainRule330    have mappedU : φ(u) ∈ polynomialRing(R, J) :=331      RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J), u332        with *333    have mappedV : φ(v) ∈ polynomialRing(R, J) :=334      RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J), v335        with *336    have mappedProduct := RingHom.map_mul at φ, polynomialRing(R, n),337      polynomialRing(R, J), u, v with *338    have leibniz := partialDerivative.mul at R, J, j, φ(u), φ(v) with *339    rw [mappedProduct, leibniz, ruleU, ruleV]340    have valuesU := chainTerms.values at R, J, φ, u, j, n with *341    have valuesV := chainTerms.values at R, J, φ, v, j, n with *342    have equations : ∀ k ∈ n, chainTerms(R, J, φ, u * v, j, n)(k) =343        call2(Ring.add(polynomialRing(R, J)),344          call2(Ring.mul(polynomialRing(R, J)),345            chainTerms(R, J, φ, u, j, n)(k), φ(v)),346          call2(Ring.mul(polynomialRing(R, J)),347            φ(u), chainTerms(R, J, φ, v, j, n)(k)))348      intros k hk349      have termProduct := chainTerms.value at R, J, φ, u * v, j, n, k with *350      have termU := chainTerms.value at R, J, φ, u, j, n, k with *351      have termV := chainTerms.value at R, J, φ, v, j, n, k with *352      have derivativeU :353          partialDerivative(R, n, k)(u) ∈ polynomialRing(R, n) :=354        partialDerivative.map_mem at R, n, k, u with *355      have derivativeV :356          partialDerivative(R, n, k)(v) ∈ polynomialRing(R, n) :=357        partialDerivative.map_mem at R, n, k, v with *358      have leftIn :359          partialDerivative(R, n, k)(u) * v ∈ polynomialRing(R, n) :=360        Ring.mul_mem with *361      have rightIn :362          u * partialDerivative(R, n, k)(v) ∈ polynomialRing(R, n) :=363        Ring.mul_mem with *364      have derivativeProduct := partialDerivative.mul at R, n, k, u, v with *365      have mappedDerivativeU :366          φ(partialDerivative(R, n, k)(u)) ∈ polynomialRing(R, J) :=367        RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J),368          partialDerivative(R, n, k)(u) with *369      have mappedDerivativeV :370          φ(partialDerivative(R, n, k)(v)) ∈ polynomialRing(R, J) :=371        RingHom.map_mem at φ, polynomialRing(R, n), polynomialRing(R, J),372          partialDerivative(R, n, k)(v) with *373      have mappedSum := RingHom.map_add at φ, polynomialRing(R, n),374        polynomialRing(R, J), partialDerivative(R, n, k)(u) * v,375        u * partialDerivative(R, n, k)(v) with *376      have mappedLeft := RingHom.map_mul at φ, polynomialRing(R, n),377        polynomialRing(R, J), partialDerivative(R, n, k)(u), v with *378      have mappedRight := RingHom.map_mul at φ, polynomialRing(R, n),379        polynomialRing(R, J), u, partialDerivative(R, n, k)(v) with *380      have pieces := variableTerms with k, hk381      have derivativeJK := pieces.right382      rw [termProduct, termU, termV, derivativeProduct, mappedSum,383        mappedLeft, mappedRight]384      ring385    have combined := indexedRingSum.identity_bilinear at polynomialRing(R, J),386      chainTerms(R, J, φ, u, j, n), chainTerms(R, J, φ, v, j, n),387      chainTerms(R, J, φ, u * v, j, n), φ(u), φ(v), n388      with ringJ, mappedU, mappedV, valuesU, valuesV, equations389    rw combined390    rfl391  have generated := polynomials.generated at n, R.carrier, R.add, R.zero,392    R.neg, R.mul, R.one, chainPolynomials(R, J, φ, j, n), f393    with parts, fParts, constants, variables, sums, products394  have data := chainPolynomials.membership.forward with generated395  exact data.right396397end ChainRule398