Skip to content

Commit

Permalink
use @assume_effects :terminates_locally on bezout, similarly to `…
Browse files Browse the repository at this point in the history
…gcdx`

- also flip output order to align with `gcdx`'s
  • Loading branch information
thchr committed Oct 18, 2024
1 parent 0a54c1f commit 55b9d0a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions .vendor/SmithNormalForm/src/bezout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ Calculates Bézout coefficients.
This is analogous to `gcdx`, and solves the same problem, but does not necessarily return
identical solutions.
"""
function bezout(a::R, b::R) where {R}
Base.@assume_effects :terminates_locally function bezout(a::R, b::R) where {R}
rev = a < b
x, y = rev ? (a,b) : (b,a)

s0, s1 = oneunit(R), zero(R)
t0, t1 = zero(R), oneunit(R)

while y != zero(R)
q = div(x, y)
x, y = y, x - y * q
while !iszero(y)
q, r = divrem(x, y)
x, y = y, r
s0, s1 = s1, s0 - q * s1
t0, t1 = t1, t0 - q * t1
end

s, t = rev ? (s0, t0) : (t0, s0)
g = x

if g == a
if g == a # differs from `gcdx` here (also in `rev` clauses above)
s = one(R)
t = zero(R)
elseif g == -a
s = -one(R)
t = zero(R)
end

return s, t, g
return g, s, t
end
4 changes: 2 additions & 2 deletions .vendor/SmithNormalForm/src/snf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function rowpivot(U::AbstractArray{R,2},
(iszero(b) || i == k) && continue
a = D[i,j]

s, t, g = bezout(a, b)
g, s, t = bezout(a, b)
x = divide(a, g)
y = divide(b, g)

Expand All @@ -103,7 +103,7 @@ function colpivot(V::AbstractArray{R,2},
(iszero(b) || j == k) && continue
a = D[i,j]

s, t, g = bezout(a, b)
g, s, t = bezout(a, b)
x = divide(a, g)
y = divide(b, g)

Expand Down
4 changes: 2 additions & 2 deletions .vendor/SmithNormalForm/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ using SmithNormalForm
a = 12
b = 42

s,t,g = SmithNormalForm.bezout(a,b)
g,s,t = SmithNormalForm.bezout(a,b)
@test s*a + t*b == g
g,s,t = gcdx(a,b)
@test s*a + t*b == g

s,t,g = SmithNormalForm.bezout(b,a)
g,s,t = SmithNormalForm.bezout(b,a)
@test s*b + t*a == g
g,s,t = gcdx(b,a)
@test s*b + t*a == g
Expand Down

0 comments on commit 55b9d0a

Please sign in to comment.