
huffman@29993: (* Title:      Inner_Product.thy
huffman@29993:    Author:     Brian Huffman
huffman@29993: *)
huffman@29993: 
huffman@29993: header {* Inner Product Spaces and the Gradient Derivative *}
huffman@29993: 
huffman@29993: theory Inner_Product
haftmann@30663: imports Complex_Main FrechetDeriv
huffman@29993: begin
huffman@29993: 
huffman@29993: subsection {* Real inner product spaces *}
huffman@29993: 
huffman@31492: text {*
huffman@31492:   Temporarily relax type constraints for @{term "open"},
huffman@31492:   @{term dist}, and @{term norm}.
huffman@31492: *}
huffman@31492: 
huffman@31492: setup {* Sign.add_const_constraint
huffman@31492:   (@{const_name "open"}, SOME @{typ "'a::open set \<Rightarrow> bool"}) *}
huffman@31446: 
huffman@31446: setup {* Sign.add_const_constraint
huffman@31446:   (@{const_name dist}, SOME @{typ "'a::dist \<Rightarrow> 'a \<Rightarrow> real"}) *}
huffman@31446: 
huffman@31446: setup {* Sign.add_const_constraint
huffman@31446:   (@{const_name norm}, SOME @{typ "'a::norm \<Rightarrow> real"}) *}
huffman@31446: 
huffman@31492: class real_inner = real_vector + sgn_div_norm + dist_norm + open_dist +
huffman@29993:   fixes inner :: "'a \<Rightarrow> 'a \<Rightarrow> real"
huffman@29993:   assumes inner_commute: "inner x y = inner y x"
huffman@29993:   and inner_left_distrib: "inner (x + y) z = inner x z + inner y z"
huffman@29993:   and inner_scaleR_left: "inner (scaleR r x) y = r * (inner x y)"
huffman@29993:   and inner_ge_zero [simp]: "0 \<le> inner x x"
huffman@29993:   and inner_eq_zero_iff [simp]: "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@29993:   and norm_eq_sqrt_inner: "norm x = sqrt (inner x x)"
huffman@29993: begin
huffman@29993: 
huffman@29993: lemma inner_zero_left [simp]: "inner 0 x = 0"
huffman@30067:   using inner_left_distrib [of 0 0 x] by simp
huffman@29993: 
huffman@29993: lemma inner_minus_left [simp]: "inner (- x) y = - inner x y"
huffman@30067:   using inner_left_distrib [of x "- x" y] by simp
huffman@29993: 
huffman@29993: lemma inner_diff_left: "inner (x - y) z = inner x z - inner y z"
huffman@29993:   by (simp add: diff_minus inner_left_distrib)
huffman@29993: 
huffman@29993: text {* Transfer distributivity rules to right argument. *}
huffman@29993: 
huffman@29993: lemma inner_right_distrib: "inner x (y + z) = inner x y + inner x z"
huffman@29993:   using inner_left_distrib [of y z x] by (simp only: inner_commute)
huffman@29993: 
huffman@29993: lemma inner_scaleR_right: "inner x (scaleR r y) = r * (inner x y)"
huffman@29993:   using inner_scaleR_left [of r y x] by (simp only: inner_commute)
huffman@29993: 
huffman@29993: lemma inner_zero_right [simp]: "inner x 0 = 0"
huffman@29993:   using inner_zero_left [of x] by (simp only: inner_commute)
huffman@29993: 
huffman@29993: lemma inner_minus_right [simp]: "inner x (- y) = - inner x y"
huffman@29993:   using inner_minus_left [of y x] by (simp only: inner_commute)
huffman@29993: 
huffman@29993: lemma inner_diff_right: "inner x (y - z) = inner x y - inner x z"
huffman@29993:   using inner_diff_left [of y z x] by (simp only: inner_commute)
huffman@29993: 
huffman@29993: lemmas inner_distrib = inner_left_distrib inner_right_distrib
huffman@29993: lemmas inner_diff = inner_diff_left inner_diff_right
huffman@29993: lemmas inner_scaleR = inner_scaleR_left inner_scaleR_right
huffman@29993: 
huffman@29993: lemma inner_gt_zero_iff [simp]: "0 < inner x x \<longleftrightarrow> x \<noteq> 0"
huffman@29993:   by (simp add: order_less_le)
huffman@29993: 
huffman@29993: lemma power2_norm_eq_inner: "(norm x)\<twosuperior> = inner x x"
huffman@29993:   by (simp add: norm_eq_sqrt_inner)
huffman@29993: 
huffman@30046: lemma Cauchy_Schwarz_ineq:
huffman@29993:   "(inner x y)\<twosuperior> \<le> inner x x * inner y y"
huffman@29993: proof (cases)
huffman@29993:   assume "y = 0"
huffman@29993:   thus ?thesis by simp
huffman@29993: next
huffman@29993:   assume y: "y \<noteq> 0"
huffman@29993:   let ?r = "inner x y / inner y y"
huffman@29993:   have "0 \<le> inner (x - scaleR ?r y) (x - scaleR ?r y)"
huffman@29993:     by (rule inner_ge_zero)
huffman@29993:   also have "\<dots> = inner x x - inner y x * ?r"
huffman@29993:     by (simp add: inner_diff inner_scaleR)
huffman@29993:   also have "\<dots> = inner x x - (inner x y)\<twosuperior> / inner y y"
huffman@29993:     by (simp add: power2_eq_square inner_commute)
huffman@29993:   finally have "0 \<le> inner x x - (inner x y)\<twosuperior> / inner y y" .
huffman@29993:   hence "(inner x y)\<twosuperior> / inner y y \<le> inner x x"
huffman@29993:     by (simp add: le_diff_eq)
huffman@29993:   thus "(inner x y)\<twosuperior> \<le> inner x x * inner y y"
huffman@29993:     by (simp add: pos_divide_le_eq y)
huffman@29993: qed
huffman@29993: 
huffman@30046: lemma Cauchy_Schwarz_ineq2:
huffman@29993:   "\<bar>inner x y\<bar> \<le> norm x * norm y"
huffman@29993: proof (rule power2_le_imp_le)
huffman@29993:   have "(inner x y)\<twosuperior> \<le> inner x x * inner y y"
huffman@30046:     using Cauchy_Schwarz_ineq .
huffman@29993:   thus "\<bar>inner x y\<bar>\<twosuperior> \<le> (norm x * norm y)\<twosuperior>"
huffman@29993:     by (simp add: power_mult_distrib power2_norm_eq_inner)
huffman@29993:   show "0 \<le> norm x * norm y"
huffman@29993:     unfolding norm_eq_sqrt_inner
huffman@29993:     by (intro mult_nonneg_nonneg real_sqrt_ge_zero inner_ge_zero)
huffman@29993: qed
huffman@29993: 
huffman@29993: subclass real_normed_vector
huffman@29993: proof
huffman@29993:   fix a :: real and x y :: 'a
huffman@29993:   show "0 \<le> norm x"
huffman@29993:     unfolding norm_eq_sqrt_inner by simp
huffman@29993:   show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@29993:     unfolding norm_eq_sqrt_inner by simp
huffman@29993:   show "norm (x + y) \<le> norm x + norm y"
huffman@29993:     proof (rule power2_le_imp_le)
huffman@29993:       have "inner x y \<le> norm x * norm y"
huffman@30046:         by (rule order_trans [OF abs_ge_self Cauchy_Schwarz_ineq2])
huffman@29993:       thus "(norm (x + y))\<twosuperior> \<le> (norm x + norm y)\<twosuperior>"
huffman@29993:         unfolding power2_sum power2_norm_eq_inner
huffman@29993:         by (simp add: inner_distrib inner_commute)
huffman@29993:       show "0 \<le> norm x + norm y"
huffman@29993:         unfolding norm_eq_sqrt_inner
huffman@29993:         by (simp add: add_nonneg_nonneg)
huffman@29993:     qed
huffman@29993:   have "sqrt (a\<twosuperior> * inner x x) = \<bar>a\<bar> * sqrt (inner x x)"
huffman@29993:     by (simp add: real_sqrt_mult_distrib)
huffman@29993:   then show "norm (a *\<^sub>R x) = \<bar>a\<bar> * norm x"
huffman@29993:     unfolding norm_eq_sqrt_inner
huffman@29993:     by (simp add: inner_scaleR power2_eq_square mult_assoc)
huffman@29993: qed
huffman@29993: 
huffman@29993: end
huffman@29993: 
huffman@31492: text {*
huffman@31492:   Re-enable constraints for @{term "open"},
huffman@31492:   @{term dist}, and @{term norm}.
huffman@31492: *}
huffman@31492: 
huffman@31492: setup {* Sign.add_const_constraint
huffman@31492:   (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"}) *}
huffman@31446: 
huffman@31446: setup {* Sign.add_const_constraint
huffman@31446:   (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"}) *}
huffman@31446: 
huffman@31446: setup {* Sign.add_const_constraint
huffman@31446:   (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"}) *}
huffman@31446: 
wenzelm@30729: interpretation inner:
huffman@29993:   bounded_bilinear "inner::'a::real_inner \<Rightarrow> 'a \<Rightarrow> real"
huffman@29993: proof
huffman@29993:   fix x y z :: 'a and r :: real
huffman@29993:   show "inner (x + y) z = inner x z + inner y z"
huffman@29993:     by (rule inner_left_distrib)
huffman@29993:   show "inner x (y + z) = inner x y + inner x z"
huffman@29993:     by (rule inner_right_distrib)
huffman@29993:   show "inner (scaleR r x) y = scaleR r (inner x y)"
huffman@29993:     unfolding real_scaleR_def by (rule inner_scaleR_left)
huffman@29993:   show "inner x (scaleR r y) = scaleR r (inner x y)"
huffman@29993:     unfolding real_scaleR_def by (rule inner_scaleR_right)
huffman@29993:   show "\<exists>K. \<forall>x y::'a. norm (inner x y) \<le> norm x * norm y * K"
huffman@29993:   proof
huffman@29993:     show "\<forall>x y::'a. norm (inner x y) \<le> norm x * norm y * 1"
huffman@30046:       by (simp add: Cauchy_Schwarz_ineq2)
huffman@29993:   qed
huffman@29993: qed
huffman@29993: 
wenzelm@30729: interpretation inner_left:
huffman@29993:   bounded_linear "\<lambda>x::'a::real_inner. inner x y"
huffman@29993:   by (rule inner.bounded_linear_left)
huffman@29993: 
wenzelm@30729: interpretation inner_right:
huffman@29993:   bounded_linear "\<lambda>y::'a::real_inner. inner x y"
huffman@29993:   by (rule inner.bounded_linear_right)
huffman@29993: 
huffman@29993: 
huffman@29993: subsection {* Class instances *}
huffman@29993: 
huffman@29993: instantiation real :: real_inner
huffman@29993: begin
huffman@29993: 
huffman@29993: definition inner_real_def [simp]: "inner = op *"
huffman@29993: 
huffman@29993: instance proof
huffman@29993:   fix x y z r :: real
huffman@29993:   show "inner x y = inner y x"
huffman@29993:     unfolding inner_real_def by (rule mult_commute)
huffman@29993:   show "inner (x + y) z = inner x z + inner y z"
huffman@29993:     unfolding inner_real_def by (rule left_distrib)
huffman@29993:   show "inner (scaleR r x) y = r * inner x y"
huffman@29993:     unfolding inner_real_def real_scaleR_def by (rule mult_assoc)
huffman@29993:   show "0 \<le> inner x x"
huffman@29993:     unfolding inner_real_def by simp
huffman@29993:   show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@29993:     unfolding inner_real_def by simp
huffman@29993:   show "norm x = sqrt (inner x x)"
huffman@29993:     unfolding inner_real_def by simp
huffman@29993: qed
huffman@29993: 
huffman@29993: end
huffman@29993: 
huffman@29993: instantiation complex :: real_inner
huffman@29993: begin
huffman@29993: 
huffman@29993: definition inner_complex_def:
huffman@29993:   "inner x y = Re x * Re y + Im x * Im y"
huffman@29993: 
huffman@29993: instance proof
huffman@29993:   fix x y z :: complex and r :: real
huffman@29993:   show "inner x y = inner y x"
huffman@29993:     unfolding inner_complex_def by (simp add: mult_commute)
huffman@29993:   show "inner (x + y) z = inner x z + inner y z"
huffman@29993:     unfolding inner_complex_def by (simp add: left_distrib)
huffman@29993:   show "inner (scaleR r x) y = r * inner x y"
huffman@29993:     unfolding inner_complex_def by (simp add: right_distrib)
huffman@29993:   show "0 \<le> inner x x"
huffman@29993:     unfolding inner_complex_def by (simp add: add_nonneg_nonneg)
huffman@29993:   show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@29993:     unfolding inner_complex_def
huffman@29993:     by (simp add: add_nonneg_eq_0_iff complex_Re_Im_cancel_iff)
huffman@29993:   show "norm x = sqrt (inner x x)"
huffman@29993:     unfolding inner_complex_def complex_norm_def
huffman@29993:     by (simp add: power2_eq_square)
huffman@29993: qed
huffman@29993: 
huffman@29993: end
huffman@29993: 
huffman@29993: 
huffman@29993: subsection {* Gradient derivative *}
huffman@29993: 
huffman@29993: definition
huffman@29993:   gderiv ::
huffman@29993:     "['a::real_inner \<Rightarrow> real, 'a, 'a] \<Rightarrow> bool"
huffman@29993:           ("(GDERIV (_)/ (_)/ :> (_))" [1000, 1000, 60] 60)
huffman@29993: where
huffman@29993:   "GDERIV f x :> D \<longleftrightarrow> FDERIV f x :> (\<lambda>h. inner h D)"
huffman@29993: 
huffman@29993: lemma deriv_fderiv: "DERIV f x :> D \<longleftrightarrow> FDERIV f x :> (\<lambda>h. h * D)"
huffman@29993:   by (simp only: deriv_def field_fderiv_def)
huffman@29993: 
huffman@29993: lemma gderiv_deriv [simp]: "GDERIV f x :> D \<longleftrightarrow> DERIV f x :> D"
huffman@29993:   by (simp only: gderiv_def deriv_fderiv inner_real_def)
huffman@29993: 
huffman@29993: lemma GDERIV_DERIV_compose:
huffman@29993:     "\<lbrakk>GDERIV f x :> df; DERIV g (f x) :> dg\<rbrakk>
huffman@29993:      \<Longrightarrow> GDERIV (\<lambda>x. g (f x)) x :> scaleR dg df"
huffman@29993:   unfolding gderiv_def deriv_fderiv
huffman@29993:   apply (drule (1) FDERIV_compose)
huffman@29993:   apply (simp add: inner_scaleR_right mult_ac)
huffman@29993:   done
huffman@29993: 
huffman@29993: lemma FDERIV_subst: "\<lbrakk>FDERIV f x :> df; df = d\<rbrakk> \<Longrightarrow> FDERIV f x :> d"
huffman@29993:   by simp
huffman@29993: 
huffman@29993: lemma GDERIV_subst: "\<lbrakk>GDERIV f x :> df; df = d\<rbrakk> \<Longrightarrow> GDERIV f x :> d"
huffman@29993:   by simp
huffman@29993: 
huffman@29993: lemma GDERIV_const: "GDERIV (\<lambda>x. k) x :> 0"
huffman@29993:   unfolding gderiv_def inner_right.zero by (rule FDERIV_const)
huffman@29993: 
huffman@29993: lemma GDERIV_add:
huffman@29993:     "\<lbrakk>GDERIV f x :> df; GDERIV g x :> dg\<rbrakk>
huffman@29993:      \<Longrightarrow> GDERIV (\<lambda>x. f x + g x) x :> df + dg"
huffman@29993:   unfolding gderiv_def inner_right.add by (rule FDERIV_add)
huffman@29993: 
huffman@29993: lemma GDERIV_minus:
huffman@29993:     "GDERIV f x :> df \<Longrightarrow> GDERIV (\<lambda>x. - f x) x :> - df"
huffman@29993:   unfolding gderiv_def inner_right.minus by (rule FDERIV_minus)
huffman@29993: 
huffman@29993: lemma GDERIV_diff:
huffman@29993:     "\<lbrakk>GDERIV f x :> df; GDERIV g x :> dg\<rbrakk>
huffman@29993:      \<Longrightarrow> GDERIV (\<lambda>x. f x - g x) x :> df - dg"
huffman@29993:   unfolding gderiv_def inner_right.diff by (rule FDERIV_diff)
huffman@29993: 
huffman@29993: lemma GDERIV_scaleR:
huffman@29993:     "\<lbrakk>DERIV f x :> df; GDERIV g x :> dg\<rbrakk>
huffman@29993:      \<Longrightarrow> GDERIV (\<lambda>x. scaleR (f x) (g x)) x
huffman@29993:       :> (scaleR (f x) dg + scaleR df (g x))"
huffman@29993:   unfolding gderiv_def deriv_fderiv inner_right.add inner_right.scaleR
huffman@29993:   apply (rule FDERIV_subst)
huffman@29993:   apply (erule (1) scaleR.FDERIV)
huffman@29993:   apply (simp add: mult_ac)
huffman@29993:   done
huffman@29993: 
huffman@29993: lemma GDERIV_mult:
huffman@29993:     "\<lbrakk>GDERIV f x :> df; GDERIV g x :> dg\<rbrakk>
huffman@29993:      \<Longrightarrow> GDERIV (\<lambda>x. f x * g x) x :> scaleR (f x) dg + scaleR (g x) df"
huffman@29993:   unfolding gderiv_def
huffman@29993:   apply (rule FDERIV_subst)
huffman@29993:   apply (erule (1) FDERIV_mult)
huffman@29993:   apply (simp add: inner_distrib inner_scaleR mult_ac)
huffman@29993:   done
huffman@29993: 
huffman@29993: lemma GDERIV_inverse:
huffman@29993:     "\<lbrakk>GDERIV f x :> df; f x \<noteq> 0\<rbrakk>
huffman@29993:      \<Longrightarrow> GDERIV (\<lambda>x. inverse (f x)) x :> - (inverse (f x))\<twosuperior> *\<^sub>R df"
huffman@29993:   apply (erule GDERIV_DERIV_compose)
huffman@29993:   apply (erule DERIV_inverse [folded numeral_2_eq_2])
huffman@29993:   done
huffman@29993: 
huffman@29993: lemma GDERIV_norm:
huffman@29993:   assumes "x \<noteq> 0" shows "GDERIV (\<lambda>x. norm x) x :> sgn x"
huffman@29993: proof -
huffman@29993:   have 1: "FDERIV (\<lambda>x. inner x x) x :> (\<lambda>h. inner x h + inner h x)"
huffman@29993:     by (intro inner.FDERIV FDERIV_ident)
huffman@29993:   have 2: "(\<lambda>h. inner x h + inner h x) = (\<lambda>h. inner h (scaleR 2 x))"
huffman@29993:     by (simp add: expand_fun_eq inner_scaleR inner_commute)
huffman@29993:   have "0 < inner x x" using `x \<noteq> 0` by simp
huffman@29993:   then have 3: "DERIV sqrt (inner x x) :> (inverse (sqrt (inner x x)) / 2)"
huffman@29993:     by (rule DERIV_real_sqrt)
huffman@29993:   have 4: "(inverse (sqrt (inner x x)) / 2) *\<^sub>R 2 *\<^sub>R x = sgn x"
huffman@29993:     by (simp add: sgn_div_norm norm_eq_sqrt_inner)
huffman@29993:   show ?thesis
huffman@29993:     unfolding norm_eq_sqrt_inner
huffman@29993:     apply (rule GDERIV_subst [OF _ 4])
huffman@29993:     apply (rule GDERIV_DERIV_compose [where g=sqrt and df="scaleR 2 x"])
huffman@29993:     apply (subst gderiv_def)
huffman@29993:     apply (rule FDERIV_subst [OF _ 2])
huffman@29993:     apply (rule 1)
huffman@29993:     apply (rule 3)
huffman@29993:     done
huffman@29993: qed
huffman@29993: 
huffman@29993: lemmas FDERIV_norm = GDERIV_norm [unfolded gderiv_def]
huffman@29993: 
huffman@29993: end




