Skip to content

Commit

Permalink
lib: improve error handling for Image, Images, ...
Browse files Browse the repository at this point in the history
..., PreImage, and PreImages.

If one of these functions figures out that the second argument is not an
element or subset of the Range or the Source, it now tells the user.
  • Loading branch information
ssiccha committed May 17, 2019
1 parent f30c1e4 commit 7fa0e7e
Showing 1 changed file with 70 additions and 30 deletions.
100 changes: 70 additions & 30 deletions lib/mapping.gi
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,22 @@ InstallGlobalFunction( Image, function ( arg )
map := arg[1];
elm := arg[2];

if FamSourceEqFamElm( FamilyObj( map ), FamilyObj( elm ) ) then
if not IsMapping( map ) then
ErrorNoReturn( "<map> must be a mapping" );
fi;

if not IsMapping( map ) then
Error( "<map> must be a mapping" );
elif elm in Source( map ) then
return ImageElm( map, elm );
# image of a single element <elm> under the mapping <map>
if FamSourceEqFamElm( FamilyObj( map ), FamilyObj( elm ) ) then
if not elm in Source( map ) then
ErrorNoReturn( "<elm> must be an element of Source(<map>)" );
fi;
return ImageElm( map, elm );

# image of a set or list of elments <elm> under the mapping <map>
elif CollFamSourceEqFamElms( FamilyObj( map ), FamilyObj(elm) )
and IsSubset( Source( map ), elm ) then
elif CollFamSourceEqFamElms( FamilyObj( map ), FamilyObj(elm) ) then
if not IsSubset( Source( map ), elm ) then
ErrorNoReturn( "<coll> must be a subset of Source(<map>)" );
fi;

if IsDomain( elm ) or IsSSortedList( elm ) then
if HasSource(map) and IsIdenticalObj(Source(map),elm) then
Expand All @@ -185,16 +190,23 @@ InstallGlobalFunction( Image, function ( arg )
fi;
elif IsHomogeneousList( elm ) then
return ImagesSet( map, Set( elm ) );
else
ErrorNoReturn( "<coll> must be a domain, a set, ",
"or a homogeneous list" );
fi;

# image of the empty list
elif IsList( elm ) and IsEmpty( elm ) then

return [];

else
ErrorNoReturn( "could not match the family of <elm> or <coll> to ",
"the family of Source(<map>)" );
fi;
fi;
Error( "usage: Image(<map>), Image(<map>,<elm>), Image(<map>,<coll>)" );
ErrorNoReturn( "usage: Image(<map>), Image(<map>,<elm>), ",
"Image(<map>,<coll>)" );
end );


Expand All @@ -220,33 +232,43 @@ InstallGlobalFunction( Images, function ( arg )
elm := arg[2];

if not IsGeneralMapping( map ) then
Error( "<map> must be a general mapping" );
ErrorNoReturn( "<map> must be a general mapping" );
fi;

# image of a single element <elm> under the mapping <map>
if FamSourceEqFamElm( FamilyObj( map ), FamilyObj( elm ) )
and elm in Source( map ) then

if FamSourceEqFamElm( FamilyObj( map ), FamilyObj( elm ) ) then
if not elm in Source( map ) then
ErrorNoReturn( "<elm> must be an element of Source(<map>)" );
fi;
return ImagesElm( map, elm );

# image of a set or list of elments <elm> under the mapping <map>
elif CollFamSourceEqFamElms( FamilyObj( map ), FamilyObj(elm) )
and IsSubset( Source( map ), elm ) then
elif CollFamSourceEqFamElms( FamilyObj( map ), FamilyObj(elm) ) then
if not IsSubset( Source( map ), elm ) then
ErrorNoReturn( "<coll> must be a subset of Source(<map>)" );
fi;

if IsDomain( elm ) or IsSSortedList( elm ) then
return ImagesSet( map, elm );
elif IsHomogeneousList( elm ) then
return ImagesSet( map, Set( elm ) );
else
ErrorNoReturn( "<coll> must be a domain, a set, ",
"or a homogeneous list" );
fi;

# image of the empty list
elif IsList( elm ) and IsEmpty( elm ) then

return [];

else
ErrorNoReturn( "could not match the family of <elm> or <coll> to ",
"the family of Source(<map>)" );
fi;
fi;
Error("usage: Images(<map>), Images(<map>,<elm>), Images(<map>,<coll>)");
ErrorNoReturn( "usage: Images(<map>), Images(<map>,<elm>), ",
"Images(<map>,<coll>)" );
end );


Expand Down Expand Up @@ -275,30 +297,39 @@ InstallGlobalFunction( PreImage, function ( arg )
if FamRangeEqFamElm( FamilyObj( map ), FamilyObj( img ) ) then
if not ( IsGeneralMapping( map ) and IsInjective( map )
and IsSurjective( map ) ) then
Error( "<map> must be an inj. and surj. mapping" );
elif img in Range( map ) then
return PreImageElm( map, img );
ErrorNoReturn( "<map> must be an inj. and surj. mapping" );
elif not img in Range( map ) then
ErrorNoReturn( "<elm> must be an element of Range(<map>)" );
fi;
return PreImageElm( map, img );

# preimage of a set or list of elments <img> under <map>
elif CollFamRangeEqFamElms( FamilyObj( map ), FamilyObj( img ) )
and IsSubset( Range( map ), img ) then
elif CollFamRangeEqFamElms( FamilyObj( map ), FamilyObj( img ) ) then
if not IsSubset( Range( map ), img ) then
ErrorNoReturn( "<coll> must be a subset of Range(<map>)" );
fi;

if IsDomain( img ) or IsSSortedList( img ) then
return PreImagesSet( map, img );
elif IsHomogeneousList( img ) then
return PreImagesSet( map, Set( img ) );
else
ErrorNoReturn( "<coll> must be a domain, a set, ",
"or a homogeneous list" );
fi;

# preimage of the empty list
elif IsList( img ) and IsEmpty( img ) then

return [];

else
ErrorNoReturn( "could not match the family of <elm> or <coll> to ",
"the family of Source(<map>)" );
fi;
fi;
Error( "usage: PreImage(<map>), PreImage(<map>,<img>), ",
"PreImage(<map>,<coll>)" );
ErrorNoReturn( "usage: PreImage(<map>), PreImage(<map>,<img>), ",
"PreImage(<map>,<coll>)" );
end );


Expand All @@ -324,34 +355,43 @@ InstallGlobalFunction( PreImages, function ( arg )
img := arg[2];

if not IsGeneralMapping( map ) then
Error( "<map> must be a general mapping" );
ErrorNoReturn( "<map> must be a general mapping" );
fi;

# preimage of a single element <img> under <map>
if FamRangeEqFamElm( FamilyObj( map ), FamilyObj( img ) )
and img in Range( map ) then

if FamRangeEqFamElm( FamilyObj( map ), FamilyObj( img ) ) then
if not img in Range( map ) then
ErrorNoReturn( "<elm> must be an element of Range(<map>)" );
fi;
return PreImagesElm( map, img );

# preimage of a set or list of elements <img> under <map>
elif CollFamRangeEqFamElms( FamilyObj( map ), FamilyObj( img ) )
and IsSubset( Range( map ), img ) then
elif CollFamRangeEqFamElms( FamilyObj( map ), FamilyObj( img ) ) then
if not IsSubset( Range( map ), img ) then
ErrorNoReturn( "<coll> must be a subset of Range(<map>)" );
fi;

if IsDomain( img ) or IsSSortedList( img ) then
return PreImagesSet( map, img );
elif IsHomogeneousList( img ) then
return PreImagesSet( map, Set( img ) );
else
ErrorNoReturn( "<coll> must be a domain, a set, ",
"or a homogeneous list" );
fi;

# preimage of the empty list
elif IsList( img ) and IsEmpty( img ) then

return [];

else
ErrorNoReturn( "could not match the family of <elm> or <coll> to ",
"the family of Source(<map>)" );
fi;
fi;
Error( "usage: PreImages(<map>), PreImages(<map>,<img>), ",
"PreImages(<map>,<coll>)" );
ErrorNoReturn( "usage: PreImages(<map>), PreImages(<map>,<img>), ",
"PreImages(<map>,<coll>)" );
end );


Expand Down

0 comments on commit 7fa0e7e

Please sign in to comment.