Пример #1
0
        public static IEnumerable <object[]> TestSelectArgs()
        {
            object[] core(NdArrayI source, Expression <Func <int, int> > selector)
            => new object[]
            {
                source, selector
            };

            yield return(core(
                             NdArray.Create(new[, , ]
            {
                { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } },
                { { 12, 13, 14, 15 }, { 16, 17, 18, 19 }, { 20, 21, 22, 23 } },
            }), x => x));

            yield return(core(
                             NdArray.Create(new[, , ]
            {
                { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } },
                { { 13, 14, 15, 16 }, { 17, 18, 19, 20 }, { 21, 22, 23, 24 } },
            }), x => x + 1));

            yield return(core(RandomNdArray.RandInt32(new[] { 2, 3, 4, 5, 6, 7, 8, 9 }), x => x));

            yield return(core(RandomNdArray.RandInt32(new[] { 2, 3, 4, 5, 6, 7, 8, 9 }), x => x + 1));

            yield return(core(RandomNdArray.RandInt32(new[] { 2, 3, 4, 5, 6, 7, 8, 9 }), x => 2 * x));

            yield return(core(RandomNdArray.RandInt32(new[] { 2, 3, 4, 5, 6, 7, 8, 9 }), x => x / 2));
        }
Пример #2
0
        public static IEnumerable <object[]> TestSelectArgs()
        {
            var source = NdArray.Create(new[, , ]
            {
                { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } },
                { { 12, 13, 14, 15 }, { 16, 17, 18, 19 }, { 20, 21, 22, 23 } },
            });

            object[] core(Func <int, int> selector, NdArrayI expected)
            => new object[]
            {
                source, selector, expected
            };

            yield return(core(x => x,
                              NdArray.Create(new [, , ]
            {
                { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } },
                { { 12, 13, 14, 15 }, { 16, 17, 18, 19 }, { 20, 21, 22, 23 } },
            })));

            yield return(core(x => x + 1,
                              NdArray.Create(new[, , ]
            {
                { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } },
                { { 13, 14, 15, 16 }, { 17, 18, 19, 20 }, { 21, 22, 23, 24 } },
            })));
        }
Пример #3
0
 private static NdArray <int> CreateTestSample()
 => NdArray.Create(new[, , ]
 {
     { { 0, 1, }, { 2, 3, }, { 4, 5, }, },
     { { 6, 7, }, { 8, 9, }, { 10, 11, }, },
     { { 12, 13, }, { 14, 15, }, { 16, 17, }, },
     { { 18, 19, }, { 20, 21, }, { 22, 23, }, },
 });
Пример #4
0
        public static IEnumerable <object?[]> TestData()
        {
            var strategies = new IIterationStrategy?[]
            {
                null,
                IterationStrategy.Default,
                ParallelIterationStrategy.Instance,
            };
            var arrays = new NdArray <double>[]
            {
                NdArray.Create(new double[, ] {
                    { 1, 2, 3 },
                    { 4, 5, 6 },
                    { 7, 8, 9 }
                }),
                NdArray.Create(new double[, ] {
                    { 1, 2, 3, 4 },
                    { 5, 6, 7, 8 },
                    { 9, 10, 11, 12 },
                    { 13, 14, 15, 16 }
                }),
                NdArray.Create(new double[, ] {
                    { 10, 4, 2, 10, 5, 4, 4, 1, 1, 2 },
                    { 5, 1, 1, 7, 7, 2, 5, 5, 6, 4 },
                    { 2, 3, 6, 5, 4, 1, 9, 3, 6, 1 },
                    { 9, 3, 3, 8, 3, 5, 6, 6, 6, 5 },
                    { 0, 1, 6, 3, 0, 5, 8, 2, 8, 8 },
                    { 5, 4, 4, 7, 2, 9, 6, 1, 3, 5 },
                    { 8, 2, 6, 4, 1, 5, 8, 3, 3, 3 },
                    { 1, 3, 6, 7, 4, 10, 3, 1, 0, 4 },
                    { 10, 1, 2, 2, 3, 4, 6, 10, 8, 6 },
                    { 3, 8, 9, 4, 1, 3, 6, 10, 4, 1 }
                }),
                NdArray.Create(new double[, ] {
                    { 6, 4, 5, 3, 10, 8, 2, 2, 7, 9 },
                    { 9, 0, 2, 5, 1, 10, 4, 4, 2, 4 },
                    { 0, 1, 3, 6, 1, 2, 8, 4, 9, 1 },
                    { 5, 1, 1, 7, 1, 4, 10, 4, 7, 6 },
                    { 8, 5, 5, 4, 1, 1, 7, 10, 1, 7 },
                    { 3, 3, 6, 2, 3, 9, 6, 6, 8, 7 },
                    { 6, 1, 2, 8, 7, 5, 8, 7, 4, 5 },
                    { 1, 2, 2, 5, 0, 6, 2, 6, 9, 7 },
                    { 3, 7, 1, 8, 9, 5, 4, 1, 8, 1 },
                    { 10, 4, 8, 2, 3, 1, 1, 3, 1, 7 }
                }),
            };

            foreach (var strategy in strategies)
            {
                foreach (var array in arrays)
                {
                    yield return new object?[] { strategy, array }
                }
            }
            ;
        }
Пример #5
0
        /// <summary>
        ///     Evaluates cross operation of 3-dim vector lazily.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static NdArray <T> Cross <T>(this INdArray <T> x, INdArray <T> y)
        {
            Guard.AssertShapeMatch(new[] { 3 }, x.Shape, nameof(x) + "." + nameof(INdArray <T> .Shape));
            Guard.AssertShapeMatch(new[] { 3 }, y.Shape, nameof(y) + "." + nameof(INdArray <T> .Shape));

            var p = Op.Subtract(Op.Multiply(x[1], y[2]), Op.Multiply(x[2], y[1]));
            var q = Op.Subtract(Op.Multiply(x[2], y[0]), Op.Multiply(x[0], y[2]));
            var r = Op.Subtract(Op.Multiply(x[0], y[1]), Op.Multiply(x[1], y[0]));

            return(NdArray.Create(new[] { p, q, r }));
        }
Пример #6
0
        /// <summary>
        ///     Gets a <see cref="NdArray{T}"/> whose values are <c>0 - 1</c> random <see cref="double"/> value.
        /// </summary>
        /// <param name="shape"> [Non-Null] </param>
        /// <param name="gen"></param>
        /// <returns></returns>
        public static NdArray <double> Rand64(int[] shape, RandomGenerator?gen = default)
        {
            Guard.AssertArgumentNotNull(shape, nameof(shape));
            if (gen is null)
            {
                gen = RandomGenerator.Default;
            }

            return(NdArray
                   .Create(gen.NextFloat64(shape.Aggregate((x, y) => x * y)), shape));
        }
