Skip to content

Commit

Permalink
Merge pull request #359 from james-d-mitchell/random-matrix
Browse files Browse the repository at this point in the history
Random matrix
  • Loading branch information
wilfwilson authored Aug 23, 2017
2 parents d908888 + ea12948 commit 85cd435
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 29 deletions.
12 changes: 10 additions & 2 deletions gap/elements/semiringmat.gd
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ DeclareConstructor("AsMatrix", [IsMatrixOverSemiring,

DeclareAttribute("AsTransformation", IsMatrixOverSemiring);

DeclareGlobalFunction("RandomMatrix");
DeclareOperation("RandomMatrix", [IsOperation and IsFunction, IsPosInt]);
DeclareOperation("RandomMatrix",
[IsOperation and IsFunction, IsPosInt, IsInt]);
DeclareOperation("RandomMatrix",
[IsOperation and IsFunction, IsPosInt, IsInt, IsInt]);
DeclareOperation("RandomMatrix", [IsSemiring, IsInt]);
DeclareOperation("RandomMatrix", [IsSemiring, IsInt, IsPosInt]);
DeclareOperation("RandomMatrix", [IsSemiring, IsInt, IsList]);

DeclareConstructor("RandomMatrixCons", [IsMatrixOverSemiring,
IsPosInt]);
DeclareConstructor("RandomMatrixCons", [IsMatrixOverSemiring,
Expand All @@ -98,7 +106,7 @@ DeclareAttribute("DimensionOfMatrixOverSemiring", IsMatrixOverSemiring);
DeclareAttribute("DimensionOfMatrixOverSemiringCollection",
IsMatrixOverSemiringCollection);
DeclareAttribute("TransposedMat", IsMatrixOverSemiring);
DeclareAttribute("IsTorsion", IsMatrixOverSemiring);
DeclareProperty("IsTorsion", IsMatrixOverSemiring);

# Cannot use TypeObj since it can contain information about
# properties satisfied (or not) by the object.
Expand Down
90 changes: 67 additions & 23 deletions gap/elements/semiringmat.gi
Original file line number Diff line number Diff line change
Expand Up @@ -328,33 +328,77 @@ function(R, mat)
return Matrix(R, AsList(mat));
end);

InstallGlobalFunction(RandomMatrix,
function(arg)
if Length(arg) >= 2 and IsOperation(arg[1]) and IsFunction(arg[1])
and IsPosInt(arg[2]) then
if Length(arg) = 2 then
return RandomMatrixCons(arg[1], arg[2]);
elif Length(arg) >= 3 and IsInt(arg[3]) then
if Length(arg) = 3 then
return RandomMatrixCons(arg[1], arg[2], arg[3]);
elif Length(arg) = 4 and IsInt(arg[4]) then
return RandomMatrixCons(arg[1], arg[2], arg[3], arg[4]);
fi;
fi;
elif Length(arg) = 2 and IsSemiring(arg[1])
and (IsInt(arg[2]) and arg[2] >= 0) then
return RandomMatrixOp(arg[1], arg[2]);
elif Length(arg) = 3 and IsSemiring(arg[1])
and (IsInt(arg[2]) and arg[2] >= 0)
and (IsList(arg[3]) or IsPosInt(arg[3])) then
return RandomMatrixOp(arg[1], arg[2], arg[3]);
InstallMethod(RandomMatrix, "for an operation and pos int",
[IsOperation and IsFunction, IsPosInt],
function(op, dim)
return RandomMatrixCons(op, dim);
end);

InstallMethod(RandomMatrix, "for an operation, pos int, and int",
[IsOperation and IsFunction, IsPosInt, IsInt],
function(op, dim, threshold)
return RandomMatrixCons(op, dim, threshold);
end);

InstallMethod(RandomMatrix, "for an operation, pos int, int, and int",
[IsOperation and IsFunction, IsPosInt, IsInt, IsInt],
function(op, dim, threshold, period)
return RandomMatrixCons(op, dim, threshold, period);
end);

InstallMethod(RandomMatrix, "for a semiring and non-negative int",
[IsSemiring, IsInt],
function(semiring, dim)
if dim < 0 then
TryNextMethod();
fi;
return RandomMatrixOp(semiring, dim);
end);

ErrorNoReturn("Semigroups: RandomMatrix: usage,\n",
"the arguments must be: filter, pos int[, pos int[,",
" pos int]],");
InstallMethod(RandomMatrix, "for a semiring, non-negative int, and pos int",
[IsSemiring, IsInt, IsPosInt],
function(semiring, dim, rank)
if dim < 0 then
TryNextMethod();
fi;
return RandomMatrixOp(semiring, dim, rank);
end);

InstallMethod(RandomMatrix, "for a semiring, non-negative int, and list",
[IsSemiring, IsInt, IsList],
function(semiring, dim, ranks)
if dim < 0 then
TryNextMethod();
fi;
return RandomMatrixOp(semiring, dim, ranks);
end);

#InstallGlobalFunction(RandomMatrix,
#function(arg)
# if Length(arg) >= 2 and IsOperation(arg[1]) and IsFunction(arg[1])
# and IsPosInt(arg[2]) then
# if Length(arg) = 2 then
# elif Length(arg) >= 3 and IsInt(arg[3]) then
# if Length(arg) = 3 then
# return RandomMatrixCons(arg[1], arg[2], arg[3]);
# elif Length(arg) = 4 and IsInt(arg[4]) then
# return RandomMatrixCons(arg[1], arg[2], arg[3], arg[4]);
# fi;
# fi;
# elif Length(arg) = 2 and IsSemiring(arg[1])
# and (IsInt(arg[2]) and arg[2] >= 0) then
# return RandomMatrixOp(arg[1], arg[2]);
# elif Length(arg) = 3 and IsSemiring(arg[1])
# and (IsInt(arg[2]) and arg[2] >= 0)
# and (IsList(arg[3]) or IsPosInt(arg[3])) then
# return RandomMatrixOp(arg[1], arg[2], arg[3]);
# fi;
#
# ErrorNoReturn("Semigroups: RandomMatrix: usage,\n",
# "the arguments must be: filter, pos int[, pos int[,",
# " pos int]],");
#end);

InstallMethod(AsTransformation, "for a matrix over semiring",
[IsMatrixOverSemiring],
function(mat)
Expand Down
2 changes: 1 addition & 1 deletion gap/semigroups/semimaxplus.gd
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ DeclareOperation("FullTropicalMaxPlusMonoid", [IsPosInt, IsPosInt]);
DeclareOperation("FullTropicalMinPlusMonoid", [IsPosInt, IsPosInt]);

DeclareOperation("NormalizeSemigroup", [IsMaxPlusMatrixSemigroup]);
DeclareAttribute("IsTorsion", IsMaxPlusMatrixSemigroup);
DeclareProperty("IsTorsion", IsMaxPlusMatrixSemigroup);
2 changes: 1 addition & 1 deletion gap/semigroups/semimaxplus.gi
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ function(S)
end);

InstallMethod(IsTorsion,
"for max-plus matrix semigroups",
"for a max-plus matrix semigroup",
[IsMaxPlusMatrixSemigroup],
function(S)
local gens, dim, func, m, rad, T;
Expand Down
4 changes: 2 additions & 2 deletions tst/standard/semiringmat.tst
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ gap> RandomMatrix(IsNTPMatrix, 2, 2, 2);;

#T# semiringmat: RandomMatrix, 6
gap> RandomMatrix(7, 2, 3);
Error, Semigroups: RandomMatrix: usage,
the arguments must be: filter, pos int[, pos int[, pos int]],
Error, no method found! For debugging hints type ?Recovery from NoMethodFound
Error, no 1st choice method found for `RandomMatrix' on 3 arguments
#T# semiringmat: RandomMatrix, for a semiring, 7
gap> RandomMatrix(Integers, 20);;
Expand Down

0 comments on commit 85cd435

Please sign in to comment.