Ejemplo n.º 1
0
        /// <summary>
        /// Repeats the array along the dimension.
        /// </summary>
        /// <param name="repetitions">The repetitions.</param>
        /// <returns>ArithArray.</returns>
        /// <exception cref="InvalidOperationException">
        /// repetitions must be at least the same length as the number of array dimensions
        /// or
        /// All dimensions must be repeated at least once
        /// </exception>
        public SuperArray RepeatTensor(params long[] repetitions)
        {
            if (repetitions.Length < this.DimensionCount)
            {
                throw new InvalidOperationException("repetitions must be at least the same length as the number of array dimensions");
            }
            if (repetitions.Any(x => x < 1))
            {
                throw new InvalidOperationException("All dimensions must be repeated at least once");
            }

            var paddedSrc  = this.PadToDimCount(repetitions.Length);
            var resultSize = paddedSrc.Shape.Zip(repetitions, (s, r) => s * r).ToArray();

            var result = new SuperArray(resultSize, this.ElementType);

            var urTensor = result.CopyRef();

            for (int i = 0; i < paddedSrc.DimensionCount; ++i)
            {
                var oldUrTensor = urTensor;
                urTensor = urTensor.Unfold(i, paddedSrc.Shape[i], paddedSrc.Shape[i]);
                oldUrTensor.Dispose();
            }

            paddedSrc = paddedSrc.PadToDimCount(urTensor.DimensionCount);
            var expandedSrc = paddedSrc.Expand(urTensor.Shape);

            Helper.Copy(urTensor, expandedSrc);

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Formats the array.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="array">The array.</param>
        private static void FormatTensor(StringBuilder builder, SuperArray array)
        {
            var storageFormat = GetStorageFormat(array.Storage, array);
            var format        = storageFormat.Item1;
            var scale         = storageFormat.Item2;
            var sz            = storageFormat.Item3;

            var  startingLength = builder.Length;
            var  counter        = Enumerable.Repeat((long)0, array.DimensionCount - 2).ToArray();
            bool finished       = false;

            counter[0] = -1;
            while (true)
            {
                for (int i = 0; i < array.DimensionCount - 2; ++i)
                {
                    counter[i]++;
                    if (counter[i] >= array.Shape[i])
                    {
                        if (i == array.DimensionCount - 3)
                        {
                            finished = true;
                            break;
                        }
                        counter[i] = 0;
                    }
                    else
                    {
                        break;
                    }
                }

                if (finished)
                {
                    break;
                }

                if (builder.Length - startingLength > 1)
                {
                    builder.AppendLine();
                }

                builder.Append('(');
                var tensorCopy = array.CopyRef();
                for (int i = 0; i < array.DimensionCount - 2; ++i)
                {
                    var newCopy = tensorCopy.Select(0, counter[i]);
                    tensorCopy.Dispose();
                    tensorCopy = newCopy;
                    builder.Append(counter[i]).Append(',');
                }

                builder.AppendLine(".,.) = ");
                FormatMatrix(builder, tensorCopy, " ");

                tensorCopy.Dispose();
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Ases the contiguous.
 /// </summary>
 /// <param name="src">The source.</param>
 /// <returns>NDArray.</returns>
 public static SuperArray AsContiguous(SuperArray src)
 {
     if (src.IsContiguous())
     {
         return(src.CopyRef());
     }
     else
     {
         return(NewContiguous(src));
     }
 }