Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public void FlattenTo(scoped System.Span<T> destination) { }
public System.Numerics.Tensors.ReadOnlyTensorSpan<T> Slice(params scoped System.ReadOnlySpan<System.Buffers.NRange> ranges) { throw null; }
public System.Numerics.Tensors.ReadOnlyTensorSpan<T> Slice(params scoped System.ReadOnlySpan<nint> startIndexes) { throw null; }
public override string ToString() { throw null; }
public string ToString(params scoped System.ReadOnlySpan<nint> maximumLengths) { throw null; }
public bool TryCopyTo(scoped in System.Numerics.Tensors.TensorSpan<T> destination) { throw null; }
public bool TryFlattenTo(scoped System.Span<T> destination) { throw null; }
public bool TryGetSpan(scoped System.ReadOnlySpan<nint> startIndexes, int length, out System.ReadOnlySpan<T> span) { throw null; }
Expand Down Expand Up @@ -570,9 +571,6 @@ public static void ResizeTo<T>(scoped in System.Numerics.Tensors.Tensor<T> tenso
public static ref readonly System.Numerics.Tensors.TensorSpan<T> TanPi<T>(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan<T> x, in System.Numerics.Tensors.TensorSpan<T> destination) where T : System.Numerics.ITrigonometricFunctions<T> { throw null; }
public static System.Numerics.Tensors.Tensor<T> Tan<T>(in System.Numerics.Tensors.ReadOnlyTensorSpan<T> x) where T : System.Numerics.ITrigonometricFunctions<T> { throw null; }
public static ref readonly System.Numerics.Tensors.TensorSpan<T> Tan<T>(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan<T> x, in System.Numerics.Tensors.TensorSpan<T> destination) where T : System.Numerics.ITrigonometricFunctions<T> { throw null; }
public static string ToString<T>(this in System.Numerics.Tensors.ReadOnlyTensorSpan<T> tensor, System.ReadOnlySpan<nint> maximumLengths) { throw null; }
public static string ToString<T>(this in System.Numerics.Tensors.TensorSpan<T> tensor, System.ReadOnlySpan<nint> maximumLengths) { throw null; }
public static string ToString<T>(this System.Numerics.Tensors.Tensor<T> tensor, System.ReadOnlySpan<nint> maximumLengths) { throw null; }
public static System.Numerics.Tensors.Tensor<T> TrailingZeroCount<T>(in System.Numerics.Tensors.ReadOnlyTensorSpan<T> x) where T : System.Numerics.IBinaryInteger<T> { throw null; }
public static ref readonly System.Numerics.Tensors.TensorSpan<T> TrailingZeroCount<T>(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan<T> x, in System.Numerics.Tensors.TensorSpan<T> destination) where T : System.Numerics.IBinaryInteger<T> { throw null; }
public static System.Numerics.Tensors.Tensor<T> Transpose<T>(System.Numerics.Tensors.Tensor<T> tensor) { throw null; }
Expand Down Expand Up @@ -1218,6 +1216,7 @@ public void FlattenTo(scoped System.Span<T> destination) { }
public System.Numerics.Tensors.TensorSpan<T> Slice(params scoped System.ReadOnlySpan<System.Buffers.NRange> ranges) { throw null; }
public System.Numerics.Tensors.TensorSpan<T> Slice(params scoped System.ReadOnlySpan<nint> startIndexes) { throw null; }
public override string ToString() { throw null; }
public string ToString(params scoped System.ReadOnlySpan<nint> maximumLengths) { throw null; }
public bool TryCopyTo(scoped in System.Numerics.Tensors.TensorSpan<T> destination) { throw null; }
public bool TryFlattenTo(scoped System.Span<T> destination) { throw null; }
public bool TryGetSpan(scoped System.ReadOnlySpan<nint> startIndexes, int length, out System.Span<T> span) { throw null; }
Expand Down Expand Up @@ -1297,6 +1296,7 @@ void System.Numerics.Tensors.ITensor.Fill(object value) { }
static System.Numerics.Tensors.Tensor<T> System.Numerics.Tensors.ITensor<System.Numerics.Tensors.Tensor<T>, T>.CreateFromShapeUninitialized(scoped System.ReadOnlySpan<nint> lengths, bool pinned) { throw null; }
static System.Numerics.Tensors.Tensor<T> System.Numerics.Tensors.ITensor<System.Numerics.Tensors.Tensor<T>, T>.CreateFromShapeUninitialized(scoped System.ReadOnlySpan<nint> lengths, scoped System.ReadOnlySpan<nint> strides, bool pinned) { throw null; }
public System.Numerics.Tensors.Tensor<T> ToDenseTensor() { throw null; }
public override string ToString() { throw null; }
public string ToString(params scoped System.ReadOnlySpan<nint> maximumLengths) { throw null; }
public bool TryCopyTo(scoped in System.Numerics.Tensors.TensorSpan<T> destination) { throw null; }
public bool TryFlattenTo(scoped System.Span<T> destination) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,20 @@ ref Unsafe.Add(ref _reference, linearOffset),
);
}

