|
| 1 | +/- |
| 2 | +Copyright (c) 2025 Ilmārs Cīrulis. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Ilmārs Cīrulis, Alex Meiburg |
| 5 | +-/ |
| 6 | +import Mathlib.Analysis.RCLike.Basic |
| 7 | + |
| 8 | +/-! |
| 9 | +# Normalized vector |
| 10 | +
|
| 11 | +Function that returns unit length vector that points in the same direction |
| 12 | +(if the given vector is nonzero vector) or returns zero vector |
| 13 | +(if the given vector is zero vector). |
| 14 | +-/ |
| 15 | + |
| 16 | +variable {V : Type*} [NormedAddCommGroup V] [NormedSpace ℝ V] |
| 17 | + |
| 18 | +/-- For a nonzero vector `x`, `normalize x` is the unit-length vector that points |
| 19 | +in the same direction as `x`. If `x = 0`, then `normalize x = 0`. -/ |
| 20 | +noncomputable def NormedSpace.normalize (x : V) : V := ‖x‖⁻¹ • x |
| 21 | + |
| 22 | +namespace NormedSpace |
| 23 | + |
| 24 | +@[simp] |
| 25 | +theorem normalize_zero_eq_zero : normalize (0 : V) = 0 := by |
| 26 | + simp [normalize] |
| 27 | + |
| 28 | +@[simp] |
| 29 | +theorem norm_smul_normalize (x : V) : ‖x‖ • normalize x = x := by |
| 30 | + by_cases hx : x = 0 |
| 31 | + all_goals simp [normalize, hx] |
| 32 | + |
| 33 | +@[simp] |
| 34 | +lemma norm_normalize_eq_one_iff {x : V} : ‖normalize x‖ = 1 ↔ x ≠ 0 := |
| 35 | + ⟨by rintro hx rfl; simp at hx, fun h ↦ by simp [normalize, h, norm_smul]⟩ |
| 36 | + |
| 37 | +lemma normalize_eq_self_of_norm_eq_one {x : V} (h : ‖x‖ = 1) : normalize x = x := by |
| 38 | + simp [normalize, h] |
| 39 | + |
| 40 | +@[simp] |
| 41 | +theorem normalize_normalize (x : V) : normalize (normalize x) = normalize x := by |
| 42 | + by_cases hx : x = 0 |
| 43 | + · simp [hx] |
| 44 | + · simp [normalize_eq_self_of_norm_eq_one, hx] |
| 45 | + |
| 46 | +@[simp] |
| 47 | +theorem normalize_neg (x : V) : normalize (- x) = - normalize x := by |
| 48 | + simp [normalize] |
| 49 | + |
| 50 | +theorem normalize_smul_of_pos {r : ℝ} (hr : 0 < r) (x : V) : |
| 51 | + normalize (r • x) = normalize x := by |
| 52 | + simp [normalize, norm_smul, smul_smul, abs_of_pos hr, mul_assoc, inv_mul_cancel₀ hr.ne'] |
| 53 | + |
| 54 | +theorem normalize_smul_of_neg {r : ℝ} (hr : r < 0) (x : V) : |
| 55 | + normalize (r • x) = - normalize x := by |
| 56 | + simpa using normalize_smul_of_pos (show 0 < -r by linarith) (-x) |
| 57 | + |
| 58 | +end NormedSpace |
0 commit comments