Esempio n. 1
0
        /// <summary>
        /// Returns the sum of this vector and the specified vector.
        /// </summary>
        /// <param name="that">that the vector to add to this vector</param>
        /// <returns>the sum of this vector and that vector</returns>
        /// <exception cref="ArgumentException">if the dimensions of the two vectors are not equal</exception>
        public SparseVector Plus(SparseVector that)
        {
            if (_dims != that._dims)
            {
                throw new ArgumentException("Vector lengths disagree");
            }
            var c = new SparseVector(_dims);

            foreach (int i in _st.Keys())
            {
                c.Put(i, Get(i));                                          // c = this
            }
            foreach (int i in that._st.Keys())
            {
                c.Put(i, that.Get(i) + c.Get(i));                                    // c = c + that
            }
            return(c);
        }
        public void Run()
        {
            var a = new SparseVector(10);
            var b = new SparseVector(10);

            a.Put(3, 0.50);
            a.Put(9, 0.75);
            a.Put(6, 0.11);
            a.Put(6, 0.00);
            b.Put(3, 0.60);
            b.Put(4, 0.90);
            Console.WriteLine("a = " + a);
            Console.WriteLine("b = " + b);
            Console.WriteLine("a dot b = " + a.Dot(b));
            Console.WriteLine("a + b   = " + a.Plus(b));

            Console.ReadLine();
        }
Esempio n. 3
0
        /// <summary>
        /// Returns the scalar-vector product of this vector with the specified scalar.
        /// </summary>
        /// <param name="alpha">alpha the scalar</param>
        /// <returns>the scalar-vector product of this vector with the specified scalar</returns>
        public SparseVector Scale(double alpha)
        {
            SparseVector c = new SparseVector(_dims);

            foreach (int i in _st.Keys())
            {
                c.Put(i, alpha * Get(i));
            }
            return(c);
        }
Esempio n. 4
0
 /// <summary>
 /// Returns the scalar-vector product of this vector with the specified scalar.
 /// </summary>
 /// <param name="alpha">alpha the scalar</param>
 /// <returns>the scalar-vector product of this vector with the specified scalar</returns>
 public SparseVector Scale(double alpha)
 {
     SparseVector c = new SparseVector(_dims);
     foreach (int i in _st.Keys()) c.Put(i, alpha * Get(i));
     return c;
 }
Esempio n. 5
0
 /// <summary>
 /// Returns the sum of this vector and the specified vector.
 /// </summary>
 /// <param name="that">that the vector to add to this vector</param>
 /// <returns>the sum of this vector and that vector</returns>
 /// <exception cref="ArgumentException">if the dimensions of the two vectors are not equal</exception>
 public SparseVector Plus(SparseVector that)
 {
     if (_dims != that._dims) throw new ArgumentException("Vector lengths disagree");
     var c = new SparseVector(_dims);
     foreach (int i in _st.Keys()) c.Put(i, Get(i));                // c = this
     foreach (int i in that._st.Keys()) c.Put(i, that.Get(i) + c.Get(i));     // c = c + that
     return c;
 }