Skip to content

Commit 82092f3

Browse files
committed
Better error message
1 parent d808a59 commit 82092f3

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/arithmetic.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,23 @@ function is_invertible(a::Mod{N})::Bool where {N}
2020
end
2121
is_invertible(a::GaussMod)::Bool = is_invertible(real(a * a'))
2222

23-
inv(a::Mod{N}) where {N} = Mod{N}(invmod(value(a), N))
23+
function _invmod(x::S, m::T) where {S<:Integer,T<:Integer}
24+
(g, v, _) = gcdx(x, m)
25+
if g != 1
26+
error("Mod{$m}($x) is not invertible")
27+
end
28+
return v
29+
end
30+
31+
inv(a::Mod{N}) where {N} = Mod{N}(_invmod(value(a), N))
2432
function inv(a::GaussMod{N}) where {N}
2533
d = real(a * a')
26-
return inv(d) * a'
34+
try
35+
result = inv(d) * a'
36+
return result
37+
catch
38+
error("$a is not invertible")
39+
end
2740
end
2841

2942
(/)(a::AbstractMod, b::AbstractMod) = a * inv(b)

0 commit comments

Comments
 (0)