Skip to content

Commit d130167

Browse files
committed
More tests.
1 parent a0b985f commit d130167

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

CSparse.Tests/Complex/SparseMatrixTest.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ public void TestFrobeniusNorm()
203203
Assert.AreEqual(expected, actual);
204204
}
205205

206+
[TestCase(2, 2)]
207+
[TestCase(2, 3)]
208+
public void TestEnumerateIndexed(int rows, int columns)
209+
{
210+
var data = MatrixHelper.LoadSparse(rows, columns);
211+
212+
var A = data.A;
213+
214+
double sum = 0;
215+
216+
A.EnumerateIndexed((i, j, a) => sum += a.Real * a.Real + a.Imaginary * a.Imaginary);
217+
218+
Assert.AreEqual(A.FrobeniusNorm(), Math.Sqrt(sum));
219+
}
220+
206221
[TestCase(2, 2)]
207222
[TestCase(2, 3)]
208223
public void TestGetRow(int rows, int columns)
@@ -470,6 +485,33 @@ public void TestOfIndexed(int rows, int columns)
470485
Assert.IsTrue(sparseA.Equals(sparseB));
471486
}
472487

488+
[TestCase(2, 2)]
489+
[TestCase(2, 3)]
490+
public void TestOfIndexed_Coo(int rows, int columns)
491+
{
492+
var sparseData = MatrixHelper.LoadSparse(rows, columns);
493+
494+
var sparseA = sparseData.A;
495+
496+
var coo = new CoordinateStorage<Complex>(rows, columns, sparseA.NonZerosCount);
497+
498+
foreach (var a in sparseA.EnumerateIndexed())
499+
{
500+
coo.At(a.Item1, a.Item2, a.Item3);
501+
}
502+
503+
var sparseB = SparseMatrix.OfIndexed(coo);
504+
505+
Assert.IsTrue(sparseA.Equals(sparseB));
506+
507+
var sparseC = SparseMatrix.OfIndexed(coo, true);
508+
509+
Assert.IsTrue(sparseA.Equals(sparseC));
510+
Assert.IsNull(coo.Values);
511+
Assert.IsNull(coo.RowIndices);
512+
Assert.IsNull(coo.ColumnIndices);
513+
}
514+
473515
[TestCase(2, 2)]
474516
[TestCase(2, 3)]
475517
public void TestOfColumnMajor(int rows, int columns)
@@ -485,6 +527,53 @@ public void TestOfColumnMajor(int rows, int columns)
485527
Assert.IsTrue(sparseA.Equals(sparseB));
486528
}
487529

530+
[TestCase(2, 2)]
531+
[TestCase(2, 3)]
532+
public void TestOfArray(int rows, int columns)
533+
{
534+
var sparseData = MatrixHelper.LoadSparse(rows, columns);
535+
536+
var sparseA = sparseData.A;
537+
538+
var array = new Complex[rows, columns];
539+
540+
foreach (var a in sparseA.EnumerateIndexed())
541+
{
542+
array[a.Item1, a.Item2] = a.Item3;
543+
}
544+
545+
var sparseB = SparseMatrix.OfArray(array);
546+
547+
Assert.IsTrue(sparseA.Equals(sparseB));
548+
}
549+
550+
[TestCase(2, 2)]
551+
[TestCase(2, 3)]
552+
public void TestOfJaggedArray(int rows, int columns)
553+
{
554+
var sparseData = MatrixHelper.LoadSparse(rows, columns);
555+
556+
var sparseA = sparseData.A;
557+
558+
var array = new Complex[rows][];
559+
560+
for (int i = 0; i < rows; i++)
561+
{
562+
var values = new Complex[columns];
563+
564+
for (int j = 0; j < columns; j++)
565+
{
566+
values[j] = sparseA.At(i, j);
567+
}
568+
569+
array[i] = values;
570+
}
571+
572+
var sparseB = SparseMatrix.OfJaggedArray(array);
573+
574+
Assert.IsTrue(sparseA.Equals(sparseB));
575+
}
576+
488577
[Test]
489578
public void TestOfDiagonalArray()
490579
{

0 commit comments

Comments
 (0)