Пример #7
0
        private static IEnumerable <object[]> TestCaseForXDim()
        {
            object[] core(int[] axesMap, NdArray <int> from, NdArray <int> to)
            => new object[]
            {
                axesMap, from, to
            };

            yield return(core(
                             new[] { 0 },
                             NdArray.Create(new[] { 0 }),
                             NdArray.Create(new[] { 0 })));

            yield return(core(
                             new[] { 2, 1, 0 },
                             NdArray.Create(new[, , ] {
                {
                    { 000, 001, 002, 003 },
                    { 010, 011, 012, 013 },
                    { 020, 021, 022, 023 },
                },
                {
                    { 100, 101, 102, 103 },
                    { 110, 111, 112, 113 },
                    { 120, 121, 122, 123 },
                },
            }),
                             NdArray.Create(new[, , ] {
                {
                    { 000, 100 },
                    { 010, 110 },
                    { 020, 120 },
                },
                {
                    { 001, 101 },
                    { 011, 111 },
                    { 021, 121 },
                },
                {
                    { 002, 102 },
                    { 012, 112 },
                    { 022, 122 },
                },
                {
                    { 003, 103 },
                    { 013, 113 },
                    { 023, 123 },
                },
            })));
        }
Пример #8
0
        private static IEnumerable <object[]> TestCaseFor2Dim()
        {
            object[] core(NdArray <int> from, NdArray <int> to)
            => new[] { from, to };

            yield return(core(
                             NdArray.Create(new[, ] {
                { 0 }
            }),
                             NdArray.Create(new[, ] {
                { 0 }
            })));

            yield return(core(
                             NdArray.Create(new[, ] {
                { 0, 1 },
                { 2, 3 },
            }),
                             NdArray.Create(new[, ] {
                { 0, 2 },
                { 1, 3 },
            })));

            yield return(core(
                             NdArray.Create(new[, ] {
                { 0, 1, 2 },
                { 3, 4, 5 },
            }),
                             NdArray.Create(new[, ] {
                { 0, 3 },
                { 1, 4 },
                { 2, 5 },
            })));

            yield return(core(
                             NdArray.Create(new[, ] {
                { 0, 1, 2 },
                { 3, 4, 5 },
                { 6, 7, 8 },
            }),
                             NdArray.Create(new[, ] {
                { 0, 3, 6 },
                { 1, 4, 7 },
                { 2, 5, 8 },
            })));

            yield break;
        }
Пример #9
0
    public void Execute()
    {
        var source = NdArray.Create(new double[, , ]
        {
            { { 0.0, 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0, 7.0 }, { 8.0, 9.0, 10.0, 11.0 } },
            {
                { 12.0, 13.0, 14.0, 15.0 }, { 16.0, 17.0, 18.0, 19.0 },
                { 20.0, 21.0, 22.0, 23.0 }
            }
        });

        WriteLine(source[0, 0, 0]);    // 0.0
        WriteLine(source[0, 0, 3]);    // 3.0
        WriteLine(source[0, 2, 0]);    // 8.0
        WriteLine(source[1, 0, 0]);    // 12.0
        WriteLine(source[1, 2, 3]);    // 23.0
        WriteLine(source.GetItem(0));  // 0.0
        WriteLine(source.GetItem(10)); // 10.0
        WriteLine(source.GetItem(20)); // 20.0

        // `NdArray<T>` is available in foreach statement.
        // But it is not `IEnumerable < T >` to prevent unintended use of LINQ to Objects.
        // If you want to handle `NdArray<T>` as `IEnumerable<T>` explicitly,
        // please use `AsEnumerable()`.
        foreach (var x in source)
        {
            WriteLine(x);
        }

        WriteLine(source is IEnumerable <double>);                // false
        WriteLine(source.AsEnumerable() is IEnumerable <double>); // true

        // You can also use `AsEnumerable(int axis)` to iterate along specified axis.
        foreach (var x in source.AsEnumerable(1))
        {
            WriteLine(x);
        }

        /*  NdArray({{ 0,  1,  2,  3},
         *           {12, 13, 14, 15}})
         *  NdArray({{ 4,  5,  6,  7},
         *           {16, 17, 18, 19}})
         *  NdArray({{ 8,  9, 10, 11},
         *           {20, 21, 22, 23}})
         */
        WriteLine(source.AsEnumerable(1) is IEnumerable <NdArray <double> >); // true
    }
Пример #10
0
        public static IEnumerable <object[]> TestData()
        {
            object[] core(NdArray <double> a, double det)
            => new object[]
            {
                a, det
            };

            yield return(core(NdArray.Create(new double[, ] {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
            }), 0));

            yield return(core(NdArray.Create(new double[, ] {
                { 1, 2, 3, 4 },
                { 5, 6, 7, 8 },
                { 9, 10, 11, 12 },
                { 13, 14, 15, 16 }
            }), 0));

            yield return(core(NdArray.Create(new double[, ] {
                { 10, 4, 2, 10, 5, 4, 4, 1, 1, 2 },
                { 5, 1, 1, 7, 7, 2, 5, 5, 6, 4 },
                { 2, 3, 6, 5, 4, 1, 9, 3, 6, 1 },
                { 9, 3, 3, 8, 3, 5, 6, 6, 6, 5 },
                { 0, 1, 6, 3, 0, 5, 8, 2, 8, 8 },
                { 5, 4, 4, 7, 2, 9, 6, 1, 3, 5 },
                { 8, 2, 6, 4, 1, 5, 8, 3, 3, 3 },
                { 1, 3, 6, 7, 4, 10, 3, 1, 0, 4 },
                { 10, 1, 2, 2, 3, 4, 6, 10, 8, 6 },
                { 3, 8, 9, 4, 1, 3, 6, 10, 4, 1 }
            }), 18492754));

            yield return(core(NdArray.Create(new double[, ] {
                { 6, 4, 5, 3, 10, 8, 2, 2, 7, 9 },
                { 9, 0, 2, 5, 1, 10, 4, 4, 2, 4 },
                { 0, 1, 3, 6, 1, 2, 8, 4, 9, 1 },
                { 5, 1, 1, 7, 1, 4, 10, 4, 7, 6 },
                { 8, 5, 5, 4, 1, 1, 7, 10, 1, 7 },
                { 3, 3, 6, 2, 3, 9, 6, 6, 8, 7 },
                { 6, 1, 2, 8, 7, 5, 8, 7, 4, 5 },
                { 1, 2, 2, 5, 0, 6, 2, 6, 9, 7 },
                { 3, 7, 1, 8, 9, 5, 4, 1, 8, 1 },
                { 10, 4, 8, 2, 3, 1, 1, 3, 1, 7 }
            }), -167329180));
        }
