dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.21k stars 4.72k forks source link

System.Numerics.Tensors Should omit `.Reshape` function. #106539

Closed soerenwolfers closed 1 month ago

soerenwolfers commented 2 months ago

The output of this.Reshape currently can expose entries of the backing memory that aren't available on this, which to me seems a fatal flaw.

It feels like .Reshape was added to match numpy functionality (correct me if that's wrong), but it's behaving subtly differently and I don't think there is value in having it over requiring users to create explicit new tensors.

For example,

var a = new TensorSpan<double>(array: [0d, 1, 2, 3]);  // [0, 1, 2, 3]
var b = a.Reshape(lengths: new IntPtr[] { 2, 2 });  // [[0, 1], [2, 3]]
var c = b.Slice(ranges: new NRange[] { ..2, ..1});  // [[0], [2]]
var d = c.Reshape(lengths: new IntPtr[] {2, 1}); // [[0], [1]]!!!
Console.WriteLine(d[new IntPtr[] { 1, 0 }]); // 1

returns 1 whereas

import numpy as np
a = np.array([0, 1, 2, 3])  # [0, 1, 2, 3]
b = a.reshape([2, 2])   # [[0, 1], [2, 3]]
c = b[:2, :1]  # [[0], [2]]
d = c.reshape([2, 1])  # [[0], [2]] 
d[1, 0]  # 2

returns 2.

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @dotnet/area-system-numerics-tensors See info in area-owners.md if you want to be subscribed.