Skip to content

Commit

Permalink
feat(Data/Complex/Basic): Adds imaginary number representation (#12427)
Browse files Browse the repository at this point in the history
Adds a representation for Complex numbers and DualNumber so they can be used in #eval statements.




Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
  • Loading branch information
Javernus and eric-wieser committed Apr 28, 2024
1 parent 0033bf0 commit e9a5e8d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Mathlib/Algebra/DualNumber.lean
Expand Up @@ -189,4 +189,10 @@ theorem lift_inlAlgHom_eps :
lift.apply_symm_apply <| AlgHom.id R A[ε]
#align dual_number.lift_eps DualNumber.lift_inlAlgHom_epsₓ

/-- Show DualNumber with values x and y as an "x + y*ε" string -/
instance instRepr [Repr R] : Repr (DualNumber R) where
reprPrec f p :=
(if p > 65 then (Std.Format.bracket "(" . ")") else (·)) <|
reprPrec f.fst 65 ++ " + " ++ reprPrec f.snd 70 ++ "*ε"

end DualNumber
10 changes: 10 additions & 0 deletions Mathlib/Data/Complex/Basic.lean
Expand Up @@ -941,6 +941,16 @@ theorem im_eq_sub_conj (z : ℂ) : (z.im : ℂ) = (z - conj z) / (2 * I) := by
mul_div_cancel_left₀ _ (mul_ne_zero two_ne_zero I_ne_zero : 2 * I ≠ 0)]
#align complex.im_eq_sub_conj Complex.im_eq_sub_conj

/-- Show the imaginary number ⟨x, y⟩ as an "x + y*I" string
Note that the Real numbers used for x and y will show as cauchy sequences due to the way Real
numbers are represented.
-/
unsafe instance instRepr : Repr ℂ where
reprPrec f p :=
(if p > 65 then (Std.Format.bracket "(" . ")") else (·)) <|
reprPrec f.re 65 ++ " + " ++ reprPrec f.im 70 ++ "*I"

end Complex

assert_not_exists Multiset
Expand Down
34 changes: 34 additions & 0 deletions test/Complex.lean
@@ -0,0 +1,34 @@
import Mathlib.Data.Complex.Basic
import Mathlib.Algebra.DualNumber

open DualNumber

/--
info: Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/) + Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)*I
-/
#guard_msgs in
#eval (0 : ℂ)

/--
info: Real.ofCauchy (sorry /- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... -/) + Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)*I
-/
#guard_msgs in
#eval (1 : ℂ)

/--
info: Real.ofCauchy (sorry /- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ... -/) + Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)*I
-/
#guard_msgs in
#eval (4 : ℂ)

/--
info: Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/) + Real.ofCauchy (sorry /- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... -/)*I
-/
#guard_msgs in
#eval (Complex.I : ℂ)

/--
info: Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/) + Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)*I + (Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/) + Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)*I)*ε
-/
#guard_msgs in
#eval (0 : ℂ[ε])
19 changes: 19 additions & 0 deletions test/DualNumber.lean
@@ -0,0 +1,19 @@
import Mathlib.Algebra.DualNumber

open DualNumber

/-- info: 0 + 0*ε -/
#guard_msgs in
#eval (0 : ℕ[ε])

/-- info: 2 + 0*ε -/
#guard_msgs in
#eval (2 : ℕ[ε])

/-- info: 6 + 0*ε -/
#guard_msgs in
#eval (2 + 4 : ℕ[ε])

/-- info: 2 + 0*ε + (0 + 0*ε)*ε -/
#guard_msgs in
#eval (2 : (ℕ[ε])[ε])

0 comments on commit e9a5e8d

Please sign in to comment.