-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcauchy_schwarz.iml
More file actions
328 lines (252 loc) · 9.61 KB
/
cauchy_schwarz.iml
File metadata and controls
328 lines (252 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
(* A proof of the Cauchy-Schwarz Inequality in Imandra.
Theorem 78 in the 100 Theorems list.
Grant Passmore, Imandra
*)
(* ============================================================
We prove:
1. cauchy_schwarz_1: ⟨u,v⟩² ≤ ⟨u,u⟩⟨v,v⟩
2. cauchy_schwarz_2: |⟨u,v⟩| ≤ ||u|| ||v|| (relational form)
3. Equality condition: u = av implies equality
============================================================ *)
type vec = real list
(* Inner product / dot product *)
let rec dot (u : vec) (v : vec) : real =
match u, v with
| [], [] -> 0.0
| x :: xs, y :: ys -> Real.(x * y + dot xs ys)
| _, _ -> 0.0
(* Squared norm: ||v||² = ⟨v,v⟩ *)
let norm_sq (v : vec) : real = dot v v
(* Scalar multiplication *)
let rec scalar_mult (a : real) (v : vec) : vec =
match v with
| [] -> []
| x :: xs -> Real.(a * x) :: scalar_mult a xs
(* Vector subtraction *)
let rec vec_sub (u : vec) (v : vec) : vec =
match u, v with
| [], [] -> []
| x :: xs, y :: ys -> Real.(x - y) :: vec_sub xs ys
| _, _ -> []
(* Zero vector predicate *)
let rec is_zero_vec (v : vec) : bool =
match v with
| [] -> true
| x :: xs -> x = 0.0 && is_zero_vec xs
(* Same length predicate *)
let rec same_length (u : vec) (v : vec) : bool =
match u, v with
| [], [] -> true
| _ :: xs, _ :: ys -> same_length xs ys
| _, _ -> false
(* Real absolute value *)
let abs_real (x : real) : real =
if x >=. 0.0 then x else Real.(~- x)
(* is_sqrt x y means: x is the non-negative sqrt of y
(x >= 0, y >= 0, x*x = y) *)
let is_sqrt (x : real) (y : real) : bool =
x >=. 0.0 && y >=. 0.0 && Real.(x * x) = y
(* ============================================================
ALGEBRAIC FACTS
============================================================ *)
lemma square_nonneg x = Real.(x * x) >=. 0.0
[@@by nonlin ()]
lemma square_plus_nonneg x y =
y >=. 0.0 ==> Real.(x * x + y) >=. 0.0
[@@by nonlin ()]
lemma nonzero_square_pos x =
x <> 0.0 ==> Real.(x * x) >. 0.0
[@@by nonlin ()]
lemma pos_plus_nonneg_pos a b =
a >. 0.0 && b >=. 0.0 ==> Real.(a + b) >. 0.0
[@@by nonlin ()]
lemma nonneg_plus_pos_pos a b =
a >=. 0.0 && b >. 0.0 ==> Real.(a + b) >. 0.0
[@@by nonlin ()]
lemma algebra_simplification a b =
a >. 0.0 ==>
Real.(a - 2.0 * (b / a) * b + (b / a) * (b / a) * a) = Real.(a - b * b / a)
[@@by nonlin ()]
lemma cs_algebra a b c =
a >=. 0.0 && c >. 0.0 && Real.(a - b * b / c) >=. 0.0
==> Real.(b * b) <=. Real.(a * c)
[@@by nonlin ()]
lemma prod_nonneg x y =
x >=. 0.0 && y >=. 0.0 ==> Real.(x * y) >=. 0.0
[@@by nonlin ()]
lemma square_of_prod x1 x2 =
Real.(x1 * x2 * (x1 * x2)) = Real.(x1 * x1 * (x2 * x2))
[@@by nonlin ()]
(* ============================================================
ABS AND SQRT
============================================================ *)
lemma abs_nonneg x = abs_real x >=. 0.0
[@@by auto @> nonlin ()]
lemma abs_squared x = Real.(abs_real x * abs_real x) = Real.(x * x)
[@@by auto @> nonlin ()]
lemma abs_is_sqrt x = is_sqrt (abs_real x) Real.(x * x)
[@@by [%use abs_nonneg x] @> [%use abs_squared x] @> [%use square_nonneg x] @> auto]
lemma sqrt_mult_square x1 x2 y1 y2 =
Real.(x1 * x1) = y1 && Real.(x2 * x2) = y2 ==>
Real.(x1 * x2 * (x1 * x2)) = Real.(y1 * y2)
[@@by [%use square_of_prod x1 x2] @> nonlin ()]
lemma sqrt_mult x1 x2 y1 y2 =
is_sqrt x1 y1 && is_sqrt x2 y2 ==> is_sqrt Real.(x1 * x2) Real.(y1 * y2)
[@@by [%use prod_nonneg x1 x2]
@> [%use prod_nonneg y1 y2]
@> [%use sqrt_mult_square x1 x2 y1 y2]
@> auto]
(* ============================================================
MONOTONICITY OF SQRT!
============================================================ *)
lemma mono_help_1 x y =
Real.(x >= y && x >= 0.0 && y >= 0.0 ==> x*x >= y*y)
[@@by nonlin()]
lemma mono_help_2 x y =
Real.(x > 0.0 && y > 0.0 && x*x = y*y) ==> x=y
[@@by nonlin()]
lemma mono_help_3 x y =
Real.(x = 0.0 && x*x = y*y) ==> x=y
[@@by nonlin()]
lemma sqrt_monotone x1 x2 y1 y2 =
is_sqrt x1 y1 && is_sqrt x2 y2 && y1 <=. y2 ==> x1 <=. x2
[@@by intros @> [%expand is_sqrt x1 y1] @> [%expand is_sqrt x2 y2]
@> lift_ifs @> [%replace y1] @> [%replace y2]
@> [%use mono_help_1 x1 x2] @> [%use mono_help_2 x1 x2]
@> [%use mono_help_3 x1 x2] @> auto]
(* ============================================================
VECTOR LEMMAS
============================================================ *)
(* Norm squared is non-negative *)
lemma norm_sq_nonneg v = norm_sq v >=. 0.0
[@@by induction ~vars:["v"] () @>| [
auto;
[%use square_plus_nonneg (List.hd v) (dot (List.tl v) (List.tl v))] @> auto
]]
(* Non-zero vector has positive norm squared *)
lemma nonzero_vec_pos_norm v =
not (is_zero_vec v) ==> dot v v >. 0.0
[@@by induction () @>| [
auto;
[%use nonzero_square_pos (List.hd v)]
@> [%use pos_plus_nonneg_pos Real.(List.hd v * List.hd v) (dot (List.tl v) (List.tl v))]
@> [%use nonneg_plus_pos_pos Real.(List.hd v * List.hd v) (dot (List.tl v) (List.tl v))]
@> [%use norm_sq_nonneg (List.tl v)]
@> auto
]]
(* same_length preserved under scalar multiplication *)
lemma same_length_scalar a u v =
same_length u v ==> same_length u (scalar_mult a v)
[@@by auto]
(* Zero vector has zero dot product *)
lemma dot_zero_vec_right u v =
same_length u v && is_zero_vec v ==> dot u v = 0.0
[@@by auto]
(* Zero vector has zero norm *)
lemma norm_sq_zero_vec v =
is_zero_vec v ==> norm_sq v = 0.0
[@@by auto]
(* ||u - tv||² expansion *)
lemma norm_sq_diff_projection t u v =
same_length u v ==>
norm_sq (vec_sub u (scalar_mult t v)) =
Real.(dot u u - 2.0 * t * dot u v + t * t * dot v v)
[@@by auto]
(* ============================================================
CAUCHY-SCHWARZ INTERMEDIATE LEMMAS
============================================================ *)
(* Projection formula is non-negative (from ||u - tv||² >= 0) *)
lemma projection_nonneg t u v =
same_length u v ==>
Real.(dot u u - 2.0 * t * dot u v + t * t * dot v v) >=. 0.0
[@@by
[%use norm_sq_nonneg (vec_sub u (scalar_mult t v))]
@> [%use norm_sq_diff_projection t u v]
@> auto]
[@@disable dot, scalar_mult, vec_sub, same_length, norm_sq]
(* Optimal projection gives key inequality *)
lemma optimal_projection_nonneg u v =
same_length u v && dot v v >. 0.0 ==>
Real.(dot u u - dot u v * dot u v / dot v v) >=. 0.0
[@@by
[%use projection_nonneg Real.(dot u v / dot v v) u v]
@> [%use algebra_simplification (dot v v) (dot u v)]
@> auto]
[@@disable dot, scalar_mult, vec_sub, same_length, norm_sq]
(* Zero case: when v = 0 *)
lemma cauchy_schwarz_zero_case u v =
same_length u v && is_zero_vec v ==>
Real.(dot u v * dot u v) <=. Real.(dot u u * dot v v)
[@@by
[%use dot_zero_vec_right u v]
@> [%use norm_sq_zero_vec v]
@> auto]
[@@disable dot, is_zero_vec, norm_sq]
(* Non-zero case: main proof *)
lemma cauchy_schwarz_nonzero_case u v =
same_length u v && not (is_zero_vec v) ==>
Real.(dot u v * dot u v) <=. Real.(dot u u * dot v v)
[@@by
[%use nonzero_vec_pos_norm v]
@> [%use norm_sq_nonneg u]
@> [%use optimal_projection_nonneg u v]
@> [%use cs_algebra (dot u u) (dot u v) (dot v v)]
@> auto]
[@@disable dot, scalar_mult, vec_sub, same_length, norm_sq, is_zero_vec]
(* ============================================================
MAIN THEOREM: CAUCHY-SCHWARZ INEQUALITY (FIRST FORM)
⟨u,v⟩² ≤ ⟨u,u⟩⟨v,v⟩
============================================================ *)
theorem cauchy_schwarz_1 u v =
same_length u v ==>
Real.(dot u v * dot u v) <=. Real.(dot u u * dot v v)
[@@by
[%use cauchy_schwarz_zero_case u v]
@> [%use cauchy_schwarz_nonzero_case u v]
@> auto]
[@@disable dot, scalar_mult, vec_sub, same_length, norm_sq, is_zero_vec]
(* ============================================================
MAIN THEOREM: CAUCHY-SCHWARZ INEQUALITY (SECOND FORM)
|⟨u,v⟩| ≤ ||u|| ||v||
This uses the relational is_sqrt to express the norm.
Given eu = sqrt(norm_sq u) and ev = sqrt(norm_sq v),
we have |dot u v| <= eu * ev
============================================================ *)
theorem cauchy_schwarz_2 u v eu ev =
same_length u v && is_sqrt eu (norm_sq u) && is_sqrt ev (norm_sq v) ==>
abs_real (dot u v) <=. Real.(eu * ev)
[@@by
[%use cauchy_schwarz_1 u v]
@> [%use abs_is_sqrt (dot u v)]
@> [%use sqrt_mult eu ev (norm_sq u) (norm_sq v)]
@> [%use sqrt_monotone (abs_real (dot u v)) Real.(eu * ev) Real.(dot u v * dot u v) Real.(norm_sq u * norm_sq v)]
@> [%use norm_sq_nonneg u]
@> [%use norm_sq_nonneg v]
@> auto]
[@@disable dot, scalar_mult, vec_sub, same_length, norm_sq, is_zero_vec, is_sqrt, abs_real]
(* ============================================================
EQUALITY CONDITIONS
Equality holds iff u and v are linearly dependent
============================================================ *)
(* same_length of scalar_mult with itself *)
lemma same_length_scalar_mult_self a v =
same_length (scalar_mult a v) v
[@@by induction ~vars:["v"] () @>| [auto; auto]]
(* Dot product of scalar mult with original *)
lemma dot_scalar_mult_self a v =
dot (scalar_mult a v) v = Real.(a * dot v v)
[@@by induction ~vars:["v"] () @>| [auto; auto]]
(* Norm of scalar multiple *)
lemma norm_sq_scalar a v =
norm_sq (scalar_mult a v) = Real.(a * a * norm_sq v)
[@@by induction ~vars:["v"] () @>| [auto; auto]]
(* EQUALITY: If u = av (linearly dependent), then CS holds with equality *)
lemma linear_dependence_implies_equality a v =
Real.(dot (scalar_mult a v) v * dot (scalar_mult a v) v) =
Real.(norm_sq (scalar_mult a v) * norm_sq v)
[@@by
[%use dot_scalar_mult_self a v]
@> [%use norm_sq_scalar a v]
@> [%use square_nonneg (dot v v)]
@> auto]
[@@disable dot, scalar_mult, norm_sq]