Ejemplo n.º 1
0
    public static dynamic arange(object start, object stop, object step)
    {
        dynamic returnValue = null;

        switch (start)
        {
        case int startCast:
        {
            int stopCast = (int)stop;
            int stepCast = (int)step;

            returnValue = new NumSharp.Core.NDArray().arange(stopCast, startCast, stepCast);

            break;
        }

        case float startCast_:
        {
            int startCast = (int)start;
            int stopCast  = (int)stop;
            int stepCast  = (int)step;

            returnValue = new NumSharp.Core.NDArray().arange(stopCast, startCast, stepCast);

            break;
        }
        }
        return(returnValue);
    }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrieve element
        /// low performance, use generic Data<T> method for performance sensitive invoke
        /// </summary>
        /// <param name="select"></param>
        /// <returns></returns>
        public object this[params int[] select]
        {
            get
            {
                if (select.Length == Shape.NDim)
                {
                    switch (values)
                    {
                    case double[] values:
                        return(values[Shape.GetIndexInShape(select)]);

                    case int[] values:
                        return(values[Shape.GetIndexInShape(select)]);
                    }

                    return(null);
                }
                else
                {
                    int start  = Shape.GetIndexInShape(select);
                    int length = Shape.DimOffset[select.Length - 1];

                    var nd = new NDArray(dtype);

                    switch (values)
                    {
                    case double[]     values:
                        Span <double> double8 = GetData <double>();
                        nd.Storage.Set(double8.Slice(start, length).ToArray());
                        break;

                    case int[]     values:
                        Span <int> int32 = GetData <int>();
                        nd.Storage.Set(int32.Slice(start, length).ToArray());
                        break;
                    }

                    int[] shape = new int[Shape.NDim - select.Length];
                    for (int i = select.Length; i < Shape.NDim; i++)
                    {
                        shape[i - select.Length] = Shape[i];
                    }
                    nd.Storage.Shape = new Shape(shape);

                    return(nd);
                }
            }

            set
            {
                if (select.Length == Shape.NDim)
                {
                    switch (values)
                    {
                    case double[] values:
                        values[Shape.GetIndexInShape(select)] = (double)value;
                        break;

                    case int[] values:
                        values[Shape.GetIndexInShape(select)] = (int)value;
                        break;
                    }
                }
                else
                {
                    int start  = Shape.GetIndexInShape(select);
                    int length = Shape.DimOffset[Shape.NDim - 1];

                    switch (value)
                    {
                    case double v:
                        Span <double> data1     = GetData <double>();
                        var           elements1 = data1.Slice(start, length);

                        for (int i = 0; i < elements1.Length; i++)
                        {
                            elements1[i] = v;
                        }

                        break;

                    case int v:
                        Span <int> data2     = GetData <int>();
                        var        elements2 = data2.Slice(start, length);

                        for (int i = 0; i < elements2.Length; i++)
                        {
                            elements2[i] = v;
                        }

                        break;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Return a new float array of given shape, filled with zeros.
        /// </summary>
        /// <param name="np"></param>
        /// <param name="shape"></param>
        /// <returns></returns>
        public NDArray zeros(params int[] shape)
        {
            var nd = new NDArray(float64, new Shape(shape));

            return(nd);
        }
Ejemplo n.º 4
0
        public NDArray power(ValueType exponent)
        {
            var powerArray = new NDArray(this.dtype, this.shape);

            Array dataSysArr      = this.Storage.GetData();
            Array powerDataSysArr = powerArray.Storage.GetData();

            switch (dataSysArr)
            {
            case double[] data:
            {
                var powerData = powerDataSysArr as double[];

                for (int idx = 0; idx < data.Length; idx++)
                {
                    powerData[idx] = Math.Pow(data[idx], (double)exponent);
                }

                break;
            }

            case float[] data:
            {
                var powerData = powerDataSysArr as float[];

                for (int idx = 0; idx < data.Length; idx++)
                {
                    powerData[idx] = (float)Math.Pow((double)data[idx], (double)exponent);
                }

                break;
            }

            case Complex[] data:
            {
                var powerData = powerDataSysArr as Complex[];

                for (int idx = 0; idx < data.Length; idx++)
                {
                    powerData[idx] = Complex.Pow(data[idx], (double)exponent);
                }

                break;
            }

            case int[] data:
            {
                var powerData = powerDataSysArr as int[];

                for (int idx = 0; idx < data.Length; idx++)
                {
                    powerData[idx] = (int)Math.Pow((double)data[idx], (double)exponent);
                }

                break;
            }

            default:
            {
                throw new IncorrectTypeException();
            }
            }
            return(powerArray);
        }