Browse Source

LA: tweak vector.ToString formatting (auto column count + width)

provider
Christoph Ruegg 12 years ago
parent
commit
ab13e9c113
  1. 175
      src/Numerics/LinearAlgebra/Vector.BCL.cs
  2. 115
      src/UnitTests/LinearAlgebraTests/Complex/DenseVectorTest.TextHandling.cs
  3. 114
      src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.TextHandling.cs
  4. 11
      src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs
  5. 115
      src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorTest.TextHandling.cs
  6. 114
      src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.TextHandling.cs
  7. 11
      src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs
  8. 150
      src/UnitTests/LinearAlgebraTests/Double/DenseVectorTest.TextHandling.cs
  9. 149
      src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.TextHandling.cs
  10. 11
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs
  11. 150
      src/UnitTests/LinearAlgebraTests/Single/DenseVectorTest.TextHandling.cs
  12. 149
      src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.TextHandling.cs
  13. 11
      src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs
  14. 8
      src/UnitTests/UnitTests.csproj

175
src/Numerics/LinearAlgebra/Vector.BCL.cs

@ -32,6 +32,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using MathNet.Numerics.LinearAlgebra.Storage;
using MathNet.Numerics.Properties;
@ -251,113 +252,163 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// Returns a <see cref="System.String"/> that describes the type, dimensions and shape of this vector.
/// Returns a string that describes the type, dimensions and shape of this vector.
/// </summary>
public virtual string ToTypeString()
{
return string.Format("{0} {1}-{2}", GetType().Name, Count, typeof (T).Name);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the content of this vector, row by row.
/// </summary>
public string ToVectorString(int maxLines, int maxPerLine, IFormatProvider provider = null)
{
return ToVectorString(maxLines, maxPerLine, 12, "G6", provider);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the content of this vector, row by row.
/// </summary>
public string ToVectorString(int maxLines, int maxPerLine, int padding, string format = null, IFormatProvider provider = null)
public string[,] ToVectorStringArray(int maxPerColumn, int maxWidth, int padding, string ellipsis, Func<T, string> formatValue)
{
int fullLines = Count/maxPerLine;
int lastLine = Count%maxPerLine;
bool incomplete = false;
if (fullLines > maxLines || fullLines == maxLines && lastLine > 0)
var columns = new List<Tuple<int, string[]>>();
int chars = 0;
int offset = 0;
while (offset < Count)
{
fullLines = maxLines - 1;
lastLine = maxPerLine - 1;
incomplete = true;
// full column
int height = Math.Min(maxPerColumn, Count - offset);
var candidate = FormatCompleteColumn(offset, height, formatValue);
chars += candidate.Item1 + padding;
if (chars > maxWidth)
{
break;
}
columns.Add(candidate);
offset += height;
}
const string separator = " ";
string pdots = "...".PadLeft(padding);
if (format == null)
if (offset < Count)
{
format = "G8";
// we're not done yet, but adding the last column has failed
// --> make the last column partial
var last = columns[columns.Count - 1];
var c = last.Item2;
c[c.Length - 4] = ellipsis;
c[c.Length - 3] = ellipsis;
c[c.Length - 2] = formatValue(At(Count - 2));
c[c.Length - 1] = formatValue(At(Count - 1));
}
var stringBuilder = new StringBuilder();
var iterator = Enumerate().GetEnumerator();
for (var line = 0; line < fullLines; line++)
int rows = columns[0].Item2.Length;
int cols = columns.Count;
var array = new string[rows, cols];
int colIndex = 0;
foreach (var column in columns)
{
if (line > 0)
for (int k = 0; k < column.Item2.Length; k++)
{
stringBuilder.Append(Environment.NewLine);
array[k, colIndex] = column.Item2[k];
}
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
for (var column = 1; column < maxPerLine; column++)
for (int k = column.Item2.Length; k < rows; k++)
{
stringBuilder.Append(separator);
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
array[k, colIndex] = "";
}
colIndex++;
}
return array;
}
static string FormatStringArrayToString(string[,] array, string columnSeparator, string rowSeparator)
{
var rows = array.GetLength(0);
var cols = array.GetLength(1);
if (lastLine > 0)
var widths = new int[cols];
for (int i = 0; i < rows; i++)
{
if (fullLines > 0)
{
stringBuilder.Append(Environment.NewLine);
}
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
for (var column = 1; column < lastLine; column++)
for (int j = 0; j < cols; j++)
{
stringBuilder.Append(separator);
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
widths[j] = Math.Max(widths[j], array[i, j].Length);
}
if (incomplete)
}
var sb = new StringBuilder();
for (int i = 0; i < rows; i++)
{
sb.Append(array[i, 0].PadLeft(widths[0]));
for (int j = 1; j < cols; j++)
{
stringBuilder.Append(separator);
stringBuilder.Append(pdots);
sb.Append(columnSeparator);
sb.Append(array[i, j].PadLeft(widths[j]));
}
sb.Append(rowSeparator);
}
return sb.ToString();
}
Tuple<int, string[]> FormatCompleteColumn(int offset, int height, Func<T, string> formatValue)
{
var c = new string[height];
int index = 0;
for (var k = 0; k < height; k++)
{
c[index++] = formatValue(At(offset + k));
}
int w = c.Max(x => x.Length);
return new Tuple<int, string[]>(w, c);
}
/// <summary>
/// Returns a string that represents the content of this vector, column by column.
/// </summary>
public string ToVectorString(int maxPerColumn, int maxWidth, string ellipsis, string columnSeparator, string rowSeparator, Func<T, string> formatValue)
{
return FormatStringArrayToString(
ToVectorStringArray(maxPerColumn, maxWidth, columnSeparator.Length, ellipsis, formatValue),
columnSeparator, rowSeparator);
}
/// <summary>
/// Returns a string that represents the content of this vector, column by column.
/// </summary>
public string ToVectorString(int maxPerColumn, int maxWidth, string format = null, IFormatProvider provider = null)
{
if (format == null)
{
format = "G6";
}
return ToVectorString(maxPerColumn, maxWidth, "..", " ", Environment.NewLine, x => x.ToString(format, provider));
}
/// <summary>
/// Returns a string that represents the content of this vector, column by column.
/// </summary>
public string ToVectorString(string format = null, IFormatProvider provider = null)
{
if (format == null)
{
format = "G6";
}
return stringBuilder.ToString();
return ToVectorString(12, 80, "..", " ", Environment.NewLine, x => x.ToString(format, provider));
}
/// <summary>
/// Returns a <see cref="System.String"/> that summarizes this vector.
/// Returns a string that summarizes this vector.
/// </summary>
public string ToString(int maxLines, int maxPerLine, IFormatProvider provider = null)
public string ToString(int maxPerColumn, int maxColumns, string format = null, IFormatProvider provider = null)
{
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(maxLines, maxPerLine, provider));
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(maxPerColumn, maxColumns, format, provider));
}
/// <summary>
/// Returns a <see cref="System.String"/> that summarizes this vector.
/// Returns a string that summarizes this vector.
/// The maximum number of cells can be configured in the <see cref="Control"/> class.
/// </summary>
public override sealed string ToString()
{
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(Control.MaxToStringRows, Control.MaxToStringColumns));
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString());
}
/// <summary>
/// Returns a <see cref="System.String"/> that summarizes this vector.
/// Returns a string that summarizes this vector.
/// The maximum number of cells can be configured in the <see cref="Control"/> class.
/// The format string is ignored.
/// </summary>
public string ToString(string format, IFormatProvider formatProvider)
public string ToString(string format = null, IFormatProvider formatProvider = null)
{
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(Control.MaxToStringRows, Control.MaxToStringColumns, formatProvider));
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(format, formatProvider));
}
}
}

