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 MapInplace(Func <T, T, T> f, VolumeStorage <T> other) { for (var i = 0; i < this.Shape.TotalLength; i++) { this.Set(i, f(this.Get(i), other.Get(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))); } } } } }
public override void CopyFrom(VolumeStorage <T> source) { var src = source as NcwhVolumeStorage <T>; if (!ReferenceEquals(this, src)) { if (this.Shape.TotalLength != src.Shape.TotalLength) { throw new ArgumentException($"origin and destination volume should have the same number of weight ({this.Shape.TotalLength} != {src.Shape})."); } Array.Copy(src._storage, this._storage, this._storage.Length); } }
/// <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))); } } } } }
protected Volume(VolumeStorage <T> storage) { Count++; this.Storage = storage; }
public override void CopyFrom(VolumeStorage <T> source) { var real = source as NcwhVolumeStorage <T>; Array.Copy(real._storage, this._storage, this._storage.Length); }
protected Volume(VolumeStorage <T> storage) { this.Storage = storage; }
public abstract void CopyFrom(VolumeStorage <T> source);
public bool Equals(VolumeStorage <T> other) { throw new NotImplementedException(); }
/// <summary> /// Creates a volume with given shape, filled with the provided value and using the same storage type as provided /// example /// </summary> /// <param name="example"></param> /// <param name="value"></param> /// <param name="shape"></param> /// <returns></returns> public abstract Volume <T> SameAs(VolumeStorage <T> example, T value, Shape shape);
/// <summary> /// Creates a volume with given shape, using provided storage as internal storage (no copy) /// </summary> /// <param name="storage"></param> /// <param name="shape"></param> /// <returns></returns> public abstract Volume <T> Build(VolumeStorage <T> storage, Shape shape);
public abstract bool Equals(VolumeStorage <T> other);