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