Пример #11
0
        public static IEnumerable <object[]> TestData()
        {
            object[] core(NdArray <double> a, NdArray <double> b, NdArray <double> x)
            => new object[]
            {
                a, b, x
            };

            yield return(core(
                             NdArray.Create(new double[, ] {
                { 1, 1, 1, 1 },
                { 1, 1, 1, -1 },
                { 1, 1, -1, 1 },
                { 1, -1, 1, 1 },
            }),
                             NdArray.Create(new double[] { 0, 4, -4, 2 }),
                             NdArray.Create(new double[] { 1, -1, 2, -2 })
                             ));

            yield return(core(
                             NdArray.Create(new double[, ] {
                { 1.0, 1.0, 1.0, 1.0 },
                { 1.0, 1.0, 1.0, -1.0 },
                { 1.0, 1.0, -1.0, 1.0 },
                { 1.0, -1.0, 1.0, 1.0 },
            }),
                             NdArray.Create(new double[, ] {
                { 0.0, 1.0 },
                { 4.0, 2.0 },
                { -4.0, 3.0 },
                { 2.0, 4.0 },
            }),
                             // NdArray.Create(new double[,] { {  0.0,  4.0, -4.0,  2.0 },
                             //                                {  1.0,  2.0,  3.0,  4.0 } }),
                             NdArray.Create(new double[, ] {
                { 1.0, 4.0 },
                { -1.0, -1.5 },
                { 2.0, -1.0 },
                { -2.0, -0.5 },
            })
                             // NdArray.Create(new double[,] { {  1.0, -1.0,  2.0, -2.0 },
                             //                                {  4.0, -1.5, -1.0, -0.5 }, })
                             ));

            yield break;
        }
Пример #12
0
    public void Execute()
    {
        // Creates with 1-D array and shape definition.
        var ndarray1 = NdArray.Create(new double[24], new int[] { 2, 3, 4 });

        WriteLine(ndarray1);

        // Creates with multi-dimension array.
        var ndarray2 = NdArray.Create(new double[2, 3, 4]);

        WriteLine(ndarray2);

        // Basic NdArray is immutable.
        // If you need mutable NdArray, use `CreateMutable` instead of `Create`.
        var ndarray3 = NdArray.CreateMutable(new double[2, 3, 4]);

        WriteLine(ndarray3);

        // You can convert mutable <-> immutable NdArray with `ToImmutable`/`ToMutable`.
        // These methods create copy.
        var ndarray4 = ndarray3.ToImmutable();

        WriteLine(ndarray4);
        var ndarray5 = ndarray1.ToMutable();

        WriteLine(ndarray5);

        // You can also convert mutable -> immutable with `MoveTommutable`.
        // This method moves internal buffer, but does not create copy.
        // Please note this method destroys the source mutable NdArray.
        var ndarray6 = ndarray3.MoveToImmutable();

        WriteLine(ndarray6);

        // If generic data type T has `0`/`1` value, you can use `Zeros`/`Ones`.
        var ndarray7 = NdArray.Zeros <double>(new int[] { 2, 3, 4 });

        WriteLine(ndarray7);
        var ndarray8 = NdArray.Ones <double>(new int[] { 2, 3, 4 });

        WriteLine(ndarray8);
    }
Пример #13
0
        public static IEnumerable <object[]> TestWhereArgs()
        {
            var source = NdArray.Create(new[, , ]
            {
                { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } },
                { { 12, 13, 14, 15 }, { 16, 17, 18, 19 }, { 20, 21, 22, 23 } },
            });

            bool predicate(NdArrayI x) => x[0, 0] == 0;

            object[] core(
                int axis,
                NdArrayI expected)
            => new object[]
            {
                source, axis, (Func <NdArrayI, bool>)predicate, expected
            };

            yield return(core(0,
                              NdArray.Create(new [, , ]
            {
                { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } },
            })));

            yield return(core(1,
                              NdArray.Create(new[, , ]
            {
                { { 0, 1, 2, 3 } },
                { { 12, 13, 14, 15 } },
            })));

            yield return(core(2,
                              NdArray.Create(new[, , ]
            {
                { { 0 }, { 4 }, { 8 } },
                { { 12 }, { 16 }, { 20 } },
            })));
        }
