/// <summary> /// Initializes a new instance of the <see cref="TestableDoubleMatrixDoubleMatrixMultiplication{TExpected}"/> class. /// </summary> /// <param name="expected">The expected result or exception.</param> /// <param name="left">The left operand.</param> /// <param name="right">The right operand.</param> public TestableDoubleMatrixDoubleMatrixMultiplication( TExpected expected, TestableDoubleMatrix left, TestableDoubleMatrix right) : base( expected, left, right, leftWritableRightWritableOps: new Func <DoubleMatrix, DoubleMatrix, DoubleMatrix>[2] { (l, r) => l * r, (l, r) => DoubleMatrix.Multiply(l, r) }, leftReadOnlyRightWritableOps: new Func <ReadOnlyDoubleMatrix, DoubleMatrix, DoubleMatrix>[2] { (l, r) => l * r, (l, r) => ReadOnlyDoubleMatrix.Multiply(l, r) }, leftWritableRightReadOnlyOps: new Func <DoubleMatrix, ReadOnlyDoubleMatrix, DoubleMatrix>[2] { (l, r) => l * r, (l, r) => ReadOnlyDoubleMatrix.Multiply(l, r) }, leftReadOnlyRightReadOnlyOps: new Func <ReadOnlyDoubleMatrix, ReadOnlyDoubleMatrix, DoubleMatrix>[2] { (l, r) => l * r, (l, r) => ReadOnlyDoubleMatrix.Multiply(l, r) } ) { }
/// <summary> /// Initializes a new instance of the <see cref="TestableDoubleMatrixDoubleMatrixDivision{TExpected}"/> class. /// </summary> /// <param name="expected">The expected result or exception.</param> /// <param name="left">The left operand.</param> /// <param name="right">The right operand.</param> public TestableDoubleMatrixDoubleMatrixDivision( TExpected expected, TestableDoubleMatrix left, TestableDoubleMatrix right) : base( expected, left, right, leftWritableRightWritableOps: new Func <DoubleMatrix, DoubleMatrix, DoubleMatrix>[2] { (l, r) => l / r, (l, r) => DoubleMatrix.Divide(l, r) }, leftReadOnlyRightWritableOps: new Func <ReadOnlyDoubleMatrix, DoubleMatrix, DoubleMatrix>[2] { (l, r) => l / r, (l, r) => ReadOnlyDoubleMatrix.Divide(l, r) }, leftWritableRightReadOnlyOps: new Func <DoubleMatrix, ReadOnlyDoubleMatrix, DoubleMatrix>[2] { (l, r) => l / r, (l, r) => ReadOnlyDoubleMatrix.Divide(l, r) }, leftReadOnlyRightReadOnlyOps: new Func <ReadOnlyDoubleMatrix, ReadOnlyDoubleMatrix, DoubleMatrix>[2] { (l, r) => l / r, (l, r) => ReadOnlyDoubleMatrix.Divide(l, r) } ) { }
#pragma warning disable CS0618 // Type or member is obsolete private static void TestSerializationToPath( TestableDoubleMatrix testableMatrix, string partialPath) { // dense { var expected = testableMatrix.AsDense; var path = "dense-" + partialPath; CsvMatrixSerializer.Serialize(path, expected); var actual = CsvMatrixSerializer.Deserialize(path); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // sparse { var expected = testableMatrix.AsSparse; var path = "sparse-" + partialPath; CsvMatrixSerializer.Serialize(path, expected); var actual = CsvMatrixSerializer.Deserialize(path); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // read-only dense { var expected = testableMatrix.AsDense; var path = "read-only-dense-" + partialPath; CsvMatrixSerializer.Serialize(path, expected.AsReadOnly()); var actual = CsvMatrixSerializer.Deserialize(path); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // read-only sparse { var expected = testableMatrix.AsSparse; var path = "read-only-sparse-" + partialPath; CsvMatrixSerializer.Serialize(path, expected.AsReadOnly()); var actual = CsvMatrixSerializer.Deserialize(path); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } }
/// <summary> /// Initializes a new instance of /// the <see cref="SumOfSquaredDeviationsOnAll" /> class. /// </summary> /// <param name="expected">The expected result.</param> /// <param name="data">The data operand.</param> protected OnAllSumOfSquaredDeviations( TExpected expected, TestableDoubleMatrix data) : base( expected: expected, data: data, dataWritableOps: new Func <DoubleMatrix, double>[1] { Stat.SumOfSquaredDeviations }, dataReadOnlyOps: new Func <ReadOnlyDoubleMatrix, double>[1] { Stat.SumOfSquaredDeviations }) { }
/// <summary> /// Initializes a new instance of /// the <see cref="MeanOnAll" /> class. /// </summary> /// <param name="expected">The expected result.</param> /// <param name="data">The data operand.</param> protected OnAllMean( TExpected expected, TestableDoubleMatrix data) : base( expected: expected, data: data, dataWritableOps: new Func <DoubleMatrix, double>[1] { Stat.Mean }, dataReadOnlyOps: new Func <ReadOnlyDoubleMatrix, double>[1] { Stat.Mean }) { }
/// <summary> /// Initializes a new instance of /// the <see cref="AlongDimensionSumOfSquaredDeviations" /> class. /// </summary> /// <param name="expected">The expected result.</param> /// <param name="data">The data operand.</param> protected AlongDimensionSumOfSquaredDeviations( TExpected expected, TestableDoubleMatrix data, DataOperation dataOperation) : base( expected: expected, data: data, dataOperation: dataOperation, dataWritableOps: new Func <DoubleMatrix, DataOperation, DoubleMatrix>[1] { Stat.SumOfSquaredDeviations }, dataReadOnlyOps: new Func <ReadOnlyDoubleMatrix, DataOperation, DoubleMatrix>[1] { Stat.SumOfSquaredDeviations }) { }
/// <summary> /// Initializes a new instance of /// the <see cref="AlongDimensionMax" /> class. /// </summary> /// <param name="expected">The expected result.</param> /// <param name="data">The data operand.</param> protected AlongDimensionMax( TExpected expected, TestableDoubleMatrix data, DataOperation dataOperation) : base( expected: expected, data: data, dataOperation: dataOperation, dataWritableOps: new Func <DoubleMatrix, DataOperation, IndexValuePair[]>[1] { Stat.Max }, dataReadOnlyOps: new Func <ReadOnlyDoubleMatrix, DataOperation, IndexValuePair[]>[1] { Stat.Max }) { }
/// <summary> /// Initializes a new instance of the <see cref="TestableDoubleMatrixNegation{TExpected}"/> class. /// </summary> /// <param name="expected">The expected result or exception.</param> /// <param name="operand">The operand.</param> public TestableDoubleMatrixNegation( TExpected expected, TestableDoubleMatrix operand) : base( expected, operand, operandWritableOps: new Func <DoubleMatrix, DoubleMatrix>[2] { (o) => - o, (o) => DoubleMatrix.Negate(o) }, operandReadOnlyOps: new Func <ReadOnlyDoubleMatrix, DoubleMatrix>[2] { (o) => - o, (o) => ReadOnlyDoubleMatrix.Negate(o) } ) { }
/// <summary> /// Initializes a new instance of /// the <see cref="AlongDimensionVariance" /> class. /// </summary> /// <param name="expected">The expected result.</param> /// <param name="data">The data operand.</param> protected AlongDimensionVariance( TExpected expected, TestableDoubleMatrix data, bool adjustForBias, DataOperation dataOperation) : base( expected: expected, data: data, adjustForBias: adjustForBias, dataOperation: dataOperation, dataWritableOps: new Func <DoubleMatrix, bool, DataOperation, DoubleMatrix>[1] { Stat.Variance }, dataReadOnlyOps: new Func <ReadOnlyDoubleMatrix, bool, DataOperation, DoubleMatrix>[1] { Stat.Variance }) { }
/// <summary> /// Initializes a new instance of /// the <see cref="AlongDimensionQuantile" /> class. /// </summary> /// <param name="expected">The expected result.</param> /// <param name="data">The data operand.</param> /// <param name="probabilities">The probabilities.</param> /// <param name="dataOperation">The data operation.</param> protected AlongDimensionQuantile( TExpected expected, TestableDoubleMatrix data, DoubleMatrix probabilities, DataOperation dataOperation) : base( expected: expected, data: data, probabilities: probabilities, dataOperation: dataOperation, dataWritableOps: new Func <DoubleMatrix, DoubleMatrix, DataOperation, DoubleMatrix[]>[1] { Stat.Quantile }, dataReadOnlyOps: new Func <ReadOnlyDoubleMatrix, DoubleMatrix, DataOperation, DoubleMatrix[]>[1] { Stat.Quantile }) { }
public TestableComplexScalarDoubleMatrixDivision( TExpected expected, Complex left, TestableDoubleMatrix right) : base( expected, left, right, leftScalarRightWritableOps: new Func <Complex, DoubleMatrix, ComplexMatrix>[2] { (l, r) => l / r, (l, r) => DoubleMatrix.Divide(l, r) }, leftScalarRightReadOnlyOps: new Func <Complex, ReadOnlyDoubleMatrix, ComplexMatrix>[2] { (l, r) => l / r, (l, r) => ReadOnlyDoubleMatrix.Divide(l, r) } ) { }
public TestableDoubleMatrixComplexScalarMultiplication( TExpected expected, TestableDoubleMatrix left, Complex right) : base( expected, left, right, leftWritableRightScalarOps: new Func <DoubleMatrix, Complex, ComplexMatrix>[2] { (l, r) => l * r, (l, r) => DoubleMatrix.Multiply(l, r) }, leftReadOnlyRightScalarOps: new Func <ReadOnlyDoubleMatrix, Complex, ComplexMatrix>[2] { (l, r) => l * r, (l, r) => ReadOnlyDoubleMatrix.Multiply(l, r) } ) { }
/// <summary> /// Gets a matrix testable for CSV serialization. /// </summary> private TestableDoubleMatrix GetTestableMatrix( bool hasName, bool hasRowNames, bool hasColumnNames, out string partialPath) { StringBuilder stringBuilder = new(); string matrixName = null; if (hasName) { matrixName = "MatrixName"; stringBuilder.Append($"{EntryType}-named-"); } else { stringBuilder.Append($"{EntryType}-unnamed-"); } Dictionary <int, string> rowNames = null; if (hasRowNames) { rowNames = new Dictionary <int, string>() { { 0, "Row0" }, { 1, "Row1" }, { 2, "Row2" }, { 3, "Row3" }, }; stringBuilder.Append("with-row-names-"); } else { stringBuilder.Append("without-row-names-"); } Dictionary <int, string> columnNames = null; if (hasColumnNames) { columnNames = new Dictionary <int, string>() { { 0, "Column0" }, { 1, "Column1" }, { 2, "Column2" }, { 3, "Column3" }, { 4, "Column4" }, }; stringBuilder.Append("with-col-names"); } else { stringBuilder.Append("without-col-names"); } stringBuilder.Append(".csv"); partialPath = stringBuilder.ToString(); var testableMatrix = new TestableDoubleMatrix( asColumnMajorDenseArray: new double[20] { 0, 0, -2, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, numberOfRows: 4, numberOfColumns: 5, isUpperHessenberg: false, isLowerHessenberg: false, isUpperTriangular: false, isLowerTriangular: false, isSymmetric: false, isSkewSymmetric: false, upperBandwidth: 1, lowerBandwidth: 2, name: matrixName, rowNames: rowNames, columnNames: columnNames); return(testableMatrix); }
private static void TestSerializationToStream( TestableDoubleMatrix testableMatrix) { // dense { var expected = testableMatrix.AsDense; MemoryStream stream = new(); var textWriter = new StreamWriter(stream); CsvDoubleMatrixSerializer.Serialize(textWriter, expected); stream.Position = 0; var textReader = new StreamReader(stream); var actual = CsvDoubleMatrixSerializer.Deserialize(textReader); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // sparse { var expected = testableMatrix.AsSparse; MemoryStream stream = new(); var textWriter = new StreamWriter(stream); CsvDoubleMatrixSerializer.Serialize(textWriter, expected); stream.Position = 0; var textReader = new StreamReader(stream); var actual = CsvDoubleMatrixSerializer.Deserialize(textReader); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // read-only dense { var expected = testableMatrix.AsDense; MemoryStream stream = new(); var textWriter = new StreamWriter(stream); CsvDoubleMatrixSerializer.Serialize(textWriter, expected.AsReadOnly()); stream.Position = 0; var textReader = new StreamReader(stream); var actual = CsvDoubleMatrixSerializer.Deserialize(textReader); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } // read-only sparse { var expected = testableMatrix.AsSparse; MemoryStream stream = new(); var textWriter = new StreamWriter(stream); CsvDoubleMatrixSerializer.Serialize(textWriter, expected.AsReadOnly()); stream.Position = 0; var textReader = new StreamReader(stream); var actual = CsvDoubleMatrixSerializer.Deserialize(textReader); DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy); } }