Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert some examples to doctests #1709

Merged
merged 9 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions docs/src/arb.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,32 @@ f = accuracy_bits(a)
Printing real balls can at first sight be confusing. Lets look at the following
example:

```julia
RR = ArbField(64)
```jldoctest
julia> RR = ArbField(64)
Real Field with 64 bits of precision and error bounds

a = RR(1)
b = RR(2)
c = RR(12)
julia> a = RR(1)
1.0000000000000000000

x = ball(a, b)
y = ball(c, b)
julia> b = RR(2)
2.0000000000000000000

mid = midpoint(x)
rad = radius(x)
julia> c = RR(12)
12.000000000000000000

print(x, "\n", y, "\n", mid, "\n", rad)
```
julia> x = ball(a, b)
[+/- 3.01]

which generates
julia> y = ball(c, b)
[1e+1 +/- 4.01]

```
julia> mid = midpoint(x)
1.0000000000000000000

julia> rad = radius(x)
[2.0000000037252902985 +/- 3.81e-20]

julia> print(x, "\n", y, "\n", mid, "\n", rad)
[+/- 3.01]
[1e+1 +/- 4.01]
1.0000000000000000000
Expand Down
32 changes: 23 additions & 9 deletions docs/src/complex.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,30 @@ return it. A copy of the original is not made.

Here are some examples of coercing elements into the Arb complex field.

```
RR = RealField()
CC = ComplexField()
```jldoctest
julia> RR = RealField()
Real field

julia> CC = ComplexField()
Complex field

julia> a = CC(3)
3.0000000000000000000

julia> b = CC(QQ(2,3))
[0.6666666666666666666 +/- 8.48e-20]

julia> c = CC("3 +/- 0.0001")
[3.000 +/- 1.01e-4]

julia> d = CC("-1.24e+12345")
[-1.240000000000000000e+12345 +/- 1.16e+12326]

julia> f = CC("nan +/- inf")
nan

a = CC(3)
b = CC(QQ(2,3))
c = CC("3 +/- 0.0001")
d = CC("-1.24e+12345")
f = CC("nan +/- inf")
g = CC(RR(3))
julia> g = CC(RR(3))
3.0000000000000000000
```

In addition to the above, developers of custom complex field types must ensure
Expand Down
28 changes: 18 additions & 10 deletions docs/src/constructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
with the same name as the type itself. For example, to construct a `BigInt` object in Julia, we simply
call the `BigInt` constructor:

```
n = BigInt("1234567898765434567898765434567876543456787654567890")
```jldoctest
julia> n = parse(BigInt, "1234567898765434567898765434567876543456787654567890")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this has been broken all along. the new one doesn't quite fit the text above but I think it is better than something broken

1234567898765434567898765434567876543456787654567890
```

Julia also uses constructors to convert between types. For example, to convert an `Int` to a `BigInt`:

```
m = BigInt(123)
```jldoctest
julia> m = BigInt(123)
123
```

## How we construct objects in Nemo
Expand All @@ -27,8 +29,9 @@

Consider the following simple example, to create a Flint multiprecision integer:

```
n = ZZ("12345678765456787654567890987654567898765678909876567890")
```jldoctest

Check failure on line 32 in docs/src/constructors.md

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in src/constructors.md:32-35 ```jldoctest julia> n = ZZ("12345678765456787654567890987654567898765678909876567890") 12345678765456787654567890987654567898765678909876567890 ``` Subexpression: n = ZZ("12345678765456787654567890987654567898765678909876567890") Evaluated output: ERROR: UndefVarError: `ZZ` not defined Stacktrace: [1] top-level scope @ none:1 Expected output: 12345678765456787654567890987654567898765678909876567890 diff = Warning: Diff output requires color. 12345678765456787654567890987654567898765678909876567890ERROR: UndefVarError: `ZZ` not defined Stacktrace: [1] top-level scope @ none:1
julia> n = ZZ("12345678765456787654567890987654567898765678909876567890")
lgoettgens marked this conversation as resolved.
Show resolved Hide resolved
12345678765456787654567890987654567898765678909876567890
```

