public void Map(Func <T, int, T> f, VolumeStorage <T> result) { for (var i = 0; i < this.Shape.TotalLength; i++) { result.Set(i, f(this.Get(i), i)); } }
public void MapEx(Func <T, T, T> f, VolumeStorage <T> other, VolumeStorage <T> result) { var w = (int)this.Shape.GetDimension(0); var h = (int)this.Shape.GetDimension(1); var C = (int)this.Shape.GetDimension(2); var N = (int)this.Shape.GetDimension(3); var otherWIsOne = (int)other.Shape.GetDimension(0) == 1; var otherHIsOne = (int)other.Shape.GetDimension(1) == 1; var otherCIsOne = (int)other.Shape.GetDimension(2) == 1; var otherNIsOne = (int)other.Shape.GetDimension(3) == 1; for (var n = 0; n < N; n++) { for (var c = 0; c < C; c++) { for (var j = 0; j < h; j++) { for (var i = 0; i < w; i++) { result.Set(i, j, c, n, f(Get(i, j, c, n), other.Get(otherWIsOne ? 0 : i, otherHIsOne ? 0 : j, otherCIsOne ? 0 : c, otherNIsOne ? 0 : n))); } } } } }
/// <summary> /// Implement broadcast /// </summary> public void MapEx(Func <T, T, T> f, VolumeStorage <T> other, VolumeStorage <T> result) { var big = this; var small = other; if (small.Shape.TotalLength > big.Shape.TotalLength) { big = other; small = this; } else if (small.Shape.TotalLength == big.Shape.TotalLength) { if (!small.Shape.Equals(big.Shape)) { throw new ArgumentException("Volumes have the same total number of dimensions but have different shapes"); } // No broadcast to do here -> we switch to non-broacast implem this.Map(f, other, result); return; } var w = big.Shape.Dimensions[0]; var h = big.Shape.Dimensions[1]; var C = big.Shape.Dimensions[2]; var N = big.Shape.Dimensions[3]; var otherWIsOne = small.Shape.Dimensions[0] == 1; var otherHIsOne = small.Shape.Dimensions[1] == 1; var otherCIsOne = small.Shape.Dimensions[2] == 1; var otherNIsOne = small.Shape.Dimensions[3] == 1; for (var n = 0; n < N; n++) { for (var c = 0; c < C; c++) { for (var j = 0; j < h; j++) { for (var i = 0; i < w; i++) { result.Set(i, j, c, n, f(big.Get(i, j, c, n), small.Get(otherWIsOne ? 0 : i, otherHIsOne ? 0 : j, otherCIsOne ? 0 : c, otherNIsOne ? 0 : n))); } } } } }
/// <summary> /// Implement broadcast /// </summary> public void MapEx(Func <T, T, T> f, VolumeStorage <T> other, VolumeStorage <T> result) { var big = this; var small = other; if (small.Shape.TotalLength > big.Shape.TotalLength) { big = other; small = this; } else if (small.Shape.TotalLength == big.Shape.TotalLength) { if (!small.Shape.Equals(big.Shape)) { throw new ArgumentException("Volumes have the same total number of dimensions but have different shapes"); } } var w = big.Shape.GetDimension(0); var h = big.Shape.GetDimension(1); var C = big.Shape.GetDimension(2); var N = big.Shape.GetDimension(3); var otherWIsOne = small.Shape.GetDimension(0) == 1; var otherHIsOne = small.Shape.GetDimension(1) == 1; var otherCIsOne = small.Shape.GetDimension(2) == 1; var otherNIsOne = small.Shape.GetDimension(3) == 1; for (var n = 0; n < N; n++) { for (var c = 0; c < C; c++) { for (var j = 0; j < h; j++) { for (var i = 0; i < w; i++) { result.Set(i, j, c, n, f(big.Get(i, j, c, n), small.Get(otherWIsOne ? 0 : i, otherHIsOne ? 0 : j, otherCIsOne ? 0 : c, otherNIsOne ? 0 : n))); } } } } }
/// <summary> /// Implement broadcast /// </summary> public void MapEx(Func <T, T, T> f, VolumeStorage <T> other, VolumeStorage <T> result) { var big = this; var small = other; if (small.Shape.TotalLength > big.Shape.TotalLength) { big = other; small = this; } var w = big.Shape.GetDimension(0); var h = big.Shape.GetDimension(1); var C = big.Shape.GetDimension(2); var N = big.Shape.GetDimension(3); var otherWIsOne = small.Shape.GetDimension(0) == 1; var otherHIsOne = small.Shape.GetDimension(1) == 1; var otherCIsOne = small.Shape.GetDimension(2) == 1; var otherNIsOne = small.Shape.GetDimension(3) == 1; for (var n = 0; n < N; n++) { for (var c = 0; c < C; c++) { for (var j = 0; j < h; j++) { for (var i = 0; i < w; i++) { result.Set(i, j, c, n, f(big.Get(i, j, c, n), small.Get(otherWIsOne ? 0 : i, otherHIsOne ? 0 : j, otherCIsOne ? 0 : c, otherNIsOne ? 0 : n))); } } } } }