예제 #1
0
        public IVector Permute(IVector[] selections, int[] shifts, ulong outputDim, IComputationEnvironment env)
        {
            if (selections.Length != shifts.Length)
            {
                throw new Exception("number of selection vectors and number of shifts does not match");
            }
            var res = Vector <double> .Build.Dense((int)Dim);

            for (int i = 0; i < selections.Length; i++)
            {
                if (selections[i] == null)
                {
                    continue;
                }
                if (selections[i].Dim != Dim)
                {
                    throw new Exception("dimension of selection vector does not match dimension of data vector");
                }
                if (selections[i].Scale != selections[0].Scale)
                {
                    throw new Exception("scales of all selection vectors should be the same");
                }
                var s = selections[i] as RawVector;
                var t = v.PointwiseMultiply(s.v);
                res = res.Add(Rotate(t, shifts[i]));
            }
            var resVec = new RawVector(res.SubVector(0, (int)outputDim), 1, BlockSize);

            resVec.RegisterScale(Scale * selections[0].Scale);
            return(resVec);
        }
예제 #2
0
        public RawVector(RawVector vr)
        {
            this.v = Vector <double> .Build.DenseOfVector(vr.v);

            this.Scale     = vr.Scale;
            this.BlockSize = vr.BlockSize;
        }
예제 #3
0
        public IVector PointwiseMultiply(IVector v, IComputationEnvironment env)
        {
            var             vr  = v as RawVector;
            Vector <double> mul = null;

            if (this.v.Count == vr.v.Count)
            {
                mul = this.v.PointwiseMultiply(vr.v);
            }
            else if (this.v.Count == 1 && this.Format == EVectorFormat.sparse) // multiplying by constant
            {
                mul = vr.v.Multiply(this.v[0]);
            }
            else if (vr.v.Count == 1 && vr.Format == EVectorFormat.sparse) // mutliplying by constant
            {
                mul = this.v.Multiply(vr.v[0]);
            }
            else
            {
                throw new Exception("Vectors dimensions do not match");
            }
            var res = new RawVector(mul, 1, BlockSize);

            res.RegisterScale(Scale * vr.Scale);
            return(res);
        }
예제 #4
0
        public IVector Rotate(int amount, IComputationEnvironment env)
        {
            var res = new RawVector(Rotate(v, amount), 1, BlockSize);

            res.RegisterScale(Scale);
            return(res);
        }
예제 #5
0
        public IVector Mul(IVector v, IComputationEnvironment env, bool ForceDenseFormat = false)
        {
            var vr  = v as RawVector;
            var res = new RawVector(m.Multiply((Vector <double>)vr.Data), 1, v.BlockSize);

            res.RegisterScale(Scale * vr.Scale);
            return(res);
        }
예제 #6
0
        public IVector Multiply(double x, IComputationEnvironment env)
        {
            var res = this.v.Multiply(x);
            var t   = new RawVector(res, 1, BlockSize);

            t.RegisterScale(Scale);
            return(t);
        }
예제 #7
0
        public IVector SumAllSlots(IComputationEnvironment env)
        {
            var sum  = v.Sum();
            var vsum = Vector <double> .Build.DenseOfEnumerable(new double[] { sum });

            var res = new RawVector(vsum, 1, BlockSize);

            res.RegisterScale(Scale);
            return(res);
        }
예제 #8
0
        public IVector DotProduct(IVector v, IComputationEnvironment env)
        {
            var vr   = v as RawVector;
            var dot  = this.v.DotProduct(vr.v);
            var vdot = Vector <double> .Build.DenseOfEnumerable(new double[] { dot });

            var res = new RawVector(vdot, 1, BlockSize);

            res.RegisterScale(Scale * vr.Scale);
            return(res);
        }
예제 #9
0
        static public RawVector Read(StreamReader str)
        {
            RawVector vct = new RawVector(0)
            {
                BlockSize = ulong.Parse(str.ReadLine()),
                Scale     = Double.Parse(str.ReadLine()),
                v         = DelimitedReader.Read <double>(str).Column(0),
            };

            return(vct);
        }
예제 #10
0
        public IVector ConvertToColumnVector(IComputationEnvironment env)
        {
            if ((ulong)m.ColumnCount * (ulong)m.RowCount > this.BlockSize)
            {
                throw new Exception("block too long for interleaving");
            }

            var v = new RawVector(Vector <double> .Build.DenseOfEnumerable(m.Enumerate()), 1, BlockSize);

            v.RegisterScale(Scale);
            return(v);
        }
예제 #11
0
        public IVector GetRow(int rowNumber)
        {
            if (rowNumber >= m.RowCount)
            {
                throw new Exception("Row does not exist");
            }
            if (Format != EMatrixFormat.RowMajor)
            {
                throw new Exception("Row can be extracted only from a row major matrix");
            }
            var v = new RawVector(m.Row(rowNumber), 1, BlockSize);

            v.RegisterScale(Scale);
            return(v);
        }
예제 #12
0
        public IVector GetColumn(int columnNumber)
        {
            if (columnNumber >= m.ColumnCount)
            {
                throw new Exception("Column does not exist");
            }
            if (Format != EMatrixFormat.ColumnMajor)
            {
                throw new Exception("Columns can be extracted only from a column major matrix");
            }
            var col = m.Column(columnNumber);
            var v   = new RawVector(col, 1, BlockSize);

            v.RegisterScale(Scale);
            return(v);
        }
예제 #13
0
        public IVector Subtract(IVector v, IComputationEnvironment env)
        {
            if (v.Scale == 0)
            {
                return(this);
            }
            var vr = v as RawVector;

            if (Scale != 0 && Scale != vr.Scale)
            {
                throw new Exception("Scales do not match.");
            }
            var res = this.v.Subtract(vr.v);
            var t   = new RawVector(res, 1, BlockSize);

            t.RegisterScale(Scale);
            return(t);
        }
예제 #14
0
        public IVector Interleave(int shift, IComputationEnvironment env)
        {
            int  items         = (shift > 0) ? shift : -shift;
            bool allignedToEnd = (shift < 0);

            if (items == 0)
            {
                throw new Exception("number of items cannot be zero");
            }
            var w = m.Column(0);

            for (int i = 1; i < m.ColumnCount; i++)
            {
                w = w.Add(Shift(m.Column(i), shift * i));
            }
            var t = new RawVector(w, 1, BlockSize);

            t.RegisterScale(Scale);
            return(t);
        }
예제 #15
0
        public IVector DotProduct(IVector w, ulong length, IComputationEnvironment env)
        {
            var wr    = w as RawVector;
            var res   = v.PointwiseMultiply(wr.v);
            var shift = Vector <double> .Build.Dense((int)Dim);

            ulong skip = 1;

            while (skip < length)
            {
                Rotate(res, (int)skip, shift);
                res   = res.Add(shift);
                skip *= 2;
            }



            var resVector = new RawVector(res, 1, BlockSize);

            resVector.RegisterScale(Scale * wr.Scale);
            return(resVector);
        }
예제 #16
0
 public IVector LoadVector(StreamReader str)
 {
     return(RawVector.Read(str));
 }