Here `ZZ` is not a Julia type, but a callable object. However, for most purposes one can think of such
Expand All @@ -42,10 +45,15 @@
Nemo provides a set of functions for constructing such parent objects. For example, to create a parent
object for polynomials over the integers, we use the `polynomial_ring` parent object constructor.

```
R, x = polynomial_ring(ZZ, "x")
f = x^3 + 3x + 1
g = R(12)
```jldoctest

Check failure on line 48 in docs/src/constructors.md

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in src/constructors.md:48-57 ```jldoctest julia> R, x = polynomial_ring(ZZ, "x") (Univariate polynomial ring in x over ZZ, x) julia> f = x^3 + 3x + 1 x^3 + 3*x + 1 julia> g = R(12) 12 ``` Subexpression: R, x = polynomial_ring(ZZ, "x") Evaluated output: ERROR: UndefVarError: `polynomial_ring` not defined Stacktrace: [1] top-level scope @ none:1 Expected output: (Univariate polynomial ring in x over ZZ, x) diff = Warning: Diff output requires color. (Univariate polynomial ring in x over ZZ, x)ERROR: UndefVarError: `polynomial_ring` not defined Stacktrace: [1] top-level scope @ none:1

Check failure on line 48 in docs/src/constructors.md

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in src/constructors.md:48-57 ```jldoctest julia> R, x = polynomial_ring(ZZ, "x") (Univariate polynomial ring in x over ZZ, x) julia> f = x^3 + 3x + 1 x^3 + 3*x + 1 julia> g = R(12) 12 ``` Subexpression: f = x^3 + 3x + 1 Evaluated output: ERROR: UndefVarError: `x` not defined Stacktrace: [1] top-level scope @ none:1 Expected output: x^3 + 3*x + 1 diff = Warning: Diff output requires color. x^3 + 3*x + 1ERROR: UndefVarError: `x` not defined Stacktrace: [1] top-level scope @ none:1

Check failure on line 48 in docs/src/constructors.md

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in src/constructors.md:48-57 ```jldoctest julia> R, x = polynomial_ring(ZZ, "x") (Univariate polynomial ring in x over ZZ, x) julia> f = x^3 + 3x + 1 x^3 + 3*x + 1 julia> g = R(12) 12 ``` Subexpression: g = R(12) Evaluated output: ERROR: UndefVarError: `R` not defined Stacktrace: [1] top-level scope @ none:1 Expected output: 12 diff = Warning: Diff output requires color. 12ERROR: UndefVarError: `R` not defined Stacktrace: [1] top-level scope @ none:1
julia> R, x = polynomial_ring(ZZ, "x")
(Univariate polynomial ring in x over ZZ, x)

julia> f = x^3 + 3x + 1
x^3 + 3*x + 1

julia> g = R(12)
12
```

In this example, $R$ is the parent object and we use it to convert the `Int` value $12$ to an element
Expand Down
54 changes: 29 additions & 25 deletions docs/src/exact.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ in the Calcium documentation:

## Basic examples

```julia
```jldoctest
julia> C = CalciumField()
Exact Complex Field
Exact complex field

julia> exp(C(pi) * C(1im)) + 1
0
Expand All @@ -139,13 +139,13 @@ julia> 4*atan(C(1)//5) - atan(C(1)//239) == C(pi)//4
true

julia> Cx, x = polynomial_ring(C, "x")
(Univariate Polynomial Ring in x over Exact Complex Field, x)
(Univariate polynomial ring in x over exact complex field, x)

julia> (a, b) = (sqrt(C(2)), sqrt(C(3)))
(1.41421 {a where a = 1.41421 [a^2-2=0]}, 1.73205 {a where a = 1.73205 [a^2-3=0]})

julia> (x-a-b)*(x-a+b)*(x+a-b)*(x+a+b)
x^4 + (-10)*x^2 + 1
x^4 + -10*x^2 + 1
```

## Conversions and numerical evaluation
Expand All @@ -161,7 +161,7 @@ if Calcium is unable to prove that the value belongs
to the target domain, or if Calcium is unable to compute the explicit
value because of evaluation limits.

