Click or drag to resize

CsvDoubleMatrixSerializerSerialize Method (TextWriter, ReadOnlyDoubleMatrix)

Serializes the specified ReadOnlyDoubleMatrix writing a CSV document to a file using the specified TextWriter.

Namespace:  Novacta.Analytics
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public static void Serialize(
	TextWriter writer,
	ReadOnlyDoubleMatrix matrix
)

Parameters

writer
Type: System.IOTextWriter
The TextWriter used to write the CSV document.
matrix
Type: Novacta.AnalyticsReadOnlyDoubleMatrix
The ReadOnlyDoubleMatrix being serialized.
Exceptions
ExceptionCondition
ArgumentNullExceptionwriter is null.
-or-
matrix is null.
Remarks

This method writes a CSV document containing the information required to represent the state of matrix.

Examples

In the following example, a matrix instance is serialized by writing a CSV document to a stream. Hence the matrix is deserialized by reading such stream.

Serialization and deserialization of a matrix using a CSV document
using System;
using System.IO;

namespace Novacta.Analytics.CodeExamples
{
    public class CsvSerializeExample1  
    {
        public void Main()
        {
            // Create a matrix.
            var matrix = DoubleMatrix.Sparse(3, 2, capacity: 2);
            matrix[0, 1] = Double.NaN;
            matrix[2, 0] = -2.2;

            // Add a name to the matrix.
            matrix.Name = "My sparse matrix";

            // Add names to rows and columns.
            matrix.SetRowName(0, "row0");
            matrix.SetRowName(1, "row1");
            matrix.SetRowName(2, "row2");
            matrix.SetColumnName(0, "column0");
            matrix.SetColumnName(1, "column1");

            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);
            Console.WriteLine();

            // Create a stream to serialize the matrix.
            MemoryStream stream = new();
            StreamWriter writer = new(stream);

            // Serialize the matrix.
            CsvDoubleMatrixSerializer.Serialize(writer, matrix);

            // Read the CSV representation of the matrix from the stream.
            stream.Position = 0;
            StreamReader reader = new(stream);
            string line;
            Console.WriteLine("CSV representation of the matrix:");
            while ((line = reader.ReadLine()) != null) {
                Console.WriteLine(line);
            }
            Console.WriteLine();

            // Deserialize the matrix from the stream.
            stream.Position = 0;
            var deserializedMatrix = 
                CsvDoubleMatrixSerializer.Deserialize(reader);

            // Show the deserialized matrix.
            Console.WriteLine("Deserialized matrix:");
            Console.WriteLine(deserializedMatrix);

            // Serialize is overloaded to accept data as a read-only matrix:
            // serialize a read-only wrapper of the matrix.
            stream.Position = 0;
            CsvDoubleMatrixSerializer.Serialize(writer, matrix.AsReadOnly());

            // Deserialize again the matrix.
            stream.Position = 0;
            var deserializedReadOnlyMatrix = 
                CsvDoubleMatrixSerializer.Deserialize(reader);

            // Show the deserialized matrix.
            Console.WriteLine("Deserialized matrix after serialization of a read-only matrix:");
            Console.WriteLine(deserializedReadOnlyMatrix);
        }
    }
}

// Executing method Main() produces the following output:
// 
// The data matrix:
//                  [column0]        [column1]        
// [row0]           0                NaN              
// [row1]           0                0                
// [row2]           -2.2             0                
// 
// 
// 
// CSV representation of the matrix:
// Sparse|Double,3,2,My sparse matrix,2
// 0,1,NaN
// 2,0,-2.2
// 0,column0,1,column1
// 0,row0,1,row1,2,row2
// 
// Deserialized matrix:
//                  [column0]        [column1]        
// [row0]           0                NaN              
// [row1]           0                0                
// [row2]           -2.2             0                
// 
// 
// Deserialized matrix after serialization of a read-only matrix:
//                  [column0]        [column1]        
// [row0]           0                NaN              
// [row1]           0                0                
// [row2]           -2.2             0                
// 

See Also