Browse Source

LA: Rework ToString for matrices and vectors #100

v2
Christoph Ruegg 14 years ago
parent
commit
a9bd17eb96
  1. 16
      src/Numerics/Control.cs
  2. 10
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  3. 10
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  4. 2
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  5. 10
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  6. 140
      src/Numerics/LinearAlgebra/Generic/Matrix.BCL.cs
  7. 162
      src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs
  8. 10
      src/Numerics/LinearAlgebra/Single/SparseVector.cs

16
src/Numerics/Control.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -54,6 +54,10 @@ namespace MathNet.Numerics
CheckDistributionParameters = true;
ThreadSafeRandomNumberGenerators = true;
// ToString & Formatting
MaxToStringColumns = 6;
MaxToStringRows = 8;
// Parallelization & Threading
_numberOfThreads = Environment.ProcessorCount;
DisableParallelization = _numberOfThreads < 2;
@ -174,5 +178,15 @@ namespace MathNet.Numerics
{
return !DisableParallelization && NumberOfParallelWorkerThreads >= 2 && elements >= ParallelizeElements;
}
/// <summary>
/// Maximum number of columns to print in ToString methods by default.
/// </summary>
public static int MaxToStringColumns { get; set; }
/// <summary>
/// Maximum number of rows to print in ToString methods by default.
/// </summary>
public static int MaxToStringRows { get; set; }
}
}

10
src/Numerics/LinearAlgebra/Complex/SparseVector.cs

@ -1034,15 +1034,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return ret;
}
#endregion
public override string ToString(string format, IFormatProvider formatProvider)
{
if (Count > 20)
{
return String.Format("SparseVectorOfComplex({0},{1},{2})", Count, _storage.ValueCount, GetHashCode());
}
return base.ToString(format, formatProvider);
}
}
}

10
src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

@ -1034,15 +1034,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return ret;
}
#endregion
public override string ToString(string format, IFormatProvider formatProvider)
{
if (Count > 20)
{
return String.Format("SparseVectorOfComplex32({0},{1},{2})", Count, _storage.ValueCount, GetHashCode());
}
return base.ToString(format, formatProvider);
}
}
}

2
src/Numerics/LinearAlgebra/Double/Matrix.cs

@ -30,10 +30,10 @@
namespace MathNet.Numerics.LinearAlgebra.Double
{
using System;
using Generic;
using Properties;
using Storage;
using System;
/// <summary>
/// <c>double</c> version of the <see cref="Matrix{T}"/> class.

10
src/Numerics/LinearAlgebra/Double/SparseVector.cs

@ -1068,15 +1068,5 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
#endregion
public override string ToString(string format, IFormatProvider formatProvider)
{
if (Count > 20)
{
return String.Format("SparseVectorOfDouble({0},{1},{2})", Count, _storage.ValueCount, GetHashCode());
}
return base.ToString(format, formatProvider);
}
}
}

140
src/Numerics/LinearAlgebra/Generic/Matrix.BCL.cs

@ -75,67 +75,147 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return Storage.GetHashCode();
}
#if !PORTABLE
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// A new object that is a copy of this instance.
/// </returns>
public override string ToString()
object ICloneable.Clone()
{
return ToString(null, null);
return Clone();
}
#endif
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// Returns a <see cref="System.String"/> that describes the type, dimensions and shape of this matrix.
/// </summary>
/// <param name="format">
/// The format to use.
/// </param>
/// <param name="formatProvider">
/// The format provider to use.
/// </param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public virtual string ToString(string format, IFormatProvider formatProvider = null)
public virtual string ToTypeString()
{
return string.Format("{0} {1}x{2}-{3}", GetType().Name, RowCount, ColumnCount, typeof(T).Name);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the content of this matrix.
/// </summary>
public string ToMatrixString(int maxRows, int maxColumns, IFormatProvider provider)
{
return ToMatrixString(maxRows, maxColumns, 12, "G6", provider);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the content of this matrix.
/// </summary>
public string ToMatrixString(int maxRows, int maxColumns, int padding, string format, IFormatProvider provider)
{
var separator = (formatProvider.GetTextInfo().ListSeparator);
int rowN = RowCount <= maxRows ? RowCount : maxRows < 3 ? maxRows : maxRows - 1;
bool rowDots = maxRows < RowCount;
bool rowLast = rowDots && maxRows > 2;
int colN = ColumnCount <= maxColumns ? ColumnCount : maxColumns < 3 ? maxColumns : maxColumns - 1;
bool colDots = maxColumns < ColumnCount;
bool colLast = colDots && maxColumns > 2;
const string separator = " ";
const string dots = "...";
string pdots = "...".PadLeft(padding);
var stringBuilder = new StringBuilder();
for (var row = 0; row < RowCount; row++)
for (var row = 0; row < rowN; row++)
{
stringBuilder.Append(At(row, 0).ToString(format, provider).PadLeft(padding));
for (var column = 1; column < colN; column++)
{
stringBuilder.Append(separator);
stringBuilder.Append(At(row, column).ToString(format, provider).PadLeft(padding));
}
if (colDots)
{
stringBuilder.Append(separator);
stringBuilder.Append(dots);
if (colLast)
{
stringBuilder.Append(separator);
stringBuilder.Append(At(row, ColumnCount - 1).ToString(format, provider).PadLeft(12));
}
}
stringBuilder.Append(Environment.NewLine);
}
if (rowDots)
{
for (var column = 0; column < ColumnCount; column++)
stringBuilder.Append(pdots);
for (var column = 1; column < colN; column++)
{
stringBuilder.Append(separator);
stringBuilder.Append(pdots);
}
if (colDots)
{
stringBuilder.Append(At(row, column).ToString(format, formatProvider));
if (column != ColumnCount - 1)
stringBuilder.Append(separator);
stringBuilder.Append(dots);
if (colLast)
{
stringBuilder.Append(separator);
stringBuilder.Append(pdots);
}
}
stringBuilder.Append(Environment.NewLine);
}
if (row != RowCount - 1)
if (rowLast)
{
stringBuilder.Append(At(RowCount - 1, 0).ToString(format, provider).PadLeft(padding));
for (var column = 1; column < colN; column++)
{
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append(separator);
stringBuilder.Append(At(RowCount - 1, column).ToString(format, provider).PadLeft(padding));
}
if (colDots)
{
stringBuilder.Append(separator);
stringBuilder.Append(dots);
if (colLast)
{
stringBuilder.Append(separator);
stringBuilder.Append(At(RowCount - 1, ColumnCount - 1).ToString(format, provider).PadLeft(padding));
}
}
stringBuilder.Append(Environment.NewLine);
}
return stringBuilder.ToString();
}
#if !PORTABLE
/// <summary>
/// Returns a <see cref="System.String"/> that summarizes this matrix.
/// </summary>
public string ToString(int maxRows, int maxColumns, IFormatProvider provider = null)
{
return string.Concat(ToTypeString(), Environment.NewLine, ToMatrixString(maxRows, maxColumns, provider));
}
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// Returns a <see cref="System.String"/> that summarizes this matrix.
/// The maximum number of cells can be configured in the <see cref="Control"/> class.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
object ICloneable.Clone()
public override sealed string ToString()
{
return Clone();
return string.Concat(ToTypeString(), Environment.NewLine, ToMatrixString(Control.MaxToStringRows, Control.MaxToStringColumns, null));
}
#endif
/// <summary>
/// Returns a <see cref="System.String"/> that summarizes this matrix.
/// 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)
{
return string.Concat(ToTypeString(), Environment.NewLine, ToMatrixString(Control.MaxToStringRows, Control.MaxToStringColumns, formatProvider));
}
}
}

