Zulip Chat Archive

Stream: mathlib4

Topic: Complex.sqrt


Yury G. Kudryashov (Oct 22 2023 at 19:40):

What do you think about

  • introducing Complex.sqrt?
  • adding a notation typeclass SqRt (notation: √) with instances for Real, NNReal, ENNReal, and Complex?

Yaël Dillies (Oct 22 2023 at 19:58):

Sounds great, but what about just Sqrt? I know it's two words, but nobody really considers them as such nowadays.

Yury G. Kudryashov (Oct 22 2023 at 20:01):

I don't care about lowercase/uppercase.

Damiano Testa (Oct 22 2023 at 20:01):

I wish more of mathlib did not care about upper/lower case...

Yury G. Kudryashov (Oct 22 2023 at 20:11):

(my motivation: I need formulas for the components of z\sqrt{z} for a project)

Eric Wieser (Oct 22 2023 at 20:28):

Should we skip Sqrt and jump to NthRoot?

Yury G. Kudryashov (Oct 22 2023 at 20:29):

Should it take a docs#Nat or a docs#PNat ?

Eric Wieser (Oct 22 2023 at 20:30):

That would solve the cbrt (-8) = 2 problem as well (IIRC, we currently have (-8)^(1/3) = -1)

Yury G. Kudryashov (Oct 22 2023 at 20:30):

How can it be -1?

Eric Wieser (Oct 22 2023 at 20:31):

The real part of 1 ± √3i

Yury G. Kudryashov (Oct 22 2023 at 20:31):

Ah, you're right.

Eric Wieser (Oct 22 2023 at 20:31):

It would be better if I weren't

Yury G. Kudryashov (Oct 22 2023 at 20:31):

But this means refactoring the existing code.

Eric Wieser (Oct 22 2023 at 20:32):

I'm not suggesting we change the meaning of (-8)^(1/3); but that if we are going to introduced a new notation, we may as well solve multiple problems at once

Yury G. Kudryashov (Oct 22 2023 at 20:32):

Because (xn)n=x(\sqrt[n]{x})^n=x needs different assumptions depending on parity of n.

Yury G. Kudryashov (Oct 22 2023 at 20:32):

I meant existing code that uses docs#Real.sqrt.

Eric Wieser (Oct 22 2023 at 20:32):

I think it's ok to add the notation for all n but only state the existing lemmas for n=2 (in the short term)

Yury G. Kudryashov (Oct 22 2023 at 20:33):

What notation should we use for general n?

Eric Wieser (Oct 22 2023 at 20:34):

We could use Mario's superscript parser hack along with √...

Yury G. Kudryashov (Oct 22 2023 at 20:34):

Can you write the notation/macro part?

Eric Wieser (Oct 22 2023 at 20:37):

import Mathlib.Util.Superscript

def nthRoot (n : ) (r : ) :  := sorry

local syntax:arg "√" term:max : term
local syntax:arg superscript(term) "√" term:max : term
local macro_rules | `( $r:term) => `(nthRoot 2 $r)
local macro_rules | `($n:superscript  $r:term) => `(nthRoot $n $r)

-- `id` is to check the precedence looks ok
#check id 4 + 1
#check id ³√8 + 1

Eric Wieser (Oct 22 2023 at 20:38):

Those precedences are almost certainly wrong

Eric Wieser (Oct 22 2023 at 20:38):

I also don't know how to overload this with √4 notation at the same time

Eric Wieser (Oct 22 2023 at 20:38):

Maybe @Mario Carneiro can advise, also on whether this is a good idea

Kevin Buzzard (Oct 22 2023 at 21:19):

If it makes it look more like the textbooks then it's a good idea (until we are allowed to write maths properly in LaTeX and have lean read that)

Mario Carneiro (Oct 22 2023 at 21:54):

It seems fine to me, this is exactly the sort of thing I wanted the superscript parser to be useful for

Antoine Chambert-Loir (Oct 22 2023 at 23:01):

Yury G. Kudryashov said:

What do you think about

  • introducing Complex.sqrt?
  • adding a notation typeclass SqRt (notation: √) with instances for Real, NNReal, ENNReal, and Complex?
  • Do you wish to choose a determination? the same that is used for docs#Complex.log?
    so that would be fun z => Complex.exp (Complex.log z / 2)

  • SqRt is strange, why not SquareRootor the usual Sqrt?

Yury G. Kudryashov (Oct 23 2023 at 00:02):

It will be NthRoot

Yury G. Kudryashov (Oct 23 2023 at 00:02):

Yes, for complex numbers it'll be exp (log z / n).

Yury G. Kudryashov (Oct 23 2023 at 00:13):

a.k.a. z ^ (1 / n)

Yury G. Kudryashov (Oct 23 2023 at 03:09):

One more question: should it be

class NthRoot (R : Type*) (n : Nat) where
  nthRoot : R  R

or

class NthRoot (R : Type*) where
  nthRoot : Nat  R  R

Yury G. Kudryashov (Oct 23 2023 at 03:09):

I.e., should we accomodate for types with, e.g., square root but no cubic root?

Mario Carneiro (Oct 23 2023 at 03:27):

I think so, especially if it's just supposed to be a "notation typeclass"

