From b4ee5ed34f5a28e68ece3147bb263c0f63bee428 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 6 Apr 2013 05:51:58 +0200 Subject: [PATCH] LA: Adapt parsing and unit tests to modified string formatting --- src/Numerics/Control.cs | 2 +- .../LinearAlgebra/Complex/DenseVector.cs | 53 +++++++++---------- .../LinearAlgebra/Complex/SparseMatrix.cs | 1 - .../LinearAlgebra/Complex/SparseVector.cs | 52 +++++++++--------- .../LinearAlgebra/Complex32/DenseVector.cs | 52 +++++++++--------- .../LinearAlgebra/Complex32/SparseMatrix.cs | 1 - .../LinearAlgebra/Complex32/SparseVector.cs | 52 +++++++++--------- .../LinearAlgebra/Double/DenseVector.cs | 36 ++----------- .../LinearAlgebra/Double/SparseMatrix.cs | 1 - .../LinearAlgebra/Double/SparseVector.cs | 35 ++---------- .../LinearAlgebra/Generic/Matrix.BCL.cs | 18 +++++-- src/Numerics/LinearAlgebra/Generic/Matrix.cs | 8 +-- .../LinearAlgebra/Generic/Vector.BCL.cs | 19 +++++-- .../LinearAlgebra/Single/DenseVector.cs | 36 ++----------- .../LinearAlgebra/Single/SparseMatrix.cs | 1 - .../LinearAlgebra/Single/SparseVector.cs | 41 +++----------- src/Numerics/Statistics/Statistics.cs | 1 - .../Complex/DenseVectorTest.TextHandling.cs | 41 +++++++------- .../Complex/SparseVectorTest.TextHandling.cs | 41 +++++++------- .../LinearAlgebraTests/Complex/VectorTests.cs | 20 +++---- .../Complex32/DenseVectorTest.TextHandling.cs | 41 +++++++------- .../SparseVectorTest.TextHandling.cs | 41 +++++++------- .../Complex32/VectorTests.cs | 20 +++---- .../Double/DenseMatrixTests.cs | 4 +- .../Double/DenseVectorTest.TextHandling.cs | 40 +++++++------- .../Double/SparseVectorTest.TextHandling.cs | 40 +++++++------- .../LinearAlgebraTests/Double/VectorTests.cs | 18 ++++--- .../Single/DenseVectorTest.TextHandling.cs | 40 +++++++------- .../Single/SparseVectorTest.TextHandling.cs | 40 +++++++------- .../LinearAlgebraTests/Single/VectorTests.cs | 20 +++---- 30 files changed, 349 insertions(+), 466 deletions(-) diff --git a/src/Numerics/Control.cs b/src/Numerics/Control.cs index 0c71a313..30c1e802 100644 --- a/src/Numerics/Control.cs +++ b/src/Numerics/Control.cs @@ -30,8 +30,8 @@ namespace MathNet.Numerics { - using System; using Algorithms.LinearAlgebra; + using System; /// /// Sets parameters for the library. diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index ee88d9ee..37ce904c 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -28,12 +28,10 @@ // OTHER DEALINGS IN THE SOFTWARE. // - namespace MathNet.Numerics.LinearAlgebra.Complex { using Distributions; using Generic; - using NumberTheory; using Storage; using System; using System.Collections.Generic; @@ -758,39 +756,38 @@ namespace MathNet.Numerics.LinearAlgebra.Complex value = value.Substring(1, value.Length - 2).Trim(); } - // keywords - var textInfo = formatProvider.GetTextInfo(); - var keywords = new[] { textInfo.ListSeparator }; - - // lexing - var tokens = new LinkedList(); - GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0); - var token = tokens.First; - - if (token == null || tokens.Count.IsEven()) - { - throw new FormatException(); - } - // parsing - var data = new Complex[(tokens.Count + 1) >> 1]; - for (var i = 0; i < data.Length; i++) + var strongTokens = value.Split(new[] { formatProvider.GetTextInfo().ListSeparator }, StringSplitOptions.RemoveEmptyEntries); + var data = new List(); + foreach (string strongToken in strongTokens) { - if (token == null || token.Value == textInfo.ListSeparator) + var weakTokens = strongToken.Split(new[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); + string current = string.Empty; + for (int i = 0; i < weakTokens.Length; i++) { - throw new FormatException(); + current += weakTokens[i]; + if (current.EndsWith("+") || current.EndsWith("-") || current.StartsWith("(") && !current.EndsWith(")")) + { + continue; + } + var ahead = i < weakTokens.Length - 1 ? weakTokens[i + 1] : string.Empty; + if (ahead.StartsWith("+") || ahead.StartsWith("-")) + { + continue; + } + data.Add(current.ToComplex(formatProvider)); + current = string.Empty; } - - data[i] = token.Value.ToComplex(formatProvider); - - token = token.Next; - if (token != null) + if (current != string.Empty) { - token = token.Next; + throw new FormatException(); } } - - return new DenseVector(data); + if (data.Count == 0) + { + throw new FormatException(); + } + return new DenseVector(data.ToArray()); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index a883e048..28846e08 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -37,7 +37,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex using System.Collections.Generic; using System.Diagnostics; using System.Numerics; - using Threading; /// /// A Matrix class with sparse storage. The underlying storage scheme is 3-array compressed-sparse-row (CSR) Format. diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 52af8e8e..d7bd7535 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -31,7 +31,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { using Generic; - using NumberTheory; using Storage; using System; using System.Collections.Generic; @@ -941,39 +940,38 @@ namespace MathNet.Numerics.LinearAlgebra.Complex value = value.Substring(1, value.Length - 2).Trim(); } - // keywords - var textInfo = formatProvider.GetTextInfo(); - var keywords = new[] { textInfo.ListSeparator }; - - // lexing - var tokens = new LinkedList(); - GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0); - var token = tokens.First; - - if (token == null || tokens.Count.IsEven()) - { - throw new FormatException(); - } - // parsing - var data = new Complex[(tokens.Count + 1) >> 1]; - for (var i = 0; i < data.Length; i++) + var strongTokens = value.Split(new[] { formatProvider.GetTextInfo().ListSeparator }, StringSplitOptions.RemoveEmptyEntries); + var data = new List(); + foreach (string strongToken in strongTokens) { - if (token == null || token.Value == textInfo.ListSeparator) + var weakTokens = strongToken.Split(new[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); + string current = string.Empty; + for (int i = 0; i < weakTokens.Length; i++) { - throw new FormatException(); + current += weakTokens[i]; + if (current.EndsWith("+") || current.EndsWith("-") || current.StartsWith("(") && !current.EndsWith(")")) + { + continue; + } + var ahead = i < weakTokens.Length - 1 ? weakTokens[i + 1] : string.Empty; + if (ahead.StartsWith("+") || ahead.StartsWith("-")) + { + continue; + } + data.Add(current.ToComplex(formatProvider)); + current = string.Empty; } - - data[i] = token.Value.ToComplex(formatProvider); - - token = token.Next; - if (token != null) + if (current != string.Empty) { - token = token.Next; + throw new FormatException(); } } - - return new SparseVector(data); + if (data.Count == 0) + { + throw new FormatException(); + } + return new SparseVector(data.ToArray()); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 68880685..ac607077 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -32,7 +32,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { using Distributions; using Generic; - using NumberTheory; using Numerics; using Storage; using System; @@ -757,39 +756,38 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 value = value.Substring(1, value.Length - 2).Trim(); } - // keywords - var textInfo = formatProvider.GetTextInfo(); - var keywords = new[] { textInfo.ListSeparator }; - - // lexing - var tokens = new LinkedList(); - GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0); - var token = tokens.First; - - if (token == null || tokens.Count.IsEven()) - { - throw new FormatException(); - } - // parsing - var data = new Complex32[(tokens.Count + 1) >> 1]; - for (var i = 0; i < data.Length; i++) + var strongTokens = value.Split(new[] { formatProvider.GetTextInfo().ListSeparator }, StringSplitOptions.RemoveEmptyEntries); + var data = new List(); + foreach (string strongToken in strongTokens) { - if (token == null || token.Value == textInfo.ListSeparator) + var weakTokens = strongToken.Split(new[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); + string current = string.Empty; + for (int i = 0; i < weakTokens.Length; i++) { - throw new FormatException(); + current += weakTokens[i]; + if (current.EndsWith("+") || current.EndsWith("-") || current.StartsWith("(") && !current.EndsWith(")")) + { + continue; + } + var ahead = i < weakTokens.Length - 1 ? weakTokens[i + 1] : string.Empty; + if (ahead.StartsWith("+") || ahead.StartsWith("-")) + { + continue; + } + data.Add(current.ToComplex32(formatProvider)); + current = string.Empty; } - - data[i] = token.Value.ToComplex32(formatProvider); - - token = token.Next; - if (token != null) + if (current != string.Empty) { - token = token.Next; + throw new FormatException(); } } - - return new DenseVector(data); + if (data.Count == 0) + { + throw new FormatException(); + } + return new DenseVector(data.ToArray()); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 7ee8f8cd..85063f7b 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -37,7 +37,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 using System; using System.Collections.Generic; using System.Diagnostics; - using Threading; /// /// A Matrix class with sparse storage. The underlying storage scheme is 3-array compressed-sparse-row (CSR) Format. diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 0579a4a0..d17f7faf 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -31,7 +31,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { using Generic; - using NumberTheory; using Numerics; using Storage; using System; @@ -941,39 +940,38 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 value = value.Substring(1, value.Length - 2).Trim(); } - // keywords - var textInfo = formatProvider.GetTextInfo(); - var keywords = new[] { textInfo.ListSeparator }; - - // lexing - var tokens = new LinkedList(); - GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0); - var token = tokens.First; - - if (token == null || tokens.Count.IsEven()) - { - throw new FormatException(); - } - // parsing - var data = new Complex32[(tokens.Count + 1) >> 1]; - for (var i = 0; i < data.Length; i++) + var strongTokens = value.Split(new[] { formatProvider.GetTextInfo().ListSeparator }, StringSplitOptions.RemoveEmptyEntries); + var data = new List(); + foreach (string strongToken in strongTokens) { - if (token == null || token.Value == textInfo.ListSeparator) + var weakTokens = strongToken.Split(new[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); + string current = string.Empty; + for (int i = 0; i < weakTokens.Length; i++) { - throw new FormatException(); + current += weakTokens[i]; + if (current.EndsWith("+") || current.EndsWith("-") || current.StartsWith("(") && !current.EndsWith(")")) + { + continue; + } + var ahead = i < weakTokens.Length - 1 ? weakTokens[i + 1] : string.Empty; + if (ahead.StartsWith("+") || ahead.StartsWith("-")) + { + continue; + } + data.Add(current.ToComplex32(formatProvider)); + current = string.Empty; } - - data[i] = token.Value.ToComplex32(formatProvider); - - token = token.Next; - if (token != null) + if (current != string.Empty) { - token = token.Next; + throw new FormatException(); } } - - return new SparseVector(data); + if (data.Count == 0) + { + throw new FormatException(); + } + return new SparseVector(data.ToArray()); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index 81e60bd0..8b0f6e3b 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -32,11 +32,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double { using Distributions; using Generic; - using NumberTheory; using Properties; using Storage; using System; - using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; @@ -852,38 +850,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double value = value.Substring(1, value.Length - 2).Trim(); } - // keywords - var textInfo = formatProvider.GetTextInfo(); - var keywords = new[] { textInfo.ListSeparator }; - - // lexing - var tokens = new LinkedList(); - GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0); - var token = tokens.First; - - if (token == null || tokens.Count.IsEven()) - { - throw new FormatException(); - } - // parsing - var data = new double[(tokens.Count + 1) >> 1]; - for (var i = 0; i < data.Length; i++) - { - if (token == null || token.Value == textInfo.ListSeparator) - { - throw new FormatException(); - } - - data[i] = Double.Parse(token.Value, NumberStyles.Any, formatProvider); - - token = token.Next; - if (token != null) - { - token = token.Next; - } - } - + var tokens = value.Split(new[] {formatProvider.GetTextInfo().ListSeparator, " ", "\t"}, StringSplitOptions.RemoveEmptyEntries); + var data = tokens.Select(t => Double.Parse(t, NumberStyles.Any, formatProvider)).ToArray(); + if (data.Length == 0) throw new FormatException(); return new DenseVector(data); } diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index d34f1c03..9bf9612a 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -36,7 +36,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double using System; using System.Collections.Generic; using System.Diagnostics; - using Threading; /// /// A Matrix class with sparse storage. The underlying storage scheme is 3-array compressed-sparse-row (CSR) Format. diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 7bd49bb2..bc104a21 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -31,7 +31,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double { using Generic; - using NumberTheory; using Storage; using System; using System.Collections.Generic; @@ -974,38 +973,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double value = value.Substring(1, value.Length - 2).Trim(); } - // keywords - var textInfo = formatProvider.GetTextInfo(); - var keywords = new[] { textInfo.ListSeparator }; - - // lexing - var tokens = new LinkedList(); - GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0); - var token = tokens.First; - - if (token == null || tokens.Count.IsEven()) - { - throw new FormatException(); - } - // parsing - var data = new double[(tokens.Count + 1) >> 1]; - for (var i = 0; i < data.Length; i++) - { - if (token == null || token.Value == textInfo.ListSeparator) - { - throw new FormatException(); - } - - data[i] = Double.Parse(token.Value, NumberStyles.Any, formatProvider); - - token = token.Next; - if (token != null) - { - token = token.Next; - } - } - + var tokens = value.Split(new[] { formatProvider.GetTextInfo().ListSeparator, " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); + var data = tokens.Select(t => Double.Parse(t, NumberStyles.Any, formatProvider)).ToArray(); + if (data.Length == 0) throw new FormatException(); return new SparseVector(data); } diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.BCL.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.BCL.cs index 7a6d7501..e758aecd 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.BCL.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.BCL.cs @@ -101,7 +101,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// Returns a that represents the content of this matrix. /// - public string ToMatrixString(int maxRows, int maxColumns, IFormatProvider provider) + public string ToMatrixString(int maxRows, int maxColumns, IFormatProvider provider = null) { return ToMatrixString(maxRows, maxColumns, 12, "G6", provider); } @@ -109,7 +109,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// Returns a that represents the content of this matrix. /// - public string ToMatrixString(int maxRows, int maxColumns, int padding, string format, IFormatProvider provider) + public string ToMatrixString(int maxRows, int maxColumns, int padding, string format = null, IFormatProvider provider = null) { int rowN = RowCount <= maxRows ? RowCount : maxRows < 3 ? maxRows : maxRows - 1; bool rowDots = maxRows < RowCount; @@ -123,10 +123,19 @@ namespace MathNet.Numerics.LinearAlgebra.Generic const string dots = "..."; string pdots = "...".PadLeft(padding); + if (format == null) + { + format = "G8"; + } + var stringBuilder = new StringBuilder(); for (var row = 0; row < rowN; row++) { + if (row > 0) + { + stringBuilder.Append(Environment.NewLine); + } stringBuilder.Append(At(row, 0).ToString(format, provider).PadLeft(padding)); for (var column = 1; column < colN; column++) { @@ -143,11 +152,11 @@ namespace MathNet.Numerics.LinearAlgebra.Generic stringBuilder.Append(At(row, ColumnCount - 1).ToString(format, provider).PadLeft(12)); } } - stringBuilder.Append(Environment.NewLine); } if (rowDots) { + stringBuilder.Append(Environment.NewLine); stringBuilder.Append(pdots); for (var column = 1; column < colN; column++) { @@ -164,11 +173,11 @@ namespace MathNet.Numerics.LinearAlgebra.Generic stringBuilder.Append(pdots); } } - stringBuilder.Append(Environment.NewLine); } if (rowLast) { + stringBuilder.Append(Environment.NewLine); stringBuilder.Append(At(RowCount - 1, 0).ToString(format, provider).PadLeft(padding)); for (var column = 1; column < colN; column++) { @@ -185,7 +194,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic stringBuilder.Append(At(RowCount - 1, ColumnCount - 1).ToString(format, provider).PadLeft(padding)); } } - stringBuilder.Append(Environment.NewLine); } return stringBuilder.ToString(); diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs index 40ca0852..f55e73c7 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.cs @@ -30,14 +30,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { - using System; - using System.Collections.Generic; - using System.Numerics; - using System.Runtime; using Factorization; using Numerics; using Properties; using Storage; + using System; + using System.Collections.Generic; + using System.Numerics; + using System.Runtime; /// /// Defines the base class for Matrix classes. diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs b/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs index c727e601..4a6c31c0 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs @@ -250,7 +250,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// Returns a that represents the content of this vector, row by row. /// - public string ToVectorString(int maxLines, int maxPerLine, IFormatProvider provider) + public string ToVectorString(int maxLines, int maxPerLine, IFormatProvider provider = null) { return ToVectorString(maxLines, maxPerLine, 12, "G6", provider); } @@ -258,7 +258,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// Returns a that represents the content of this vector, row by row. /// - public string ToVectorString(int maxLines, int maxPerLine, int padding, string format, IFormatProvider provider) + public string ToVectorString(int maxLines, int maxPerLine, int padding, string format = null, IFormatProvider provider = null) { int fullLines = Count/maxPerLine; int lastLine = Count%maxPerLine; @@ -274,11 +274,20 @@ namespace MathNet.Numerics.LinearAlgebra.Generic const string separator = " "; string pdots = "...".PadLeft(padding); + if (format == null) + { + format = "G8"; + } + var stringBuilder = new StringBuilder(); var iterator = GetEnumerator(); for (var line = 0; line < fullLines; line++) { + if (line > 0) + { + stringBuilder.Append(Environment.NewLine); + } iterator.MoveNext(); stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding)); for (var column = 1; column < maxPerLine; column++) @@ -287,11 +296,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic iterator.MoveNext(); stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding)); } - stringBuilder.Append(Environment.NewLine); } if (lastLine > 0) { + 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++) @@ -305,7 +317,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic stringBuilder.Append(separator); stringBuilder.Append(pdots); } - stringBuilder.Append(Environment.NewLine); } return stringBuilder.ToString(); diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index 4323a6c0..6d5b28fe 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -32,10 +32,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single { using Distributions; using Generic; - using NumberTheory; using Storage; using System; - using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; @@ -842,38 +840,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single value = value.Substring(1, value.Length - 2).Trim(); } - // keywords - var textInfo = formatProvider.GetTextInfo(); - var keywords = new[] { textInfo.ListSeparator }; - - // lexing - var tokens = new LinkedList(); - GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0); - var token = tokens.First; - - if (token == null || tokens.Count.IsEven()) - { - throw new FormatException(); - } - // parsing - var data = new float[(tokens.Count + 1) >> 1]; - for (var i = 0; i < data.Length; i++) - { - if (token == null || token.Value == textInfo.ListSeparator) - { - throw new FormatException(); - } - - data[i] = float.Parse(token.Value, NumberStyles.Any, formatProvider); - - token = token.Next; - if (token != null) - { - token = token.Next; - } - } - + var tokens = value.Split(new[] { formatProvider.GetTextInfo().ListSeparator, " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); + var data = tokens.Select(t => Single.Parse(t, NumberStyles.Any, formatProvider)).ToArray(); + if (data.Length == 0) throw new FormatException(); return new DenseVector(data); } diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 67346b6b..496539ae 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -36,7 +36,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single using System; using System.Collections.Generic; using System.Diagnostics; - using Threading; /// /// A Matrix class with sparse storage. The underlying storage scheme is 3-array compressed-sparse-row (CSR) Format. diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 8dbb5a05..8c1e1db4 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -30,15 +30,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single { + using Generic; + using Storage; using System; using System.Collections.Generic; + using System.Diagnostics; using System.Globalization; using System.Linq; - using Generic; - using NumberTheory; - using Storage; using Threading; - using System.Diagnostics; /// /// A vector with sparse storage. @@ -978,38 +977,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single value = value.Substring(1, value.Length - 2).Trim(); } - // keywords - var textInfo = formatProvider.GetTextInfo(); - var keywords = new[] { textInfo.ListSeparator }; - - // lexing - var tokens = new LinkedList(); - GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0); - var token = tokens.First; - - if (token == null || tokens.Count.IsEven()) - { - throw new FormatException(); - } - // parsing - var data = new float[(tokens.Count + 1) >> 1]; - for (var i = 0; i < data.Length; i++) - { - if (token == null || token.Value == textInfo.ListSeparator) - { - throw new FormatException(); - } - - data[i] = float.Parse(token.Value, NumberStyles.Any, formatProvider); - - token = token.Next; - if (token != null) - { - token = token.Next; - } - } - + var tokens = value.Split(new[] { formatProvider.GetTextInfo().ListSeparator, " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); + var data = tokens.Select(t => Single.Parse(t, NumberStyles.Any, formatProvider)).ToArray(); + if (data.Length == 0) throw new FormatException(); return new SparseVector(data); } diff --git a/src/Numerics/Statistics/Statistics.cs b/src/Numerics/Statistics/Statistics.cs index fdaf0069..6bff4d0d 100644 --- a/src/Numerics/Statistics/Statistics.cs +++ b/src/Numerics/Statistics/Statistics.cs @@ -301,7 +301,6 @@ namespace MathNet.Numerics.Statistics /// Approximately median-unbiased regardless of the sample distribution (R8). /// /// The data sample sequence. - /// Quantile selector, between 0.0 and 1.0 (inclusive). public static Func QuantileFunc(this IEnumerable data) { if (data == null) throw new ArgumentNullException("data"); diff --git a/src/UnitTests/LinearAlgebraTests/Complex/DenseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Complex/DenseVectorTest.TextHandling.cs index 030c5785..54a4287f 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/DenseVectorTest.TextHandling.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/DenseVectorTest.TextHandling.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex { - using System; - using System.Globalization; using LinearAlgebra.Complex; using NUnit.Framework; + using System; + using System.Globalization; /// /// Dense vector text handling tests. @@ -44,20 +48,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex /// Expected result. [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)")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -66,17 +70,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex /// String to parse. /// Expected result. /// Culture name. - [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")] + [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")] #if !PORTABLE - [TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1);(3,4, 1);(5,6, 1)", "de-DE")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -96,11 +100,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex [TestCase(null)] [TestCase("")] [TestCase(",")] - [TestCase("1,")] - [TestCase(",1")] - [TestCase("1,2,")] - [TestCase(",1,2,")] - [TestCase("1,,2,,3")] [TestCase("1e+")] [TestCase("1e")] [TestCase("()")] diff --git a/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.TextHandling.cs index e8edf63d..128c5f32 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.TextHandling.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.TextHandling.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex { - using System; - using System.Globalization; using LinearAlgebra.Complex; using NUnit.Framework; + using System; + using System.Globalization; /// /// Sparse vector text handling tests. @@ -43,20 +47,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex /// Expected result. [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)")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -65,17 +69,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex /// String to parse. /// Expected result. /// Culture name. - [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")] + [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")] #if !PORTABLE - [TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1);(3,4, 1);(5,6, 1)", "de-DE")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -95,11 +99,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex [TestCase(null)] [TestCase("")] [TestCase(",")] - [TestCase("1,")] - [TestCase(",1")] - [TestCase("1,2,")] - [TestCase(",1,2,")] - [TestCase("1,,2,,3")] [TestCase("1e+")] [TestCase("1e")] [TestCase("()")] diff --git a/src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs index 12c91b7f..9de85582 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,16 +30,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex { + using Distributions; + using LinearAlgebra.Complex; + using LinearAlgebra.Generic; + using NUnit.Framework; using System; using System.Collections; using System.Collections.Generic; - using System.Globalization; using System.Linq; using System.Numerics; - using Distributions; - using LinearAlgebra.Complex; - using LinearAlgebra.Generic; - using NUnit.Framework; /// /// Abstract class with the common set of vector tests. @@ -84,9 +87,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public void CanConvertVectorToString() { var vector = CreateVector(Data); - var str = vector.ToString(); - var sep = CultureInfo.CurrentCulture.TextInfo.ListSeparator; - Assert.AreEqual(string.Format("(1{0} 1){1}(2{0} 1){1}(3{0} 1){1}(4{0} 1){1}(5{0} 1)", ",", sep), str); + var str = vector.ToVectorString(1, int.MaxValue, 1); + Assert.AreEqual("(1, 1) (2, 1) (3, 1) (4, 1) (5, 1)", str); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorTest.TextHandling.cs index af61bc21..02495a7e 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorTest.TextHandling.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorTest.TextHandling.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 { - using System; - using System.Globalization; using LinearAlgebra.Complex32; using NUnit.Framework; + using System; + using System.Globalization; /// /// Dense vector text handling tests. @@ -44,20 +48,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 /// Expected result. [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)")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -66,17 +70,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 /// String to parse. /// Expected result. /// Culture name. - [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")] + [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")] #if !PORTABLE - [TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1);(3,4, 1);(5,6, 1)", "de-DE")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -96,11 +100,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [TestCase(null)] [TestCase("")] [TestCase(",")] - [TestCase("1,")] - [TestCase(",1")] - [TestCase("1,2,")] - [TestCase(",1,2,")] - [TestCase("1,,2,,3")] [TestCase("1e+")] [TestCase("1e")] [TestCase("()")] diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.TextHandling.cs index 13d2638f..c2060a02 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.TextHandling.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.TextHandling.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 { - using System; - using System.Globalization; using LinearAlgebra.Complex32; using NUnit.Framework; + using System; + using System.Globalization; /// /// Sparse vector text handling tests. @@ -43,20 +47,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 /// Expected result. [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)")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -65,17 +69,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 /// String to parse. /// Expected result. /// Culture name. - [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")] + [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")] #if !PORTABLE - [TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1);(3,4, 1);(5,6, 1)", "de-DE")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -95,11 +99,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [TestCase(null)] [TestCase("")] [TestCase(",")] - [TestCase("1,")] - [TestCase(",1")] - [TestCase("1,2,")] - [TestCase(",1,2,")] - [TestCase("1,,2,,3")] [TestCase("1e+")] [TestCase("1e")] [TestCase("()")] diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs index 4eb61a9d..cbe089e5 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,15 +30,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 { - using System; - using System.Collections; - using System.Collections.Generic; - using System.Globalization; - using System.Linq; using Distributions; using LinearAlgebra.Complex32; using LinearAlgebra.Generic; using NUnit.Framework; + using System; + using System.Collections; + using System.Collections.Generic; + using System.Linq; using Complex32 = Numerics.Complex32; /// @@ -84,9 +87,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public void CanConvertVectorToString() { var vector = CreateVector(Data); - var str = vector.ToString(); - var sep = CultureInfo.CurrentCulture.TextInfo.ListSeparator; - Assert.AreEqual(string.Format("(1{0} 1){1}(2{0} 1){1}(3{0} 1){1}(4{0} 1){1}(5{0} 1)", ",", sep), str); + var str = vector.ToVectorString(1, int.MaxValue, 1); + Assert.AreEqual("(1, 1) (2, 1) (3, 1) (4, 1) (5, 1)", str); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/DenseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/DenseMatrixTests.cs index 2b8acebc..39664b1c 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/DenseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/DenseMatrixTests.cs @@ -26,10 +26,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { - using System; - using System.Collections.Generic; using LinearAlgebra.Double; using NUnit.Framework; + using System; + using System.Collections.Generic; /// /// Dense matrix tests. diff --git a/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTest.TextHandling.cs index 1dba194e..b6648ee8 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTest.TextHandling.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTest.TextHandling.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { - using System; - using System.Globalization; using LinearAlgebra.Double; using NUnit.Framework; + using System; + using System.Globalization; /// /// Dense vector text handling tests. @@ -44,16 +48,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double /// Expected result. [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")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -62,15 +66,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double /// String to parse. /// Expected result. /// Culture name. - [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")] - [TestCase(" 1,2;3,4 ; 5,6 ", "1,2;3,4;5,6", "de-DE")] + [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")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -78,17 +82,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double /// /// Vector as string. [TestCase("15")] - [TestCase("1{0}2{1}3{0}4{1}5{0}6")] + [TestCase("1{0}2 3{0}4 5{0}6")] public void CanParseDoubleDenseVectors(string vectorAsString) { var mappedString = String.Format( vectorAsString, - CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, - CultureInfo.CurrentCulture.TextInfo.ListSeparator); + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); var vector = DenseVector.Parse(mappedString); - Assert.AreEqual(mappedString, vector.ToString()); + Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1)); } /// @@ -132,11 +135,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [TestCase(null)] [TestCase("")] [TestCase(",")] - [TestCase("1,")] - [TestCase(",1")] - [TestCase("1,2,")] - [TestCase(",1,2,")] - [TestCase("1,,2,,3")] [TestCase("1e+")] [TestCase("1e")] [TestCase("()")] diff --git a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.TextHandling.cs index 45412891..8da4eb1c 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.TextHandling.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.TextHandling.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { - using System; - using System.Globalization; using LinearAlgebra.Double; using NUnit.Framework; + using System; + using System.Globalization; /// /// Sparse vector text handling tests. @@ -43,16 +47,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double /// Expected result. [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")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -61,15 +65,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double /// String to parse. /// Expected result. /// Culture name. - [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")] - [TestCase(" 1,2;3,4 ; 5,6 ", "1,2;3,4;5,6", "de-DE")] + [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")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -77,17 +81,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double /// /// Vector as string. [TestCase("15")] - [TestCase("1{0}2{1}3{0}4{1}5{0}6")] + [TestCase("1{0}2 3{0}4 5{0}6")] public void CanParseDoubleSparseVectors(string vectorAsString) { var mappedString = String.Format( vectorAsString, - CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, - CultureInfo.CurrentCulture.TextInfo.ListSeparator); + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); var vector = SparseVector.Parse(mappedString); - Assert.AreEqual(mappedString, vector.ToString()); + Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1)); } /// @@ -131,11 +134,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [TestCase(null)] [TestCase("")] [TestCase(",")] - [TestCase("1,")] - [TestCase(",1")] - [TestCase("1,2,")] - [TestCase(",1,2,")] - [TestCase("1,,2,,3")] [TestCase("1e+")] [TestCase("1e")] [TestCase("()")] diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs index 8c27f575..ce1f4db2 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,14 +30,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { - using System; - using System.Collections; - using System.Collections.Generic; - using System.Globalization; using Distributions; using LinearAlgebra.Double; using LinearAlgebra.Generic; using NUnit.Framework; + using System; + using System.Collections; + using System.Collections.Generic; /// /// Abstract class with the common set of vector tests. @@ -82,9 +85,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void CanConvertVectorToString() { var vector = CreateVector(Data); - var str = vector.ToString(); - var sep = CultureInfo.CurrentCulture.TextInfo.ListSeparator; - Assert.AreEqual(string.Format("1{0}2{0}3{0}4{0}5", sep), str); + var str = vector.ToVectorString(1, int.MaxValue, 1); + Assert.AreEqual("1 2 3 4 5", str); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Single/DenseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Single/DenseVectorTest.TextHandling.cs index 0bd4170e..1e03e2c3 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/DenseVectorTest.TextHandling.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/DenseVectorTest.TextHandling.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single { - using System; - using System.Globalization; using LinearAlgebra.Single; using NUnit.Framework; + using System; + using System.Globalization; /// /// Dense vector text handling tests. @@ -44,16 +48,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single /// Expected result. [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")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -62,15 +66,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single /// String to parse. /// Expected result. /// Culture name. - [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")] - [TestCase(" 1,2;3,4 ; 5,6 ", "1,2;3,4;5,6", "de-DE")] + [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")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -78,17 +82,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single /// /// Vector as string. [TestCase("15")] - [TestCase("1{0}2{1}3{0}4{1}5{0}6")] + [TestCase("1{0}2 3{0}4 5{0}6")] public void CanParseSingleDenseVectors(string vectorAsString) { var mappedString = String.Format( vectorAsString, - CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, - CultureInfo.CurrentCulture.TextInfo.ListSeparator); + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); var vector = DenseVector.Parse(mappedString); - Assert.AreEqual(mappedString, vector.ToString()); + Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1, "G")); } /// @@ -132,11 +135,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [TestCase(null)] [TestCase("")] [TestCase(",")] - [TestCase("1,")] - [TestCase(",1")] - [TestCase("1,2,")] - [TestCase(",1,2,")] - [TestCase("1,,2,,3")] [TestCase("1e+")] [TestCase("1e")] [TestCase("()")] diff --git a/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.TextHandling.cs index d8bc2f9d..902ef820 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.TextHandling.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.TextHandling.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single { - using System; - using System.Globalization; using LinearAlgebra.Single; using NUnit.Framework; + using System; + using System.Globalization; /// /// Sparse vector text handling tests. @@ -43,16 +47,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single /// Expected result. [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")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -61,15 +65,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single /// String to parse. /// Expected result. /// Culture name. - [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")] - [TestCase(" 1,2;3,4 ; 5,6 ", "1,2;3,4;5,6", "de-DE")] + [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")] + [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.ToString(formatProvider)); + Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); } /// @@ -77,17 +81,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single /// /// Vector as string. [TestCase("15")] - [TestCase("1{0}2{1}3{0}4{1}5{0}6")] + [TestCase("1{0}2 3{0}4 5{0}6")] public void CanParseSingleSparseVectors(string vectorAsString) { var mappedString = String.Format( vectorAsString, - CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, - CultureInfo.CurrentCulture.TextInfo.ListSeparator); + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); var vector = SparseVector.Parse(mappedString); - Assert.AreEqual(mappedString, vector.ToString()); + Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1, "G")); } /// @@ -131,11 +134,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [TestCase(null)] [TestCase("")] [TestCase(",")] - [TestCase("1,")] - [TestCase(",1")] - [TestCase("1,2,")] - [TestCase(",1,2,")] - [TestCase("1,,2,,3")] [TestCase("1e+")] [TestCase("1e")] [TestCase("()")] diff --git a/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs index 815395da..852e6358 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // 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 // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // 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 @@ -26,14 +30,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single { - using System; - using System.Collections; - using System.Collections.Generic; - using System.Globalization; using Distributions; using LinearAlgebra.Generic; using LinearAlgebra.Single; using NUnit.Framework; + using System; + using System.Collections; + using System.Collections.Generic; /// /// Abstract class with the common set of vector tests. @@ -73,6 +76,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single Assert.AreEqual(vector.Count, clone.Count); CollectionAssert.AreEqual(vector, clone); } +#endif /// /// Can convert vector to string. @@ -81,11 +85,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public void CanConvertVectorToString() { var vector = CreateVector(Data); - var str = vector.ToString(); - var sep = CultureInfo.CurrentCulture.TextInfo.ListSeparator; - Assert.AreEqual(string.Format("1{0}2{0}3{0}4{0}5", sep), str); + var str = vector.ToVectorString(1, int.MaxValue, 1); + Assert.AreEqual("1 2 3 4 5", str); } -#endif /// /// Can copy part of a vector to another vector.