コード例 #1
0
        /// <summary>
        ///     Return a 2-D array with ones on the diagonal and zeros elsewhere.
        /// </summary>
        /// <param name="N">Number of rows in the output.</param>
        /// <param name="M">Number of columns in the output. If None, defaults to N.</param>
        /// <param name="k">Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal.</param>
        /// <param name="dtype">Data-type of the returned array.</param>
        /// <returns>An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.</returns>
        /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.eye.html</remarks>
        public static NDArray eye(int N, int?M = null, int k = 0, Type dtype = null)
        {
            if (!M.HasValue)
            {
                M = N;
            }
            var m = np.zeros(Shape.Matrix(N, M.Value), dtype ?? typeof(double));

            if (k >= M)
            {
                return(m);
            }
            int i;

            if (k >= 0)
            {
                i = k;
            }
            else
            {
                i = (-k) * M.Value;
            }

            var flat = m.flat;
            var one  = dtype != null?Converts.ChangeType(1d, dtype.GetTypeCode()) : 1d;

            int skips = k < 0 ? Math.Abs(k) - 1 : 0;

            for (int j = k; j < flat.size; j += N + 1)
            {
                if (j < 0 || skips-- > 0)
                {
                    continue;
                }
                flat.SetAtIndex(one, j);
            }

            return(m);
        }
コード例 #2
0
ファイル: np.array.cs プロジェクト: stuarthillary/NumSharp
        public static NDArray array <T>(T[][] data)
        {
            var array = data.SelectMany(inner => inner).ToArray(); //todo! not use selectmany.

            return(new NDArray(array, Shape.Matrix(data.Length, data[0].Length)));
        }