Antoine Chambert-Loir (Oct 24 2023 at 10:21):

Yury G. Kudryashov said:

One more question: should it be

class NthRoot (R : Type*) (n : Nat) where
  nthRoot : R  R

or

class NthRoot (R : Type*) where
  nthRoot : Nat  R  R

Or the opposite: types with a cubic root but no square root (such as Real).

Yury G. Kudryashov (Oct 24 2023 at 18:34):

We do have docs#Real.sqrt anyway

Yury G. Kudryashov (Oct 24 2023 at 21:45):

Notation works but doesn't delaborate.

Yury G. Kudryashov (Oct 24 2023 at 22:00):

Also, how should √x⁻¹ be parsed: √(x⁻¹) or (√x)⁻¹?

Yury G. Kudryashov (Oct 25 2023 at 01:02):

One more question: should we make it unfold to Real.sqrt/Nat.sqrt/whatever while parsing like coe does?

Yury G. Kudryashov (Oct 25 2023 at 01:03):

We are not going to have generic lemmas about n-th roots anyway, because every instance has its assumptions.

Yury G. Kudryashov (Oct 25 2023 at 01:17):

Current version: #7907

Yaël Dillies (Oct 25 2023 at 05:37):

Yury G. Kudryashov said:

Also, how should √x⁻¹ be parsed: √(x⁻¹) or (√x)⁻¹?

I would expect the latter

Kevin Buzzard (Oct 25 2023 at 05:40):

I would expect it to depend on the length of the bar on the square root symbol which latex can offer us but unicode can't :-(

Eric Wieser (May 25 2024 at 21:53):

Yury G. Kudryashov said:

Current version: #7907

I've pushed what I consider the remaining pieces (defaulting to Real and delaboration) to this PR, and think it is probably ready; does anyone else have opinions?

Eric Wieser (May 25 2024 at 21:55):

Yury G. Kudryashov said:

One more question: should we make it unfold to Real.sqrt/Nat.sqrt/whatever while parsing like coe does?

I vote no: this is harder to implement, and it only matters for coe because there we want transitive coercions to be unfolded.

Eric Wieser (May 25 2024 at 21:57):

Kevin Buzzard said:

I would expect it to depend on the length of the bar on the square root symbol which latex can offer us but unicode can't :-(

We could have some bizarre bracketing notation like √‾x⁻¹‾

Yury G. Kudryashov (May 25 2024 at 21:59):

If we don't unfold it, then we can't use it for Nat.sqrt (because it's in Batteries).

Yury G. Kudryashov (May 25 2024 at 21:59):

Or we can backport it to Batteries (without Unicode notation) and define an instance for n-th root on Nat there.

Yury G. Kudryashov (May 26 2024 at 23:49):

@Eric Wieser What's your plan about this notation and Nat.sqrt? Backport the typeclass? Repeat lemmas? Don't use notation for Nat? Something else?

Michael Stoll (May 27 2024 at 09:54):

My 2 cents: I think it is better not to use square root notation for Nat.sqrt, to avoid confusion (since Nat.sqrt n is not actually a square root of n in general).

Eric Wieser (May 27 2024 at 09:55):

I had that thought too, but Real.sqrt r also isn't actually a square root of r when r is negative

Michael Stoll (May 27 2024 at 10:02):

But, contrary to the Nat version, this is because of a junk value, so I don't think this should be an argument. (I'd argue that Nat.sqrt of a non-square is not a junk value, but something that makes sense and is useful.)

Eric Wieser (May 27 2024 at 10:03):

Regarding lemma duplication, I'm thinking of something like

class IsLawfulNthRoot (R n) [NthRoot R n] (P : out_param <| R \to Prop) where
  nthRoot_pow (r) (h : P r) : nthRoot n r ^ n = r

Eric Wieser (May 27 2024 at 10:04):

Though maybe IsSquare is sufficient instead of p

Yaël Dillies (May 27 2024 at 10:04):

Michael, I don't really see how the situation differs. Real.sqrt of a negative real number is also something that makes sense and is useful

Eric Wieser (May 27 2024 at 10:07):

Yael, you had a number-theoretic Nat square root somewhere that rounds down the exponents of primes, rather than the real square root, right?

Michael Stoll (May 27 2024 at 10:07):

I've been using the rounded-down square root of natural numbers purposefully in informal math, but never square roots of negative reals.

Yaël Dillies (May 27 2024 at 10:07):

Eric Wieser said:

Yael, you had a number-theoretic Nat square root somewhere that rounds down the exponents of primes, rather than the real square root, right?

docs#Nat.floorRoot

Eric Wieser (May 27 2024 at 10:08):

Which one should get the nthRoot notation?

Yaël Dillies (May 27 2024 at 10:09):

There's also docs#Nat.ceilRoot

Eric Wieser (May 27 2024 at 10:09):

I guess you could imagine a ceilSqrt too

Mario Carneiro (May 27 2024 at 16:53):

Michael Stoll said:

But, contrary to the Nat version, this is because of a junk value, so I don't think this should be an argument. (I'd argue that Nat.sqrt of a non-square is not a junk value, but something that makes sense and is useful.)

It's been a while since I've seen the galois connection :counterclockwise: emoji...


Last updated: May 02 2025 at 03:31 UTC