/// <summary>Returns the string representation of the tensor span.</summary>
/// <returns>The string representation of the tensor span.</returns>
public override string ToString() => $"System.Numerics.Tensors.ReadOnlyTensorSpan<{typeof(T).Name}>[{_shape}]";
/// <summary>Returns the string representation of the tensor.</summary>
/// <returns>The string representation of the tensor.</returns>
/// <remarks>This API only lists the shape of the tensor, it does not include the contents.</remarks>
public override string ToString() => ToString([]);

/// <summary>Creates a <see cref="string"/> representation of the tensor.</summary>
/// <param name="maximumLengths">The maximum number of elements to print for each dimension of the tensor.</param>
/// <returns>A <see cref="string"/> representation of the tensor.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="maximumLengths" /> is not empty and does not contain <see cref="Rank" /> elements.</exception>
/// <remarks>
/// <para>No contents will be printed if <paramref name="maximumLengths" /> is empty.</para>
/// <para>If a given dimension contains more elements then the corresponding limit specified by <paramref name="maximumLengths" />, remaining elements will be represented by <c>..</c>.</para>
/// </remarks>
public string ToString(params scoped ReadOnlySpan<nint> maximumLengths) => Tensor.ToString(this, maximumLengths, "System.Numerics.Tensors.ReadOnlyTensorSpan");

