Browse Source

Merge pull request #75 from cuda/zlib

Removed zlib dependency and fixed a sparse matrix parsing bug
pull/76/head
Christoph Ruegg 14 years ago
parent
commit
efe5482472
  1. 93
      src/Numerics.IO/LinearAlgebra/IO/Matlab/Adler32.cs
  2. 22
      src/Numerics.IO/LinearAlgebra/IO/Matlab/MatlabParser.cs
  3. 14
      src/Numerics.IO/LinearAlgebra/IO/MatlabWriter.cs
  4. 5
      src/Numerics.IO/Numerics.IO.csproj
  5. 14
      src/UnitTests/LinearAlgebraTests/Complex/IO/MatlabWriterTests.cs
  6. 14
      src/UnitTests/LinearAlgebraTests/Complex32/IO/MatlabWriterTests.cs
  7. 14
      src/UnitTests/LinearAlgebraTests/Double/IO/MatlabWriterTests.cs
  8. 14
      src/UnitTests/LinearAlgebraTests/Single/IO/MatlabWriterTests.cs
  9. 4
      src/UnitTests/UnitTests.csproj

93
src/Numerics.IO/LinearAlgebra/IO/Matlab/Adler32.cs

@ -0,0 +1,93 @@
// <copyright file="Adler32.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
/* This code is a port and simplification of
adler32.c -- compute the Adler-32 checksum of a data stream
Copyright (C) 1995-2011 Mark Adler
zlib license:
Copyright (C) 1995-2012 Jean-loup Gailly and Mark Adler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jean-loup Gailly Mark Adler
jloup@gzip.org madler@alumni.caltech.edu
*/
namespace MathNet.Numerics.LinearAlgebra.IO.Matlab
{
internal static class Adler32
{
/* largest prime smaller than 65536 */
const int Base = 65521;
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
const int Nmax = 5552;
/// <summary>
/// Computes the Adler-32 checksum of the given data.
/// </summary>
/// <param name="data">The data to create the checksum.</param>
/// <returns>The checksum</returns>
public static uint Compute(byte[] data)
{
uint adler = 1;
uint sum2 = 0;
var len = data.Length;
var offset = 0;
while (len > 0)
{
var tlen = len < Nmax ? len : Nmax;
len -= tlen;
do
{
adler += data[offset++];
sum2 += adler;
} while (--tlen > 0);
adler %= Base;
sum2 %= Base;
}
return adler | (sum2 << 16);
}
}
}

22
src/Numerics.IO/LinearAlgebra/IO/Matlab/MatlabParser.cs

