[Merged by Bors] - feat(Data/Complex/Basic): Adds imaginary number representation#12427
[Merged by Bors] - feat(Data/Complex/Basic): Adds imaginary number representation#12427
Conversation
Adds a representation for complex numbers so #eval of a complex number displays values.
Mathlib/Data/Complex/Basic.lean
Outdated
| numbers are represented. | ||
| -/ | ||
| unsafe instance repr : Repr Complex where | ||
| reprPrec f p := Repr.addAppParen (_root_.repr f.re ++ " + " ++ _root_.repr f.im ++ "*I") p |
There was a problem hiding this comment.
Probably this should be conditional on the precedence of + (65) rather than "app" (1024)
There was a problem hiding this comment.
I am struggling to make this happen, partly because I cannot think of a way to test this. I have updated everything else based on your feedback, thanks for that.
How would I go about defining the + precedence?
There was a problem hiding this comment.
Maybe the easy way to test this would be to add a Repr for DualNumber (x + y*eps) at the same time, and check that DualNumber Complex has parentheses in the right place?
There was a problem hiding this comment.
I will work on that, good suggestion.
There was a problem hiding this comment.
I wrote this piece of code in Algebra/DualNumber.lean:
/-- Show DualNumber with values x and y as an "x + y*ε" string -/
instance instRepr [Repr R] : Repr (DualNumber R) where
reprPrec f _p := repr f.fst ++ " + " ++ repr f.snd ++ "*ε"
I made Repr Complex print p, which was 0 for both cases (for a, b (both complex) in a + bε). This suggests to me that I am not getting sufficient information to conditionally print the brackets. I will for now opt to always print parentheses. I checked for Repr Rat as well, and that one does not add any outside brackets even though it is represented as a / b, which I'd presume should get parentheses if squared, for example.
Maybe I am missing something, but I tried finding other Repr's that handle conditionally adding brackets, and couldn't find anything.
I will also add parentheses for DualNumbers, to keep them consistent.
Let me know if there is a way, of course, but I do not see it.
There was a problem hiding this comment.
The trick is that you should call reprPrec to pass in a p yourself
There was a problem hiding this comment.
Bingo. I hardcoded the precedences for now, so if it is possible to use variables for those, let me know where I can find those and I will update those.
There was a problem hiding this comment.
This is a great question; the best I could come up with was
elab:max "prec_of% " t:term : term => do
let ⟨.node _ name _⟩ := t | throwErrorAt t "Cannot find syntax node"
let p ← unsafe Lean.evalConst Lean.ParserDescr name
let .trailingNode _ prec _ _ := p | throwErrorAt t "Cannot find precedence"
return Lean.toExpr prec
#eval prec_of% _ * _Adds a representation for DualNumbers so they can shown in an #eval statement.
53fb2be to
792187c
Compare
3f85282 to
7460dfb
Compare
eric-wieser
left a comment
There was a problem hiding this comment.
Can you add a test file that prints out a DualNumber (DualNumber Nat) and a DualNumber Complex?
|
Test files have been added. |
267b0d9 to
d5cfa6b
Compare
eric-wieser
left a comment
There was a problem hiding this comment.
bors merge
Thanks!
I fixed the #guard_msgs thing; you should have seen a blue light bulb in vscode that you can click on.
Regarding the precedences; I think the correct choice is 70, as this is the precedence of multiplication, not 81. I've pushed this change, and all your tests still pass.
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>
|
Thank you for the final touches! I looked through them for my own reference, so next time, I can handle more of the contribution process on my own. |
|
Pull request successfully merged into master. Build succeeded: |
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>
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>
Adds a representation for Complex numbers and DualNumber so they can be used in #eval statements.
Zulip.