Пример #14
0
        public void Create()
        {
            void doNothing <T>(T value)
            {
            }

            Assert.Equal(new IndexArray(0),
                         NdArray.Create(new int[0]).Shape);
            Assert.Equal(new IndexArray(0, 0),
                         NdArray.Create(new int[0, 0]).Shape);
            Assert.Equal(new IndexArray(0, 0, 0),
                         NdArray.Create(new int[0, 0, 0]).Shape);
            Assert.Equal(new IndexArray(0, 0, 0, 0),
                         NdArray.Create(new int[0, 0, 0, 0]).Shape);
            Assert.Equal(new IndexArray(0, 0, 0, 0, 0),
                         NdArray.Create(new int[0, 0, 0, 0, 0]).Shape);
            Assert.Equal(new IndexArray(0, 0, 0, 0, 0, 0),
                         NdArray.Create(new int[0, 0, 0, 0, 0, 0]).Shape);

            {
                var ndarray = NdArray.Create(new int[] { 0, 1, 2, 3 });
                Assert.Equal(new IndexArray(4), ndarray.Shape);
                Assert.Equal(0, ndarray[0]);
                Assert.Equal(1, ndarray[1]);
                Assert.Equal(2, ndarray[2]);
                Assert.Equal(3, ndarray[3]);
                Assert.Equal(0, ndarray[-4]);
                Assert.Equal(1, ndarray[-3]);
                Assert.Equal(2, ndarray[-2]);
                Assert.Equal(3, ndarray[-1]);
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[4]));
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[0, 0]));
            }
            {
                var ndarray = NdArray.Create(new int[, ] {
                    { 0, 1, 2, 3 }, { 4, 5, 6, 7 }
                });
                Assert.Equal(new IndexArray(2, 4), ndarray.Shape);
                Assert.Equal(0, ndarray[0, 0]);
                Assert.Equal(1, ndarray[0, 1]);
                Assert.Equal(2, ndarray[0, 2]);
                Assert.Equal(3, ndarray[0, 3]);
                Assert.Equal(4, ndarray[1, 0]);
                Assert.Equal(5, ndarray[1, 1]);
                Assert.Equal(6, ndarray[1, 2]);
                Assert.Equal(7, ndarray[1, 3]);
                Assert.Equal(0, ndarray[0, -4]);
                Assert.Equal(1, ndarray[0, -3]);
                Assert.Equal(2, ndarray[0, -2]);
                Assert.Equal(3, ndarray[0, -1]);
                Assert.Equal(4, ndarray[1, -4]);
                Assert.Equal(5, ndarray[1, -3]);
                Assert.Equal(6, ndarray[1, -2]);
                Assert.Equal(7, ndarray[1, -1]);
                Assert.Equal(0, ndarray[-2, 0]);
                Assert.Equal(1, ndarray[-2, 1]);
                Assert.Equal(2, ndarray[-2, 2]);
                Assert.Equal(3, ndarray[-2, 3]);
                Assert.Equal(4, ndarray[-1, 0]);
                Assert.Equal(5, ndarray[-1, 1]);
                Assert.Equal(6, ndarray[-1, 2]);
                Assert.Equal(7, ndarray[-1, 3]);
                Assert.Equal(0, ndarray[-2, -4]);
                Assert.Equal(1, ndarray[-2, -3]);
                Assert.Equal(2, ndarray[-2, -2]);
                Assert.Equal(3, ndarray[-2, -1]);
                Assert.Equal(4, ndarray[-1, -4]);
                Assert.Equal(5, ndarray[-1, -3]);
                Assert.Equal(6, ndarray[-1, -2]);
                Assert.Equal(7, ndarray[-1, -1]);
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[0]));
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[2, 0]));
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[0, 4]));
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[0, 0, 0]));
            }
            {
                var ndarray = NdArray.Create(new int[, , ]
                {
                    { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } },
                    { { 12, 13, 14, 15 }, { 16, 17, 18, 19 }, { 20, 21, 22, 23 } }
                });
                Assert.Equal(new IndexArray(2, 3, 4), ndarray.Shape);
                Assert.Equal(0, ndarray[0, 0, 0]);
                Assert.Equal(1, ndarray[0, 0, 1]);
                Assert.Equal(2, ndarray[0, 0, 2]);
                Assert.Equal(3, ndarray[0, 0, 3]);
                Assert.Equal(4, ndarray[0, 1, 0]);
                Assert.Equal(5, ndarray[0, 1, 1]);
                Assert.Equal(6, ndarray[0, 1, 2]);
                Assert.Equal(7, ndarray[0, 1, 3]);
                Assert.Equal(8, ndarray[0, 2, 0]);
                Assert.Equal(9, ndarray[0, 2, 1]);
                Assert.Equal(10, ndarray[0, 2, 2]);
                Assert.Equal(11, ndarray[0, 2, 3]);
                Assert.Equal(12, ndarray[1, 0, 0]);
                Assert.Equal(13, ndarray[1, 0, 1]);
                Assert.Equal(14, ndarray[1, 0, 2]);
                Assert.Equal(15, ndarray[1, 0, 3]);
                Assert.Equal(16, ndarray[1, 1, 0]);
                Assert.Equal(17, ndarray[1, 1, 1]);
                Assert.Equal(18, ndarray[1, 1, 2]);
                Assert.Equal(19, ndarray[1, 1, 3]);
                Assert.Equal(20, ndarray[1, 2, 0]);
                Assert.Equal(21, ndarray[1, 2, 1]);
                Assert.Equal(22, ndarray[1, 2, 2]);
                Assert.Equal(23, ndarray[1, 2, 3]);
                Assert.Equal(0, ndarray[-2, -3, -4]);
                Assert.Equal(1, ndarray[-2, -3, -3]);
                Assert.Equal(2, ndarray[-2, -3, -2]);
                Assert.Equal(3, ndarray[-2, -3, -1]);
                Assert.Equal(4, ndarray[-2, -2, -4]);
                Assert.Equal(5, ndarray[-2, -2, -3]);
                Assert.Equal(6, ndarray[-2, -2, -2]);
                Assert.Equal(7, ndarray[-2, -2, -1]);
                Assert.Equal(8, ndarray[-2, -1, -4]);
                Assert.Equal(9, ndarray[-2, -1, -3]);
                Assert.Equal(10, ndarray[-2, -1, -2]);
                Assert.Equal(11, ndarray[-2, -1, -1]);
                Assert.Equal(12, ndarray[-1, -3, -4]);
                Assert.Equal(13, ndarray[-1, -3, -3]);
                Assert.Equal(14, ndarray[-1, -3, -2]);
                Assert.Equal(15, ndarray[-1, -3, -1]);
                Assert.Equal(16, ndarray[-1, -2, -4]);
                Assert.Equal(17, ndarray[-1, -2, -3]);
                Assert.Equal(18, ndarray[-1, -2, -2]);
                Assert.Equal(19, ndarray[-1, -2, -1]);
                Assert.Equal(20, ndarray[-1, -1, -4]);
                Assert.Equal(21, ndarray[-1, -1, -3]);
                Assert.Equal(22, ndarray[-1, -1, -2]);
                Assert.Equal(23, ndarray[-1, -1, -1]);
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[0]));
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[0, 0]));
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[2, 0, 0]));
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[0, 3, 0]));
                Assert.Throws <ArgumentOutOfRangeException>(() => doNothing(ndarray[0, 0, 4]));
                Assert.Throws <ArgumentOutOfRangeException>(() =>
                                                            doNothing(ndarray[0, 0, 0, 0]));
            }
        }
Пример #15
0
        public static IEnumerable <object[]> TestSelectByAxesArgs()
        {
            var source = NdArray.Create(new[, , ]
            {
                { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } },
                { { 12, 13, 14, 15 }, { 16, 17, 18, 19 }, { 20, 21, 22, 23 } },
            });

            object[] core(int[] projectionAxes, Func <NdArrayI, int> selector, NdArrayI expected)
            => new object[]
            {
                source, projectionAxes, selector, expected
            };

            yield return(core(new[] { 0 }, x => x[0],
                              NdArray.Create(new[, ]
            {
                { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 },
            })));

            yield return(core(new[] { 1 }, x => x[0],
                              NdArray.Create(new[, ]
            {
                { 0, 1, 2, 3 },
                { 12, 13, 14, 15 },
            })));

            yield return(core(new[] { 2 }, x => x[0],
                              NdArray.Create(new[, ]
            {
                { 0, 4, 8 },
                { 12, 16, 20 },
            })));

            yield return(core(new[] { 0, 1 }, x => x[0, 0],
                              NdArray.Create(new[]
            {
                0, 1, 2, 3
            })));

            yield return(core(new[] { 0, 1 }, x => x[0, 0],
                              NdArray.Create(new[]
            {
                0, 1, 2, 3
            })));

            yield return(core(new[] { 1, 2 }, x => x[0, 0],
                              NdArray.Create(new[]
            {
                0, 12
            })));

            yield return(core(new[] { 2, 1 }, x => x[0, 0],
                              NdArray.Create(new[]
            {
                0, 12
            })));

            yield return(core(new[] { 0, 2 }, x => x[0, 0],
                              NdArray.Create(new[]
            {
                0, 4, 8
            })));

            yield return(core(new[] { 2, 0 }, x => x[0, 0],
                              NdArray.Create(new[]
            {
                0, 4, 8
            })));
        }
