public void Serialize(ref MessagePackWriter writer, ref int idx, T[,,,] value, IFormatterResolver formatterResolver) { if (value == null) { writer.WriteNil(ref idx); return; } var i = value.GetLength(0); var j = value.GetLength(1); var k = value.GetLength(2); var l = value.GetLength(3); var formatter = formatterResolver.GetFormatterWithVerify <T>(); writer.WriteFixedArrayHeaderUnsafe(ArrayLength, ref idx); writer.WriteInt32(i, ref idx); writer.WriteInt32(j, ref idx); writer.WriteInt32(k, ref idx); writer.WriteInt32(l, ref idx); writer.WriteArrayHeader(value.Length, ref idx); foreach (var item in value) { formatter.Serialize(ref writer, ref idx, item, formatterResolver); } }
internal void AssertArray <T>(ndarray arrayData, T[,,,] expectedData) { int lengthd0 = expectedData.GetLength(0); int lengthd1 = expectedData.GetLength(1); int lengthd2 = expectedData.GetLength(2); int lengthd3 = expectedData.GetLength(3); AssertShape(arrayData, lengthd0, lengthd1, lengthd2, lengthd3); AssertDataTypes(arrayData, expectedData); for (int i = 0; i < lengthd0; i++) { ndarray dim1Data = arrayData[i] as ndarray; for (int j = 0; j < lengthd1; j++) { ndarray dim2Data = dim1Data[j] as ndarray; for (int k = 0; k < lengthd2; k++) { ndarray dim3Data = dim2Data[k] as ndarray; for (int l = 0; l < lengthd3; l++) { T E1 = expectedData[i, j, k, l]; T A1 = (T)dim3Data[l]; Assert.AreEqual(E1, A1); } } } } }
public int Serialize(ref byte[] bytes, int offset, T[,,,] value, IFormatterResolver formatterResolver) { if (value == null) { return(MessagePackBinary.WriteNil(ref bytes, offset)); } else { var i = value.GetLength(0); var j = value.GetLength(1); var k = value.GetLength(2); var l = value.GetLength(3); var startOffset = offset; var formatter = formatterResolver.GetFormatterWithVerify <T>(); offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, ArrayLength); offset += MessagePackBinary.WriteInt32(ref bytes, offset, i); offset += MessagePackBinary.WriteInt32(ref bytes, offset, j); offset += MessagePackBinary.WriteInt32(ref bytes, offset, k); offset += MessagePackBinary.WriteInt32(ref bytes, offset, l); offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); foreach (var item in value) { offset += formatter.Serialize(ref bytes, offset, item, formatterResolver); } return(offset - startOffset); } }
public void Serialize(NetDataWriter writer, T[,,,] value, NetDataSerializerOptions options) { if (value == null) { writer.Write(false); } else { writer.Write(true); var i = value.GetLength(0); var j = value.GetLength(1); var k = value.GetLength(2); var l = value.GetLength(3); INetDataFormatter <T> formatter = options.Resolver.GetFormatter <T>(); writer.Write(ArrayLength); writer.Write(i); writer.Write(j); writer.Write(k); writer.Write(l); writer.Write(value.Length); foreach (T item in value) { formatter.Serialize(writer, item, options); } } }
public void Serialize(ref MessagePackWriter writer, T[,,,] value, IFormatterResolver resolver) { if (value == null) { writer.WriteNil(); } else { var i = value.GetLength(0); var j = value.GetLength(1); var k = value.GetLength(2); var l = value.GetLength(3); var formatter = resolver.GetFormatterWithVerify <T>(); writer.WriteArrayHeader(ArrayLength); writer.Write(i); writer.Write(j); writer.Write(k); writer.Write(l); writer.WriteArrayHeader(value.Length); foreach (var item in value) { formatter.Serialize(ref writer, item, resolver); } } }
public void Serialize(ref MessagePackWriter writer, T[,,,] value, MessagePackSerializerOptions options) { if (value == null) { writer.WriteNil(); } else { var i = value.GetLength(0); var j = value.GetLength(1); var k = value.GetLength(2); var l = value.GetLength(3); IMessagePackFormatter <T> formatter = options.Resolver.GetFormatterWithVerify <T>(); writer.WriteArrayHeader(ArrayLength); writer.Write(i); writer.Write(j); writer.Write(k); writer.Write(l); writer.WriteArrayHeader(value.Length); foreach (T item in value) { writer.CancellationToken.ThrowIfCancellationRequested(); formatter.Serialize(ref writer, item, options); } } }
public static T[][][][] ConvertToJaggedArray4 <T>(T[, , ,] multiArray) { int firstElement = multiArray.GetLength(0); int secondElement = multiArray.GetLength(1); int thirdElement = multiArray.GetLength(2); int fourthElement = multiArray.GetLength(3); T[][][][] jaggedArray = new T[firstElement][][][]; for (int c = 0; c < firstElement; c++) { jaggedArray[c] = new T[secondElement][][]; for (int r = 0; r < secondElement; r++) { jaggedArray[c][r] = new T[thirdElement][]; for (int p = 0; p < thirdElement; p++) { jaggedArray[c][r][p] = new T[fourthElement]; for (int q = 0; q < fourthElement; q++) { jaggedArray[c][r][p][q] = multiArray[c, r, p, q]; } } } } return(jaggedArray); }
public static NDarray <T> array <T>(T[,,,] data, Dtype dtype = null, bool?copy = null, string order = null, bool?subok = null, int?ndmin = null) where T : struct { var __self__ = self; var d1_array = data.Cast <T>().ToArray(); var shape = new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3)); var ndarray = array <T>(d1_array, dtype, copy, order, subok, ndmin); return(new NDarray <T>(ndarray.reshape(shape))); }
/// <summary> /// Gets the dimensions of an array. /// </summary> /// <typeparam name="T">The type of values stored in the array.</typeparam> /// <param name="array">The array.</param> /// <returns>The dimensions.</returns> public static Index4D GetDimensions <T>(this T[,,,] array) { Contracts.Requires.That(array != null); return(new Index4D( array.GetLength((int)Axis4D.X), array.GetLength((int)Axis4D.Y), array.GetLength((int)Axis4D.Z), array.GetLength((int)Axis4D.W))); }
/// <summary> /// Cloning of four-dimensional array /// </summary> public static T[,,,] Copy <T>(T[,,,] bytes) { int x = bytes.GetLength(0), y = bytes.GetLength(1), z = bytes.GetLength(2), m = bytes.GetLength(3); var newBytes = new T[x, y, z, m]; Array.Copy(bytes, newBytes, bytes.Length); return(newBytes); }
public void Serialize(ref JsonWriter writer, T[,,,] value, IJsonFormatterResolver formatterResolver) { if (value == null) { writer.WriteNull(); } else { var formatter = formatterResolver.GetFormatterWithVerify <T>(); var iLength = value.GetLength(0); var jLength = value.GetLength(1); var kLength = value.GetLength(2); var lLength = value.GetLength(3); writer.WriteBeginArray(); for (var i = 0; i < iLength; i++) { if (i != 0) { writer.WriteValueSeparator(); } writer.WriteBeginArray(); for (var j = 0; j < jLength; j++) { if (j != 0) { writer.WriteValueSeparator(); } writer.WriteBeginArray(); for (var k = 0; k < kLength; k++) { if (k != 0) { writer.WriteValueSeparator(); } writer.WriteBeginArray(); for (var l = 0; l < lLength; l++) { if (l != 0) { writer.WriteValueSeparator(); } formatter.Serialize(ref writer, value[i, j, k, l], formatterResolver); } writer.WriteEndArray(); } writer.WriteEndArray(); } writer.WriteEndArray(); } writer.WriteEndArray(); } }
/// <summary> /// Creates a khiva array object. /// </summary> /// <typeparam name="T">Type of the elements of the array.</typeparam> /// <param name="values">4 dimensional array with the data.</param> /// <param name="doublePrecision">If Complex array has double precision. Default to false.</param> /// <returns>KhivaArray created.</returns> public static unsafe KhivaArray Create <T>(T[,,,] values, bool doublePrecision = false) where T : unmanaged { if (values == null) { throw new Exception("Null elems object provided"); } fixed(T *data = &values[0, 0, 0, 0]) return(Create <T>(4, new long[] { values.GetLength(3), values.GetLength(2), values.GetLength(1), values.GetLength(0) }, data, doublePrecision)); }
public static void Fill4 <T>(this T[,,,] a, T value) { for (int i = 0; i < a.GetLength(0); ++i) { for (int j = 0; j < a.GetLength(1); ++j) { for (int k = 0; k < a.GetLength(2); ++k) { for (int t = 0; t < a.GetLength(3); ++t) { a[i, j, k, t] = value; } } } } }
/// <summary> /// Do action for each item of multidimensional array. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TRet"></typeparam> /// <param name="this"></param> /// <param name="task"></param> /// <returns></returns> public static IEnumerable <TRet> Select <T, TRet>(this T[,,,] @this, Func <T, TRet> selector) { for (int i0 = 0; i0 < @this.GetLength(0); i0++) { for (int i1 = 0; i1 < @this.GetLength(1); i1++) { for (int i2 = 0; i2 < @this.GetLength(2); i2++) { for (int i3 = 0; i3 < @this.GetLength(3); i3++) { yield return(selector(@this[i0, i1, i2, i3])); } } } } }
public static void ForEach <T>(this T[,,,] source, Action <T, int, int, int, int> action) { for (int k = 0; k < source.GetLength(0); k++) { for (int d = 0; d < source.GetLength(1); d++) { for (int h = 0; h < source.GetLength(2); h++) { for (int w = 0; w < source.GetLength(3); w++) { action(source[k, d, h, w], k, d, h, w); } } } } }
/// <summary> /// Do action for each item of multidimensional array. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="this"></param> /// <param name="task"></param> /// <returns></returns> public static T[,,,] Each <T>(this T[,,,] @this, Action <T, int, int, int, int> task) { for (int i0 = 0; i0 < @this.GetLength(0); i0++) { for (int i1 = 0; i1 < @this.GetLength(1); i1++) { for (int i2 = 0; i2 < @this.GetLength(2); i2++) { for (int i3 = 0; i3 < @this.GetLength(3); i3++) { task(@this[i0, i1, i2, i3], i0, i1, i2, i3); } } } } return(@this); }
private void TryLoadArray(object value) { if (Dimensions == 1) { _oneDimensionArray = (T[])value; XLength = _oneDimensionArray.GetLength(0); YLength = 0; ZLength = 0; WLength = 0; } else if (Dimensions == 2) { _twoDimensionArray = (T[, ])value; XLength = _twoDimensionArray.GetLength(0); YLength = _twoDimensionArray.GetLength(1); ZLength = 0; WLength = 0; } else if (Dimensions == 3) { _threeDimensionArray = (T[, , ])value; XLength = _threeDimensionArray.GetLength(0); YLength = _threeDimensionArray.GetLength(1); ZLength = _threeDimensionArray.GetLength(2); WLength = 0; } else if (Dimensions == 4) { _fourDimensionArray = (T[, , , ])value; XLength = _fourDimensionArray.GetLength(0); YLength = _fourDimensionArray.GetLength(1); ZLength = _fourDimensionArray.GetLength(2); WLength = _fourDimensionArray.GetLength(3); } else { throw new NotSupportedException($"Invalid dimension {Dimensions}. Right now only dimensions 1, 2 and 3 are supported"); } }
internal void AssertArray <T>(ndarray arrayData, T[,,,] expectedData) { int lengthd0 = expectedData.GetLength(0); int lengthd1 = expectedData.GetLength(1); int lengthd2 = expectedData.GetLength(2); int lengthd3 = expectedData.GetLength(3); AssertShape(arrayData, lengthd0, lengthd1, lengthd2, lengthd3); for (int i = 0; i < lengthd0; i++) { ndarray dim1Data = arrayData[i] as ndarray; for (int j = 0; j < lengthd1; j++) { ndarray dim2Data = dim1Data[j] as ndarray; for (int k = 0; k < lengthd2; k++) { ndarray dim3Data = dim2Data[k] as ndarray; for (int l = 0; l < lengthd3; l++) { double E1 = Convert.ToDouble(expectedData[i, j, k, l]); double A1 = Convert.ToDouble(dim3Data[l]); if (double.IsNaN(E1) && double.IsNaN(A1)) { continue; } if (double.IsInfinity(E1) && double.IsInfinity(A1)) { continue; } Assert.AreEqual(E1, A1, 0.00000001); } } } } }
/// <summary> /// Creates a copy of this four-dimensional array as a DenseTensor<T> /// </summary> /// <typeparam name="T">Type contained in the array to copy to the DenseTensor<T>.</typeparam> /// <param name="array">The array to create a DenseTensor<T> from.</param> /// <param name="reverseStride">False (default) to indicate that the first dimension is most major (farthest apart) and the last dimension is most minor (closest together): akin to row-major in a rank-2 tensor. True to indicate that the last dimension is most major (farthest apart) and the first dimension is most minor (closest together): akin to column-major in a rank-2 tensor.</param> /// <returns>A 4-dimensional DenseTensor<T> with the same dimensions and content as <paramref name="array"/>.</returns> public static DenseTensor<T> ToTensor<T>(this T[,,,] array, bool reverseStride = false) { if (reverseStride) { // we need logic from the DenseTensor ctor to be applied during copying return new DenseTensor<T>(array, reverseStride); } else { // it's more efficient to copy and flatten to 1D T[] and construct DenseTensor with Memory<T> T[] copy = new T[array.Length]; var dimensions = new int[] { array.GetLength(0), array.GetLength(1), array.GetLength(2), array.GetLength(3) }; long idx = 0; foreach (var item in array) { copy[idx++] = item; } return new DenseTensor<T>(new Memory<T>(copy), dimensions); } }
/// <summary> /// Converts jagged array to multidimensional array. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="this"></param> /// <returns></returns> public static T[][][][] ToJaggedArray <T>(this T[,,,] @this) { var ret = new T[@this.GetLength(0)][][][]; for (int i0 = 0; i0 < @this.Length; i0++) { ret[i0] = new T[@this.GetLength(1)][][]; for (int i1 = 0; i1 < @this.GetLength(1); i1++) { ret[i0][i1] = new T[@this.GetLength(2)][]; for (int i2 = 0; i2 < @this.GetLength(2); i2++) { ret[i0][i1][i2] = new T[@this.GetLength(3)]; for (int i3 = 0; i3 < @this.GetLength(3); i3++) { ret[i0][i1][i2][i3] = @this[i0, i1, i2, i3]; } } } } return(ret); }
public static NDArray array <T>(T[,,,] data) where T : unmanaged { if (data == null) { throw new ArgumentNullException(nameof(data)); } return(new NDArray(ArraySlice.FromArray(data.Cast <T>().ToArray()), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3)))); }
public static NDArray array <T>(T[,,,] data) { var array = data.Cast <T>().ToArray(); return(new NDArray(data.Cast <T>().ToArray(), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3)))); }
public static NDArray array <T>(T[,,,] data) { var array = data.Cast <T>().ToArray(); //todo! not use ienumerable. return(new NDArray(data.Cast <T>().ToArray(), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3)))); }
/// <summary> /// Gets a 32-bit integer that represents the number of elements in the specified dimension of the array. /// </summary> /// <typeparam name="T">The type of values stored in the array.</typeparam> /// <param name="array">The array.</param> /// <param name="dimension">The dimension whose length needs to be determined.</param> /// <returns>A 32-bit integer that represents the number of elements in the specified dimension.</returns> public static int GetLength <T>(this T[,,,] array, Axis4D dimension) { Contracts.Requires.That(array != null); return(array.GetLength((int)dimension)); }