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 2 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
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