Пример #16
0
        public void Zip()
        {
            var A = NdArray.Create(new double[, , ]
            {
                { { 0, 1, 2, 3 }, { 4, 5, 6, 7 } },
                { { 8, 9, 10, 11 }, { 12, 13, 14, 15 } }
            });
            var B = NdArray.Create(new double[, , ]
            {
                { { 16, 17, 18, 19 }, { 20, 21, 22, 23 } },
                { { 24, 25, 26, 27 }, { 28, 29, 30, 31 } }
            });
            var addAns = NdArray.Create(new double[, , ]
            {
                {
                    { 0 + 16, 1 + 17, 2 + 18, 3 + 19 }, { 4 + 20, 5 + 21, 6 + 22, 7 + 23 }
                },
                {
                    { 8 + 24, 9 + 25, 10 + 26, 11 + 27 },
                    { 12 + 28, 13 + 29, 14 + 30, 15 + 31 }
                }
            });
            var subAns = NdArray.Create(new double[, , ]
            {
                {
                    { 0 - 16, 1 - 17, 2 - 18, 3 - 19 }, { 4 - 20, 5 - 21, 6 - 22, 7 - 23 }
                },
                {
                    { 8 - 24, 9 - 25, 10 - 26, 11 - 27 },
                    { 12 - 28, 13 - 29, 14 - 30, 15 - 31 }
                }
            });
            var mulAns = NdArray.Create(new double[, , ]
            {
                {
                    { 0 * 16, 1 * 17, 2 * 18, 3 * 19 }, { 4 * 20, 5 * 21, 6 * 22, 7 * 23 }
                },
                {
                    { 8 * 24, 9 * 25, 10 * 26, 11 * 27 },
                    { 12 * 28, 13 * 29, 14 * 30, 15 * 31 }
                }
            });
            var divAns = NdArray.Create(new double[, , ]
            {
                {
                    { 0.0 / 16, 1.0 / 17, 2.0 / 18, 3.0 / 19 },
                    { 4.0 / 20, 5.0 / 21, 6.0 / 22, 7.0 / 23 }
                },
                {
                    { 8.0 / 24, 9.0 / 25, 10.0 / 26, 11.0 / 27 },
                    { 12.0 / 28, 13.0 / 29, 14.0 / 30, 15.0 / 31 }
                }
            });

            Assert.Equal(addAns, A.Zip(B, (a, b) => a + b));
            Assert.Equal(subAns, A.Zip(B, (a, b) => a - b));
            Assert.Equal(mulAns, A.Zip(B, (a, b) => a * b));
            Assert.Equal(divAns, A.Zip(B, (a, b) => a / b));
            Assert.Equal(addAns, (A, B).Zip((a, b) => a + b));
            Assert.Equal(subAns, (A, B).Zip((a, b) => a - b));
            Assert.Equal(mulAns, (A, B).Zip((a, b) => a * b));
            Assert.Equal(divAns, (A, B).Zip((a, b) => a / b));
        }
Пример #17
0
        public static IEnumerable <object[]> TestData()
        {
            object[] core(NdArray <double> a, NdArray <double> inv)
            => new object[]
            {
                a, inv
            };

            yield return(core(
                             NdArray.Create(new[, ] {
                { 1.0, 1.0, 1.0 },
                { 2.0, 1.0, 1.0 },
                { 2.0, 2.0, 1.0 },
            }),
                             NdArray.Create(new[, ] {
                { -1.0, 1.0, 0.0 },
                { 0.0, -1.0, 1.0 },
                { 2.0, 0.0, -1.0 },
            })
                             ));

            yield return(core(
                             NdArray.Create(new[, ] {
                { 1.0, 2.0, 3.0, 4.0 },
                { 2.0, 2.0, 3.0, 4.0 },
                { 3.0, 3.0, 3.0, 4.0 },
                { 4.0, 4.0, 4.0, 4.0 }
            }),
                             NdArray.Create(new[, ] {
                { -1.0, 1.0, 0.0, 0.0 },
                { 1.0, -2.0, 1.0, 0.0 },
                { 0.0, 1.0, -2.0, 1.0 },
                { 0.0, 0.0, 1.0, -0.75 }
            })
                             ));

            yield return(core(
                             NdArray.Create(new[, ] {
                { 1.0, 2.0, 3.0, 4.0, 5.0 },
                { 2.0, 2.0, 3.0, 4.0, 5.0 },
                { 3.0, 3.0, 3.0, 4.0, 5.0 },
                { 4.0, 4.0, 4.0, 4.0, 5.0 },
                { 5.0, 5.0, 5.0, 5.0, 5.0 }
            }),
                             NdArray.Create(new[, ] {
                { -1.0, 1.0, 0.0, 0.0, 0.0 },
                { 1.0, -2.0, 1.0, 0.0, 0.0 },
                { 0.0, 1.0, -2.0, 1.0, 0.0 },
                { 0.0, 0.0, 1.0, -2.0, 1.0 },
                { 0.0, 0.0, 0.0, 1.0, -0.8 }
            })
                             ));

            yield return(core(
                             NdArray.Create(new[, ] {
                { 5.0, 5.0, 5.0, 5.0, 5.0 },
                { 5.0, 4.0, 4.0, 4.0, 4.0 },
                { 5.0, 4.0, 3.0, 3.0, 3.0 },
                { 5.0, 4.0, 3.0, 2.0, 2.0 },
                { 5.0, 4.0, 3.0, 2.0, 1.0 }
            }),
                             NdArray.Create(new[, ] {
                { -0.8, 1.0, 0.0, 0.0, 0.0 },
                { 1.0, -2.0, 1.0, 0.0, 0.0 },
                { 0.0, 1.0, -2.0, 1.0, 0.0 },
                { 0.0, 0.0, 1.0, -2.0, 1.0 },
                { 0.0, 0.0, 0.0, 1.0, -1.0 }
            })
                             ));
        }
