Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic DisplayString function #5163

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 217 additions & 3 deletions lib/matobj.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ InstallMethod( String,
String( Unpack( v ) ), " )" ) );

BindGlobal( "ViewStringForMatrixObj",
M -> Concatenation( "<matrix object of dimensions ",
M -> Concatenation( "<a matrix object of dimensions ",
String( NumberRows( M ) ), "x", String( NumberColumns( M ) ),
" over ", String( BaseDomain( M ) ), ">" ) );

Expand All @@ -1419,8 +1419,222 @@ InstallMethod( ViewString,
ViewStringForMatrixObj );

InstallMethod( DisplayString,
[ IsMatrixOrMatrixObj ],
ViewStringForMatrixObj );
[ IsMatrixOrMatrixObj ],
function(M)
local i,j,m,numberCols,numberRows,baseDom,zeroEle,outputstring, MaxRowSize, MaxColumnSize, maxp, bl, arr, arrrow, l, k, max, counter, icounter;
numberRows := NrRows(M);
numberCols := NrCols(M);
baseDom := BaseDomain(M);
zeroEle := Zero(baseDom);
MaxRowSize := 16;
MaxColumnSize := 16;
maxp:=1;
bl:=" ";
outputstring := "<a ";
if IsMutable(M) then
Append(outputstring, "mutable ");
else
Append(outputstring, "immutable ");
fi;
Append(outputstring,Concatenation(String(numberRows),"x",String(numberCols),"-matrix over ",String(baseDom)," [\n"));
if numberCols > MaxColumnSize then
if numberRows > MaxRowSize then
arr := ZeroMatrix(Rationals,6,6);
for i in [1,2,3] do
for j in [1,2,3] do
arr[i,j] := String(M[i,j]);
od;
counter := 4;
for j in [numberCols-2,numberCols-1,numberCols] do
arr[i,counter] := String(M[i,j]);
counter := counter + 1;
od;
od;
danielrademacher marked this conversation as resolved.
Show resolved Hide resolved
icounter := 4;
for i in [numberRows-2,numberRows-1,numberRows] do
for j in [1,2,3] do
arr[icounter,j] := String(M[i,j]);
od;
counter := 4;
for j in [numberCols-2,numberCols-1,numberCols] do
arr[icounter,counter] := String(M[i,j]);
counter := counter + 1;
od;
icounter := icounter + 1;
od;
max := Maximum( List( arr,
function(x)
if Length(x) = 0 then
return 1;
else
return Maximum( List(x,Length) );
fi;
end) );
for i in [1,2,3] do
Append(outputstring, "[ " );
for j in [1,2,3] do
if zeroEle = M[i,j] then
Append(outputstring, String( ".", max + maxp ) );
else
Append(outputstring, String( arr[ i ][ j ], max + maxp ) );
fi;
Append(outputstring," " );
od;
Append(outputstring," ... ");
counter := 4;
for j in [numberCols-2,numberCols-1,numberCols] do
if zeroEle = M[i,j] then
Append(outputstring, String( ".", max + maxp ) );
else
Append(outputstring, String( arr[ i ][ counter ], max + maxp ) );
fi;
if counter = 6 then
Append(outputstring,Concatenation( " ]",bl ));
else
Append(outputstring," " );
fi;
counter := counter + 1;
od;
Append(outputstring,"\n");
od;
Append(outputstring," .......... \n");
icounter := 4;
for i in [numberRows-2,numberRows-1,numberRows] do
Append(outputstring, "[ " );
for j in [1,2,3] do
if zeroEle = M[i,j] then
Append(outputstring, String( ".", max + maxp ) );
else
Append(outputstring, String( arr[ icounter ][ j ], max + maxp ) );
fi;
Append(outputstring," " );
od;
Append(outputstring," ... ");
counter := 4;
for j in [numberCols-2,numberCols-1,numberCols] do
if zeroEle = M[i,j] then
Append(outputstring, String( ".", max + maxp ) );
else
Append(outputstring, String( arr[ icounter ][ counter ], max + maxp ) );
fi;
if counter = 6 then
Append(outputstring,Concatenation( " ]",bl ));
else
Append(outputstring," " );
fi;
od;
Append(outputstring,"]\n");
icounter := icounter + 1;
od;
fi;
elif numberRows > MaxRowSize then
arr := ZeroMatrix(Rationals,6,numberCols);
for i in [1,2,3] do
for j in [1..numberCols] do
arr[i,j] := String(M[i,j]);
od;
od;
icounter := 4;
for i in [numberRows-2,numberRows-1,numberRows] do
for j in [1..numberCols] do
arr[icounter,j] := String(M[i,j]);
od;
icounter := icounter + 1;
od;
max := Maximum( List( arr,
function(x)
if Length(x) = 0 then
return 1;
else
return Maximum( List(x,Length) );
fi;
end) );
for i in [1,2,3] do
Append(outputstring, "[ " );
for j in [1..numberCols] do
if zeroEle = M[i,j] then
Append(outputstring, String( ".", max + maxp ) );
else
Append(outputstring, String( arr[ i ][ j ], max + maxp ) );
fi;
if j = numberCols then
Append(outputstring,Concatenation( " ]",bl ));
else
Append(outputstring," " );
fi;
od;
Append(outputstring,"\n");
od;
Append(outputstring," .......... \n");
icounter := 4;
for i in [numberRows-2,numberRows-1,numberRows] do
Append(outputstring,"[ ");
for j in [1..numberCols] do
if zeroEle = M[i,j] then
Append(outputstring, String( ".", max + maxp ) );
else
Append(outputstring, String( arr[ icounter ][ j ], max + maxp ) );
fi;
if j = numberCols then
Append(outputstring,Concatenation( " ]",bl ));
else
Append(outputstring," " );
fi;
od;
Append(outputstring,"\n");
icounter := icounter + 1;
od;
else
arr := MutableCopyMat(M);
for i in [1..numberRows] do
for j in [1..numberCols] do
arr[i,j] := String(M[i,j]);
od;
od;
max := Maximum( List( arr,
function(x)
if Length(x) = 0 then
return 1;
else
return Maximum( List(x,Length) );
fi;
end) );
for l in [ 1 .. Length( arr ) ] do
Append(outputstring, "[ " );
for k in [ 1 .. Length( arr[ l ] ) ] do
if zeroEle = M[l,k] then
Append(outputstring, String( ".", max + maxp ) );
else
Append(outputstring, String( arr[ l ][ k ], max + maxp ) );
fi;
if k = Length( arr[ l ] ) then
Append(outputstring,Concatenation( " ]",bl ));
else
Append(outputstring," " );
fi;
od;
Append(outputstring, "\n" );
od;
# for i in [1..numberRows] do
# for j in [1..numberCols] do
# if j = 1 then
# Append(outputstring,"[");
# else
# Append(outputstring," ");
# fi;
# if zeroEle = M[i,j] then
# Append(outputstring,".");
# else
# Append(outputstring,String(M[i,j]));
# fi;
# od;
# Append(outputstring,"]\n");
# od;
fi;
Append(outputstring,"]");
Append(outputstring,">\n");
return outputstring;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be done. Which file would you prefer?

end);

InstallMethod( String,
[ IsMatrixObj ],
Expand Down