Skip to content

Commit eb89b3d

Browse files
authored
Merge pull request #999 from jkalias/matrix-0-by-1
Bug in string description for mx0 and 0xn matrices
2 parents bc0bd6e + f05f20c commit eb89b3d

File tree

3 files changed

+123
-12
lines changed

3 files changed

+123
-12
lines changed

RELEASENOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Random: xoshiro256StarStar fix out of range exception *~Charlie Turndorf*
1212
* Precision: Perf: pre-compute negative powers *~Febin*
1313
* Optimizations: Remove static properties in LevenbergMarquardtMinimizer *~Jong Hyun Kim*
14-
* Root Finding: Newton-Raphson better handling of zero-evaluations
14+
* Root Finding: Newton-Raphson better handling of zero-evaluations *~jkalias*
1515
* Fit.Curve and FindMinimum extended to accept two more parameters
1616
* Fixed an index out of bounds issue when calculating BFGS minimizer with one variable *~Shiney*
1717
* Fixed Sparse COO NormalizeDuplicates *~Mohamed Moussa*

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

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

98+
[Test]
99+
public void DescriptionForMatrixWithRowsButNoColumns()
100+
{
101+
var matrix = GetMatrixWithRowsButNoColumns();
102+
Assert.AreEqual("DenseMatrix 3x0-Double\r\n[empty]\r\n[empty]\r\n[empty]\r\n", matrix.ToString());
103+
}
104+
105+
[Test]
106+
public void DescriptionForMatrixWithColumnsButNoRows()
107+
{
108+
var matrix = GetMatrixWithColumnsButNoRows();
109+
Assert.AreEqual("DenseMatrix 0x3-Double\r\n[empty] [empty] [empty]", matrix.ToString());
110+
}
111+
112+
[Test]
113+
public void DescriptionForMatrixWithNoRowsAndNoColumns()
114+
{
115+
var matrix = TestMatrices["Wide2x3"];
116+
matrix = matrix.RemoveRow(matrix.RowCount - 1);
117+
matrix = matrix.RemoveRow(matrix.RowCount - 1);
118+
119+
matrix = matrix.RemoveColumn(matrix.ColumnCount - 1);
120+
matrix = matrix.RemoveColumn(matrix.ColumnCount - 1);
121+
matrix = matrix.RemoveColumn(matrix.ColumnCount - 1);
122+
123+
Assert.AreEqual("DenseMatrix 0x0-Double\r\n[empty]", matrix.ToString());
124+
}
125+
126+
[Test]
127+
public void MatrixMultiplicationWhenMatricesHaveNoRowsAndColumns()
128+
{
129+
var a = GetMatrixWithRowsButNoColumns();
130+
var b = GetMatrixWithColumnsButNoRows();
131+
var result = a * b;
132+
Assert.AreEqual(new DenseMatrix(3), result);
133+
}
134+
98135
/// <summary>
99136
/// Matrix from two-dimensional array is a copy.
100137
/// </summary>
@@ -209,5 +246,21 @@ public void MatrixToMatrixString()
209246
}
210247
}
211248
}
249+
250+
private Matrix<double> GetMatrixWithRowsButNoColumns()
251+
{
252+
var matrix = TestMatrices["Tall3x2"];
253+
matrix = matrix.RemoveColumn(matrix.ColumnCount - 1);
254+
matrix = matrix.RemoveColumn(matrix.ColumnCount - 1);
255+
return matrix;
256+
}
257+
258+
private Matrix<double> GetMatrixWithColumnsButNoRows()
259+
{
260+
var matrix = TestMatrices["Wide2x3"];
261+
matrix = matrix.RemoveRow(matrix.RowCount - 1);
262+
matrix = matrix.RemoveRow(matrix.RowCount - 1);
263+
return matrix;
264+
}
212265
}
213266
}

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)