public static Real[] ToRealArray(Array data) { Type arrayType = data.GetType().GetElementType(); Real[] resultData = new Real[data.Length]; //型の不一致をここで吸収 if (arrayType != typeof(RealType) && arrayType != typeof(Real)) { //一次元の長さの配列を用意 Array array = Array.CreateInstance(arrayType, data.Length); //一次元化して Buffer.BlockCopy(data, 0, array, 0, Marshal.SizeOf(arrayType) * resultData.Length); data = new RealType[array.Length]; //型変換しつつコピー Array.Copy(array, data, array.Length); } //データを叩き込む GCHandle source = GCHandle.Alloc(data, GCHandleType.Pinned); GCHandle dest = GCHandle.Alloc(resultData, GCHandleType.Pinned); RealTool.CopyMemory(dest.AddrOfPinnedObject(), source.AddrOfPinnedObject(), sizeof(RealType) * data.Length); source.Free(); dest.Free(); return(resultData); }
public static Array ToRealNdArray(Array data) { Type arrayType = data.GetType().GetElementType(); #if DEBUG //そもそもRealTypeなら必要ない if (arrayType == typeof(Real)) { throw new Exception(); } #endif int[] shape = new int[data.Rank]; for (int i = 0; i < shape.Length; i++) { shape[i] = data.GetLength(i); } Array resultData = Array.CreateInstance(typeof(Real), shape); //型の不一致をここで吸収 if (arrayType != typeof(RealType)) { //入力と同じ次元の配列を用意 Array array = Array.CreateInstance(typeof(RealType), shape); //型変換しつつコピー Array.Copy(data, array, array.Length); //データを叩き込む GCHandle source = GCHandle.Alloc(array, GCHandleType.Pinned); GCHandle dest = GCHandle.Alloc(resultData, GCHandleType.Pinned); RealTool.CopyMemory(dest.AddrOfPinnedObject(), source.AddrOfPinnedObject(), sizeof(RealType) * data.Length); source.Free(); dest.Free(); } else { //データを叩き込む GCHandle source = GCHandle.Alloc(data, GCHandleType.Pinned); GCHandle dest = GCHandle.Alloc(resultData, GCHandleType.Pinned); RealTool.CopyMemory(dest.AddrOfPinnedObject(), source.AddrOfPinnedObject(), sizeof(RealType) * data.Length); source.Free(); dest.Free(); } return(resultData); }
public static Array ToBaseNdArray(Array data) { int[] shape = new int[data.Rank]; for (int i = 0; i < shape.Length; i++) { shape[i] = data.GetLength(i); } Array result = Array.CreateInstance(typeof(RealType), shape); //データを叩き込む GCHandle source = GCHandle.Alloc(data, GCHandleType.Pinned); GCHandle dest = GCHandle.Alloc(result, GCHandleType.Pinned); RealTool.CopyMemory(dest.AddrOfPinnedObject(), source.AddrOfPinnedObject(), sizeof(RealType) * data.Length); source.Free(); dest.Free(); return(result); }
//多次元配列を作る public static Array GetRealNdArray(int[] shape, int max = 1, int min = 0) { Array result = Array.CreateInstance(typeof(Real), shape); Real[] tmp = new Real[result.Length]; for (int i = 0; i < tmp.Length; i++) { tmp[i] = (max - min) * Mother.Dice.NextDouble() - min; } GCHandle source = GCHandle.Alloc(tmp, GCHandleType.Pinned); GCHandle dest = GCHandle.Alloc(result, GCHandleType.Pinned); RealTool.CopyMemory(dest.AddrOfPinnedObject(), source.AddrOfPinnedObject(), result.Length * Real.Size); dest.Free(); source.Free(); return(result); }