/// <inheritdoc cref="IReadOnlyTensor{TSelf, T}.TryCopyTo(in TensorSpan{T})" />
public bool TryCopyTo(scoped in TensorSpan<T> destination)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2064,98 +2064,147 @@ public static ref readonly TensorSpan<T> StackAlongDimension<T>(scoped ReadOnlyS
#endregion

#region ToString
/// <summary>
/// Creates a <see cref="string"/> representation of the <see cref="TensorSpan{T}"/>."/>
/// </summary>
/// <param name="tensor">The <see cref="TensorSpan{T}"/> you want to represent as a string.</param>
/// <param name="maximumLengths">Maximum Length of each dimension</param>
/// <returns>A <see cref="string"/> representation of the <paramref name="tensor"/></returns>
public static string ToString<T>(this in TensorSpan<T> tensor, ReadOnlySpan<nint> maximumLengths)
=> tensor.AsReadOnlyTensorSpan().ToString(maximumLengths);

/// <summary>
/// Creates a <see cref="string"/> representation of the <see cref="ReadOnlyTensorSpan{T}"/>."/>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tensor">The <see cref="ReadOnlyTensorSpan{T}"/> you want to represent as a string.</param>
/// <param name="maximumLengths">Maximum Length of each dimension</param>
public static string ToString<T>(this in ReadOnlyTensorSpan<T> tensor, ReadOnlySpan<nint> maximumLengths)
internal static string ToString<T>(in ReadOnlyTensorSpan<T> tensor, ReadOnlySpan<nint> maximumLengths, string typeName)
{
if (maximumLengths.Length != tensor.Rank)
if (!maximumLengths.IsEmpty)
{
ThrowHelper.ThrowArgument_DimensionsNotSame(nameof(tensor));
ArgumentOutOfRangeException.ThrowIfNotEqual(maximumLengths.Length, tensor.Rank);
}

StringBuilder sb = new();
ToString(in tensor, maximumLengths, sb);
return sb.ToString();
}
var sb = new StringBuilder(typeName);

internal static void ToString<T>(in ReadOnlyTensorSpan<T> tensor, ReadOnlySpan<nint> maximumLengths, StringBuilder sb, int indentLevel = 0)
{
Debug.Assert(maximumLengths.Length != tensor.Rank);
sb.Append('<');
sb.Append(typeof(T).Name);
sb.Append('>');

sb.Append(' ', indentLevel * 2);
sb.Append('[');
sb.AppendJoin(", ", tensor.Lengths);
sb.Append(']');

if (tensor.Rank != 0)
if (!maximumLengths.IsEmpty)
{
nint length = nint.Max(tensor.Lengths[0], maximumLengths[0]);
sb.AppendLine(" {");

if (tensor.Rank != 1)
if (tensor.Rank == 1)
{
string separator = string.Empty;

for (nint i = 0; i < length; i++)
{
sb.AppendLine(separator);
nint length = nint.Min(tensor.Lengths[0], maximumLengths[0]);
ToString(tensor, length, sb, indentLevel: 1);
sb.AppendLine();
}
else
{
ToString(tensor, maximumLengths, sb);
}

TensorShape tmpShape = TensorShape.Create(tensor.Lengths[1..], tensor.Strides[1..], tensor.IsPinned);
ReadOnlyTensorSpan<T> tmpTensor = new ReadOnlyTensorSpan<T>(ref Unsafe.Add(ref tensor._reference, i * tensor.Strides[0]), tmpShape);
ToString(tmpTensor, maximumLengths[1..], sb, indentLevel + 1);
sb.Append('}');
}
return sb.ToString();
}

separator = ",";
}
private static void ToString<T>(in ReadOnlyTensorSpan<T> tensor, ReadOnlySpan<nint> maximumLengths, StringBuilder sb, int indentLevel = 0)
{
nint length = nint.Min(tensor.Lengths[0], maximumLengths[0]);

if (length != tensor.Lengths[0])
{
sb.AppendLine(separator);
sb.Append(' ', indentLevel * 2);
sb.AppendLine("...");
}
if (indentLevel != 0)
{
if (tensor.Rank != 1)
{
sb.Append(' ', indentLevel * 2);
sb.AppendLine("[");
}
else
{
string separator = " ";
ToString(tensor, length, sb, indentLevel);
return;
}
}

for (nint i = 0; i < length; i++)
{
sb.Append(separator);
sb.Append(Unsafe.Add(ref tensor._reference, i));
separator = ", ";
}
if (length != 0)
{
TensorShape tmpShape = TensorShape.Create(tensor.Lengths[1..], tensor.Strides[1..], tensor.IsPinned);

if (length != tensor.Lengths[0])
{
sb.Append(separator);
sb.Append("...");
}
ReadOnlyTensorSpan<T> tmpTensor = new ReadOnlyTensorSpan<T>(ref tensor._reference, tmpShape);
ToString(tmpTensor, maximumLengths[1..], sb, indentLevel + 1);

sb.Append(separator);
for (nint i = 1; i < length; i++)
{
sb.AppendLine(",");
tmpTensor = new ReadOnlyTensorSpan<T>(ref Unsafe.Add(ref tensor._reference, i * tensor.Strides[0]), tmpShape);
ToString(tmpTensor, maximumLengths[1..], sb, indentLevel + 1);
}

if (length != tensor.Lengths[0])
{
sb.AppendLine(",");
sb.Append(' ', (indentLevel + 1) * 2);
sb.Append("..");
}
sb.AppendLine();
}
else
{
sb.Append(' ', (indentLevel + 1) * 2);
sb.AppendLine("..");
}
sb.Append(' ', indentLevel * 2);

if (indentLevel != 0)
{
sb.Append(']');
}
}

private static void ToString<T>(in ReadOnlyTensorSpan<T> tensor, nint length, StringBuilder sb, int indentLevel)
{
sb.Append(' ', indentLevel * 2);
sb.Append('[');

if (length != 0)
{
sb.Append(tensor._reference);

for (nint i = 1; i < length; i++)
{
sb.Append(", ");
sb.Append(Unsafe.Add(ref tensor._reference, i));
}

if (length != tensor.Lengths[0])
{
sb.Append(", ..");
}
}
else
{
sb.Append("..");
}

sb.Append(']');
}

/// <summary>
/// Creates a <see cref="string"/> representation of the <see cref="Tensor{T}"/>."/>
/// </summary>
/// <param name="tensor">The <see cref="Span{T}"/> you want to represent as a string.</param>
/// <param name="maximumLengths">Maximum Length of each dimension</param>
/// <returns>A <see cref="string"/> representation of the <paramref name="tensor"/></returns>
public static string ToString<T>(this Tensor<T> tensor, ReadOnlySpan<nint> maximumLengths)
=> tensor.AsReadOnlyTensorSpan().ToString(maximumLengths);
private static StringBuilder AppendJoin<T>(this StringBuilder sb, string separator, ReadOnlySpan<T> values)
{
if (values.IsEmpty)
{
return sb;
}

if (values[0] is not null)
{
sb.Append(values[0]);
}

for (int i = 1; i < values.Length; i++)
{
sb.Append(separator);

if (values[i] is not null)
{
sb.Append(values[i]);
}
}
return sb;
}
#endregion

#region Transpose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,11 @@ ref Unsafe.Add(ref _reference, linearOffset),
);
}

