/// <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); }
/// <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; }
/// <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); }
/// <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; }