Zulip Chat Archive

Stream: maths

Topic: Computable real.sqrt


Patrick Stevens (Apr 22 2022 at 21:51):

If anyone is interested in a mini project to fill in a long-standing TODO in data.real.sqrt, I've started https://github.com/leanprover-community/mathlib/pull/13634 which adds some theorems around sqrt_aux, in preparation for showing that Heron's method defines a sequence which is Cauchy for sufficiently nicely-expressed input reals, and then for showing that it defines a real number which is equal to the square root of the input (I've already got that it's at least the square root). Feel free to pitch in!

I'm particularly sad about the amount of grubbing around I had to do to show the most basic things about rational numbers.

Kevin Buzzard (Apr 22 2022 at 22:06):

A lot of the grubbing around is stuff we have already. Do you know about library_search? I left some comments.

Patrick Stevens (Apr 22 2022 at 22:25):

Thanks - I used library_search extensively to get that far, and I have no idea why I didn't find those, but the ones you pointed out certainly work when I run library_search now! I thought I was being careful only to write a proof if I really couldn't find it, and I certainly think that library_search is the first line I write when I try and prove something; I'll put my bizarre failure down to my new and shiny covid infection.

Patrick Stevens (Apr 25 2022 at 21:07):

Sigh, I really should learn to run little experiments before trying to prove something - I just realised I spent an awful lot of effort trying to prove something that's patently false

Patrick Stevens (Apr 25 2022 at 21:08):

But I did get an epsilon down quite small - it's just not an epsilon that's any use to anyone

Patrick Stevens (Apr 27 2022 at 16:37):

Does anyone know whether Heron's formula actually converges at 0?

def sqrt_aux (f : cau_seq  abs) :   
| 0       := max 1 (f 0)
| (n + 1) := let s := sqrt_aux n in (s + (max 0 (f (n+1))) / s) / 2

I've shown that it converges and is equal (as a real number) to sqrt f if f > 0 or if f < 0, and I've shown that if f >= 0 then the sequence infinitely often gets arbitrarily close to the square root, but for the life of me I can't show that it converges when f = 0 (as a real number). Does anyone know if it's even true?

Patrick Stevens (Apr 27 2022 at 16:40):

(a number of useful facts, like sqrt_aux f i ^ 2 >= f i for all i, are present in https://github.com/leanprover-community/mathlib/pull/13634/files)

Patrick Stevens (Apr 27 2022 at 16:42):

Defining f[n_] := If[OddQ[n], 0, 1/2^(n/2)] as a representation of 0, and using an initial guess of 0.01 to simulate a difficult case, Mathematica gives {0.005, 50.0025, 25.0013, 12.5056, 6.25281, 3.1364, 1.5682, 0.804028, 0.402014, 0.239874, 0.119937, 0.125107, 0.0625534, 0.0937233, …, which certainly doesn't seem to be converging in any nice-and-easy-to-prove way

Reid Barton (Apr 27 2022 at 16:58):

Looks to me like it does not converge. If f n is 0 for many successive n then the approximations can get arbitrarily small, and then if the next f n is size ~epsilon, the next approximation could be unboundedly large

Patrick Stevens (Apr 27 2022 at 17:00):

I have increasingly been coming to that conclusion :(

Reid Barton (Apr 27 2022 at 17:01):

What I think would work is to first do this for a fixed rational number, and then diagonalize.

Reid Barton (Apr 27 2022 at 17:01):

But anyways, cau_seq is not a reasonable or useful notion of computability for real numbers

Patrick Stevens (Apr 27 2022 at 17:02):

All I really wanted to do was remove the word noncomputable from the current real.sqrt

Patrick Stevens (Apr 27 2022 at 17:03):

Do you mean cau_seq is not useful because it's just an existential quantifier which is a Prop rather than a proper sigma type with data in?

Reid Barton (Apr 27 2022 at 17:03):

noncomputable theory

Reid Barton (Apr 27 2022 at 17:05):

Right, there is no information about the rate of convergence

Jireh Loreaux (Apr 27 2022 at 17:06):

@Patrick Stevens If I give you a Cauchy sequence and tell you the billionth term is 5, you can tell me exactly zero information about the real number corresponding to that Cauchy sequence.

Patrick Stevens (Apr 27 2022 at 17:07):

Well, only because our "Cauchy" doesn't tell you the values inside the existentials, but yes

Jireh Loreaux (Apr 27 2022 at 17:07):

Right, you would need something like Bishop's constructive reals

Patrick Stevens (Apr 27 2022 at 17:26):

Ah, given that it's impossible to compute an $A$ out of an $A / \sym$, this was never going work in the first place (as I'm sure was obvious to nearly everyone) - I can't get a cau_seq out of an $\mathbb{R}$ at all

Patrick Stevens (Apr 27 2022 at 17:26):

(I'm still not used to Prop :P I still expect things to have data attached to them)

Eric Wieser (Apr 27 2022 at 17:38):

It's possible to do that extraction in meta land with docs#quot.unquot

Reid Barton (Apr 27 2022 at 18:30):

This wasn't going to be a problem, you can use quotient.lift as long as the resulting real doesn't depend on the choice of representative (which must be the case if it was going to be the square root)

Kyle Miller (Apr 27 2022 at 21:19):

Given some semi-recent changes to Lean 3, you can define an #eval-friendly typeclass that gives how to compute particular reals using rational approximations:

class real.computable (x : ) :=
(approx (ε : ) (pos : 0 < ε) : )
(is_close (ε : ) (pos : 0 < ε) : |(approx ε pos - x : )| < ε)

I defined some instances for bit0, bit1, some coercions, and addition. Here's an approximation of 2+4 that's within 1/10:

#eval (2 + 4 : ).approx (1/10) (by linarith)
-- 6

code

Kyle Miller (Apr 27 2022 at 21:21):

These sorts of instances are a place you could put an algorithm for square roots of reals.

Kyle Miller (Apr 27 2022 at 21:21):

By the way, that real.computable class is just proof-of-concept. It seemed plausible as a definition, but there could be better ones.

Mario Carneiro (Apr 27 2022 at 22:32):

This is interesting, because I recently investigated this question with regard to a proof in intuitionistic logic of the existence of the real square root function using Heron's formula in metamath, and the proof does go through: The sequence xn+1=(xn+a/xn)/2x_{n+1}=(x_n+a/x_n)/2 converges to a\sqrt a for all a0a\ge 0.

But @Reid Barton is right that this proof does not work when you use a Cauchy sequence ana_n instead of literally using aa there. Concretely, we can define a sequence ana_n such that a0=1a_0=1 and for other nn, if n=k2n=k^2 is a perfect square then an=8(12/4k)/4ka_n=8(1-2/4^k)/4^k, and otherwise an=0a_n=0. Then sqrt_aux a has the pattern 1,1,21,22,1,21,22,23,24,1,1,1,2^{-1},2^{-2},1,2^{-1},2^{-2},2^{-3},2^{-4},1,\dots: it gets arbitrarily close to 00 and is also equal to 11 infinitely often, so it does not converge. But 0an8/4k0\le a_n\le 8/4^k for n>0n>0 so it converges to 0.

Patrick Stevens (Apr 27 2022 at 22:36):

When it's less late at night I'll play with sqrt_aux x e n having max (e ^ 2) (f (n + 1)) instead of the current e = 0, and see if I can make that converge, and then hope that sqrt x n := sqrt_aux x (1 / n) n does the job

Mario Carneiro (Apr 27 2022 at 22:45):

Do we have the ability to make computable cauchy sequences of reals? That would be the easiest way to make the standard proof work


Last updated: Dec 20 2023 at 11:08 UTC