Пример #18
0
    public void Execute()
    {
        // These operator is provided in `NeodymiumDotNet.Linq`.
        // They are not operators of LINQ to Objects.

        var source1 = NdArray.Create(new int[, , ]
        {
            { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } },
            { { 12, 13, 14, 15 }, { 16, 17, 18, 19 }, { 20, 21, 22, 23 } }
        });
        var source2 = NdArray.Create(new int[, , ]
        {
            { { 0, -1, 2, -3 }, { 4, -5, 6, -7 }, { 8, -9, 10, -11 } },
            { { 12, -13, 14, -15 }, { 16, -17, 18, -19 }, { 20, -21, 22, -23 } }
        });

        // You can project each element with `Select`.
        var result1 = source1.Select(x => x * 3);

        WriteLine(result1);

        /*  result1 = NdArray({{{ 0,  3,  6,  9},
         *                      {12, 15, 18, 21},
         *                      {24, 27, 30, 33}},
         *                     {{36, 39, 42, 45},
         *                      {48, 51, 54, 57},
         *                      {60, 63, 66, 69}}})
         */

        // You can also project axes-based partial array with `Select`.
        var result2 = source1.Select(new[] { 0 }, x => x.Sum());

        WriteLine(result2);

        /*  x = NdArray({ 0, 12,}),
         *      NdArray({ 1, 13,}),
         *      ...,
         *      NdArray({11, 23,})
         *  result2 = NdArray({{12, 14, 16, 18},
         *                     {20, 22, 24, 26},
         *                     {28, 30, 32, 34}})
         */
        var result3 = source1.Select(new[] { 0, 1 }, x => x.Sum());

        WriteLine(result3);

        /*  x = NdArray({{ 0,  4,  8}, {12, 16, 20}}),
         *      NdArray({{ 1,  5,  9}, {13, 17, 21}}),
         *      NdArray({{ 2,  6, 10}, {14, 18, 22}}),
         *      NdArray({{ 3,  7, 11}, {15, 19, 23}})
         *  result3 = NdArray({60, 66, 72, 78})
         */

        // If you want to apply calculation for each elements of 2 or more NdArrays,
        // please use `Zip`.
        var result4 = source1.Zip(source2, (x, y) => x + y);

        WriteLine(result4);

        /*  result4 = NdArray({{{ 0,  0,  4,  0},
         *                      { 8,  0, 12,  0},
         *                      {16,  0, 20,  0}},
         *                     {{24,  0, 28,  0},
         *                      {32,  0, 36,  0},
         *                      {40,  0, 44,  0}}})
         */
        // You can also code following expression.
        var result4_alt = (source1, source2).Zip((x, y) => x + y);

        // You can filter along an axis with `Where`.
        // NOTE: `Where` operator needs a single axis due to maintain rank between source and result.
        var result5 = source1.Where(filterAxis: 0, x => x.Sum() < 70);

        WriteLine(result5);

        /*  x = NdArray({{  0,  1,  2,  3}, { 4,  5,  6,  7}, { 8,  9, 10, 11}}),
         *      NdArray({{ 12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23}})
         *  result5 = NdArray({{{  0,  1,  2,  3}, { 4,  5,  6,  7}, { 8,  9, 10, 11}}})
         */
        var result6 = source1.Where(filterAxis: 2, x => x.Sum() % 12 == 0);

        WriteLine(result6);

        /*  x = NdArray({{ 0,  4,  8}, {12, 16, 20}}),
         *      NdArray({{ 1,  5,  9}, {13, 17, 21}}),
         *      NdArray({{ 2,  6, 10}, {14, 18, 22}}),
         *      NdArray({{ 3,  7, 11}, {15, 19, 23}})
         *  result6 = NdArray({{{ 0,  2},
         *                      { 4,  6},
         *                      { 8, 10}},
         *                     {{12, 14},
         *                      {16, 18},
         *                      {20, 22}}})
         */
    }
Пример #19
0
        /// <summary>
        ///     Entity of .npy binary loading, excluding header loading.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="header"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        private static NdArray <T> LoadCore <T>(NpyHeader header, Stream stream)
        {
            NotSupportedException unsupportedFileFormatError()
            => new NotSupportedException(".npy binary is unsupported format.");

            InvalidCastException typeMismatchError()
            => new InvalidCastException("Generic type is mismatch with .npy binary.");

            var byteSize = header.NumpyType.ByteSize ?? 0;

            if (byteSize == 0)
            {
                throw unsupportedFileFormatError();
            }
            var totalLength = header.Shape.TotalLength;
            var bufferSize  = byteSize * totalLength;

            T[] core <T2>(Func <int, T2> readAt)
            {
                if (typeof(T) != typeof(T2))
                {
                    throw typeMismatchError();
                }
                T2[] retvalArray = new T2[totalLength];
                for (var i = 0; i < totalLength; ++i)
                {
                    retvalArray[i] = readAt(i);
                }
                return(retvalArray as T[]);
            }

            var shape  = header.Shape;
            var buffer = new byte[bufferSize];

            stream.Read(buffer, 0, bufferSize);
            var converter = header.NumpyType.Endian == Endian.Little
                ? LittleEndiannessBitConverter.Instance
                : BigEndiannessBitConverter.Instance;

            switch (header.NumpyType.TypeKind)
            {
            case TypeKind.Boolean:
                return(NdArray.Create(core(i => buffer[i] > 0), shape));

            case TypeKind.UInt8:
                return(NdArray.Create(core(i => buffer[i]), shape));

            case TypeKind.UInt16:
                return(NdArray.Create(core(i => converter.ReadPrimitive <ushort>(buffer.AsSpan(2 * i))),
                                      shape));

            case TypeKind.UInt32:
                return(NdArray.Create(core(i => converter.ReadPrimitive <uint>(buffer.AsSpan(4 * i))),
                                      shape));

            case TypeKind.UInt64:
                return(NdArray.Create(core(i => converter.ReadPrimitive <ulong>(buffer.AsSpan(8 * i))),
                                      shape));

            case TypeKind.Int8:
                return(NdArray.Create(core(i => (sbyte)buffer[i]), shape));

            case TypeKind.Int16:
                return(NdArray.Create(core(i => converter.ReadPrimitive <short>(buffer.AsSpan(2 * i))),
                                      shape));

            case TypeKind.Int32:
                return(NdArray.Create(core(i => converter.ReadPrimitive <int>(buffer.AsSpan(4 * i))),
                                      shape));

            case TypeKind.Int64:
                return(NdArray.Create(core(i => converter.ReadPrimitive <long>(buffer.AsSpan(8 * i))),
                                      shape));

            case TypeKind.Float16:
                throw new NotImplementedException();

            case TypeKind.Float32:
                return(NdArray.Create(core(i => converter.ReadPrimitive <float>(buffer.AsSpan(4 * i))),
                                      shape));

            case TypeKind.Float64:
                return(NdArray.Create(core(i => converter.ReadPrimitive <double>(buffer.AsSpan(8 * i))),
                                      shape));

            case TypeKind.Unicode:
                return
                    (NdArray.Create(core(i => Encoding.UTF32.GetString(buffer, byteSize * i, byteSize)),
                                    shape));

            default:
                throw unsupportedFileFormatError();
            }
        }
        public static IEnumerable <object[]> TestElementFormatArgs()
        {
            yield return(new object[]
            {
                NdArray.Create(new[] { 0, 1, 2, 3 }),
                "NdArray({0000, 0001, 0002, 0003})"
            });

            yield return(new object[]
            {
                NdArray.Create(new[, ] {
                    { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }
                }),
                "NdArray({{0000, 0001, 0002, 0003}," + _NewLine +
                "         {0004, 0005, 0006, 0007}," + _NewLine +
                "         {0008, 0009, 0010, 0011}})"
            });

            yield return(new object[]
            {
                NdArray.Create(new[, ]
                {
                    {
                        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                        15, 16, 17, 18, 19
                    },
                    {
                        20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
                        34, 35, 36, 37, 38, 39
                    },
                    {
                        40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
                        54, 55, 56, 57, 58, 59
                    },
                    {
                        60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
                        74, 75, 76, 77, 78, 79
                    },
                    {
                        80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
                        94, 95, 96, 97, 98, 99
                    },
                    {
                        100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
                        114, 115, 116, 117, 118, 119
                    }
                }),
                "NdArray({{0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007, 0008, 0009, 0010," +
                _NewLine +
                "          0011, 0012, 0013, 0014, 0015, 0016, 0017, 0018, 0019}," + _NewLine +
                "         {0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027, 0028, 0029, 0030," +
                _NewLine +
                "          0031, 0032, 0033, 0034, 0035, 0036, 0037, 0038, 0039}," + _NewLine +
                "         {0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047, 0048, 0049, 0050," +
                _NewLine +
                "          0051, 0052, 0053, 0054, 0055, 0056, 0057, 0058, 0059}," + _NewLine +
                "         {0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, 0068, 0069, 0070," +
                _NewLine +
                "          0071, 0072, 0073, 0074, 0075, 0076, 0077, 0078, 0079}," + _NewLine +
                "         {0080, 0081, 0082, 0083, 0084, 0085, 0086, 0087, 0088, 0089, 0090," +
                _NewLine +
                "          0091, 0092, 0093, 0094, 0095, 0096, 0097, 0098, 0099}," + _NewLine +
                "         {0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107, 0108, 0109, 0110," +
                _NewLine +
                "          0111, 0112, 0113, 0114, 0115, 0116, 0117, 0118, 0119}})"
            });

            yield return(new object[]
            {
                NdArray.Zeros <int>(new int[] { 30, 30 }),
                "NdArray({{0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         ...," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}," + _NewLine +
                "         {0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, ..., 0000," +
                _NewLine +
                "          0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000}})"
            });
        }
        public static IEnumerable <object[]> TestBasicArgs()
        {
            yield return(new object[]
            {
                NdArray.Create(new[] { 0, 1, 2, 3 }),
                "NdArray({0, 1, 2, 3})"
            });

            yield return(new object[]
            {
                NdArray.Create(new[, ] {
                    { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }
                }),
                "NdArray({{ 0,  1,  2,  3}," + _NewLine +
                "         { 4,  5,  6,  7}," + _NewLine +
                "         { 8,  9, 10, 11}})"
            });

            yield return(new object[]
            {
                NdArray.Create(new[, ]
                {
                    {
                        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                        15, 16, 17, 18, 19
                    },
                    {
                        20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
                        34, 35, 36, 37, 38, 39
                    },
                    {
                        40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
                        54, 55, 56, 57, 58, 59
                    },
                    {
                        60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
                        74, 75, 76, 77, 78, 79
                    },
                    {
                        80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
                        94, 95, 96, 97, 98, 99
                    },
                    {
                        100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
                        114, 115, 116, 117, 118, 119
                    }
                }),
                "NdArray({{  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13," +
                _NewLine +
                "           14,  15,  16,  17,  18,  19}," + _NewLine +
                "         { 20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,  32,  33," +
                _NewLine +
                "           34,  35,  36,  37,  38,  39}," + _NewLine +
                "         { 40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53," +
                _NewLine +
                "           54,  55,  56,  57,  58,  59}," + _NewLine +
                "         { 60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,  72,  73," +
                _NewLine +
                "           74,  75,  76,  77,  78,  79}," + _NewLine +
                "         { 80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93," +
                _NewLine +
                "           94,  95,  96,  97,  98,  99}," + _NewLine +
                "         {100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113," +
                _NewLine +
                "          114, 115, 116, 117, 118, 119}})"
            });

            yield return(new object[]
            {
                NdArray.Zeros <int>(new int[] { 30, 30 }),
                "NdArray({{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         ...," + _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}," +
                _NewLine +
                "         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}})"
            });
        }