/// <inheritdoc cref="ReadOnlyTensorSpan{T}.ToString" />
public override string ToString() => $"System.Numerics.Tensors.TensorSpan<{typeof(T).Name}>[{_shape}]";
/// <inheritdoc cref="ReadOnlyTensorSpan{T}.ToString()" />
public override string ToString() => ToString([]);

/// <inheritdoc cref="ReadOnlyTensorSpan{T}.ToString(ReadOnlySpan{nint})" />
public string ToString(params scoped ReadOnlySpan<nint> maximumLengths) => Tensor.ToString(AsReadOnlyTensorSpan(), maximumLengths, "System.Numerics.Tensors.TensorSpan");

/// <inheritdoc cref="IReadOnlyTensor{TSelf, T}.TryCopyTo(in TensorSpan{T})" />
public bool TryCopyTo(scoped in TensorSpan<T> destination) => AsReadOnlyTensorSpan().TryCopyTo(destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,21 +288,11 @@ public Tensor<T> ToDenseTensor()
/// <inheritdoc cref="IReadOnlyTensor{TSelf, T}.TryGetSpan(ReadOnlySpan{NIndex}, int, out ReadOnlySpan{T})" />
public bool TryGetSpan(scoped ReadOnlySpan<NIndex> startIndexes, int length, out ReadOnlySpan<T> span) => AsReadOnlyTensorSpan().TryGetSpan(startIndexes, length, out span);

/// <summary>
/// Creates a <see cref="string"/> representation of the <see cref="TensorSpan{T}"/>."/>
/// </summary>
/// <param name="maximumLengths">Maximum Length of each dimension</param>
/// <returns>A <see cref="string"/> representation of the <see cref="Tensor{T}"/></returns>
public string ToString(params ReadOnlySpan<nint> maximumLengths)
{
var sb = new StringBuilder($"System.Numerics.Tensors.Tensor<{typeof(T).Name}>[{_shape}]");

sb.AppendLine("{");
Tensor.ToString(AsReadOnlyTensorSpan(), maximumLengths, sb);
sb.AppendLine("}");
/// <inheritdoc cref="ReadOnlyTensorSpan{T}.ToString()" />
public override string ToString() => ToString([]);

return sb.ToString();
}
/// <inheritdoc cref="ReadOnlyTensorSpan{T}.ToString(ReadOnlySpan{nint})" />
public string ToString(params scoped ReadOnlySpan<nint> maximumLengths) => Tensor.ToString(AsReadOnlyTensorSpan(), maximumLengths, "System.Numerics.Tensors.Tensor");

//
// IEnumerable
Expand Down
Loading
Loading