115
src/UnitTests/LinearAlgebraTests/Complex/DenseVectorTest.TextHandling.cs

@ -1,115 +0,0 @@
// <copyright file="DenseVectorTest.TextHandling.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) 2009-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>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{
using LinearAlgebra.Complex;
using NUnit.Framework;
using System;
using System.Globalization;
/// <summary>
/// Dense vector text handling tests.
/// </summary>
[TestFixture, Category("LA")]
public class DenseVectorTextHandlingTest
{
/// <summary>
/// Can parse a Complex dense vectors with invariant culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
[TestCase("2", "(2, 0)")]
[TestCase("(3)", "(3, 0)")]
[TestCase("[1,2,3]", "(1, 0) (2, 0) (3, 0)")]
[TestCase(" [ 1.1 , 2.1 , 3.1 ] ", "(1.1, 0) (2.1, 0) (3.1, 0)")]
[TestCase(" [ -1.1 , 2.1 , +3.1 ] ", "(-1.1, 0) (2.1, 0) (3.1, 0)")]
[TestCase(" [1.2,3.4 , 5.6] ", "(1.2, 0) (3.4, 0) (5.6, 0)")]
[TestCase("[1+1i,2+1i,3+1i]", "(1, 1) (2, 1) (3, 1)")]
[TestCase(" [ 1.1 + 1i , 2.1+1i , 3.1+1i ] ", "(1.1, 1) (2.1, 1) (3.1, 1)")]
[TestCase(" [ -1.1 + 1i , 2.1-1i , +3.1+1i ] ", "(-1.1, 1) (2.1, -1) (3.1, 1)")]
[TestCase(" [1.2+2.3i ,3.4+4.5i , 5.6+ 6.7i] ", "(1.2, 2.3) (3.4, 4.5) (5.6, 6.7)")]
public void CanParseComplexDenseVectorsWithInvariant(string stringToParse, string expectedToString)
{
var formatProvider = CultureInfo.InvariantCulture;
var vector = DenseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse a Complex dense vectors with culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
/// <param name="culture">Culture name.</param>
[TestCase(" 1.2 + 1i , 3.4 + 1i , 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "en-US")]
//[TestCase(" 1.2 + 1i ; 3.4 + 1i ; 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH
#if !PORTABLE
[TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1) (3,4, 1) (5,6, 1)", "de-DE")]
#endif
public void CanParseComplexDenseVectorsWithCulture(string stringToParse, string expectedToString, string culture)
{
var formatProvider = new CultureInfo(culture);
var vector = DenseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Parse if missing closing paren throws <c>FormatException</c>.
/// </summary>
[Test]
public void ParseIfMissingClosingParenThrowsFormatException()
{
Assert.That(() => DenseVector.Parse("(1"), Throws.TypeOf<FormatException>());
Assert.That(() => DenseVector.Parse("[1"), Throws.TypeOf<FormatException>());
}
/// <summary>
/// Try parse a bad value with invariant returns <c>false</c>.
/// </summary>
/// <param name="str">Input string.</param>
[TestCase(null)]
[TestCase("")]
[TestCase(",")]
[TestCase("1e+")]
[TestCase("1e")]
[TestCase("()")]
[TestCase("[ ]")]
public void TryParseBadValueWithInvariantReturnsFalse(string str)
{
DenseVector vector;
var ret = DenseVector.TryParse(str, CultureInfo.InvariantCulture, out vector);
Assert.IsFalse(ret);
Assert.IsNull(vector);
}
}
}

114
src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.TextHandling.cs

@ -1,114 +0,0 @@
// <copyright file="SparseVectorTest.TextHandling.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) 2009-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>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{
using LinearAlgebra.Complex;
using NUnit.Framework;
using System;
using System.Globalization;
/// <summary>
/// Sparse vector text handling tests.
/// </summary>
public class SparseVectorTextHandlingTest
{
/// <summary>
/// Can parse a Complex sparse vectors with invariant culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
[TestCase("2", "(2, 0)")]
[TestCase("(3)", "(3, 0)")]
[TestCase("[1,2,3]", "(1, 0) (2, 0) (3, 0)")]
[TestCase(" [ 1.1 , 2.1 , 3.1 ] ", "(1.1, 0) (2.1, 0) (3.1, 0)")]
[TestCase(" [ -1.1 , 2.1 , +3.1 ] ", "(-1.1, 0) (2.1, 0) (3.1, 0)")]
[TestCase(" [1.2,3.4 , 5.6] ", "(1.2, 0) (3.4, 0) (5.6, 0)")]
[TestCase("[1+1i,2+1i,3+1i]", "(1, 1) (2, 1) (3, 1)")]
[TestCase(" [ 1.1 + 1i , 2.1+1i , 3.1+1i ] ", "(1.1, 1) (2.1, 1) (3.1, 1)")]
[TestCase(" [ -1.1 + 1i , 2.1-1i , +3.1+1i ] ", "(-1.1, 1) (2.1, -1) (3.1, 1)")]
[TestCase(" [1.2+2.3i ,3.4+4.5i , 5.6+ 6.7i] ", "(1.2, 2.3) (3.4, 4.5) (5.6, 6.7)")]
public void CanParseComplexSparseVectorsWithInvariant(string stringToParse, string expectedToString)
{
var formatProvider = CultureInfo.InvariantCulture;
var vector = SparseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse a Complex sparse vectors with culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
/// <param name="culture">Culture name.</param>
[TestCase(" 1.2 + 1i , 3.4 + 1i , 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "en-US")]
//[TestCase(" 1.2 + 1i ; 3.4 + 1i ; 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH
#if !PORTABLE
[TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1) (3,4, 1) (5,6, 1)", "de-DE")]
#endif
public void CanParseComplexSparseVectorsWithCulture(string stringToParse, string expectedToString, string culture)
{
var formatProvider = new CultureInfo(culture);
var vector = SparseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Parse if missing closing paren throws <c>FormatException</c>.
/// </summary>
[Test]
public void ParseIfMissingClosingParenThrowsFormatException()
{
Assert.That(() => SparseVector.Parse("(1"), Throws.TypeOf<FormatException>());
Assert.That(() => SparseVector.Parse("[1"), Throws.TypeOf<FormatException>());
}
/// <summary>
/// Try parse a bad value with invariant returns <c>false</c>.
/// </summary>
/// <param name="str">Input string.</param>
[TestCase(null)]
[TestCase("")]
[TestCase(",")]
[TestCase("1e+")]
[TestCase("1e")]
[TestCase("()")]
[TestCase("[ ]")]
public void TryParseBadValueWithInvariantReturnsFalse(string str)
{
SparseVector vector;
var ret = SparseVector.TryParse(str, CultureInfo.InvariantCulture, out vector);
Assert.IsFalse(ret);
Assert.IsNull(vector);
}
}
}

11
src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs

@ -85,17 +85,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
}
#endif
/// <summary>
/// Can convert vector to string.
/// </summary>
[Test]
public void CanConvertVectorToString()
{
var vector = CreateVector(Data);
var str = vector.ToVectorString(1, int.MaxValue, 1);
Assert.AreEqual("(1, 1) (2, 1) (3, 1) (4, 1) (5, 1)", str);
}
/// <summary>
/// Can copy part of a vector to another vector.
/// </summary>

115
src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorTest.TextHandling.cs

@ -1,115 +0,0 @@
// <copyright file="DenseVectorTest.TextHandling.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) 2009-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>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{
using LinearAlgebra.Complex32;
using NUnit.Framework;
using System;
using System.Globalization;
/// <summary>
/// Dense vector text handling tests.
/// </summary>
[TestFixture, Category("LA")]
public class DenseVectorTextHandlingTest
{
/// <summary>
/// Can parse a Complex32 dense vectors with invariant culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
[TestCase("2", "(2, 0)")]
[TestCase("(3)", "(3, 0)")]
[TestCase("[1,2,3]", "(1, 0) (2, 0) (3, 0)")]
[TestCase(" [ 1.1 , 2.1 , 3.1 ] ", "(1.1, 0) (2.1, 0) (3.1, 0)")]
[TestCase(" [ -1.1 , 2.1 , +3.1 ] ", "(-1.1, 0) (2.1, 0) (3.1, 0)")]
[TestCase(" [1.2,3.4 , 5.6] ", "(1.2, 0) (3.4, 0) (5.6, 0)")]
[TestCase("[1+1i,2+1i,3+1i]", "(1, 1) (2, 1) (3, 1)")]
[TestCase(" [ 1.1 + 1i , 2.1+1i , 3.1+1i ] ", "(1.1, 1) (2.1, 1) (3.1, 1)")]
[TestCase(" [ -1.1 + 1i , 2.1-1i , +3.1+1i ] ", "(-1.1, 1) (2.1, -1) (3.1, 1)")]
[TestCase(" [1.2+2.3i ,3.4+4.5i , 5.6+ 6.7i] ", "(1.2, 2.3) (3.4, 4.5) (5.6, 6.7)")]
public void CanParseComplex32DenseVectorsWithInvariant(string stringToParse, string expectedToString)
{
var formatProvider = CultureInfo.InvariantCulture;
var vector = DenseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse a Complex32 dense vectors with culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
/// <param name="culture">Culture name.</param>
[TestCase(" 1.2 + 1i , 3.4 + 1i , 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "en-US")]
//[TestCase(" 1.2 + 1i ; 3.4 + 1i ; 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH
#if !PORTABLE
[TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1) (3,4, 1) (5,6, 1)", "de-DE")]
#endif
public void CanParseComplex32DenseVectorsWithCulture(string stringToParse, string expectedToString, string culture)
{
var formatProvider = new CultureInfo(culture);
var vector = DenseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Parse if missing closing paren throws <c>FormatException</c>.
/// </summary>
[Test]
public void ParseIfMissingClosingParenThrowsFormatException()
{
Assert.That(() => DenseVector.Parse("(1"), Throws.TypeOf<FormatException>());
Assert.That(() => DenseVector.Parse("[1"), Throws.TypeOf<FormatException>());
}
/// <summary>
/// Try parse a bad value with invariant returns <c>false</c>.
/// </summary>
/// <param name="str">Input string.</param>
[TestCase(null)]
[TestCase("")]
[TestCase(",")]
[TestCase("1e+")]
[TestCase("1e")]
[TestCase("()")]
[TestCase("[ ]")]
public void TryParseBadValueWithInvariantReturnsFalse(string str)
{
DenseVector vector;
var ret = DenseVector.TryParse(str, CultureInfo.InvariantCulture, out vector);
Assert.IsFalse(ret);
Assert.IsNull(vector);
}
}
}

114
src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.TextHandling.cs

@ -1,114 +0,0 @@
// <copyright file="SparseVectorTest.TextHandling.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) 2009-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>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{
using LinearAlgebra.Complex32;
using NUnit.Framework;
using System;
using System.Globalization;
/// <summary>
/// Sparse vector text handling tests.
/// </summary>
public class SparseVectorTextHandlingTest
{
/// <summary>
/// Can parse a Complex32 sparse vectors with invariant culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
[TestCase("2", "(2, 0)")]
[TestCase("(3)", "(3, 0)")]
[TestCase("[1,2,3]", "(1, 0) (2, 0) (3, 0)")]
[TestCase(" [ 1.1 , 2.1 , 3.1 ] ", "(1.1, 0) (2.1, 0) (3.1, 0)")]
[TestCase(" [ -1.1 , 2.1 , +3.1 ] ", "(-1.1, 0) (2.1, 0) (3.1, 0)")]
[TestCase(" [1.2,3.4 , 5.6] ", "(1.2, 0) (3.4, 0) (5.6, 0)")]
[TestCase("[1+1i,2+1i,3+1i]", "(1, 1) (2, 1) (3, 1)")]
[TestCase(" [ 1.1 + 1i , 2.1+1i , 3.1+1i ] ", "(1.1, 1) (2.1, 1) (3.1, 1)")]
[TestCase(" [ -1.1 + 1i , 2.1-1i , +3.1+1i ] ", "(-1.1, 1) (2.1, -1) (3.1, 1)")]
[TestCase(" [1.2+2.3i ,3.4+4.5i , 5.6+ 6.7i] ", "(1.2, 2.3) (3.4, 4.5) (5.6, 6.7)")]
public void CanParseComplex32SparseVectorsWithInvariant(string stringToParse, string expectedToString)
{
var formatProvider = CultureInfo.InvariantCulture;
var vector = SparseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse a Complex32 sparse vectors with culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
/// <param name="culture">Culture name.</param>
[TestCase(" 1.2 + 1i , 3.4 + 1i , 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "en-US")]
//[TestCase(" 1.2 + 1i ; 3.4 + 1i ; 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH
#if !PORTABLE
[TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1) (3,4, 1) (5,6, 1)", "de-DE")]
#endif
public void CanParseComplex32SparseVectorsWithCulture(string stringToParse, string expectedToString, string culture)
{
var formatProvider = new CultureInfo(culture);
var vector = SparseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Parse if missing closing paren throws <c>FormatException</c>.
/// </summary>
[Test]
public void ParseIfMissingClosingParenThrowsFormatException()
{
Assert.That(() => SparseVector.Parse("(1"), Throws.TypeOf<FormatException>());
Assert.That(() => SparseVector.Parse("[1"), Throws.TypeOf<FormatException>());
}
/// <summary>
/// Try parse a bad value with invariant returns <c>false</c>.
/// </summary>
/// <param name="str">Input string.</param>
[TestCase(null)]
[TestCase("")]
[TestCase(",")]
[TestCase("1e+")]
[TestCase("1e")]
[TestCase("()")]
[TestCase("[ ]")]
public void TryParseBadValueWithInvariantReturnsFalse(string str)
{
SparseVector vector;
var ret = SparseVector.TryParse(str, CultureInfo.InvariantCulture, out vector);
Assert.IsFalse(ret);
Assert.IsNull(vector);
}
}
}

11
src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs

@ -81,17 +81,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
}
#endif
/// <summary>
/// Can convert vector to string.
/// </summary>
[Test]
public void CanConvertVectorToString()
{
var vector = CreateVector(Data);
var str = vector.ToVectorString(1, int.MaxValue, 1);
Assert.AreEqual("(1, 1) (2, 1) (3, 1) (4, 1) (5, 1)", str);
}
/// <summary>
/// Can copy part of a vector to another vector.
/// </summary>

150
src/UnitTests/LinearAlgebraTests/Double/DenseVectorTest.TextHandling.cs

@ -1,150 +0,0 @@
// <copyright file="DenseVectorTest.TextHandling.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) 2009-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>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
using LinearAlgebra.Double;
using NUnit.Framework;
using System;
using System.Globalization;
/// <summary>
/// Dense vector text handling tests.
/// </summary>
[TestFixture, Category("LA")]
public class DenseVectorTextHandlingTest
{
/// <summary>
/// Can parse a double dense vectors with invariant culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
[TestCase("2", "2")]
[TestCase("(3)", "3")]
[TestCase("[1,2,3]", "1 2 3")]
[TestCase(" [ 1 , 2 , 3 ] ", "1 2 3")]
[TestCase(" [ -1 , 2 , +3 ] ", "-1 2 3")]
[TestCase(" [1.2,3.4 , 5.6] ", "1.2 3.4 5.6")]
public void CanParseDoubleDenseVectorsWithInvariant(string stringToParse, string expectedToString)
{
var formatProvider = CultureInfo.InvariantCulture;
var vector = DenseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse a double dense vectors with culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
/// <param name="culture">Culture name.</param>
[TestCase(" 1.2,3.4 , 5.6 ", "1.2 3.4 5.6", "en-US")]
//[TestCase(" 1.2;3.4 ; 5.6 ", "1.2 3.4 5.6", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH
[TestCase(" 1,2;3,4 ; 5,6 ", "1,2 3,4 5,6", "de-DE")]
public void CanParseDoubleDenseVectorsWithCulture(string stringToParse, string expectedToString, string culture)
{
var formatProvider = new CultureInfo(culture);
var vector = DenseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse double dense vectors.
/// </summary>
/// <param name="vectorAsString">Vector as string.</param>
[TestCase("15")]
[TestCase("1{0}2 3{0}4 5{0}6")]
public void CanParseDoubleDenseVectors(string vectorAsString)
{
var mappedString = String.Format(
vectorAsString,
CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
var vector = DenseVector.Parse(mappedString);
Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1));
}
/// <summary>
/// Parse if missing closing paren throws <c>FormatException</c>.
/// </summary>
[Test]
public void ParseIfMissingClosingParenThrowsFormatException()
{
Assert.That(() => DenseVector.Parse("(1"), Throws.TypeOf<FormatException>());
Assert.That(() => DenseVector.Parse("[1"), Throws.TypeOf<FormatException>());
}
/// <summary>
/// Can try parse a double dense vector.
/// </summary>
[Test]
public void CanTryParseDoubleDenseVector()
{
var data = new[] { 1.2, 3.4, 5.6e-78 };
var text = String.Format(
"{1}{0}{2}{0}{3}",
CultureInfo.CurrentCulture.TextInfo.ListSeparator,
data[0],
data[1],
data[2]);
DenseVector vector;
var ret = DenseVector.TryParse(text, out vector);
Assert.IsTrue(ret);
AssertHelpers.AlmostEqualRelative(data, (double[])vector, 14);
ret = DenseVector.TryParse(text, CultureInfo.CurrentCulture, out vector);
Assert.IsTrue(ret);
AssertHelpers.AlmostEqualRelative(data, (double[])vector, 14);
}
/// <summary>
/// Try parse a bad value with invariant returns <c>false</c>.
/// </summary>
/// <param name="str">Input string.</param>
[TestCase(null)]
[TestCase("")]
[TestCase(",")]
[TestCase("1e+")]
[TestCase("1e")]
[TestCase("()")]
[TestCase("[ ]")]
public void TryParseBadValueWithInvariantReturnsFalse(string str)
{
DenseVector vector;
var ret = DenseVector.TryParse(str, CultureInfo.InvariantCulture, out vector);
Assert.IsFalse(ret);
Assert.IsNull(vector);
}
}
}

149
src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.TextHandling.cs

@ -1,149 +0,0 @@
// <copyright file="SparseVectorTest.TextHandling.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) 2009-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>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
using LinearAlgebra.Double;
using NUnit.Framework;
using System;
using System.Globalization;
/// <summary>
/// Sparse vector text handling tests.
/// </summary>
public class SparseVectorTextHandlingTest
{
/// <summary>
/// Can parse a double sparse vectors with invariant culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
[TestCase("2", "2")]
[TestCase("(3)", "3")]
[TestCase("[1,2,3]", "1 2 3")]
[TestCase(" [ 1 , 2 , 3 ] ", "1 2 3")]
[TestCase(" [ -1 , 2 , +3 ] ", "-1 2 3")]
[TestCase(" [1.2,3.4 , 5.6] ", "1.2 3.4 5.6")]
public void CanParseDoubleSparseVectorsWithInvariant(string stringToParse, string expectedToString)
{
var formatProvider = CultureInfo.InvariantCulture;
var vector = SparseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse a double sparse vectors with culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
/// <param name="culture">Culture name.</param>
[TestCase(" 1.2,3.4 , 5.6 ", "1.2 3.4 5.6", "en-US")]
//[TestCase(" 1.2;3.4 ; 5.6 ", "1.2 3.4 5.6", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH
[TestCase(" 1,2;3,4 ; 5,6 ", "1,2 3,4 5,6", "de-DE")]
public void CanParseDoubleSparseVectorsWithCulture(string stringToParse, string expectedToString, string culture)
{
var formatProvider = new CultureInfo(culture);
var vector = SparseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse double sparse vectors.
/// </summary>
/// <param name="vectorAsString">Vector as string.</param>
[TestCase("15")]
[TestCase("1{0}2 3{0}4 5{0}6")]
public void CanParseDoubleSparseVectors(string vectorAsString)
{
var mappedString = String.Format(
vectorAsString,
CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
var vector = SparseVector.Parse(mappedString);
Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1));
}
/// <summary>
/// Parse if missing closing paren throws <c>FormatException</c>.
/// </summary>
[Test]
public void ParseIfMissingClosingParenThrowsFormatException()
{
Assert.That(() => SparseVector.Parse("(1"), Throws.TypeOf<FormatException>());
Assert.That(() => SparseVector.Parse("[1"), Throws.TypeOf<FormatException>());
}
/// <summary>
/// Can try parse a double sparse vector.
/// </summary>
[Test]
public void CanTryParseDoubleSparseVector()
{
var data = new[] { 1.2, 3.4, 5.6e-78 };
var text = String.Format(
"{1}{0}{2}{0}{3}",
CultureInfo.CurrentCulture.TextInfo.ListSeparator,
data[0],
data[1],
data[2]);
SparseVector vector;
var ret = SparseVector.TryParse(text, out vector);
Assert.IsTrue(ret);
AssertHelpers.AlmostEqualRelative(data, vector.ToArray(), 14);
ret = SparseVector.TryParse(text, CultureInfo.CurrentCulture, out vector);
Assert.IsTrue(ret);
AssertHelpers.AlmostEqualRelative(data, vector.ToArray(), 14);
}
/// <summary>
/// Try parse a bad value with invariant returns <c>false</c>.
/// </summary>
/// <param name="str">Input string.</param>
[TestCase(null)]
[TestCase("")]
[TestCase(",")]
[TestCase("1e+")]
[TestCase("1e")]
[TestCase("()")]
[TestCase("[ ]")]
public void TryParseBadValueWithInvariantReturnsFalse(string str)
{
SparseVector vector;
var ret = SparseVector.TryParse(str, CultureInfo.InvariantCulture, out vector);
Assert.IsFalse(ret);
Assert.IsNull(vector);
}
}
}

11
src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

@ -77,17 +77,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
#endif
/// <summary>
/// Can convert vector to string.
/// </summary>
[Test]
public void CanConvertVectorToString()
{
var vector = CreateVector(Data);
var str = vector.ToVectorString(1, int.MaxValue, 1);
Assert.AreEqual("1 2 3 4 5", str);
}
/// <summary>
/// Can copy part of a vector to another vector.
/// </summary>

150
src/UnitTests/LinearAlgebraTests/Single/DenseVectorTest.TextHandling.cs

@ -1,150 +0,0 @@
// <copyright file="DenseVectorTest.TextHandling.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) 2009-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>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{
using LinearAlgebra.Single;
using NUnit.Framework;
using System;
using System.Globalization;
/// <summary>
/// Dense vector text handling tests.
/// </summary>
[TestFixture, Category("LA")]
public class DenseVectorTextHandlingTest
{
/// <summary>
/// Can parse a float dense vectors with invariant culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
[TestCase("2", "2")]
[TestCase("(3)", "3")]
[TestCase("[1,2,3]", "1 2 3")]
[TestCase(" [ 1 , 2 , 3 ] ", "1 2 3")]
[TestCase(" [ -1 , 2 , +3 ] ", "-1 2 3")]
[TestCase(" [1.2,3.4 , 5.6] ", "1.2 3.4 5.6")]
public void CanParseSingleDenseVectorsWithInvariant(string stringToParse, string expectedToString)
{
var formatProvider = CultureInfo.InvariantCulture;
var vector = DenseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse a float dense vectors with culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
/// <param name="culture">Culture name.</param>
[TestCase(" 1.2,3.4 , 5.6 ", "1.2 3.4 5.6", "en-US")]
//[TestCase(" 1.2;3.4 ; 5.6 ", "1.2 3.4 5.6", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH
[TestCase(" 1,2;3,4 ; 5,6 ", "1,2 3,4 5,6", "de-DE")]
public void CanParseSingleDenseVectorsWithCulture(string stringToParse, string expectedToString, string culture)
{
var formatProvider = new CultureInfo(culture);
var vector = DenseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse float dense vectors.
/// </summary>
/// <param name="vectorAsString">Vector as string.</param>
[TestCase("15")]
[TestCase("1{0}2 3{0}4 5{0}6")]
public void CanParseSingleDenseVectors(string vectorAsString)
{
var mappedString = String.Format(
vectorAsString,
CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
var vector = DenseVector.Parse(mappedString);
Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1, "G"));
}
/// <summary>
/// Parse if missing closing paren throws <c>FormatException</c>.
/// </summary>
[Test]
public void ParseIfMissingClosingParenThrowsFormatException()
{
Assert.That(() => DenseVector.Parse("(1"), Throws.TypeOf<FormatException>());
Assert.That(() => DenseVector.Parse("[1"), Throws.TypeOf<FormatException>());
}
/// <summary>
/// Can try parse a float dense vector.
/// </summary>
[Test]
public void CanTryParseSingleDenseVector()
{
var data = new[] { 1.2f, 3.4f, 5.6e-78f };
var text = String.Format(
"{1}{0}{2}{0}{3}",
CultureInfo.CurrentCulture.TextInfo.ListSeparator,
data[0],
data[1],
data[2]);
DenseVector vector;
var ret = DenseVector.TryParse(text, out vector);
Assert.IsTrue(ret);
AssertHelpers.AlmostEqualRelative(data, (float[])vector, 14);
ret = DenseVector.TryParse(text, CultureInfo.CurrentCulture, out vector);
Assert.IsTrue(ret);
AssertHelpers.AlmostEqualRelative(data, (float[])vector, 14);
}
/// <summary>
/// Try parse a bad value with invariant returns <c>false</c>.
/// </summary>
/// <param name="str">Input string.</param>
[TestCase(null)]
[TestCase("")]
[TestCase(",")]
[TestCase("1e+")]
[TestCase("1e")]
[TestCase("()")]
[TestCase("[ ]")]
public void TryParseBadValueWithInvariantReturnsFalse(string str)
{
DenseVector vector;
var ret = DenseVector.TryParse(str, CultureInfo.InvariantCulture, out vector);
Assert.IsFalse(ret);
Assert.IsNull(vector);
}
}
}

149
src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.TextHandling.cs

@ -1,149 +0,0 @@
// <copyright file="SparseVectorTest.TextHandling.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) 2009-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>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{
using LinearAlgebra.Single;
using NUnit.Framework;
using System;
using System.Globalization;
/// <summary>
/// Sparse vector text handling tests.
/// </summary>
public class SparseVectorTextHandlingTest
{
/// <summary>
/// Can parse a float sparse vectors with invariant culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
[TestCase("2", "2")]
[TestCase("(3)", "3")]
[TestCase("[1,2,3]", "1 2 3")]
[TestCase(" [ 1 , 2 , 3 ] ", "1 2 3")]
[TestCase(" [ -1 , 2 , +3 ] ", "-1 2 3")]
[TestCase(" [1.2,3.4 , 5.6] ", "1.2 3.4 5.6")]
public void CanParseSingleSparseVectorsWithInvariant(string stringToParse, string expectedToString)
{
var formatProvider = CultureInfo.InvariantCulture;
var vector = SparseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse a float sparse vectors with culture.
/// </summary>
/// <param name="stringToParse">String to parse.</param>
/// <param name="expectedToString">Expected result.</param>
/// <param name="culture">Culture name.</param>
[TestCase(" 1.2,3.4 , 5.6 ", "1.2 3.4 5.6", "en-US")]
//[TestCase(" 1.2;3.4 ; 5.6 ", "1.2 3.4 5.6", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH
[TestCase(" 1,2;3,4 ; 5,6 ", "1,2 3,4 5,6", "de-DE")]
public void CanParseSingleSparseVectorsWithCulture(string stringToParse, string expectedToString, string culture)
{
var formatProvider = new CultureInfo(culture);
var vector = SparseVector.Parse(stringToParse, formatProvider);
Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider));
}
/// <summary>
/// Can parse float sparse vectors.
/// </summary>
/// <param name="vectorAsString">Vector as string.</param>
[TestCase("15")]
[TestCase("1{0}2 3{0}4 5{0}6")]
public void CanParseSingleSparseVectors(string vectorAsString)
{
var mappedString = String.Format(
vectorAsString,
CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
var vector = SparseVector.Parse(mappedString);
Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1, "G"));
}
/// <summary>
/// Parse if missing closing paren throws <c>FormatException</c>.
/// </summary>
[Test]
public void ParseIfMissingClosingParenThrowsFormatException()
{
Assert.That(() => SparseVector.Parse("(1"), Throws.TypeOf<FormatException>());
Assert.That(() => SparseVector.Parse("[1"), Throws.TypeOf<FormatException>());
}
/// <summary>
/// Can try parse a float sparse vector.
/// </summary>
[Test]
public void CanTryParseSingleSparseVector()
{
var data = new[] { 1.2f, 3.4f, 5.6e-10f };
var text = String.Format(
"{1}{0}{2}{0}{3}",
CultureInfo.CurrentCulture.TextInfo.ListSeparator,
data[0],
data[1],
data[2]);
SparseVector vector;
var ret = SparseVector.TryParse(text, out vector);
Assert.IsTrue(ret);
AssertHelpers.AlmostEqualRelative(data, vector.ToArray(), 14);
ret = SparseVector.TryParse(text, CultureInfo.CurrentCulture, out vector);
Assert.IsTrue(ret);
AssertHelpers.AlmostEqualRelative(data, vector.ToArray(), 14);
}
/// <summary>
/// Try parse a bad value with invariant returns <c>false</c>.
/// </summary>
/// <param name="str">Input string.</param>
[TestCase(null)]
[TestCase("")]
[TestCase(",")]
[TestCase("1e+")]
[TestCase("1e")]
[TestCase("()")]
[TestCase("[ ]")]
public void TryParseBadValueWithInvariantReturnsFalse(string str)
{
SparseVector vector;
var ret = SparseVector.TryParse(str, CultureInfo.InvariantCulture, out vector);
Assert.IsFalse(ret);
Assert.IsNull(vector);
}
}
}

11
src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs

@ -77,17 +77,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
CollectionAssert.AreEqual(vector, clone);
}
#endif
/// <summary>
/// Can convert vector to string.
/// </summary>
[Test]
public void CanConvertVectorToString()
{
var vector = CreateVector(Data);
var str = vector.ToVectorString(1, int.MaxValue, 1);
Assert.AreEqual("1 2 3 4 5", str);
}
/// <summary>
/// Can copy part of a vector to another vector.

8
src/UnitTests/UnitTests.csproj

@ -153,7 +153,6 @@
<Compile Include="LinearAlgebraProviderTests\Single\LinearAlgebraProviderTests.cs" />
<Compile Include="LinearAlgebraTests\Complex32\DenseMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Complex32\DenseVectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\Complex32\DenseVectorTest.TextHandling.cs" />
<Compile Include="LinearAlgebraTests\Complex32\DenseVectorTests.cs" />
<Compile Include="LinearAlgebraTests\Complex32\DiagonalMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Complex32\Factorization\CholeskyTests.cs" />
@ -191,7 +190,6 @@
<Compile Include="LinearAlgebraTests\Complex32\SparseMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Complex32\SparseVectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\Complex32\SparseVectorTest.cs" />
<Compile Include="LinearAlgebraTests\Complex32\SparseVectorTest.TextHandling.cs" />
<Compile Include="LinearAlgebraTests\Complex32\UserDefinedMatrix.cs" />
<Compile Include="LinearAlgebraTests\Complex32\UserDefinedMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Complex32\UserDefinedVector.cs" />
@ -202,7 +200,6 @@
<Compile Include="LinearAlgebraTests\Complex32\VectorTests.Norm.cs" />
<Compile Include="LinearAlgebraTests\Complex\DenseMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Complex\DenseVectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\Complex\DenseVectorTest.TextHandling.cs" />
<Compile Include="LinearAlgebraTests\Complex\DenseVectorTests.cs" />
<Compile Include="LinearAlgebraTests\Complex\DiagonalMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Complex\Factorization\CholeskyTests.cs" />
@ -240,7 +237,6 @@
<Compile Include="LinearAlgebraTests\Complex\SparseMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Complex\SparseVectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\Complex\SparseVectorTest.cs" />
<Compile Include="LinearAlgebraTests\Complex\SparseVectorTest.TextHandling.cs" />
<Compile Include="LinearAlgebraTests\Complex\UserDefinedMatrix.cs" />
<Compile Include="LinearAlgebraTests\Complex\UserDefinedMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Complex\UserDefinedVector.cs" />
@ -257,7 +253,6 @@
<Compile Include="LinearAlgebraTests\Double\Solvers\StopCriterion\ResidualStopCriteriumTest.cs" />
<Compile Include="LinearAlgebraTests\Double\SparseVectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\Double\DenseMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Double\DenseVectorTest.TextHandling.cs" />
<Compile Include="LinearAlgebraTests\Double\DenseVectorTests.cs" />
<Compile Include="LinearAlgebraTests\Double\DenseVectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\Double\DiagonalMatrixTests.cs" />
@ -289,7 +284,6 @@
<Compile Include="LinearAlgebraTests\Double\Solvers\Preconditioners\UnitPreconditionerTest.cs" />
<Compile Include="LinearAlgebraTests\Double\SparseMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Double\SparseVectorTest.cs" />
<Compile Include="LinearAlgebraTests\Double\SparseVectorTest.TextHandling.cs" />
<Compile Include="LinearAlgebraTests\Double\UserDefinedMatrix.cs" />
<Compile Include="LinearAlgebraTests\Double\UserDefinedMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Double\UserDefinedVector.cs" />
@ -304,7 +298,6 @@
<Compile Include="LinearAlgebraTests\MatrixStructureTheory.Reform.cs" />
<Compile Include="LinearAlgebraTests\Single\DenseMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Single\DenseVectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\Single\DenseVectorTest.TextHandling.cs" />
<Compile Include="LinearAlgebraTests\Single\DenseVectorTests.cs" />
<Compile Include="LinearAlgebraTests\Single\DiagonalMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Single\Factorization\CholeskyTests.cs" />
@ -342,7 +335,6 @@
<Compile Include="LinearAlgebraTests\Single\SparseMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Single\SparseVectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\Single\SparseVectorTest.cs" />
<Compile Include="LinearAlgebraTests\Single\SparseVectorTest.TextHandling.cs" />
<Compile Include="LinearAlgebraTests\Single\UserDefinedMatrix.cs" />
<Compile Include="LinearAlgebraTests\Single\UserDefinedMatrixTests.cs" />
<Compile Include="LinearAlgebraTests\Single\UserDefinedVector.cs" />

Loading…
Cancel
Save