162
src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs

@ -77,60 +77,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return Storage.GetHashCode();
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override sealed string ToString()
{
return ToString(null, null);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="formatProvider">
/// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.
/// </param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public string ToString(IFormatProvider formatProvider)
{
return ToString(null, formatProvider);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="format">
/// The format to use.
/// </param>
/// <param name="formatProvider">
/// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.
/// </param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public virtual string ToString(string format, IFormatProvider formatProvider = null)
{
var separator = (formatProvider.GetTextInfo().ListSeparator);
var stringBuilder = new StringBuilder();
for (var index = 0; index < Count; index++)
{
stringBuilder.Append(At(index).ToString(format, formatProvider));
if (index != Count - 1)
{
stringBuilder.Append(separator);
}
}
return stringBuilder.ToString();
}
#if !PORTABLE
/// <summary>
@ -292,5 +238,113 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
{
return Storage.Enumerate().GetEnumerator();
}
/// <summary>
/// Returns a <see cref="System.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)
{
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, IFormatProvider provider)
{
int fullLines = Count/maxPerLine;
int lastLine = Count%maxPerLine;
bool incomplete = false;
if (fullLines > maxLines || fullLines == maxLines && lastLine > 0)
{
fullLines = maxLines - 1;
lastLine = maxPerLine - 1;
incomplete = true;
}
const string separator = " ";
string pdots = "...".PadLeft(padding);
var stringBuilder = new StringBuilder();
var iterator = GetEnumerator();
for (var line = 0; line < fullLines; line++)
{
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
for (var column = 1; column < maxPerLine; column++)
{
stringBuilder.Append(separator);
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
}
stringBuilder.Append(Environment.NewLine);
}
if (lastLine > 0)
{
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
for (var column = 1; column < lastLine; column++)
{
stringBuilder.Append(separator);
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
}
if (incomplete)
{
stringBuilder.Append(separator);
stringBuilder.Append(pdots);
}
stringBuilder.Append(Environment.NewLine);
}
return stringBuilder.ToString();
}
/// <summary>
/// Returns a <see cref="System.String"/> that summarizes this vector.
/// </summary>
public string ToString(int maxLines, int maxPerLine, IFormatProvider provider = null)
{
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(maxLines, maxPerLine, provider));
}
/// <summary>
/// Returns a <see cref="System.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, null));
}
/// <summary>
/// Returns a <see cref="System.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)
{
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(Control.MaxToStringRows, Control.MaxToStringColumns, formatProvider));
}
/// <summary>
/// Returns a <see cref="System.String"/> that summarizes this vector.
/// </summary>
[Obsolete("Scheduled for removal in v3.0.")]
public string ToString(IFormatProvider formatProvider)
{
return ToString(null, formatProvider);
}
}
}

10
src/Numerics/LinearAlgebra/Single/SparseVector.cs

@ -1072,15 +1072,5 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
#endregion
public override string ToString(string format, IFormatProvider formatProvider)
{
if (Count > 20)
{
return String.Format("SparseVectorOfSingle({0},{1},{2})", Count, _storage.ValueCount, GetHashCode());
}
return base.ToString(format, formatProvider);
}
}
}

Loading…
Cancel
Save