Hello All,
I have searched a bit looking for a matrix inverse modulo an integer, and
could not find a function. Would there be any interest in adding a function?
I have needed it a fair amount. Here is a copy of a non-(optimal/efficient)
function I wrote with bugs and all.
Thanks,
Jason
function minvmod{T<:Integer}(Arr::Array{T,2},N)
A=copy(Arr)
n=size(A)[1]
Ainv=eye(T,n)
for i=1:n
if gcd(A[i,i],N) != 1
isz=true
else
isz=false
end
k=i+1
while isz
if gcd(A[i,k],N) == 1
isz=false
A[:,i],A[:,k]=A[:,k],A[:,i]
Ainv[:,i],Ainv[:,k]=Ainv[:,k],Ainv[:,i]
else
k+=1
end
if k>n
return false
end
end
tem=invmod(A[i,i],N)
for j=1:n
A[i,j] =convert(T,mod(tem*A[i,j],N))
Ainv[i,j]=convert(T,mod(tem*Ainv[i,j],N))
end
for j=union(1:i-1,i+1:n)
if A[j,i] != convert(T,0)
tem=A[j,i]
for k=1:n
A[j,k]=convert(T,mod(A[j,k]-tem*A[i,k],N))
Ainv[j,k]=convert(T,mod(Ainv[j,k]-tem*Ainv[i,k],N))
end
end
end
end
return Ainv
end