@ -24,6 +24,8 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System.IO.Compression;
namespace MathNet.Numerics.LinearAlgebra.IO.Matlab
{
using System;
@ -34,7 +36,6 @@ namespace MathNet.Numerics.LinearAlgebra.IO.Matlab
using System.Text;
using Generic;
using Properties;
using zlib;
using Complex32 = Numerics.Complex32;
/// <summary>
@ -221,11 +222,12 @@ namespace MathNet.Numerics.LinearAlgebra.IO.Matlab
private static byte[] DecompressBlock(byte[] compressed, ref DataType type)
{
byte[] data;
using (var decompressed = new MemoryStream())
using (var compressedStream = new MemoryStream(compressed, 2, compressed.Length-6))
{
using (var decompressor = new ZOutputStream(decompressed))
using (var decompressor = new DeflateStream(compressedStream, CompressionMode.Decompress))
using(var decompressed = new MemoryStream())
{
decompressor.Write(compressed, 0, compressed.Length);
decompressor.CopyTo(decompressed);
decompressed.Position = 0;
var buf = new byte[4];
decompressed.Read(buf, 0, 4);
@ -410,7 +412,7 @@ namespace MathNet.Numerics.LinearAlgebra.IO.Matlab
for (var i = 0; i < ir.Count; i++)
{
var row = ir[i];
if (jc[col + 1] == i)
while(jc[col + 1] == i)
{
col++;
}
@ -467,7 +469,7 @@ namespace MathNet.Numerics.LinearAlgebra.IO.Matlab
for (var i = 0; i < ir.Count; i++)
{
var row = ir[i];
if (jc[col + 1] == i)
while (jc[col + 1] == i)
{
col++;
}
@ -526,7 +528,7 @@ namespace MathNet.Numerics.LinearAlgebra.IO.Matlab
for (var i = 0; i < ir.Count; i++)
{
var row = ir[i];
if (jc[col + 1] == i)
while (jc[col + 1] == i)
{
col++;
}
@ -582,7 +584,7 @@ namespace MathNet.Numerics.LinearAlgebra.IO.Matlab
for (var i = 0; i < ir.Count; i++)
{
var row = ir[i];
if (jc[col + 1] == i)
while (jc[col + 1] == i)
{
col++;
}
@ -643,7 +645,7 @@ namespace MathNet.Numerics.LinearAlgebra.IO.Matlab
for (var i = 0; i < ir.Count; i++)
{
var row = ir[i];
if (jc[col + 1] == i)
while (jc[col + 1] == i)
{
col++;
}
@ -700,7 +702,7 @@ namespace MathNet.Numerics.LinearAlgebra.IO.Matlab
for (var i = 0; i < ir.Count; i++)
{
var row = ir[i];
if (jc[col + 1] == i)
while (jc[col + 1] == i)
{
col++;
}

14
src/Numerics.IO/LinearAlgebra/IO/MatlabWriter.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -29,8 +29,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using zlib;
namespace MathNet.Numerics.LinearAlgebra.IO
{
@ -285,13 +285,19 @@ namespace MathNet.Numerics.LinearAlgebra.IO
/// <returns>The compressed data.</returns>
private static byte[] CompressData(byte[] data)
{
var adler = BitConverter.GetBytes(Adler32.Compute(data));
using (var compressedStream = new MemoryStream())
{
using (var outputStream = new ZOutputStream(compressedStream, zlibConst.Z_DEFAULT_COMPRESSION))
compressedStream.WriteByte(0x58);
compressedStream.WriteByte(0x85);
using (var outputStream = new DeflateStream(compressedStream, CompressionMode.Compress, true))
{
outputStream.Write(data, 0, data.Length);
}
compressedStream.WriteByte(adler[3]);
compressedStream.WriteByte(adler[2]);
compressedStream.WriteByte(adler[1]);
compressedStream.WriteByte(adler[0]);
return compressedStream.ToArray();
}
}

5
src/Numerics.IO/Numerics.IO.csproj

@ -60,10 +60,6 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="zlib.net, Version=1.0.3.0, Culture=neutral, PublicKeyToken=47d7877cb3620160">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\zlib.net.1.0.4.0\lib\zlib.net.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="LinearAlgebra\Complex32\IO\DelimitedReader.cs" />
@ -76,6 +72,7 @@
<Compile Include="LinearAlgebra\IO\DelimitedWriter.cs" />
<Compile Include="LinearAlgebra\IO\MatlabReader.cs" />
<Compile Include="LinearAlgebra\IO\MatlabWriter.cs" />
<Compile Include="LinearAlgebra\IO\Matlab\Adler32.cs" />
<Compile Include="LinearAlgebra\IO\Matlab\ArrayClass.cs" />
<Compile Include="LinearAlgebra\IO\Matlab\ArrayFlags.cs" />
<Compile Include="LinearAlgebra\IO\Matlab\DataType.cs" />

14
src/UnitTests/LinearAlgebraTests/Complex/IO/MatlabWriterTests.cs

@ -97,16 +97,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO
}
Matrix mat3 = new SparseMatrix(5, 4);
for (var i = 0; i < mat3.ColumnCount; i++)
{
mat3[i, i] = new Complex(i + .1, i + .1);
}
mat3[0, 0] = new Complex(1.1, 1.1);
mat3[0, 2] = new Complex(2.2, 2.2);
mat3[4, 3] = new Complex(3.3, 3.3);
Matrix mat4 = new SparseMatrix(3, 5);
for (var i = 0; i < mat4.RowCount; i++)
{
mat4[i, i] = new Complex(i + .1, i + .1);
}
mat4[0, 0] = new Complex(1.1, 1.1);
mat4[0, 2] = new Complex(2.2, 2.2);
mat4[2, 4] = new Complex(3.3, 3.3);
var write = new[] { mat1, mat2, mat3, mat4 };

14
src/UnitTests/LinearAlgebraTests/Complex32/IO/MatlabWriterTests.cs

@ -97,16 +97,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO
}
Matrix mat3 = new SparseMatrix(5, 4);
for (var i = 0; i < mat3.ColumnCount; i++)
{
mat3[i, i] = new Complex32(i + .1f, i + .1f);
}
mat3[0, 0] = new Complex32(1.1f, 1.1f);
mat3[0, 2] = new Complex32(2.2f, 2.2f);
mat3[4, 3] = new Complex32(3.3f, 3.3f);
Matrix mat4 = new SparseMatrix(3, 5);
for (var i = 0; i < mat4.RowCount; i++)
{
mat4[i, i] = new Complex32(i + .1f, i + .1f);
}
mat4[0, 0] = new Complex32(1.1f, 1.1f);
mat4[0, 2] = new Complex32(2.2f, 2.2f);
mat4[2, 4] = new Complex32(3.3f, 3.3f);
var write = new[] { mat1, mat2, mat3, mat4 };

14
src/UnitTests/LinearAlgebraTests/Double/IO/MatlabWriterTests.cs

@ -96,16 +96,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO
}
Matrix mat3 = new SparseMatrix(5, 4);
for (var i = 0; i < mat3.ColumnCount; i++)
{
mat3[i, i] = i + .1;
}
mat3[0, 0] = 1.1;
mat3[0, 2] = 2.2;
mat3[4, 3] = 3.3;
Matrix mat4 = new SparseMatrix(3, 5);
for (var i = 0; i < mat4.RowCount; i++)
{
mat4[i, i] = i + .1;
}
mat4[0, 0] = 1.1;
mat4[0, 2] = 2.2;
mat4[2, 4] = 3.3;
var write = new[] { mat1, mat2, mat3, mat4 };

14
src/UnitTests/LinearAlgebraTests/Single/IO/MatlabWriterTests.cs

@ -95,16 +95,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO
}
Matrix mat3 = new SparseMatrix(5, 4);
for (var i = 0; i < mat3.ColumnCount; i++)
{
mat3[i, i] = i + .1f;
}
mat3[0, 0] = 1.1f;
mat3[0, 2] = 2.2f;
mat3[4, 3] = 3.3f;
Matrix mat4 = new SparseMatrix(3, 5);
for (var i = 0; i < mat4.RowCount; i++)
{
mat4[i, i] = i + .1f;
}
mat4[0, 0] = 1.1f;
mat4[0, 2] = 2.2f;
mat4[2, 4] = 3.3f;
var write = new[] { mat1, mat2, mat3, mat4 };

4
src/UnitTests/UnitTests.csproj

@ -62,10 +62,6 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="zlib.net, Version=1.0.3.0, Culture=neutral, PublicKeyToken=47d7877cb3620160">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\zlib.net.1.0.4.0\lib\zlib.net.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ArrayHelpers.cs" />

Loading…
Cancel
Save