Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

[Merged by Bors] - feat(data/complex/is_R_or_C): add typeclass for real or complex #3934

Closed
wants to merge 31 commits into from

Conversation

dupuisf
Copy link
Collaborator

@dupuisf dupuisf commented Aug 24, 2020


This PR defines the typeclass is_R_or_C intended to have only two instances:
and . It is meant for definitions and theorems which hold for both the
real and the complex case, such as inner products and Hilbert spaces. Its API
follows closely that of .

@dupuisf dupuisf added the WIP Work in progress label Aug 24, 2020
@fpvandoorn
Copy link
Member

Nice!

I think (eventually) we want to move away from using complex.conj (and other operations), and use the conj that has as argument is_R_or_C. I think it works analogously to using has_mul.mul everywhere, and never using nat.mul. The fact that this class has only one interesting instance that uses conj doesn't really matter.

@cipher1024
Copy link
Collaborator

I'm fairly ignorant of the application here but I'm wondering, is there a reason to not define the class as:

class is_R_or_C (K : Type) := 
(from_R : R -> K)
(to_C : K -> C)
(to_C_from_R : \forall x : R, to_C (from_R x) = coe x)

and derive the rest from that? That could probably be generalized into an interval between types and applied to other situations like nat and int

@fpvandoorn
Copy link
Member

@cipher1024 I don't think you can define/proof the current fields of is_R_or_C from your suggestion.

@dupuisf
Copy link
Collaborator Author

dupuisf commented Aug 24, 2020

@cipher1024 Right -- more precisely, I don't see how to avoid being able to prove things like ∃ x, x^2 = -1 this way.

@dupuisf
Copy link
Collaborator Author

dupuisf commented Aug 24, 2020

@fpvandoorn For conj, I think it might be a good idea to have a class like has_star or something, which would be used for complex conjugation, adjoints of linear operators, etc.

@[simp] lemma conj_re (z : K) : re (conj z) = re z := is_R_or_C.conj_re_ax z
@[simp] lemma conj_im (z : K) : im (conj z) = -(im z) := is_R_or_C.conj_im_ax z
@[simp] lemma conj_of_real (r : ℝ) : conj (𝓚 r) = (𝓚 r) :=
(@is_R_or_C.ext_iff K _ _ _ (conj (𝓚 r)) (𝓚 r)).mpr $ by simp
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
(@is_R_or_C.ext_iff K _ _ _ (conj (𝓚 r)) (𝓚 r)).mpr $ by simp
by { rw ext_iff, simp only [of_real_im, conj_im, eq_self_iff_true, conj_re, and_self, neg_zero] }

squeezing this simp makes the file compile a bit faster. I don't have a profiler output, but the orange bar hangs around noticeably long with a raw simp and goes away immediately with this simp only

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually the proofs of conj_neg_I and conj_conj take a while, too. Maybe we've made a bad choice with our simp normal form?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I haven't been too careful with the simp lemmas, I basically left things the way they were in complex.lean.

Copy link
Collaborator

@bryangingechen bryangingechen left a comment

Choose a reason for hiding this comment

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

Should the file be named is_R_or_C.lean instead of R_or_C.lean too?

src/data/complex/R_or_C.lean Outdated Show resolved Hide resolved
src/data/complex/R_or_C.lean Outdated Show resolved Hide resolved
src/data/complex/R_or_C.lean Outdated Show resolved Hide resolved
src/data/complex/R_or_C.lean Outdated Show resolved Hide resolved
src/data/complex/R_or_C.lean Outdated Show resolved Hide resolved
src/data/complex/R_or_C.lean Outdated Show resolved Hide resolved
jalex-stark and others added 9 commits August 24, 2020 22:25
Co-authored-by: Bryan Gin-ge Chen <bryangingechen@gmail.com>
Co-authored-by: Bryan Gin-ge Chen <bryangingechen@gmail.com>
Co-authored-by: Bryan Gin-ge Chen <bryangingechen@gmail.com>
Co-authored-by: Bryan Gin-ge Chen <bryangingechen@gmail.com>
Co-authored-by: Bryan Gin-ge Chen <bryangingechen@gmail.com>
Co-authored-by: Bryan Gin-ge Chen <bryangingechen@gmail.com>
@fpvandoorn fpvandoorn added awaiting-author A reviewer has asked the author a question or requested changes and removed awaiting-review The author would like community review of the PR labels Aug 27, 2020
dupuisf and others added 8 commits August 27, 2020 20:21
Co-authored-by: Floris van Doorn <fpvdoorn@gmail.com>
Co-authored-by: Floris van Doorn <fpvdoorn@gmail.com>
Co-authored-by: Floris van Doorn <fpvdoorn@gmail.com>
Co-authored-by: Floris van Doorn <fpvdoorn@gmail.com>
Co-authored-by: Floris van Doorn <fpvdoorn@gmail.com>
Co-authored-by: Floris van Doorn <fpvdoorn@gmail.com>
@dupuisf
Copy link
Collaborator Author

