Пример #1
0
        /// <summary>
        /// Returns the inner product of this vector with the specified vector.
        /// </summary>
        /// <param name="that">that the other vector</param>
        /// <returns>the dot product between this vector and that vector</returns>
        /// <exception cref="ArgumentException">if the lengths of the two vectors are not equal</exception>
        public double Dot(SparseVector that)
        {
            if (_dims != that._dims)
            {
                throw new ArgumentException("Vector lengths disagree");
            }
            var sum = 0.0;

            // iterate over the vector with the fewest nonzeros
            if (_st.Size() <= that._st.Size())
            {
                foreach (int i in _st.Keys())
                {
                    if (that._st.Contains(i))
                    {
                        sum += Get(i) * that.Get(i);
                    }
                }
            }
            else
            {
                foreach (int i in that._st.Keys())
                {
                    if (_st.Contains(i))
                    {
                        sum += Get(i) * that.Get(i);
                    }
                }
            }
            return(sum);
        }
Пример #2
0
 /// <summary>
 /// find a key with the highest frequency count
 /// </summary>
 public void SetMax()
 {
     _st.Put(_max, 0);
     foreach (var word in _st.Keys())
     {
         if (_st.Get(word) == null)
         {
             continue;
         }
         if (_st.Get(word) > _st.Get(_max))
         {
             _max = word;
         }
     }
 }