Skip to content

Commit eae439b

Browse files
authored
Add iroot and check argument for root (#1118)
* Add iroot and make root take a check argument.
1 parent 7314d4a commit eae439b

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

docs/src/integer.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,20 @@ isqrtrem(::fmpz)
285285
root(::fmpz, ::Int)
286286
```
287287

288+
```@docs
289+
iroot(::fmpz, ::Int)
290+
```
291+
288292
**Examples**
289293

290294
```julia
291295
a = ZZ(13)
296+
b = ZZ(27)
292297

293-
b = isqrt(a)
298+
c = isqrt(a)
294299
s, r = isqrtrem(a)
295-
c = root(a, 3)
300+
d = iroot(a, 3)
301+
k = root(b, 3; check=true)
296302
```
297303

298304
### Number theoretic functionality

src/flint/fmpz.jl

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export fmpz, FlintZZ, FlintIntegerRing, parent, show, convert, hash,
4343
euler_phi, fibonacci, moebius_mu, primorial, rising_factorial,
4444
number_of_partitions, canonical_unit, isunit, isequal, addeq!, mul!,
4545
issquare, square_root, issquare_with_square_root, next_prime,
46-
iszero, rand, rand_bits, binomial, factorial, rand_bits_prime
46+
iszero, rand, rand_bits, binomial, factorial, rand_bits_prime, iroot
4747

4848
###############################################################################
4949
#
@@ -1132,16 +1132,36 @@ function Base.sqrt(x::fmpz)
11321132
end
11331133

11341134
@doc Markdown.doc"""
1135-
root(x::fmpz, n::Int)
1135+
root(x::fmpz, n::Int; check::Bool=true)
11361136
1137-
Return the floor of the $n$-the root of $x$. We require $n > 0$ and that
1138-
$x \geq 0$ if $n$ is even.
1137+
Return the $n$-the root of $x$. We require $n > 0$ and that
1138+
$x \geq 0$ if $n$ is even. By default the function tests whether the input was
1139+
a perfect $n$-th power and if not raises an exception. If `check=false` this
1140+
check is omitted.
11391141
"""
1140-
function root(x::fmpz, n::Int)
1142+
function root(x::fmpz, n::Int; check::Bool=true)
11411143
x < 0 && iseven(n) && throw(DomainError((x, n), "Argument `x` must be positive if exponent `n` is even"))
11421144
n <= 0 && throw(DomainError(n, "Exponent must be positive"))
11431145
z = fmpz()
1144-
ccall((:fmpz_root, libflint), Nothing,
1146+
res = ccall((:fmpz_root, libflint), Bool,
1147+
(Ref{fmpz}, Ref{fmpz}, Int), z, x, n)
1148+
#= Check disabled until flint-2.9 comes out
1149+
check && !res && error("Not a perfect n-th power (n = $n)")
1150+
=#
1151+
return z
1152+
end
1153+
1154+
@doc Markdown.doc"""
1155+
iroot(x::fmpz, n::Int)
1156+
1157+
Return the integer truncation of the $n$-the root of $x$ (round towards zero).
1158+
We require $n > 0$ and that $x \geq 0$ if $n$ is even.
1159+
"""
1160+
function iroot(x::fmpz, n::Int)
1161+
x < 0 && iseven(n) && throw(DomainError((x, n), "Argument `x` must be positive if exponent `n` is even"))
1162+
n <= 0 && throw(DomainError(n, "Exponent must be positive"))
1163+
z = fmpz()
1164+
ccall((:fmpz_root, libflint), Bool,
11451165
(Ref{fmpz}, Ref{fmpz}, Int), z, x, n)
11461166
return z
11471167
end

test/flint/fmpz-test.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,23 @@ end
510510
@test_throws DomainError isqrtrem(-fmpz(12))
511511

512512
@test root(fmpz(1000), 3) == 10
513+
@test root(-fmpz(27), 3) == -3
514+
@test root(fmpz(27), 3; check=true) == 3
513515

514516
@test_throws DomainError root(-fmpz(1000), 4)
515-
516517
@test_throws DomainError root(fmpz(1000), -3)
518+
519+
#= Disabled until Flint-2.9 comes out
520+
@test_throws ErrorException root(fmpz(1100), 3; check=true)
521+
@test_throws ErrorException root(-fmpz(40), 3; check=true)
522+
=#
523+
524+
@test iroot(fmpz(1000), 3) == 10
525+
@test iroot(fmpz(1100), 3) == 10
526+
@test iroot(-fmpz(40), 3) == -3
527+
528+
@test_throws DomainError iroot(-fmpz(1000), 4)
529+
@test_throws DomainError iroot(fmpz(1000), -3)
517530
end
518531

519532
@testset "fmpz.extended_gcd" begin

0 commit comments

Comments
 (0)