{-# OPTIONS --safe --cubical-compatible --guardedness --exact-split --double-check #-}
module ALMA.Physics.Quantum.Rotation where
open import Data.Rational using (ℚ; 0ℚ; 1ℚ; _+_; _*_; _-_; -_; _/_)
open import Data.Rational.Properties using
(+-assoc; +-comm; +-identityʳ; *-comm; *-identityˡ; *-distribʳ-+)
open import Data.Integer.Base using (ℤ; 1ℤ) renaming (_+_ to _ℤ+_)
open import Data.Product.Base using (_×_; _,_)
open import Relation.Binary.PropositionalEquality using
(_≡_; refl; sym; cong; cong₂; module ≡-Reasoning)
open import ALMA.Physics.Quantum.Gaussian using (ℂ; re; im; norm²)
open import ALMA.Physics.RationalKit as Kit using
(+interchange; swap-mid; sq+; sq-; cancel-pair)
private
cross-vanish : ∀ x y u v → u * v ≡ x * y →
(x * x + (y * y + (- (x * y) + - (x * y)))) +
(u * u + (v * v + (u * v + u * v))) ≡
(x * x + u * u) + (y * y + v * v)
cross-vanish x y u v eq = let open ≡-Reasoning in begin
(x² + (y² + (P))) + (u² + (v² + (Q₀)))
≡⟨ cong (λ z → (x² + (y² + P)) + (u² + (v² + (z + z)))) eq ⟩
(x² + (y² + P)) + (u² + (v² + Q))
≡⟨ cong₂ _+_ (sym (+-assoc x² y² P)) (sym (+-assoc u² v² Q)) ⟩
((x² + y²) + P) + ((u² + v²) + Q)
≡⟨ +interchange (x² + y²) P (u² + v²) Q ⟩
((x² + y²) + (u² + v²)) + (P + Q)
≡⟨ cong (_+ (P + Q)) (+interchange x² y² u² v²) ⟩
((x² + u²) + (y² + v²)) + (P + Q)
≡⟨ cong (((x² + u²) + (y² + v²)) +_) (cancel-pair t) ⟩
((x² + u²) + (y² + v²)) + 0ℚ
≡⟨ +-identityʳ ((x² + u²) + (y² + v²)) ⟩
(x² + u²) + (y² + v²) ∎
where
x² = x * x ; y² = y * y ; u² = u * u ; v² = v * v
t = x * y
P = - t + - t
Q₀ = u * v + u * v
Q = t + t
factor-left : ∀ c s a →
(c * a) * (c * a) + (s * a) * (s * a) ≡ (c * c + s * s) * (a * a)
factor-left c s a = let open ≡-Reasoning in begin
(c * a) * (c * a) + (s * a) * (s * a)
≡⟨ cong₂ _+_ (swap-mid c a c a) (swap-mid s a s a) ⟩
(c * c) * (a * a) + (s * s) * (a * a)
≡⟨ sym (*-distribʳ-+ (a * a) (c * c) (s * s)) ⟩
(c * c + s * s) * (a * a) ∎
factor-right : ∀ c s d →
(s * d) * (s * d) + (c * d) * (c * d) ≡ (c * c + s * s) * (d * d)
factor-right c s d = let open ≡-Reasoning in begin
(s * d) * (s * d) + (c * d) * (c * d)
≡⟨ cong₂ _+_ (swap-mid s d s d) (swap-mid c d c d) ⟩
(s * s) * (d * d) + (c * c) * (d * d)
≡⟨ +-comm ((s * s) * (d * d)) ((c * c) * (d * d)) ⟩
(c * c) * (d * d) + (s * s) * (d * d)
≡⟨ sym (*-distribʳ-+ (d * d) (c * c) (s * s)) ⟩
(c * c + s * s) * (d * d) ∎
cross-eq : ∀ c s a d → (s * a) * (c * d) ≡ (c * a) * (s * d)
cross-eq c s a d = let open ≡-Reasoning in begin
(s * a) * (c * d)
≡⟨ swap-mid s a c d ⟩
(s * c) * (a * d)
≡⟨ cong (_* (a * d)) (*-comm s c) ⟩
(c * s) * (a * d)
≡⟨ sym (swap-mid c a s d) ⟩
(c * a) * (s * d) ∎
rot-real : ∀ c s a d → c * c + s * s ≡ 1ℚ →
(c * a - s * d) * (c * a - s * d) +
(s * a + c * d) * (s * a + c * d) ≡ a * a + d * d
rot-real c s a d h = let open ≡-Reasoning in begin
(x - y) * (x - y) + (u + v) * (u + v)
≡⟨ cong₂ _+_ (sq- x y) (sq+ u v) ⟩
(x² + (y² + (- (x * y) + - (x * y)))) +
(u² + (v² + (u * v + u * v)))
≡⟨ cross-vanish x y u v (cross-eq c s a d) ⟩
(x² + u²) + (y² + v²)
≡⟨ cong₂ _+_ (factor-left c s a) (factor-right c s d) ⟩
(c * c + s * s) * (a * a) + (c * c + s * s) * (d * d)
≡⟨ cong₂ _+_ (cong (_* (a * a)) h) (cong (_* (d * d)) h) ⟩
1ℚ * (a * a) + 1ℚ * (d * d)
≡⟨ cong₂ _+_ (*-identityˡ (a * a)) (*-identityˡ (d * d)) ⟩
a * a + d * d ∎
where
x = c * a ; y = s * d ; u = s * a ; v = c * d
x² = x * x ; y² = y * y ; u² = u * u ; v² = v * v
rot0 rot1 : ℚ → ℚ → ℂ → ℂ → ℂ
rot0 c s z w = (c * re z - s * re w , c * im z - s * im w)
rot1 c s z w = (s * re z + c * re w , s * im z + c * im w)
rot : ℚ → ℚ → ℂ → ℂ → ℂ × ℂ
rot c s z w = (rot0 c s z w , rot1 c s z w)
rot-pres : ∀ c s → c * c + s * s ≡ 1ℚ → ∀ z w →
let (φ₀ , φ₁) = rot c s z w in
norm² φ₀ + norm² φ₁ ≡ norm² z + norm² w
rot-pres c s h (a , b) (d , e) = let open ≡-Reasoning in begin
(sq (c * a - s * d) + sq (c * b - s * e)) +
(sq (s * a + c * d) + sq (s * b + c * e))
≡⟨ +interchange (sq (c * a - s * d)) (sq (c * b - s * e))
(sq (s * a + c * d)) (sq (s * b + c * e)) ⟩
(sq (c * a - s * d) + sq (s * a + c * d)) +
(sq (c * b - s * e) + sq (s * b + c * e))
≡⟨ cong₂ _+_ (rot-real c s a d h) (rot-real c s b e h) ⟩
(a * a + d * d) + (b * b + e * e)
≡⟨ +interchange (a * a) (d * d) (b * b) (e * e) ⟩
(a * a + b * b) + (d * d + e * e) ∎
where
sq : ℚ → ℚ
sq x = x * x
i3 i4 : ℤ
i3 = (1ℤ ℤ+ 1ℤ) ℤ+ 1ℤ
i4 = i3 ℤ+ 1ℤ
c₃₄₅ s₃₄₅ : ℚ
c₃₄₅ = i3 / 5
s₃₄₅ = i4 / 5
pythagoras-345 : c₃₄₅ * c₃₄₅ + s₃₄₅ * s₃₄₅ ≡ 1ℚ
pythagoras-345 = refl
rotation-345 : ∀ z w →
let (φ₀ , φ₁) = rot c₃₄₅ s₃₄₅ z w in
norm² φ₀ + norm² φ₁ ≡ norm² z + norm² w
rotation-345 = rot-pres c₃₄₅ s₃₄₅ pythagoras-345