Beispiel #1
0
 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));
     }
 }
Beispiel #2
0
 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);
            }
        }
Beispiel #5
0
        /// <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)));
                        }
                    }
                }
            }
        }
Beispiel #6
0
        /// <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)));
                        }
                    }
                }
            }
        }
Beispiel #8
0
        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);
        }
Beispiel #10
0
 protected Volume(VolumeStorage <T> storage)
 {
     this.Storage = storage;
 }
Beispiel #11
0
 public abstract void CopyFrom(VolumeStorage <T> source);
Beispiel #12
0
 public bool Equals(VolumeStorage <T> other)
 {
     throw new NotImplementedException();
 }
Beispiel #13
0
 /// <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);
Beispiel #14
0
 /// <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);
Beispiel #15
0
 public abstract bool Equals(VolumeStorage <T> other);