Skip to content

[Merged by Bors] - feat(Data/Complex/Basic): Adds imaginary number representation#12427

Closed
Javernus wants to merge 12 commits intomasterfrom
javernus-imaginary-representation
Closed

[Merged by Bors] - feat(Data/Complex/Basic): Adds imaginary number representation#12427
Javernus wants to merge 12 commits intomasterfrom
javernus-imaginary-representation

Conversation

@Javernus
Copy link
Copy Markdown

@Javernus Javernus commented Apr 25, 2024

Adds a representation for Complex numbers and DualNumber so they can be used in #eval statements.


Zulip.

Open in Gitpod

Adds a representation for complex numbers so #eval of a complex number displays values.
@github-actions github-actions bot added the new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! label Apr 25, 2024
numbers are represented.
-/
unsafe instance repr : Repr Complex where
reprPrec f p := Repr.addAppParen (_root_.repr f.re ++ " + " ++ _root_.repr f.im ++ "*I") p
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this should be conditional on the precedence of + (65) rather than "app" (1024)

Copy link
Copy Markdown
Author

@Javernus Javernus Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

@eric-wieser eric-wieser Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will work on that, good suggestion.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trick is that you should call reprPrec to pass in a p yourself

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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% _ * _

Jake Jongejans added 2 commits April 25, 2024 14:18
Adds a representation for DualNumbers so they can shown in an #eval statement.
@Javernus Javernus force-pushed the javernus-imaginary-representation branch from 53fb2be to 792187c Compare April 26, 2024 12:09
@Javernus Javernus force-pushed the javernus-imaginary-representation branch from 3f85282 to 7460dfb Compare April 26, 2024 13:26
Copy link
Copy Markdown
Member

@eric-wieser eric-wieser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test file that prints out a DualNumber (DualNumber Nat) and a DualNumber Complex?

@Javernus
Copy link
Copy Markdown
Author

Test files have been added.

@Javernus Javernus force-pushed the javernus-imaginary-representation branch from 267b0d9 to d5cfa6b Compare April 28, 2024 18:55
Copy link
Copy Markdown
Member

@eric-wieser eric-wieser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ghost ghost added the ready-to-merge This PR has been sent to bors. label Apr 28, 2024
mathlib-bors bot pushed a commit that referenced this pull request Apr 28, 2024
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>
@Javernus
Copy link
Copy Markdown
Author

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.

@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Apr 28, 2024

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title feat(Data/Complex/Basic): Adds imaginary number representation [Merged by Bors] - feat(Data/Complex/Basic): Adds imaginary number representation Apr 28, 2024
@mathlib-bors mathlib-bors bot closed this Apr 28, 2024
@mathlib-bors mathlib-bors bot deleted the javernus-imaginary-representation branch April 28, 2024 22:28
apnelson1 pushed a commit that referenced this pull request May 12, 2024
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>
callesonne pushed a commit that referenced this pull request May 16, 2024
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! ready-to-merge This PR has been sent to bors.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants