Quansight-Labs/numpy.net

sliced array (using ndarray[":", "::-1"]) index error

rainyl opened this issue · 6 comments

Description

version: 0.9.83.8

Let's see an example directly

using NumpyDotNet;

namespace MyNamespace
{
    public class Pro
    {
        public static void Main()
        {
            var a = np.asarray(new int[,] { {1, 0, 2, 5, 3}, {12, 11, 13, 15, 10} });
            var b = a[":", "::-1"] as ndarray;
            Console.WriteLine(b);
            // correct: 3, 5, 2, 0, 1
            Console.WriteLine($"correct: 3, 5, 2, 0, 1");
            Console.WriteLine($"output: {b[0, 0]}, {b[0, 1]}, {b[0, 2]}, {b[0, 3]}, {b[0, 4]}");
            // correct: 10, 15, 13, 11, 12
            Console.WriteLine($"correct: 10, 15, 13, 11, 12");
            Console.WriteLine($"output: {b[1, 0]}, {b[1, 1]}, {b[1, 2]}, {b[1, 3]}, {b[1, 4]}");
            Console.ReadLine();
        }
    }
}

and the output:

shape=(2, 5), INT32
{ { 3, 5, 2, 0, 1 },
  { 10, 15, 13, 11, 12 } }

correct: 3, 5, 2, 0, 1
output: 3, 5, 3, 3, 3
correct: 10, 15, 13, 11, 12
output: 10, 1, 0, 2, 5

The printed array b seems to be correct, but the indexed values are wrong, I believe you understand what I said.

I took a quick look at this. It is not obvious what is the root cause of the problem.
It is related to the "::-1" slice selector. The internal index logic is not handling it correctly.

As a temporary fix, you can b = b.Copy() to get a version that will work properly.

I am little busy right now, but when I get a chance, I will try to debug this for you.

thanks for your kind reply :)

by the way, i am wondering why you use so many object as parameters and returned values, in my naive opinion, the boxing and unboxing will waste many resources, overloading may be a better solution?

I use object when the python API allows multiple different types of input parameters. Often the API will accept arrays, ndarrays, value types, nulls, etc... The input type often determines the output type. If there was a limited set allowed values then overloading may be acceptable. However if the end result is 25 overloaded functions to cover every possible case then that may not be worth it.

Please provide an example of a function that you think might be better with overloads.

Well, really thanks for your explanation, I am just curious about it.

version 0.9.84.00 fixes this problem. No .copy is necessary

Great! Thanks for your efforts!