Beispiel #1
0
        public static NDArray array(Array array, Type dtype = null, int ndim = 1)
        {
            dtype = (dtype == null) ? array.GetType().GetElementType() : dtype;

            var nd = new NDArray(dtype, new Shape(new int[] { array.Length }));

            if ((array.Rank == 1) && (!array.GetType().GetElementType().IsArray))
            {
                nd.ReplaceData(array);
            }
            else
            {
                throw new Exception("Method is not implemeneted for multidimensional arrays or jagged arrays.");
            }

            return(dtype == null ? nd : nd.astype(dtype));
        }
Beispiel #2
0
        /// <summary>
        /// Draw random samples from a normal (Gaussian) distribution.
        /// </summary>
        /// <param name="loc">Mean of the distribution</param>
        /// <param name="scale">Standard deviation of the distribution</param>
        /// <param name="dims"></param>
        /// <returns></returns>
        public NDArray normal(double loc, double scale, params int[] dims)
        {
            var array = new NDArray(typeof(double), new Shape(dims));

            double[] arr = array.Data <double>();

            for (int i = 0; i < array.size; i++)
            {
                double u1            = 1.0 - randomizer.NextDouble(); //uniform(0,1] random doubles
                double u2            = 1.0 - randomizer.NextDouble();
                double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
                                       Math.Sin(2.0 * Math.PI * u2); //random normal(0,1)
                double randNormal = loc + scale * randStdNormal;     //random normal(mean,stdDev^2)
                arr[i] = randNormal;
            }

            array.ReplaceData(arr);

            return(array);
        }
Beispiel #3
0
        /// <summary>
        /// Return evenly spaced values within a given interval.
        ///
        /// Values are generated within the half-open interval [start, stop)
        /// (in other words, the interval including start but excluding stop).
        /// For integer arguments the function is equivalent to the Python built-in
        /// range function, but returns an ndarray rather than a list.
        ///
        /// When using a non-integer step, such as 0.1, the results will often not
        /// be consistent.  It is better to use numpy.linspace for these cases.
        /// </summary>
        /// <param name="start">
        /// Start of interval.  The interval includes this value.  The default
        /// start value is 0.
        /// </param>
        /// <param name="stop">
        /// End of interval.  The interval does not include this value, except
        /// in some cases where step is not an integer and floating point
        /// round-off affects the length of out.
        /// </param>
        /// <param name="step">
        /// Spacing between values.  For any output out, this is the distance
        /// between two adjacent values, out[i+1] - out[i].  The default
        /// step size is 1.  If step is specified as a position argument,
        /// start must also be given.
        /// </param>
        /// <returns>
        /// Array of evenly spaced values.
        ///
        /// For floating point arguments, the length of the result is
        /// ceil((stop - start)/step).  Because of floating point overflow,
        /// this rule may result in the last element of out being greater
        /// than stop.
        /// </returns>
        public static NDArray arange(int start, int stop, int step = 1)
        {
            if (start > stop)
            {
                throw new Exception("parameters invalid, start is greater than stop.");
            }

            int length = (int)Math.Ceiling((stop - start + 0.0) / step);
            int index  = 0;

            var nd = new NDArray(np.int32, new Shape(length));

            var a = new int[length];

            for (int i = start; i < stop; i += step)
            {
                a[index++] = i;
            }

            nd.ReplaceData(a);

            return(nd);
        }
Beispiel #4
0
        /// <summary>
        ///     Draw samples from a uniform distribution.
        ///     Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform.
        /// </summary>
        /// <param name="low">Lower boundary of the output interval. All values generated will be greater than or equal to low. The default value is 0.</param>
        /// <param name="high">Upper boundary of the output interval. All values generated will be less than high. The default value is 1.0.</param>
        /// <param name="size">Output shape. If the given shape is, e.g., m, n, k, then m * n * k samples are drawn. If size is None (default), a single value is returned if low and high are both scalars. </param>
        /// <returns>NDArray with values of type <see cref="double"/></returns>
        public NDArray uniform(double low, double high, params int[] size)
        {
            if (size == null || size.Length == 0) //return scalar
            {
                var ret  = new NDArray <double>(new Shape(1));
                var data = new double[] { low + randomizer.NextDouble() * (high - low) };
                ret.ReplaceData(data);
                return(ret);
            }

            var result = new NDArray <double>(size);
            ArraySlice <double> resultArray = result.Data <double>();

            //parallelism is prohibited to make sure the result come out presistant
            double diff = high - low;

            for (int i = 0; i < result.size; ++i)
            {
                resultArray[i] = low + randomizer.NextDouble() * diff;
            }

            result.ReplaceData(resultArray); //incase of a view //todo! incase of a view?
            return(result);
        }