dupuisf commented Aug 28, 2020

@fpvandoorn Thanks for the suggestions! Regarding norm_cast, it doesn't seem to work since the "coercions" used here aren't real coercions, they're just a regular function. In fact many of these lemmas were initially copy/pasted from complex.lean and I had to remove the norm_cast attribute since I was getting errors about the lhs not containing any coercions.

@dupuisf dupuisf added awaiting-review The author would like community review of the PR and removed awaiting-author A reviewer has asked the author a question or requested changes labels Aug 28, 2020
@sgouezel
Copy link
Collaborator

I am a little bit confused about the intended applications of this file. For instance, in the file analysis/normed_space/hahn_banach, we have Hahn-Banach theorem over the reals and over the complexes, and we introduce a specific typeclass to formulate the theorems for both (where the complex instance is deduced from the real instance and a trick). Do I understand correctly that the current typeclass is_R_or_C could not be used instead of this specific typeclass? In the same way, an important property of reals and complexes is that the mean value inequality holds (where the complex one is again deduced from the real one) -- could one use is_R_or_C to state this, or will we need to introduce yet another typeclass?

@dupuisf
Copy link
Collaborator Author

dupuisf commented Aug 29, 2020

@sgouezel The main application I have in mind is defining the complex inner product, and other cases in which the real case follows directly from the complex case by replacing re by identity, im by zero, etc. For Hahn-Banach, I don't immediately see a reason why it wouldn't work, though one would have to port things like linear_map.extend_to_ℂ to linear_map.extend_to_ℝ_or_ℂ, etc. But in this case the benefits might be more limited, since one derives the complex case from the real one and not the other way around.

@sgouezel
Copy link
Collaborator

ok, thanks for the clarification. It might be a good idea to explain this in the file-level docstring, and to sketch an application there.

@dupuisf
Copy link
Collaborator Author

dupuisf commented Aug 29, 2020

I've added some more details to the file-level docstring, and changed the normed_field requirement to nondiscrete_normed_field, since it looks like that will be needed in some applications.

@dupuisf
Copy link
Collaborator Author

dupuisf commented Aug 29, 2020

I didn't go into too much detail regarding the applications, since I plan to PR inner products based on this fairly soon.

@fpvandoorn
Copy link
Member

LGTM

bors merge

@github-actions github-actions bot added ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.) and removed awaiting-review The author would like community review of the PR labels Aug 30, 2020
bors bot pushed a commit that referenced this pull request Aug 30, 2020
Co-authored-by: jalex-stark <alexmaplegm@gmail.com>
Co-authored-by: Frédéric Dupuis <31101893+dupuisf@users.noreply.github.com>
@bors
Copy link

bors bot commented Aug 30, 2020

Pull request successfully merged into master.

Build succeeded:

@bors bors bot changed the title feat(data/complex/is_R_or_C): add typeclass for real or complex [Merged by Bors] - feat(data/complex/is_R_or_C): add typeclass for real or complex Aug 30, 2020
@bors bors bot closed this Aug 30, 2020
@bors bors bot deleted the R_or_C branch August 30, 2020 06:13
robertylewis pushed a commit that referenced this pull request Aug 31, 2020
Co-authored-by: jalex-stark <alexmaplegm@gmail.com>
Co-authored-by: Frédéric Dupuis <31101893+dupuisf@users.noreply.github.com>
PatrickMassot pushed a commit that referenced this pull request Sep 8, 2020
Co-authored-by: jalex-stark <alexmaplegm@gmail.com>
Co-authored-by: Frédéric Dupuis <31101893+dupuisf@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants