Ejemplo n.º 1
0
        /// <summary>
        ///     [Pure] Projects each element of a NdArray into a new form.
        /// </summary>
        /// <typeparam name="TSource"> The type of the elements of <paramref name="ndarray"/>. </typeparam>
        /// <typeparam name="TResult"> The type of the value returned by <paramref name="selector"/>. </typeparam>
        /// <param name="ndarray"> A NdArray for values to invoke a transform function on. </param>
        /// <param name="selector"> A transform function to apply to each element. </param>
        /// <param name="strategy">
        ///     [nullable] A strategy object to iterate calculation for each element.
        ///     <c>null</c> means to use <see cref="IterationStrategy.Default"/>.
        /// </param>
        /// <returns> [<c>$ReturnValue.Shape == NdArray.Shape</c>] </returns>
        public static NdArray <TResult> Select <TSource, TResult>(
            this NdArray <TSource> ndarray,
            Func <TSource, TResult> selector,
            IIterationStrategy?strategy = default)
        {
            Guard.AssertArgumentNotNull(ndarray, nameof(ndarray));
            Guard.AssertArgumentNotNull(selector, nameof(selector));

            var len    = ndarray.Length;
            var entity = new RawNdArrayImpl <TResult>(ndarray.Shape);
            var array  = entity.Buffer;

            if (strategy is null || strategy is IterationStrategy)
            {
                if (ndarray.Entity is RawNdArrayImpl <TSource> raw)
                {
                    for (var i = 0; i < len; ++i)
                    {
                        array.Span[i] = selector(raw.Buffer.Span[i]);
                    }
                }
                else
                {
                    for (var i = 0; i < len; ++i)
                    {
                        array.Span[i] = selector(ndarray.GetItem(i));
                    }
                }
            }
Ejemplo n.º 2
0
 /// <summary>
 ///     When <typeparamref name="T"/> is primitive type, saves <see cref="NdArray{T}"/> instance to .npy binary.
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="ndArray"></param>
 /// <exception cref="NotSupportedException"></exception>
 /// <typeparam name="T"> A primitive type. </typeparam>
 public static void Save <T>(Stream stream, NdArray <T> ndArray)
     where T : unmanaged
 => SaveCore(stream,
             ndArray,
             DType.UInt16,
             (i, buf) => LittleEndianConv.TryWritePrimitive(buf.AsSpan(), ndArray.GetItem(i)));