src/HOL/Library/Inner_Product.thy
author wenzelm
Mon Dec 28 01:28:28 2015 +0100 (2015-12-28)
changeset 61945 1135b8de26c3
parent 61915 e9812a95d108
child 62101 26c0a70f78a3
permissions -rw-r--r--
more symbols;
     1 (*  Title:      HOL/Library/Inner_Product.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 section \<open>Inner Product Spaces and the Gradient Derivative\<close>
     6 
     7 theory Inner_Product
     8 imports "~~/src/HOL/Complex_Main"
     9 begin
    10 
    11 subsection \<open>Real inner product spaces\<close>
    12 
    13 text \<open>
    14   Temporarily relax type constraints for @{term "open"},
    15   @{term dist}, and @{term norm}.
    16 \<close>
    17 
    18 setup \<open>Sign.add_const_constraint
    19   (@{const_name "open"}, SOME @{typ "'a::open set \<Rightarrow> bool"})\<close>
    20 
    21 setup \<open>Sign.add_const_constraint
    22   (@{const_name dist}, SOME @{typ "'a::dist \<Rightarrow> 'a \<Rightarrow> real"})\<close>
    23 
    24 setup \<open>Sign.add_const_constraint
    25   (@{const_name norm}, SOME @{typ "'a::norm \<Rightarrow> real"})\<close>
    26 
    27 class real_inner = real_vector + sgn_div_norm + dist_norm + open_dist +
    28   fixes inner :: "'a \<Rightarrow> 'a \<Rightarrow> real"
    29   assumes inner_commute: "inner x y = inner y x"
    30   and inner_add_left: "inner (x + y) z = inner x z + inner y z"
    31   and inner_scaleR_left [simp]: "inner (scaleR r x) y = r * (inner x y)"
    32   and inner_ge_zero [simp]: "0 \<le> inner x x"
    33   and inner_eq_zero_iff [simp]: "inner x x = 0 \<longleftrightarrow> x = 0"
    34   and norm_eq_sqrt_inner: "norm x = sqrt (inner x x)"
    35 begin
    36 
    37 lemma inner_zero_left [simp]: "inner 0 x = 0"
    38   using inner_add_left [of 0 0 x] by simp
    39 
    40 lemma inner_minus_left [simp]: "inner (- x) y = - inner x y"
    41   using inner_add_left [of x "- x" y] by simp
    42 
    43 lemma inner_diff_left: "inner (x - y) z = inner x z - inner y z"
    44   using inner_add_left [of x "- y" z] by simp
    45 
    46 lemma inner_setsum_left: "inner (\<Sum>x\<in>A. f x) y = (\<Sum>x\<in>A. inner (f x) y)"
    47   by (cases "finite A", induct set: finite, simp_all add: inner_add_left)
    48 
    49 text \<open>Transfer distributivity rules to right argument.\<close>
    50 
    51 lemma inner_add_right: "inner x (y + z) = inner x y + inner x z"
    52   using inner_add_left [of y z x] by (simp only: inner_commute)
    53 
    54 lemma inner_scaleR_right [simp]: "inner x (scaleR r y) = r * (inner x y)"
    55   using inner_scaleR_left [of r y x] by (simp only: inner_commute)
    56 
    57 lemma inner_zero_right [simp]: "inner x 0 = 0"
    58   using inner_zero_left [of x] by (simp only: inner_commute)
    59 
    60 lemma inner_minus_right [simp]: "inner x (- y) = - inner x y"
    61   using inner_minus_left [of y x] by (simp only: inner_commute)
    62 
    63 lemma inner_diff_right: "inner x (y - z) = inner x y - inner x z"
    64   using inner_diff_left [of y z x] by (simp only: inner_commute)
    65 
    66 lemma inner_setsum_right: "inner x (\<Sum>y\<in>A. f y) = (\<Sum>y\<in>A. inner x (f y))"
    67   using inner_setsum_left [of f A x] by (simp only: inner_commute)
    68 
    69 lemmas inner_add [algebra_simps] = inner_add_left inner_add_right
    70 lemmas inner_diff [algebra_simps]  = inner_diff_left inner_diff_right
    71 lemmas inner_scaleR = inner_scaleR_left inner_scaleR_right
    72 
    73 text \<open>Legacy theorem names\<close>
    74 lemmas inner_left_distrib = inner_add_left
    75 lemmas inner_right_distrib = inner_add_right
    76 lemmas inner_distrib = inner_left_distrib inner_right_distrib
    77 
    78 lemma inner_gt_zero_iff [simp]: "0 < inner x x \<longleftrightarrow> x \<noteq> 0"
    79   by (simp add: order_less_le)
    80 
    81 lemma power2_norm_eq_inner: "(norm x)\<^sup>2 = inner x x"
    82   by (simp add: norm_eq_sqrt_inner)
    83 
    84 text \<open>Identities involving real multiplication and division.\<close>
    85 
    86 lemma inner_mult_left: "inner (of_real m * a) b = m * (inner a b)"
    87   by (metis real_inner_class.inner_scaleR_left scaleR_conv_of_real)
    88 
    89 lemma inner_mult_right: "inner a (of_real m * b) = m * (inner a b)"
    90   by (metis real_inner_class.inner_scaleR_right scaleR_conv_of_real)
    91 
    92 lemma inner_mult_left': "inner (a * of_real m) b = m * (inner a b)"
    93   by (simp add: of_real_def)
    94 
    95 lemma inner_mult_right': "inner a (b * of_real m) = (inner a b) * m"
    96   by (simp add: of_real_def real_inner_class.inner_scaleR_right)
    97 
    98 lemma Cauchy_Schwarz_ineq:
    99   "(inner x y)\<^sup>2 \<le> inner x x * inner y y"
   100 proof (cases)
   101   assume "y = 0"
   102   thus ?thesis by simp
   103 next
   104   assume y: "y \<noteq> 0"
   105   let ?r = "inner x y / inner y y"
   106   have "0 \<le> inner (x - scaleR ?r y) (x - scaleR ?r y)"
   107     by (rule inner_ge_zero)
   108   also have "\<dots> = inner x x - inner y x * ?r"
   109     by (simp add: inner_diff)
   110   also have "\<dots> = inner x x - (inner x y)\<^sup>2 / inner y y"
   111     by (simp add: power2_eq_square inner_commute)
   112   finally have "0 \<le> inner x x - (inner x y)\<^sup>2 / inner y y" .
   113   hence "(inner x y)\<^sup>2 / inner y y \<le> inner x x"
   114     by (simp add: le_diff_eq)
   115   thus "(inner x y)\<^sup>2 \<le> inner x x * inner y y"
   116     by (simp add: pos_divide_le_eq y)
   117 qed
   118 
   119 lemma Cauchy_Schwarz_ineq2:
   120   "\<bar>inner x y\<bar> \<le> norm x * norm y"
   121 proof (rule power2_le_imp_le)
   122   have "(inner x y)\<^sup>2 \<le> inner x x * inner y y"
   123     using Cauchy_Schwarz_ineq .
   124   thus "\<bar>inner x y\<bar>\<^sup>2 \<le> (norm x * norm y)\<^sup>2"
   125     by (simp add: power_mult_distrib power2_norm_eq_inner)
   126   show "0 \<le> norm x * norm y"
   127     unfolding norm_eq_sqrt_inner
   128     by (intro mult_nonneg_nonneg real_sqrt_ge_zero inner_ge_zero)
   129 qed
   130 
   131 lemma norm_cauchy_schwarz: "inner x y \<le> norm x * norm y"
   132   using Cauchy_Schwarz_ineq2 [of x y] by auto
   133 
   134 subclass real_normed_vector
   135 proof
   136   fix a :: real and x y :: 'a
   137   show "norm x = 0 \<longleftrightarrow> x = 0"
   138     unfolding norm_eq_sqrt_inner by simp
   139   show "norm (x + y) \<le> norm x + norm y"
   140     proof (rule power2_le_imp_le)
   141       have "inner x y \<le> norm x * norm y"
   142         by (rule norm_cauchy_schwarz)
   143       thus "(norm (x + y))\<^sup>2 \<le> (norm x + norm y)\<^sup>2"
   144         unfolding power2_sum power2_norm_eq_inner
   145         by (simp add: inner_add inner_commute)
   146       show "0 \<le> norm x + norm y"
   147         unfolding norm_eq_sqrt_inner by simp
   148     qed
   149   have "sqrt (a\<^sup>2 * inner x x) = \<bar>a\<bar> * sqrt (inner x x)"
   150     by (simp add: real_sqrt_mult_distrib)
   151   then show "norm (a *\<^sub>R x) = \<bar>a\<bar> * norm x"
   152     unfolding norm_eq_sqrt_inner
   153     by (simp add: power2_eq_square mult.assoc)
   154 qed
   155 
   156 end
   157 
   158 lemma inner_divide_left:
   159   fixes a :: "'a :: {real_inner,real_div_algebra}"
   160   shows "inner (a / of_real m) b = (inner a b) / m"
   161   by (metis (no_types) divide_inverse inner_commute inner_scaleR_right mult.left_neutral mult.right_neutral mult_scaleR_right of_real_inverse scaleR_conv_of_real times_divide_eq_left)
   162 
   163 lemma inner_divide_right:
   164   fixes a :: "'a :: {real_inner,real_div_algebra}"
   165   shows "inner a (b / of_real m) = (inner a b) / m"
   166   by (metis inner_commute inner_divide_left)
   167 
   168 text \<open>
   169   Re-enable constraints for @{term "open"},
   170   @{term dist}, and @{term norm}.
   171 \<close>
   172 
   173 setup \<open>Sign.add_const_constraint
   174   (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"})\<close>
   175 
   176 setup \<open>Sign.add_const_constraint
   177   (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"})\<close>
   178 
   179 setup \<open>Sign.add_const_constraint
   180   (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"})\<close>
   181 
   182 lemma bounded_bilinear_inner:
   183   "bounded_bilinear (inner::'a::real_inner \<Rightarrow> 'a \<Rightarrow> real)"
   184 proof
   185   fix x y z :: 'a and r :: real
   186   show "inner (x + y) z = inner x z + inner y z"
   187     by (rule inner_add_left)
   188   show "inner x (y + z) = inner x y + inner x z"
   189     by (rule inner_add_right)
   190   show "inner (scaleR r x) y = scaleR r (inner x y)"
   191     unfolding real_scaleR_def by (rule inner_scaleR_left)
   192   show "inner x (scaleR r y) = scaleR r (inner x y)"
   193     unfolding real_scaleR_def by (rule inner_scaleR_right)
   194   show "\<exists>K. \<forall>x y::'a. norm (inner x y) \<le> norm x * norm y * K"
   195   proof
   196     show "\<forall>x y::'a. norm (inner x y) \<le> norm x * norm y * 1"
   197       by (simp add: Cauchy_Schwarz_ineq2)
   198   qed
   199 qed
   200 
   201 lemmas tendsto_inner [tendsto_intros] =
   202   bounded_bilinear.tendsto [OF bounded_bilinear_inner]
   203 
   204 lemmas isCont_inner [simp] =
   205   bounded_bilinear.isCont [OF bounded_bilinear_inner]
   206 
   207 lemmas has_derivative_inner [derivative_intros] =
   208   bounded_bilinear.FDERIV [OF bounded_bilinear_inner]
   209 
   210 lemmas bounded_linear_inner_left =
   211   bounded_bilinear.bounded_linear_left [OF bounded_bilinear_inner]
   212 
   213 lemmas bounded_linear_inner_right =
   214   bounded_bilinear.bounded_linear_right [OF bounded_bilinear_inner]
   215 
   216 lemmas bounded_linear_inner_left_comp = bounded_linear_inner_left[THEN bounded_linear_compose]
   217 
   218 lemmas bounded_linear_inner_right_comp = bounded_linear_inner_right[THEN bounded_linear_compose]
   219 
   220 lemmas has_derivative_inner_right [derivative_intros] =
   221   bounded_linear.has_derivative [OF bounded_linear_inner_right]
   222 
   223 lemmas has_derivative_inner_left [derivative_intros] =
   224   bounded_linear.has_derivative [OF bounded_linear_inner_left]
   225 
   226 lemma differentiable_inner [simp]:
   227   "f differentiable (at x within s) \<Longrightarrow> g differentiable at x within s \<Longrightarrow> (\<lambda>x. inner (f x) (g x)) differentiable at x within s"
   228   unfolding differentiable_def by (blast intro: has_derivative_inner)
   229 
   230 
   231 subsection \<open>Class instances\<close>
   232 
   233 instantiation real :: real_inner
   234 begin
   235 
   236 definition inner_real_def [simp]: "inner = op *"
   237 
   238 instance
   239 proof
   240   fix x y z r :: real
   241   show "inner x y = inner y x"
   242     unfolding inner_real_def by (rule mult.commute)
   243   show "inner (x + y) z = inner x z + inner y z"
   244     unfolding inner_real_def by (rule distrib_right)
   245   show "inner (scaleR r x) y = r * inner x y"
   246     unfolding inner_real_def real_scaleR_def by (rule mult.assoc)
   247   show "0 \<le> inner x x"
   248     unfolding inner_real_def by simp
   249   show "inner x x = 0 \<longleftrightarrow> x = 0"
   250     unfolding inner_real_def by simp
   251   show "norm x = sqrt (inner x x)"
   252     unfolding inner_real_def by simp
   253 qed
   254 
   255 end
   256 
   257 instantiation complex :: real_inner
   258 begin
   259 
   260 definition inner_complex_def:
   261   "inner x y = Re x * Re y + Im x * Im y"
   262 
   263 instance
   264 proof
   265   fix x y z :: complex and r :: real
   266   show "inner x y = inner y x"
   267     unfolding inner_complex_def by (simp add: mult.commute)
   268   show "inner (x + y) z = inner x z + inner y z"
   269     unfolding inner_complex_def by (simp add: distrib_right)
   270   show "inner (scaleR r x) y = r * inner x y"
   271     unfolding inner_complex_def by (simp add: distrib_left)
   272   show "0 \<le> inner x x"
   273     unfolding inner_complex_def by simp
   274   show "inner x x = 0 \<longleftrightarrow> x = 0"
   275     unfolding inner_complex_def
   276     by (simp add: add_nonneg_eq_0_iff complex_Re_Im_cancel_iff)
   277   show "norm x = sqrt (inner x x)"
   278     unfolding inner_complex_def complex_norm_def
   279     by (simp add: power2_eq_square)
   280 qed
   281 
   282 end
   283 
   284 lemma complex_inner_1 [simp]: "inner 1 x = Re x"
   285   unfolding inner_complex_def by simp
   286 
   287 lemma complex_inner_1_right [simp]: "inner x 1 = Re x"
   288   unfolding inner_complex_def by simp
   289 
   290 lemma complex_inner_ii_left [simp]: "inner ii x = Im x"
   291   unfolding inner_complex_def by simp
   292 
   293 lemma complex_inner_ii_right [simp]: "inner x ii = Im x"
   294   unfolding inner_complex_def by simp
   295 
   296 
   297 subsection \<open>Gradient derivative\<close>
   298 
   299 definition
   300   gderiv ::
   301     "['a::real_inner \<Rightarrow> real, 'a, 'a] \<Rightarrow> bool"
   302           ("(GDERIV (_)/ (_)/ :> (_))" [1000, 1000, 60] 60)
   303 where
   304   "GDERIV f x :> D \<longleftrightarrow> FDERIV f x :> (\<lambda>h. inner h D)"
   305 
   306 lemma gderiv_deriv [simp]: "GDERIV f x :> D \<longleftrightarrow> DERIV f x :> D"
   307   by (simp only: gderiv_def has_field_derivative_def inner_real_def mult_commute_abs)
   308 
   309 lemma GDERIV_DERIV_compose:
   310     "\<lbrakk>GDERIV f x :> df; DERIV g (f x) :> dg\<rbrakk>
   311      \<Longrightarrow> GDERIV (\<lambda>x. g (f x)) x :> scaleR dg df"
   312   unfolding gderiv_def has_field_derivative_def
   313   apply (drule (1) has_derivative_compose)
   314   apply (simp add: ac_simps)
   315   done
   316 
   317 lemma has_derivative_subst: "\<lbrakk>FDERIV f x :> df; df = d\<rbrakk> \<Longrightarrow> FDERIV f x :> d"
   318   by simp
   319 
   320 lemma GDERIV_subst: "\<lbrakk>GDERIV f x :> df; df = d\<rbrakk> \<Longrightarrow> GDERIV f x :> d"
   321   by simp
   322 
   323 lemma GDERIV_const: "GDERIV (\<lambda>x. k) x :> 0"
   324   unfolding gderiv_def inner_zero_right by (rule has_derivative_const)
   325 
   326 lemma GDERIV_add:
   327     "\<lbrakk>GDERIV f x :> df; GDERIV g x :> dg\<rbrakk>
   328      \<Longrightarrow> GDERIV (\<lambda>x. f x + g x) x :> df + dg"
   329   unfolding gderiv_def inner_add_right by (rule has_derivative_add)
   330 
   331 lemma GDERIV_minus:
   332     "GDERIV f x :> df \<Longrightarrow> GDERIV (\<lambda>x. - f x) x :> - df"
   333   unfolding gderiv_def inner_minus_right by (rule has_derivative_minus)
   334 
   335 lemma GDERIV_diff:
   336     "\<lbrakk>GDERIV f x :> df; GDERIV g x :> dg\<rbrakk>
   337      \<Longrightarrow> GDERIV (\<lambda>x. f x - g x) x :> df - dg"
   338   unfolding gderiv_def inner_diff_right by (rule has_derivative_diff)
   339 
   340 lemma GDERIV_scaleR:
   341     "\<lbrakk>DERIV f x :> df; GDERIV g x :> dg\<rbrakk>
   342      \<Longrightarrow> GDERIV (\<lambda>x. scaleR (f x) (g x)) x
   343       :> (scaleR (f x) dg + scaleR df (g x))"
   344   unfolding gderiv_def has_field_derivative_def inner_add_right inner_scaleR_right
   345   apply (rule has_derivative_subst)
   346   apply (erule (1) has_derivative_scaleR)
   347   apply (simp add: ac_simps)
   348   done
   349 
   350 lemma GDERIV_mult:
   351     "\<lbrakk>GDERIV f x :> df; GDERIV g x :> dg\<rbrakk>
   352      \<Longrightarrow> GDERIV (\<lambda>x. f x * g x) x :> scaleR (f x) dg + scaleR (g x) df"
   353   unfolding gderiv_def
   354   apply (rule has_derivative_subst)
   355   apply (erule (1) has_derivative_mult)
   356   apply (simp add: inner_add ac_simps)
   357   done
   358 
   359 lemma GDERIV_inverse:
   360     "\<lbrakk>GDERIV f x :> df; f x \<noteq> 0\<rbrakk>
   361      \<Longrightarrow> GDERIV (\<lambda>x. inverse (f x)) x :> - (inverse (f x))\<^sup>2 *\<^sub>R df"
   362   apply (erule GDERIV_DERIV_compose)
   363   apply (erule DERIV_inverse [folded numeral_2_eq_2])
   364   done
   365 
   366 lemma GDERIV_norm:
   367   assumes "x \<noteq> 0" shows "GDERIV (\<lambda>x. norm x) x :> sgn x"
   368 proof -
   369   have 1: "FDERIV (\<lambda>x. inner x x) x :> (\<lambda>h. inner x h + inner h x)"
   370     by (intro has_derivative_inner has_derivative_ident)
   371   have 2: "(\<lambda>h. inner x h + inner h x) = (\<lambda>h. inner h (scaleR 2 x))"
   372     by (simp add: fun_eq_iff inner_commute)
   373   have "0 < inner x x" using \<open>x \<noteq> 0\<close> by simp
   374   then have 3: "DERIV sqrt (inner x x) :> (inverse (sqrt (inner x x)) / 2)"
   375     by (rule DERIV_real_sqrt)
   376   have 4: "(inverse (sqrt (inner x x)) / 2) *\<^sub>R 2 *\<^sub>R x = sgn x"
   377     by (simp add: sgn_div_norm norm_eq_sqrt_inner)
   378   show ?thesis
   379     unfolding norm_eq_sqrt_inner
   380     apply (rule GDERIV_subst [OF _ 4])
   381     apply (rule GDERIV_DERIV_compose [where g=sqrt and df="scaleR 2 x"])
   382     apply (subst gderiv_def)
   383     apply (rule has_derivative_subst [OF _ 2])
   384     apply (rule 1)
   385     apply (rule 3)
   386     done
   387 qed
   388 
   389 lemmas has_derivative_norm = GDERIV_norm [unfolded gderiv_def]
   390 
   391 end