Click or drag to resize

CsvComplexMatrixSerializerSerialize Method (TextWriter, ReadOnlyComplexMatrix)

Serializes the specified ReadOnlyComplexMatrix 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,
	ReadOnlyComplexMatrix matrix
)

Parameters

writer
Type: System.IOTextWriter
The TextWriter used to write the CSV document.
matrix
Type: Novacta.AnalyticsReadOnlyComplexMatrix
The ReadOnlyComplexMatrix 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, the content of a well formatted CSV document is deserialized to create a matrix instance.

Deserializing a CSV document to create a matrix
using System;
using System.IO;

namespace Novacta.Analytics.CodeExamples
{
    public class CsvSerializeExample0  
    {
        public void Main()
        {
            // Create the CSV representation of a dense matrix.
            string[] data = new string[5] {
                "Dense|Complex,3,2,MatrixName",
                ",column0,column1",
                "row0,(1.0 -2.0),(NaN NaN)",
                "row1,(2.0 -3.0),(4.0 -1.0)",
                "row2,(0 0),(5.0 7.0)"
            };

            // Show the matrix CSV representation.
            Console.WriteLine("CSV representation of the matrix:");
            for (int i = 0; i < data.Length; i++) {
                Console.WriteLine(data[i]);
            }
            Console.WriteLine();

            // Create a stream containing the CSV content.
            MemoryStream stream = new();
            StreamWriter writer = new(stream);
            for (int i = 0; i < data.Length; i++) {
                writer.WriteLine(data[i].ToCharArray());
                writer.Flush();
            }
            stream.Position = 0;

            // Create a reader for the stream.
            StreamReader reader = new(stream);

            // Deserialize the CSV document contained in the stream.
            ComplexMatrix matrix = 
                CsvComplexMatrixSerializer.Deserialize(reader);

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

// Executing method Main() produces the following output:
// 
// CSV representation of the matrix:
// Dense|Complex,3,2,MatrixName
// ,column0,column1
// row0,(1.0 -2.0),(NaN NaN)
// row1,(2.0 -3.0),(4.0 -1.0)
// row2,(0 0),(5.0 7.0)
// 
// Deserialized matrix:
//                  [column0]                             [column1]                             
// [row0]           (                1,               -2) (              NaN,              NaN) 
// [row1]           (                2,               -3) (                4,               -1) 
// [row2]           (                0,                0) (                5,                7) 
// 

See Also