Пример #22
0
        public void Slice()
        {
            void core(NdArray <int> input, IndexOrRange[] indices, NdArray <int> ans)
            => Assert.Equal(ans, input[indices]);

            core(NdArray.Create(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }),
                 new IndexOrRange[] { Range.Create(0, 8) },
                 NdArray.Create(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 })
                 );
            core(NdArray.Create(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }),
                 new IndexOrRange[] { Range.Create(0, 4) },
                 NdArray.Create(new int[] { 0, 1, 2, 3 })
                 );
            core(NdArray.Create(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }),
                 new IndexOrRange[] { Range.Create(0, -4) },
                 NdArray.Create(new int[] { 0, 1, 2, 3 })
                 );
            core(NdArray.Create(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }),
                 new IndexOrRange[] { Range.Create(0, 8, 2) },
                 NdArray.Create(new int[] { 0, 2, 4, 6 })
                 );

            core(NdArray.Create(new int[, ] {
                { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13, 14 }, { 15, 16, 17, 18, 19 }
            }),
                 new IndexOrRange[] { Range.Create(0, 4), Range.Create(0, 5) },
                 NdArray.Create(new int[, ]
            {
                { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13, 14 },
                { 15, 16, 17, 18, 19 }
            })
                 );
            core(NdArray.Create(new int[, ] {
                { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13, 14 }, { 15, 16, 17, 18, 19 }
            }),
                 new IndexOrRange[] { Range.Create(1, 3), Range.Create(1, 4) },
                 NdArray.Create(new int[, ] {
                { 6, 7, 8 }, { 11, 12, 13 }
            })
                 );
            core(NdArray.Create(new int[, ] {
                { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13, 14 }, { 15, 16, 17, 18, 19 }
            }),
                 new IndexOrRange[] { Range.Create(1, -1), Range.Create(1, -1) },
                 NdArray.Create(new int[, ] {
                { 6, 7, 8 }, { 11, 12, 13 }
            })
                 );
            core(NdArray.Create(new int[, ] {
                { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13, 14 }, { 15, 16, 17, 18, 19 }
            }),
                 new IndexOrRange[] { Range.Create(1, 4, 2), Range.Create(1, 4, 2) },
                 NdArray.Create(new int[, ] {
                { 6, 8 }, { 16, 18 }
            })
                 );

            core(NdArray.Create(new int[, , ]
            {
                {
                    { 0, 1, 2, 3, 4, 5 }, { 10, 11, 12, 13, 14, 15 },
                    { 20, 21, 22, 23, 24, 25 }, { 30, 31, 32, 33, 34, 35 },
                    { 40, 41, 42, 43, 44, 45 }
                },
                {
                    { 100, 101, 102, 103, 104, 105 }, { 110, 111, 112, 113, 114, 115 },
                    { 120, 121, 122, 123, 124, 125 }, { 130, 131, 132, 133, 134, 135 },
                    { 140, 141, 142, 143, 144, 145 }
                },
                {
                    { 200, 201, 202, 203, 204, 205 }, { 210, 211, 212, 213, 214, 215 },
                    { 220, 221, 222, 223, 224, 225 }, { 230, 231, 232, 233, 234, 235 },
                    { 240, 241, 242, 243, 244, 245 }
                },
                {
                    { 300, 301, 302, 303, 304, 305 }, { 310, 311, 312, 313, 314, 315 },
                    { 320, 321, 322, 323, 324, 325 }, { 330, 331, 332, 333, 334, 335 },
                    { 340, 341, 342, 343, 344, 345 }
                },
            }),
                 new IndexOrRange[]
                 { Range.Create(0, 4), Range.Create(0, 5), Range.Create(0, 6) },
                 NdArray.Create(new int[, , ]
            {
                {
                    { 0, 1, 2, 3, 4, 5 }, { 10, 11, 12, 13, 14, 15 },
                    { 20, 21, 22, 23, 24, 25 }, { 30, 31, 32, 33, 34, 35 },
                    { 40, 41, 42, 43, 44, 45 }
                },
                {
                    { 100, 101, 102, 103, 104, 105 }, { 110, 111, 112, 113, 114, 115 },
                    { 120, 121, 122, 123, 124, 125 }, { 130, 131, 132, 133, 134, 135 },
                    { 140, 141, 142, 143, 144, 145 }
                },
                {
                    { 200, 201, 202, 203, 204, 205 }, { 210, 211, 212, 213, 214, 215 },
                    { 220, 221, 222, 223, 224, 225 }, { 230, 231, 232, 233, 234, 235 },
                    { 240, 241, 242, 243, 244, 245 }
                },
                {
                    { 300, 301, 302, 303, 304, 305 }, { 310, 311, 312, 313, 314, 315 },
                    { 320, 321, 322, 323, 324, 325 }, { 330, 331, 332, 333, 334, 335 },
                    { 340, 341, 342, 343, 344, 345 }
                },
            })
                 );
            core(NdArray.Create(new int[, , ]
            {
                {
                    { 0, 1, 2, 3, 4, 5 }, { 10, 11, 12, 13, 14, 15 },
                    { 20, 21, 22, 23, 24, 25 }, { 30, 31, 32, 33, 34, 35 },
                    { 40, 41, 42, 43, 44, 45 }
                },
                {
                    { 100, 101, 102, 103, 104, 105 }, { 110, 111, 112, 113, 114, 115 },
                    { 120, 121, 122, 123, 124, 125 }, { 130, 131, 132, 133, 134, 135 },
                    { 140, 141, 142, 143, 144, 145 }
                },
                {
                    { 200, 201, 202, 203, 204, 205 }, { 210, 211, 212, 213, 214, 215 },
                    { 220, 221, 222, 223, 224, 225 }, { 230, 231, 232, 233, 234, 235 },
                    { 240, 241, 242, 243, 244, 245 }
                },
                {
                    { 300, 301, 302, 303, 304, 305 }, { 310, 311, 312, 313, 314, 315 },
                    { 320, 321, 322, 323, 324, 325 }, { 330, 331, 332, 333, 334, 335 },
                    { 340, 341, 342, 343, 344, 345 }
                },
            }),
                 new IndexOrRange[]
                 { Range.Create(1, 3), Range.Create(1, 4), Range.Create(1, 5) },
                 NdArray.Create(new int[, , ]
            {
                { { 111, 112, 113, 114 }, { 121, 122, 123, 124 }, { 131, 132, 133, 134 } },
                { { 211, 212, 213, 214 }, { 221, 222, 223, 224 }, { 231, 232, 233, 234 } },
            })
                 );
            core(NdArray.Create(new int[, , ]
            {
                {
                    { 0, 1, 2, 3, 4, 5 }, { 10, 11, 12, 13, 14, 15 },
                    { 20, 21, 22, 23, 24, 25 }, { 30, 31, 32, 33, 34, 35 },
                    { 40, 41, 42, 43, 44, 45 }
                },
                {
                    { 100, 101, 102, 103, 104, 105 }, { 110, 111, 112, 113, 114, 115 },
                    { 120, 121, 122, 123, 124, 125 }, { 130, 131, 132, 133, 134, 135 },
                    { 140, 141, 142, 143, 144, 145 }
                },
                {
                    { 200, 201, 202, 203, 204, 205 }, { 210, 211, 212, 213, 214, 215 },
                    { 220, 221, 222, 223, 224, 225 }, { 230, 231, 232, 233, 234, 235 },
                    { 240, 241, 242, 243, 244, 245 }
                },
                {
                    { 300, 301, 302, 303, 304, 305 }, { 310, 311, 312, 313, 314, 315 },
                    { 320, 321, 322, 323, 324, 325 }, { 330, 331, 332, 333, 334, 335 },
                    { 340, 341, 342, 343, 344, 345 }
                },
            }),
                 new IndexOrRange[]
                 { Range.Create(1, -1), Range.Create(1, -1), Range.Create(1, -1) },
                 NdArray.Create(new int[, , ]
            {
                { { 111, 112, 113, 114 }, { 121, 122, 123, 124 }, { 131, 132, 133, 134 } },
                { { 211, 212, 213, 214 }, { 221, 222, 223, 224 }, { 231, 232, 233, 234 } },
            })
                 );
            core(NdArray.Create(new int[, , ]
            {
                {
                    { 0, 1, 2, 3, 4, 5 }, { 10, 11, 12, 13, 14, 15 },
                    { 20, 21, 22, 23, 24, 25 }, { 30, 31, 32, 33, 34, 35 },
                    { 40, 41, 42, 43, 44, 45 }
                },
                {
                    { 100, 101, 102, 103, 104, 105 }, { 110, 111, 112, 113, 114, 115 },
                    { 120, 121, 122, 123, 124, 125 }, { 130, 131, 132, 133, 134, 135 },
                    { 140, 141, 142, 143, 144, 145 }
                },
                {
                    { 200, 201, 202, 203, 204, 205 }, { 210, 211, 212, 213, 214, 215 },
                    { 220, 221, 222, 223, 224, 225 }, { 230, 231, 232, 233, 234, 235 },
                    { 240, 241, 242, 243, 244, 245 }
                },
                {
                    { 300, 301, 302, 303, 304, 305 }, { 310, 311, 312, 313, 314, 315 },
                    { 320, 321, 322, 323, 324, 325 }, { 330, 331, 332, 333, 334, 335 },
                    { 340, 341, 342, 343, 344, 345 }
                },
            }),
                 new IndexOrRange[]
                 { Range.Create(0, 4, 3), Range.Create(0, 5, 3), Range.Create(0, 6, 3) },
                 NdArray.Create(new int[, , ]
            {
                { { 0, 3, }, { 30, 33 } },
                { { 300, 303, }, { 330, 333 } },
            })
                 );
        }