Skip to content

Commit 8517ae3

Browse files
committed
Bug fix: matrix.ToString() when matrix has no rows or columns
1 parent bc0bd6e commit 8517ae3

File tree

2 files changed

+101
-11
lines changed

2 files changed

+101
-11
lines changed

src/Numerics.Tests/LinearAlgebraTests/Double/DenseMatrixTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,38 @@ public void MatrixFrom1DArrayIsReference()
9595
Assert.AreEqual(10.0, data[0]);
9696
}
9797

98+
[Test]
99+
public void DescriptionForMatrixWithRowsButNoColumns()
100+
{
101+
var matrix = TestMatrices["Tall3x2"];
102+
matrix = matrix.RemoveColumn(matrix.ColumnCount - 1);
103+
matrix = matrix.RemoveColumn(matrix.ColumnCount - 1);
104+
Assert.AreEqual("DenseMatrix 3x0-Double\r\n[empty]\r\n[empty]\r\n[empty]\r\n", matrix.ToString());
105+
}
106+
107+
[Test]
108+
public void DescriptionForMatrixWithColumnsButNoRows()
109+
{
110+
var matrix = TestMatrices["Wide2x3"];
111+
matrix = matrix.RemoveRow(matrix.RowCount - 1);
112+
matrix = matrix.RemoveRow(matrix.RowCount - 1);
113+
Assert.AreEqual("DenseMatrix 0x3-Double\r\n[empty] [empty] [empty]", matrix.ToString());
114+
}
115+
116+
[Test]
117+
public void DescriptionForMatrixWithNoRowsAndNoColumns()
118+
{
119+
var matrix = TestMatrices["Wide2x3"];
120+
matrix = matrix.RemoveRow(matrix.RowCount - 1);
121+
matrix = matrix.RemoveRow(matrix.RowCount - 1);
122+
123+
matrix = matrix.RemoveColumn(matrix.ColumnCount - 1);
124+
matrix = matrix.RemoveColumn(matrix.ColumnCount - 1);
125+
matrix = matrix.RemoveColumn(matrix.ColumnCount - 1);
126+
127+
Assert.AreEqual("DenseMatrix 0x0-Double\r\n[empty]", matrix.ToString());
128+
}
129+
98130
/// <summary>
99131
/// Matrix from two-dimensional array is a copy.
100132
/// </summary>

src/Numerics/LinearAlgebra/Matrix.BCL.cs

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,10 @@ Tuple<int, string[]> FormatColumn(int column, int height, int upper, int lower,
279279
{
280280
c[index++] = formatValue(At(row, column));
281281
}
282-
int w = c.Max(x => x.Length);
282+
283+
int w = height != 0
284+
? c.Max(x => x.Length)
285+
: 0;
283286
if (withEllipsis)
284287
{
285288
c[upper] = ellipsis;
@@ -289,6 +292,7 @@ Tuple<int, string[]> FormatColumn(int column, int height, int upper, int lower,
289292

290293
static string FormatStringArrayToString(string[,] array, string columnSeparator, string rowSeparator)
291294
{
295+
const string emptyString = "[empty]";
292296
var rows = array.GetLength(0);
293297
var cols = array.GetLength(1);
294298

@@ -302,35 +306,89 @@ static string FormatStringArrayToString(string[,] array, string columnSeparator,
302306
}
303307

304308
var sb = new StringBuilder();
305-
for (int i = 0; i < rows; i++)
309+
if (rows > 0)
306310
{
307-
sb.Append(array[i, 0].PadLeft(widths[0]));
308-
for (int j = 1; j < cols; j++)
311+
for (int i = 0; i < rows; i++)
309312
{
310-
sb.Append(columnSeparator);
311-
sb.Append(array[i, j].PadLeft(widths[j]));
313+
if (cols > 0)
314+
{
315+
sb.Append(array[i, 0].PadLeft(widths[0]));
316+
for (int j = 1; j < cols; j++)
317+
{
318+
sb.Append(columnSeparator);
319+
sb.Append(array[i, j].PadLeft(widths[j]));
320+
}
321+
}
322+
else
323+
{
324+
sb.Append(emptyString);
325+
}
326+
327+
sb.Append(rowSeparator);
312328
}
313-
sb.Append(rowSeparator);
314329
}
330+
else
331+
{
332+
if (cols > 0)
333+
{
334+
for (int j = 0; j < cols; j++)
335+
{
336+
sb.Append(emptyString);
337+
if (j != cols - 1)
338+
{
339+
sb.Append(columnSeparator);
340+
}
341+
}
342+
}
343+
else
344+
{
345+
sb.Append(emptyString);
346+
}
347+
}
348+
315349
return sb.ToString();
316350
}
317351

318352
public string ToMatrixString(int upperRows, int lowerRows, int leftColumns, int rightColumns,
319353
string horizontalEllipsis, string verticalEllipsis, string diagonalEllipsis,
320354
string columnSeparator, string rowSeparator, Func<T, string> formatValue)
321355
{
356+
var array = ToMatrixStringArray(
357+
upperRows,
358+
lowerRows,
359+
leftColumns,
360+
rightColumns,
361+
horizontalEllipsis,
362+
verticalEllipsis,
363+
diagonalEllipsis,
364+
formatValue);
365+
322366
return FormatStringArrayToString(
323-
ToMatrixStringArray(upperRows, lowerRows, leftColumns, rightColumns, horizontalEllipsis, verticalEllipsis, diagonalEllipsis, formatValue),
324-
columnSeparator, rowSeparator);
367+
array,
368+
columnSeparator,
369+
rowSeparator);
325370
}
326371

327372
public string ToMatrixString(int upperRows, int lowerRows, int minLeftColumns, int rightColumns, int maxWidth,
328373
string horizontalEllipsis, string verticalEllipsis, string diagonalEllipsis,
329374
string columnSeparator, string rowSeparator, Func<T, string> formatValue)
330375
{
376+
var array = ToMatrixStringArray(
377+
upperRows,
378+
lowerRows,
379+
minLeftColumns,
380+
rightColumns,
381+
maxWidth,
382+
columnSeparator.Length,
383+
horizontalEllipsis,
384+
verticalEllipsis,
385+
diagonalEllipsis,
386+
formatValue);
387+
331388
return FormatStringArrayToString(
332-
ToMatrixStringArray(upperRows, lowerRows, minLeftColumns, rightColumns, maxWidth, columnSeparator.Length, horizontalEllipsis, verticalEllipsis, diagonalEllipsis, formatValue),
333-
columnSeparator, rowSeparator);
389+
array,
390+
columnSeparator,
391+
rowSeparator);
334392
}
335393

336394
/// <summary>

0 commit comments

Comments
 (0)