NdArray ForwardCpu(NdArray val) { int[] resultShape; if (val.Shape.Length > this.Shape.Length) { //The input is larger resultShape = val.Shape.ToArray(); int offset = val.Shape.Length - this.Shape.Length; for (int i = offset; i < resultShape.Length; i++) { if (resultShape[i] == 1) { resultShape[i] = this.Shape[i - offset]; } #if DEBUG else if (this.Shape[i - offset] != 1 && resultShape[i] != this.Shape[i - offset]) { throw new Exception("It is an incompatible combination"); } #endif } } else { //Designation is larger resultShape = this.Shape.ToArray(); int offset = this.Shape.Length - val.Shape.Length; for (int i = offset; i < resultShape.Length; i++) { if (resultShape[i] == 1) { resultShape[i] = val.Shape[i - offset]; } #if DEBUG else if (val.Shape[i - offset] != 1 && resultShape[i] != val.Shape[i - offset]) { throw new Exception("It is an incompatible combination"); } #endif } } NdArray result = new NdArray(resultShape, val.BatchCount, this); int indexOffset = result.Shape.Length - val.Shape.Length; for (int batchCount = 0; batchCount < result.BatchCount; batchCount++) { for (int i = 0; i < result.Length; i++) { int[] baseIndex = result.GetDimensionsIndex(i); int tmpIndexLastIndex = val.Shape.Length - 1; int valIndex = batchCount * val.Length; int rankoffset = 1; for (int j = 0; j < val.Shape.Length; j++) { if (val.Shape[tmpIndexLastIndex] > 1) { valIndex += baseIndex[tmpIndexLastIndex + indexOffset] * rankoffset; } rankoffset *= val.Shape[tmpIndexLastIndex--]; } result.Data[batchCount * result.Length + i] = val.Data[valIndex]; } } return(result); }