Skip to content

Taking an inverse of an integer matrix throws an error instead of returning fail #1184

Description

@reiniscirpons

Consider the following 3 examples:

gap> A := Matrix(Integers, [
> [1, 2, 3],
> [4, 3, -1],
> [1, 0, -3]]);
<3x3-matrix over Integers>
gap> B := Matrix(Integers, [
> [1, -1, 0],
> [0, 0, 1],
> [1, 0, 1]]);
<3x3-matrix over Integers>
gap> C := Matrix(Integers, [
> [0, 0, 0],
> [0, 1, -1],
> [1, 0, 1]]);
<3x3-matrix over Integers>
gap> A^-1;
Error, the elements in <list> must lie in <basedomain>
*[1] Error( "the elements in <list> must lie in <basedomain>" );
   @ /Users/rcirpons/Desktop/Source/gap/lib/matobjplist.gi:90
 [2] MakeIsPlistVectorRep( v![1], list, true )
   @ /Users/rcirpons/Desktop/Source/gap/lib/matobjplist.gi:319
 [3] Vector( list[i], t )
   @ /Users/rcirpons/Desktop/Source/gap/lib/matobjplist.gi:687
 [4] Matrix( n, Length( n ), M )
   @ /Users/rcirpons/Desktop/Source/gap/lib/matobjplist.gi:1215
<function "InverseSameMutability [ IsPlistMatrixRep ]">( <arguments> )
 called from read-eval loop at *stdin*:160
you can 'quit;' to quit to outer loop, or
you can 'return;' to continue
brk> quit;
gap> Display(B ^ -1);
<3x3-matrix over Integers:
[[ 0, -1, 1 ]
 [ -1, -1, 1 ]
 [ 0, 1, 0 ]
]>
gap> C^-1;
fail

Inverting A throws, however inverting B succeeds and inverting C fails but does not throw.
The error for A seems to be thrown since A is invertible over the rationals but not the integers, and the
method seems to be trying to cast the rational inverse to an integer matrix, which understandably fails.

I think the correct behavior would be to simply fail if the inverse is not an integer matrix. This can be checked rather easily, since
an integer matrix is invertible with an inverse lying in the integers precisely when the determinant is +1 or -1. So probably adding
this check prior to trying to invert, and returning fail if it is not +1 or -1 is the simplest fix. At the very least we should document this behavior if this is intended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    possible-bugA label for issues that might report bugs.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions