/// <summary>
        /// Write the Name values from the <c>MLArray</c> to the <c>BinaryWriter</c> stream.
        /// </summary>
        /// <param name="bw"><c>BinaryWriter</c> stream</param>
        /// <param name="array"><c>MLArray</c> matrix</param>
        private void WriteName(BinaryWriter bw, MLArray array)
        {
            //MemoryStream buffer = new MemoryStream();
            //BinaryWriter bufferBW = new BinaryWriter( buffer );

            byte[] nameByteArray = array.GetNameToByteArray();
            //bufferBW.Write( nameByteArray );

            OSArrayTag tag = new OSArrayTag(MatDataTypes.miINT8, nameByteArray /*buffer.ToArray()*/);

            tag.WriteTo(bw);
        }
        /// <summary>
        /// Write the Dimensions values from the <c>MLArray</c> to the <c>BinaryWriter</c> stream.
        /// </summary>
        /// <param name="bw"><c>BinaryWriter</c> stream</param>
        /// <param name="array"><c>MLArray</c> matrix</param>
        private void WriteDimensions(BinaryWriter bw, MLArray array)
        {
            MemoryStream buffer   = new MemoryStream();
            BinaryWriter bufferBW = new BinaryWriter(buffer);

            int[] dims = array.Dimensions;

            for (int i = 0; i < dims.Length; i++)
            {
                bufferBW.Write(dims[i]);
            }

            OSArrayTag tag = new OSArrayTag(MatDataTypes.miINT32, buffer.ToArray());

            tag.WriteTo(bw);
        }
        /// <summary>
        /// Write the flag values from the <c>MLArray</c> to the <c>BinaryWriter</c> stream.
        /// </summary>
        /// <param name="bw"><c>BinaryWriter</c> stream</param>
        /// <param name="array"><c>MLArray</c> matrix</param>
        private void WriteFlags(BinaryWriter bw, MLArray array)
        {
            MemoryStream buffer   = new MemoryStream();
            BinaryWriter bufferBW = new BinaryWriter(buffer);

            bufferBW.Write(array.Flags);

            if (array.IsSparse)
            {
                bufferBW.Write(((MLSparse)array).MaxNZ);
            }
            else
            {
                bufferBW.Write((int)0);
            }
            OSArrayTag tag = new OSArrayTag(MatDataTypes.miUINT32, buffer.ToArray());

            tag.WriteTo(bw);
        }
        /// <summary>
        /// Writes MATRIX into <c>BinaryWriter</c> stream
        /// </summary>
        /// <param name="output">The <c>BinaryWriter</c> stream</param>
        /// <param name="array">The <c>MLArray</c> matrix</param>
        private void WriteMatrix(BinaryWriter output, MLArray array)
        {
            OSArrayTag   tag;
            MemoryStream buffer;
            BinaryWriter bufferBW;
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);

            //flags
            WriteFlags(bw, array);

            //dimensions
            WriteDimensions(bw, array);

            // array  name
            WriteName(bw, array);

            switch (array.Type)
            {
            case MLArray.mxCHAR_CLASS:
                // write char data
                buffer   = new MemoryStream();
                bufferBW = new BinaryWriter(buffer);
                char[] ac = ((MLChar)array).ExportChar();
                for (int i = 0; i < ac.Length; i++)
                {
                    bufferBW.Write((ushort)ac[i]);
                }
                tag = new OSArrayTag(MatDataTypes.miUINT16, buffer.ToArray());
                tag.WriteTo(bw);

                break;

            case MLArray.mxDOUBLE_CLASS:
                tag = new OSArrayTag(MatDataTypes.miDOUBLE,
                                     ((MLNumericArray <double>)array).RealByteBuffer);
                tag.WriteTo(bw);
                if (array.IsComplex)
                {
                    tag = new OSArrayTag(MatDataTypes.miDOUBLE,
                                         ((MLNumericArray <double>)array).ImaginaryByteBuffer);
                    tag.WriteTo(bw);
                }
                break;

            case MLArray.mxSINGLE_CLASS:
                tag = new OSArrayTag(MatDataTypes.miDOUBLE,
                                     ((MLNumericArray <double>)array).RealByteBuffer);
                tag.WriteTo(bw);
                if (array.IsComplex)
                {
                    tag = new OSArrayTag(MatDataTypes.miDOUBLE,
                                         ((MLNumericArray <double>)array).ImaginaryByteBuffer);
                    tag.WriteTo(bw);
                }
                break;

            case MLArray.mxUINT8_CLASS:
                tag = new OSArrayTag(MatDataTypes.miUINT8,
                                     ((MLNumericArray <byte>)array).RealByteBuffer);
                tag.WriteTo(bw);
                if (array.IsComplex)
                {
                    tag = new OSArrayTag(MatDataTypes.miUINT8,
                                         ((MLNumericArray <byte>)array).ImaginaryByteBuffer);
                    tag.WriteTo(bw);
                }
                break;

            case MLArray.mxINT8_CLASS:
                tag = new OSArrayTag(MatDataTypes.miINT16,
                                     ((MLNumericArray <short>)array).RealByteBuffer);
                tag.WriteTo(bw);
                if (array.IsComplex)
                {
                    tag = new OSArrayTag(MatDataTypes.miINT16,
                                         ((MLNumericArray <short>)array).ImaginaryByteBuffer);
                    tag.WriteTo(bw);
                }
                break;

            case MLArray.mxUINT16_CLASS:
                tag = new OSArrayTag(MatDataTypes.miUINT16,
                                     ((MLNumericArray <ushort>)array).RealByteBuffer);
                tag.WriteTo(bw);
                if (array.IsComplex)
                {
                    tag = new OSArrayTag(MatDataTypes.miUINT16,
                                         ((MLNumericArray <ushort>)array).ImaginaryByteBuffer);
                    tag.WriteTo(bw);
                }
                break;

            case MLArray.mxINT16_CLASS:
                tag = new OSArrayTag(MatDataTypes.miINT16,
                                     ((MLNumericArray <short>)array).RealByteBuffer);
                tag.WriteTo(bw);
                if (array.IsComplex)
                {
                    tag = new OSArrayTag(MatDataTypes.miINT16,
                                         ((MLNumericArray <short>)array).ImaginaryByteBuffer);
                    tag.WriteTo(bw);
                }
                break;

            case MLArray.mxUINT32_CLASS:
                tag = new OSArrayTag(MatDataTypes.miUINT32,
                                     ((MLNumericArray <uint>)array).RealByteBuffer);
                tag.WriteTo(bw);
                if (array.IsComplex)
                {
                    tag = new OSArrayTag(MatDataTypes.miUINT32,
                                         ((MLNumericArray <uint>)array).ImaginaryByteBuffer);
                    tag.WriteTo(bw);
                }
                break;

            case MLArray.mxINT32_CLASS:
                tag = new OSArrayTag(MatDataTypes.miINT32,
                                     ((MLNumericArray <int>)array).RealByteBuffer);
                tag.WriteTo(bw);
                if (array.IsComplex)
                {
                    tag = new OSArrayTag(MatDataTypes.miINT32,
                                         ((MLNumericArray <int>)array).ImaginaryByteBuffer);
                    tag.WriteTo(bw);
                }
                break;

            case MLArray.mxUINT64_CLASS:
                tag = new OSArrayTag(MatDataTypes.miUINT64,
                                     ((MLNumericArray <ulong>)array).RealByteBuffer);
                tag.WriteTo(bw);
                if (array.IsComplex)
                {
                    tag = new OSArrayTag(MatDataTypes.miUINT64,
                                         ((MLNumericArray <ulong>)array).ImaginaryByteBuffer);
                    tag.WriteTo(bw);
                }
                break;

            case MLArray.mxINT64_CLASS:
                tag = new OSArrayTag(MatDataTypes.miINT64,
                                     ((MLNumericArray <long>)array).RealByteBuffer);
                tag.WriteTo(bw);
                if (array.IsComplex)
                {
                    tag = new OSArrayTag(MatDataTypes.miINT64,
                                         ((MLNumericArray <long>)array).ImaginaryByteBuffer);
                    tag.WriteTo(bw);
                }
                break;

            case MLArray.mxSTRUCT_CLASS:
                // field name length
                int itag = 4 << 16 | MatDataTypes.miINT32 & 0xffff;
                bw.Write(itag);
                bw.Write(((MLStructure)array).MaxFieldLength);

                // Get Field Names
                tag = new OSArrayTag(MatDataTypes.miINT8, ((MLStructure)array).GetKeySetToByteArray());
                tag.WriteTo(bw);

                foreach (MLArray a in ((MLStructure)array).AllFields)
                {
                    WriteMatrix(bw, a);
                }
                break;

            case MLArray.mxCELL_CLASS:
                foreach (MLArray a in ((MLCell)array).Cells)
                {
                    WriteMatrix(bw, a);
                }
                break;

            case MLArray.mxSPARSE_CLASS:
                int[] ai;

                //write ir
                buffer   = new MemoryStream();
                bufferBW = new BinaryWriter(buffer);
                ai       = ((MLSparse)array).IR;
                foreach (int i in ai)
                {
                    bufferBW.Write(i);
                }
                tag = new OSArrayTag(MatDataTypes.miINT32, buffer.ToArray());
                tag.WriteTo(bw);

                // write jc
                buffer   = new MemoryStream();
                bufferBW = new BinaryWriter(buffer);
                ai       = ((MLSparse)array).JC;
                foreach (int i in ai)
                {
                    bufferBW.Write(i);
                }
                tag = new OSArrayTag(MatDataTypes.miINT32, buffer.ToArray());
                tag.WriteTo(bw);

                //write real
                buffer   = new MemoryStream();
                bufferBW = new BinaryWriter(buffer);
                double[] ad = ((MLSparse)array).ExportReal();
                for (int i = 0; i < ad.Length; i++)
                {
                    bufferBW.Write(ad[i]);
                }
                tag = new OSArrayTag(MatDataTypes.miDOUBLE, buffer.ToArray());
                tag.WriteTo(bw);

                //write imaginary
                if (array.IsComplex)
                {
                    buffer   = new MemoryStream();
                    bufferBW = new BinaryWriter(buffer);
                    ad       = ((MLSparse)array).ExportImaginary();
                    for (int i = 0; i < ad.Length; i++)
                    {
                        bufferBW.Write(ad[i]);
                    }
                    tag = new OSArrayTag(MatDataTypes.miDOUBLE, buffer.ToArray());
                    tag.WriteTo(bw);
                }
                break;

            default:
                throw new MatlabIOException("Cannot write matrix of type: " + MLArray.TypeToString(array.Type));
            }

            // Write matrix
            output.Write(MatDataTypes.miMATRIX);       //matrix tag
            output.Write((int)ms.Length);              // size of the matrix
            output.Write(ms.ToArray());                // matrix data
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Write the Name values from the <c>MLArray</c> to the <c>BinaryWriter</c> stream.
        /// </summary>
        /// <param name="bw"><c>BinaryWriter</c> stream</param>
        /// <param name="array"><c>MLArray</c> matrix</param>
        private void WriteName( BinaryWriter bw, MLArray array )
        {
            //MemoryStream buffer = new MemoryStream();
            //BinaryWriter bufferBW = new BinaryWriter( buffer );

            byte[] nameByteArray = array.GetNameToByteArray();
            //bufferBW.Write( nameByteArray );

            OSArrayTag tag = new OSArrayTag( MatDataTypes.miINT8, nameByteArray/*buffer.ToArray()*/ );
            tag.WriteTo( bw );
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Writes MATRIX into <c>BinaryWriter</c> stream
        /// </summary>
        /// <param name="output">The <c>BinaryWriter</c> stream</param>
        /// <param name="array">The <c>MLArray</c> matrix</param>
        private void WriteMatrix( BinaryWriter output, MLArray array )
        {
            OSArrayTag tag;
            MemoryStream buffer;
            BinaryWriter bufferBW;
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter( ms );

            //flags
            WriteFlags( bw, array );

            //dimensions
            WriteDimensions( bw, array );

            // array  name
            WriteName( bw, array );

            switch( array.Type )
            {
                case MLArray.mxCHAR_CLASS:
                    // write char data
                    buffer = new MemoryStream();
                    bufferBW = new BinaryWriter( buffer );
                    char[] ac = ((MLChar)array).ExportChar();
                    for( int i = 0; i < ac.Length; i++ )
                    {
                        bufferBW.Write( (ushort)ac[i] );
                    }
                    tag = new OSArrayTag( MatDataTypes.miUINT16, buffer.ToArray() );
                    tag.WriteTo( bw );

                    break;
                case MLArray.mxDOUBLE_CLASS:
                    tag = new OSArrayTag( MatDataTypes.miDOUBLE,
                        ((MLNumericArray<double>)array).RealByteBuffer );
                    tag.WriteTo( bw );
                    if( array.IsComplex )
                    {
                        tag = new OSArrayTag( MatDataTypes.miDOUBLE,
                            ((MLNumericArray<double>)array).ImaginaryByteBuffer );
                        tag.WriteTo( bw );
                    }
                    break;
                case MLArray.mxSINGLE_CLASS:
                    tag = new OSArrayTag(MatDataTypes.miDOUBLE,
                        ((MLNumericArray<double>)array).RealByteBuffer);
                    tag.WriteTo(bw);
                    if (array.IsComplex)
                    {
                        tag = new OSArrayTag(MatDataTypes.miDOUBLE,
                            ((MLNumericArray<double>)array).ImaginaryByteBuffer);
                        tag.WriteTo(bw);
                    }
                    break;
                case MLArray.mxUINT8_CLASS:
                    tag = new OSArrayTag( MatDataTypes.miUINT8,
                        ((MLNumericArray<byte>)array).RealByteBuffer );
                    tag.WriteTo( bw );
                    if( array.IsComplex )
                    {
                        tag = new OSArrayTag( MatDataTypes.miUINT8,
                            ((MLNumericArray<byte>)array).ImaginaryByteBuffer );
                        tag.WriteTo( bw );
                    }
                    break;
                case MLArray.mxINT8_CLASS:
                    tag = new OSArrayTag( MatDataTypes.miINT16,
                        ((MLNumericArray<short>)array).RealByteBuffer );
                    tag.WriteTo( bw );
                    if( array.IsComplex )
                    {
                        tag = new OSArrayTag(MatDataTypes.miINT16,
                            ((MLNumericArray<short>)array).ImaginaryByteBuffer);
                        tag.WriteTo( bw );
                    }
                    break;
                case MLArray.mxUINT16_CLASS:
                    tag = new OSArrayTag(MatDataTypes.miUINT16,
                        ((MLNumericArray<ushort>)array).RealByteBuffer);
                    tag.WriteTo(bw);
                    if (array.IsComplex)
                    {
                        tag = new OSArrayTag(MatDataTypes.miUINT16,
                            ((MLNumericArray<ushort>)array).ImaginaryByteBuffer);
                        tag.WriteTo(bw);
                    }
                    break;
                case MLArray.mxINT16_CLASS:
                    tag = new OSArrayTag(MatDataTypes.miINT16,
                        ((MLNumericArray<short>)array).RealByteBuffer);
                    tag.WriteTo(bw);
                    if (array.IsComplex)
                    {
                        tag = new OSArrayTag(MatDataTypes.miINT16,
                            ((MLNumericArray<short>)array).ImaginaryByteBuffer);
                        tag.WriteTo(bw);
                    }
                    break;
                case MLArray.mxUINT32_CLASS:
                    tag = new OSArrayTag(MatDataTypes.miUINT32,
                        ((MLNumericArray<uint>)array).RealByteBuffer);
                    tag.WriteTo(bw);
                    if (array.IsComplex)
                    {
                        tag = new OSArrayTag(MatDataTypes.miUINT32,
                            ((MLNumericArray<uint>)array).ImaginaryByteBuffer);
                        tag.WriteTo(bw);
                    }
                    break;
                case MLArray.mxINT32_CLASS:
                    tag = new OSArrayTag(MatDataTypes.miINT32,
                        ((MLNumericArray<int>)array).RealByteBuffer);
                    tag.WriteTo(bw);
                    if (array.IsComplex)
                    {
                        tag = new OSArrayTag(MatDataTypes.miINT32,
                            ((MLNumericArray<int>)array).ImaginaryByteBuffer);
                        tag.WriteTo(bw);
                    }
                    break;
                case MLArray.mxUINT64_CLASS:
                    tag = new OSArrayTag(MatDataTypes.miUINT64,
                        ((MLNumericArray<ulong>)array).RealByteBuffer);
                    tag.WriteTo(bw);
                    if (array.IsComplex)
                    {
                        tag = new OSArrayTag(MatDataTypes.miUINT64,
                            ((MLNumericArray<ulong>)array).ImaginaryByteBuffer);
                        tag.WriteTo(bw);
                    }
                    break;
                case MLArray.mxINT64_CLASS:
                    tag = new OSArrayTag( MatDataTypes.miINT64,
                        ((MLNumericArray<long>)array).RealByteBuffer );
                    tag.WriteTo( bw );
                    if( array.IsComplex )
                    {
                        tag = new OSArrayTag( MatDataTypes.miINT64,
                            ((MLNumericArray<long>)array).ImaginaryByteBuffer );
                        tag.WriteTo( bw );
                    }
                    break;
                case MLArray.mxSTRUCT_CLASS:
                    // field name length
                    int itag = 4 << 16 | MatDataTypes.miINT32 & 0xffff;
                    bw.Write( itag );
                    bw.Write( ((MLStructure)array).MaxFieldLength );

                    // Get Field Names
                    tag = new OSArrayTag( MatDataTypes.miINT8, ((MLStructure)array).GetKeySetToByteArray() );
                    tag.WriteTo( bw );

                    foreach( MLArray a in ((MLStructure)array).AllFields )
                    {
                        WriteMatrix( bw, a );
                    }
                    break;
                case MLArray.mxCELL_CLASS:
                    foreach( MLArray a in ((MLCell)array).Cells )
                    {
                        WriteMatrix( bw, a );
                    }
                    break;
                case MLArray.mxSPARSE_CLASS:
                    int[] ai;

                    //write ir
                    buffer = new MemoryStream();
                    bufferBW = new BinaryWriter( buffer );
                    ai = ((MLSparse)array).IR;
                    foreach( int i in ai )
                    {
                        bufferBW.Write( i );
                    }
                    tag = new OSArrayTag( MatDataTypes.miINT32, buffer.ToArray() );
                    tag.WriteTo( bw );

                    // write jc
                    buffer = new MemoryStream();
                    bufferBW = new BinaryWriter( buffer );
                    ai = ((MLSparse)array).JC;
                    foreach( int i in ai )
                    {
                        bufferBW.Write( i );
                    }
                    tag = new OSArrayTag( MatDataTypes.miINT32, buffer.ToArray() );
                    tag.WriteTo( bw );

                    //write real
                    buffer = new MemoryStream();
                    bufferBW = new BinaryWriter( buffer );
                    double[] ad = ((MLSparse)array).ExportReal();
                    for( int i = 0; i < ad.Length; i++ )
                    {
                        bufferBW.Write( ad[i] );
                    }
                    tag = new OSArrayTag( MatDataTypes.miDOUBLE, buffer.ToArray() );
                    tag.WriteTo( bw );

                    //write imaginary
                    if( array.IsComplex )
                    {
                        buffer = new MemoryStream();
                        bufferBW = new BinaryWriter( buffer );
                        ad = ((MLSparse)array).ExportImaginary();
                        for( int i = 0; i < ad.Length; i++ )
                        {
                            bufferBW.Write( ad[i] );
                        }
                        tag = new OSArrayTag( MatDataTypes.miDOUBLE, buffer.ToArray() );
                        tag.WriteTo( bw );
                    }
                    break;
                default:
                    throw new MatlabIOException("Cannot write matrix of type: " + MLArray.TypeToString( array.Type ) );
            }

            // Write matrix
            output.Write( MatDataTypes.miMATRIX );  //matrix tag
            output.Write( (int)ms.Length );  // size of the matrix
            output.Write( ms.ToArray() );  // matrix data
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Write the flag values from the <c>MLArray</c> to the <c>BinaryWriter</c> stream.
        /// </summary>
        /// <param name="bw"><c>BinaryWriter</c> stream</param>
        /// <param name="array"><c>MLArray</c> matrix</param>
        private void WriteFlags( BinaryWriter bw, MLArray array )
        {
            MemoryStream buffer = new MemoryStream();
            BinaryWriter bufferBW = new BinaryWriter( buffer );

            bufferBW.Write( array.Flags );

            if( array.IsSparse )
                bufferBW.Write( ((MLSparse)array).MaxNZ );
            else
                bufferBW.Write( (int)0 );
            OSArrayTag tag = new OSArrayTag( MatDataTypes.miUINT32, buffer.ToArray() );
            tag.WriteTo( bw );
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Write the Dimensions values from the <c>MLArray</c> to the <c>BinaryWriter</c> stream.
        /// </summary>
        /// <param name="bw"><c>BinaryWriter</c> stream</param>
        /// <param name="array"><c>MLArray</c> matrix</param>
        private void WriteDimensions( BinaryWriter bw, MLArray array )
        {
            MemoryStream buffer = new MemoryStream();
            BinaryWriter bufferBW = new BinaryWriter( buffer );

            int[] dims = array.Dimensions;

            for( int i = 0; i < dims.Length; i++ )
                bufferBW.Write( dims[i] );

            OSArrayTag tag = new OSArrayTag( MatDataTypes.miINT32, buffer.ToArray() );
            tag.WriteTo( bw );
        }