```julia
```jldoctest; setup = :(C = CalciumField())
julia> QQ(C(1))
1

Expand All @@ -170,16 +170,19 @@ Root 0.707107 of 2x^2 - 1

julia> QQ(C(pi))
ERROR: unable to convert to a rational number
[...]

julia> QQ(C(10) ^ C(10^9))
ERROR: unable to convert to a rational number
[...]
```

To compute arbitrary-precision numerical enclosures, convert to
`ArbField` or `AcbField`:

```julia
julia> CC = AcbField(64);
```jldoctest; setup = :(C = CalciumField())
julia> CC = AcbField(64)
Complex Field with 64 bits of precision and error bounds

julia> CC(exp(C(1im)))
[0.54030230586813971740 +/- 9.37e-22] + [0.84147098480789650665 +/- 2.51e-21]*im
Expand All @@ -202,7 +205,7 @@ more expensive if one part is smaller than the other, or if the
number is nontrivially purely real or purely imaginary (in which
case an exact proof attempt is made).

```julia
```jldoctest; setup = :(C = CalciumField())
julia> x = sin(C(1), form=:exponential)
0.841471 + 0e-24*I {(-a^2*b+b)/(2*a) where a = 0.540302 + 0.841471*I [Exp(1.00000*I {b})], b = I [b^2+1=0]}

Expand Down Expand Up @@ -235,7 +238,7 @@ results in different internal representations ($x \in \mathbb{Q}(\sqrt{3}, \sqrt
and $y \in \mathbb{Q}(\sqrt{6})$),
the numbers compare as equal:

```julia
```jldoctest; setup = :(C = CalciumField())
julia> x = sqrt(C(2)) * sqrt(C(3))
2.44949 {a*b where a = 1.73205 [a^2-3=0], b = 1.41421 [b^2-2=0]}

Expand All @@ -259,7 +262,7 @@ For example, with default settings, Calcium is currently
able to prove that $e^{e^{-1000}} \ne 1$,
but it fails to prove $e^{e^{-3000}} \ne 1$:

```julia
```jldoctest; setup = :(C = CalciumField())
julia> x = exp(exp(C(-1000)))
1.00000 {a where a = 1.00000 [Exp(5.07596e-435 {b})], b = 5.07596e-435 [Exp(-1000)]}

Expand All @@ -271,12 +274,12 @@ julia> x = exp(exp(C(-3000)))

julia> x == 1
ERROR: Unable to perform operation (failed deciding truth of a predicate): isequal
...
[...]
```

In this case, we can get an answer by allowing a higher working precision:

```julia
```jldoctest
julia> C2 = CalciumField(options=Dict(:prec_limit => 10^5));

julia> exp(exp(C2(-3000))) == 1
Expand All @@ -286,7 +289,7 @@ false
Real numbers can be ordered and sorted the usual way.
We illustrate finding square roots that are well-approximated by integers:

```julia
```jldoctest; setup = :(C = CalciumField())
julia> sort([sqrt(C(n)) for n=0:10], by=x -> abs(x - floor(x + C(1)//2)))
11-element Vector{CalciumFieldElem}:
0
Expand All @@ -306,7 +309,7 @@ As currently implemented, order comparisons involving nonreal numbers yield
*false* (in both directions)
rather than throwing an exception:

```julia
```jldoctest; setup = :(C = CalciumField())
julia> C(1im) < C(1im)
false

Expand Down Expand Up @@ -338,17 +341,17 @@ This also applies to the special value Unknown, used in situations
where Calcium is unable to prove that a value is a number.
To enable special values, use `extended=true`.

```julia
```jldoctest
julia> C = CalciumField()
Exact Complex Field
Exact complex field

julia> 1 // C(0)
ERROR: DomainError with UnsignedInfinity:
Non-number result
...
[...]

julia> Cext = CalciumField(extended=true)
Exact Complex Field (Extended)
Exact complex field (extended)

julia> 1 // Cext(0)
UnsignedInfinity
Expand Down Expand Up @@ -378,7 +381,7 @@ Functions for computing components of real and complex numbers
will perform automatic symbolic simplifications in special cases.
In general, such operations will introduce new extension numbers.

```julia
```jldoctest; setup = :(C = CalciumField())
julia> real(C(2+3im))
2

Expand Down Expand Up @@ -423,7 +426,7 @@ ceil(a::CalciumFieldElem)
Elementary and special functions generally create new extension numbers.
In special cases, simplifications occur automatically.

```julia
```jldoctest; setup = :(C = CalciumField())
julia> exp(C(1))
2.71828 {a where a = 2.71828 [Exp(1)]}

Expand Down Expand Up @@ -454,15 +457,15 @@ julia> erf(C(1)) + erfc(C(1))

Some functions allow representing the result in different forms:

```julia
```jldoctest; setup = :(C = CalciumField())
julia> s1 = sin(C(1))
0.841471 - 0e-24*I {(-a^2*b+b)/(2*a) where a = 0.540302 + 0.841471*I [Exp(1.00000*I {b})], b = I [b^2+1=0]}
0.841471 + 0e-24*I {(-a^2*b+b)/(2*a) where a = 0.540302 + 0.841471*I [Exp(1.00000*I {b})], b = I [b^2+1=0]}
lgoettgens marked this conversation as resolved.
Show resolved Hide resolved

julia> s2 = sin(C(1), form=:direct)
0.841471 {a where a = 0.841471 [Sin(1)]}

julia> s3 = sin(C(1), form=:exponential)
0.841471 - 0e-24*I {(-a^2*b+b)/(2*a) where a = 0.540302 + 0.841471*I [Exp(1.00000*I {b})], b = I [b^2+1=0]}
0.841471 + 0e-24*I {(-a^2*b+b)/(2*a) where a = 0.540302 + 0.841471*I [Exp(1.00000*I {b})], b = I [b^2+1=0]}
lgoettgens marked this conversation as resolved.
Show resolved Hide resolved

julia> s4 = sin(C(1), form=:tangent)
0.841471 {(2*a)/(a^2+1) where a = 0.546302 [Tan(0.500000 {1/2})]}
Expand All @@ -485,19 +488,20 @@ problem in general. Calcium will sometimes fail even in elementary cases.
Here is an example of two constant trigonometric identities where
the first succeeds and the second fails:

```julia
```jldoctest; setup = :(C = CalciumField())
julia> a = sqrt(C(2)) + 1;

julia> cos(a) + cos(2*a) + cos(3*a) == sin(7*a//2)//(2*sin(a//2)) - C(1)//2
true

julia> sin(3*a) == 4 * sin(a) * sin(C(pi)//3 - a) * sin(C(pi)//3 + a)
ERROR: Unable to perform operation (failed deciding truth of a predicate): isequal
[...]
Copy link
Member

Choose a reason for hiding this comment

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

Just out of curiosity, why add these [...] to errors?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

https://documenter.juliadocs.org/dev/man/doctests/#Exceptions told me

To indicate to readers that the output result is truncated and does not display the entire (or any of) the stacktrace you may write [...] at the line where checking should stop, i.e.

and I liked that

```

A possible workaround is to fall back on a numerical comparison:

```julia
```jldoctest; setup = :(C = CalciumField(); a = sqrt(C(2)) + 1;)
julia> abs(cos(a) + cos(2*a) + cos(3*a) - (sin(7*a//2)//(2*sin(a//2)) - C(1)//2)) <= C(10)^-100
true
```
Expand Down
2 changes: 1 addition & 1 deletion docs/src/fraction.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ ceil(::QQFieldElem)

**Examples**

```julia
```jldoctest
julia> d = abs(ZZ(11)//3)
11//3

Expand Down
10 changes: 6 additions & 4 deletions docs/src/gfp.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ GF(n::ZZRingElem)

For example, one can create the Galois field of characteristic $7$ as follows.

```julia
R = GF(7)
```jldoctest
julia> R = GF(7)
Prime field of characteristic 7
```

Elements of the field are then created in the usual way.

```julia
a = R(3)
```jldoctest; setup = :(R = GF(7))
julia> a = R(3)
3
```

Elements of Galois fields have type `fpFieldElem` when $p$ is given to the
Expand Down
Loading
Loading