using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading.Tasks; using NumSharp.Backends; using NumSharp.Backends.Unmanaged; using NumSharp.Utilities; namespace NumSharp { public static partial class np { /// /// Wraps given in an alias. If is true then returns a clone. /// /// /// If is true then returns a clone. [MethodImpl((MethodImplOptions)768)] public static NDArray array(NDArray nd, bool copy = false) => copy ? new NDArray(nd.Storage.Clone()) : new NDArray(nd.Storage); /// /// Creates an from an array with an unknown size or dtype. /// /// Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement. /// Always copies if the array is larger than 1-d. /// Not used. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html [MethodImpl((MethodImplOptions)512)] [SuppressMessage("ReSharper", "InvalidXmlDocComment")] public static NDArray array(Array array, Type dtype = null, int ndmin = 1, bool copy = true, char order = 'C') { if (array == null) throw new ArgumentNullException(nameof(array)); var arrType = array.ResolveElementType(); //handle dim expansion and extract shape Shape shape; var dims = array.ResolveRank(); var missing = dims - ndmin; if (missing < 0) { shape = Arrays.Concat(Enumerable.Repeat(1, Math.Abs(missing)).ToArray(), Shape.ExtractShape(array)); } else { shape = Shape.ExtractShape(array); } //flatten if (shape.NDim > 1) { array = Arrays.Flatten(array); copy = false; } if (dtype != null && dtype != arrType) { array = ArrayConvert.To(array, dtype); copy = false; } return new NDArray(copy ? (Array)array.Clone() : array, shape, order); } /// /// Creates a Vector from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html

Always performs a copy.
public static NDArray array(params T[] data) where T : unmanaged => new NDArray(ArraySlice.FromArray(data, true), Shape.Vector(data.Length)); /// /// Creates a Vector from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The enumeration of data to create from. /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html

Always performs a copy.
public static NDArray array(IEnumerable data) where T : unmanaged { var slice = ArraySlice.FromArray(data.ToArray(), false); return new NDArray(slice, Shape.Vector(slice.Count)); } /// /// Creates a Vector from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The enumeration of data to create from. /// An with the data and shape of the given array. /// /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html

/// Always performs a copy.

/// can be used to limit the amount of items to read form . Reading stops on either size or ends. ///
public static NDArray array(IEnumerable data, int size) where T : unmanaged { var slice = new ArraySlice(new UnmanagedMemoryBlock(size)); unsafe { using (var enumerator = data.GetEnumerator()) { Func next = enumerator.MoveNext; var addr = slice.Address; for (int i = 0; i < size && next(); i++) { addr[i] = enumerator.Current; } } } return new NDArray(slice, Shape.Vector(slice.Count)); } /// /// Create a vector of dtype . /// /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static NDArray array(string chars) { if (chars == null) throw new ArgumentNullException(nameof(chars)); if (chars.Length == 0) return new NDArray(NPTypeCode.Char, 0); unsafe { var ret = new ArraySlice(new UnmanagedMemoryBlock(chars.Length)); fixed (char* strChars = chars) { var src = strChars; var dst = ret.Address; var len = sizeof(char) * chars.Length; Buffer.MemoryCopy(src, dst, len, len); } return new NDArray(ret); } } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. Shape is taken from the first item of each array/nested array. /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html

Always performs a copy.
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")] [SuppressMessage("ReSharper", "SuggestVarOrType_Elsewhere")] [MethodImpl((MethodImplOptions)512)] public static NDArray array(T[][] data) where T : unmanaged { unsafe { int len1 = data.Length; int len2 = data[0].Length; NDArray @out = new NDArray(InfoOf.NPTypeCode, new Shape(len1, len2)); var strides = @out.strides; int stride1 = strides[0]; Debug.Assert(strides[1] == 1); T* addr = (T*)@out.Address; Parallel.For(0, len1, i1 => { T* addr1 = addr + i1 * stride1; T[] src1 = data[i1]; Parallel.For(0, len2, i2 => { *(addr1 + i2) = src1[i2]; }); }); return @out; } } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. Shape is taken from the first item of each array/nested array. /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html

Always performs a copy.
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")] [SuppressMessage("ReSharper", "SuggestVarOrType_Elsewhere")] [MethodImpl((MethodImplOptions)512)] public static NDArray array(T[][][] data) where T : unmanaged { unsafe { int len1 = data.Length; int len2 = data[0].Length; int len3 = data[0][0].Length; NDArray @out = new NDArray(InfoOf.NPTypeCode, new Shape(len1, len2, len3)); var strides = @out.strides; int stride1 = strides[0]; int stride2 = strides[1]; Debug.Assert(strides[2] == 1); T* addr = (T*)@out.Address; Parallel.For(0, len1, i1 => { T* addr1 = addr + i1 * stride1; T[][] src1 = data[i1]; Parallel.For(0, len2, i2 => { T* addr2 = addr1 + i2 * stride2; T[] src2 = src1[i2]; Parallel.For(0, len3, i3 => { *(addr2 + i3) = src2[i3]; }); }); }); return @out; } } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. Shape is taken from the first item of each array/nested array. /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html

Always performs a copy.
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")] [SuppressMessage("ReSharper", "SuggestVarOrType_Elsewhere")] [MethodImpl((MethodImplOptions)512)] public static NDArray array(T[][][][] data) where T : unmanaged { unsafe { int len1 = data.Length; int len2 = data[0].Length; int len3 = data[0][0].Length; int len4 = data[0][0][0].Length; NDArray @out = new NDArray(InfoOf.NPTypeCode, new Shape(len1, len2, len3, len4)); var strides = @out.strides; int stride1 = strides[0]; int stride2 = strides[1]; int stride3 = strides[2]; Debug.Assert(strides[3] == 1); T* addr = (T*)@out.Address; Parallel.For(0, len1, i1 => { T* addr1 = addr + i1 * stride1; T[][][] src1 = data[i1]; Parallel.For(0, len2, i2 => { T* addr2 = addr1 + i2 * stride2; T[][] src2 = src1[i2]; Parallel.For(0, len3, i3 => { T* addr3 = addr2 + i3 * stride3; T[] src3 = src2[i3]; Parallel.For(0, len4, i4 => { *(addr3 + i4) = src3[i4]; }); }); }); }); return @out; } } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. Shape is taken from the first item of each array/nested array. /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html

Always performs a copy.
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")] [SuppressMessage("ReSharper", "SuggestVarOrType_Elsewhere")] [MethodImpl((MethodImplOptions)512)] public static NDArray array(T[][][][][] data) where T : unmanaged { unsafe { int len1 = data.Length; int len2 = data[0].Length; int len3 = data[0][0].Length; int len4 = data[0][0][0].Length; int len5 = data[0][0][0][0].Length; NDArray @out = new NDArray(InfoOf.NPTypeCode, new Shape(len1, len2, len3, len4, len5)); var strides = @out.strides; int stride1 = strides[0]; int stride2 = strides[1]; int stride3 = strides[2]; int stride4 = strides[3]; Debug.Assert(strides[4] == 1); T* addr = (T*)@out.Address; Parallel.For(0, len1, i1 => { T* addr1 = addr + i1 * stride1; T[][][][] src1 = data[i1]; Parallel.For(0, len2, i2 => { T* addr2 = addr1 + i2 * stride2; T[][][] src2 = src1[i2]; Parallel.For(0, len3, i3 => { T* addr3 = addr2 + i3 * stride3; T[][] src3 = src2[i3]; Parallel.For(0, len4, i4 => { T* addr4 = addr3 + i4 * stride4; T[] src4 = src3[i4]; Parallel.For(0, len5, i5 => { *(addr4 + i5) = src4[i5]; }); }); }); }); }); return @out; } } #if _REGEN %l = "data.GetLength(" %r = ")" %foreach range(1,16)% /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[#(repeat(",", i))] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(#(repeat("^l+str(n.ToString())+r", i+1, ", ")))); } % #else /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[] data, bool copy) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.Length)); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,,,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10), data.GetLength(11))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,,,,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10), data.GetLength(11), data.GetLength(12))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,,,,,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10), data.GetLength(11), data.GetLength(12), data.GetLength(13))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,,,,,,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10), data.GetLength(11), data.GetLength(12), data.GetLength(13), data.GetLength(14))); } /// /// Creates an from given . /// /// The type of given array, must be compliant to numpy's supported dtypes. /// The array to create from. /// /// If true then the array will be copied to a newly allocated memory.

/// If false then the array will be pinned by calling . /// /// An with the data and shape of the given array. /// https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html public static NDArray array(T[,,,,,,,,,,,,,,,] data, bool copy = true) where T : unmanaged { if (data == null) throw new ArgumentNullException(nameof(data)); return new NDArray(ArraySlice.FromArray(data, copy), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5), data.GetLength(6), data.GetLength(7), data.GetLength(8), data.GetLength(9), data.GetLength(10), data.GetLength(11), data.GetLength(12), data.GetLength(13), data.GetLength(14), data.GetLength(15))); } #endif } }