示例#1
0
 public static Float DotProduct(Float[] a, ref VBuffer <Float> b)
 {
     Contracts.Check(Utils.Size(a) == b.Length, "Vectors must have the same dimensionality.");
     if (b.Count == 0)
     {
         return(0);
     }
     if (b.IsDense)
     {
         return(SseUtils.DotProductDense(a, b.Values, b.Length));
     }
     return(SseUtils.DotProductSparse(a, b.Values, b.Indices, b.Count));
 }
示例#2
0
        /// <summary>
        /// Computes the dot product of two arrays
        /// Where "offset" is considered to be a's zero index
        /// </summary>
        /// <param name="a">one array</param>
        /// <param name="b">the second array (given as a VBuffer)</param>
        /// <param name="offset">offset in 'a'</param>
        /// <returns>the dot product</returns>
        public static Float DotProductWithOffset(ref VBuffer <Float> a, int offset, ref VBuffer <Float> b)
        {
            Contracts.Check(0 <= offset && offset <= a.Length);
            Contracts.Check(b.Length <= a.Length - offset, "VBuffer b must be no longer than a.Length - offset.");

            if (a.Count == 0 || b.Count == 0)
            {
                return(0);
            }
            if (a.IsDense)
            {
                if (b.IsDense)
                {
                    return(SseUtils.DotProductDense(a.Values, offset, b.Values, b.Length));
                }
                return(SseUtils.DotProductSparse(a.Values, offset, b.Values, b.Indices, b.Count));
            }
            else
            {
                Float result = 0;
                int   aMin   = Utils.FindIndexSorted(a.Indices, 0, a.Count, offset);
                int   aLim   = Utils.FindIndexSorted(a.Indices, 0, a.Count, offset + b.Length);
                if (b.IsDense)
                {
                    for (int iA = aMin; iA < aLim; ++iA)
                    {
                        result += a.Values[iA] * b.Values[a.Indices[iA] - offset];
                    }
                    return(result);
                }
                for (int iA = aMin, iB = 0; iA < aLim && iB < b.Count;)
                {
                    int aIndex = a.Indices[iA];
                    int bIndex = b.Indices[iB];
                    int comp   = (aIndex - offset) - bIndex;
                    if (comp == 0)
                    {
                        result += a.Values[iA++] * b.Values[iB++];
                    }
                    else if (comp < 0)
                    {
                        iA++;
                    }
                    else
                    {
                        iB++;
                    }
                }
                return(result);
            }
        }
示例#3
0
        /// <summary>
        /// Computes the dot product of two arrays
        /// Where "offset" is considered to be a's zero index
        /// </summary>
        /// <param name="a">one array</param>
        /// <param name="b">the second array (given as a VBuffer)</param>
        /// <param name="offset">offset in 'a'</param>
        /// <returns>the dot product</returns>
        public static Float DotProductWithOffset(Float[] a, int offset, ref VBuffer <Float> b)
        {
            Contracts.Check(0 <= offset && offset <= a.Length);
            Contracts.Check(b.Length <= a.Length - offset, "VBuffer b must be no longer than a.Length - offset.");

            if (b.Count == 0)
            {
                return(0);
            }

            if (b.IsDense)
            {
                return(SseUtils.DotProductDense(a, offset, b.Values, b.Length));
            }
            return(SseUtils.DotProductSparse(a, offset, b.Values, b.Indices, b.Count));
        }
示例#4
0
        public static Float DotProduct(ref VBuffer <Float> a, ref VBuffer <Float> b)
        {
            Contracts.Check(a.Length == b.Length, "Vectors must have the same dimensionality.");

            if (a.Count == 0 || b.Count == 0)
            {
                return(0);
            }

            if (a.IsDense)
            {
                if (b.IsDense)
                {
                    return(SseUtils.DotProductDense(a.Values, b.Values, a.Length));
                }
                return(SseUtils.DotProductSparse(a.Values, b.Values, b.Indices, b.Count));
            }

            if (b.IsDense)
            {
                return(SseUtils.DotProductSparse(b.Values, a.Values, a.Indices, a.Count));
            }
            return(DotProductSparse(a.Values, a.Indices, 0, a.Count, b.Values, b.Indices, 0, b.Count, 0));
        }
 public static float DotProductDense(float[] a, int offset, float[] b, int count) => SseUtils.DotProductDense(a, offset, b, count);
示例#6
0
 public static Float DotProduct(Float[] a, Float[] b)
 {
     Contracts.Check(Utils.Size(a) == Utils.Size(b), "Arrays must have the same length");
     Contracts.Check(Utils.Size(a) > 0);
     return(SseUtils.DotProductDense(a, b, a.Length));
 }