コード例 #1
0
ファイル: VectorUtils.cs プロジェクト: zyw400/machinelearning
 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
ファイル: VectorUtils.cs プロジェクト: zyw400/machinelearning
        /// <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
ファイル: VectorUtils.cs プロジェクト: zyw400/machinelearning
        /// <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
ファイル: VectorUtils.cs プロジェクト: zyw400/machinelearning
        private static Float L2DistSquaredHalfSparse(Float[] valuesA, int lengthA, Float[] valuesB, int[] indicesB, int countB)
        {
            Contracts.AssertValueOrNull(valuesA);
            Contracts.AssertValueOrNull(valuesB);
            Contracts.AssertValueOrNull(indicesB);
            Contracts.Assert(0 <= lengthA && lengthA <= Utils.Size(valuesA));
            Contracts.Assert(0 <= countB && countB <= Utils.Size(indicesB));
            Contracts.Assert(countB <= Utils.Size(valuesB));

            var normA = SseUtils.SumSq(valuesA, 0, lengthA);

            if (countB == 0)
            {
                return(normA);
            }
            var normB = SseUtils.SumSq(valuesB, 0, countB);
            var dotP  = SseUtils.DotProductSparse(valuesA, valuesB, indicesB, countB);
            var res   = normA + normB - 2 * dotP;

            return(res < 0 ? 0 : res);
        }
コード例 #5
0
ファイル: VectorUtils.cs プロジェクト: zyw400/machinelearning
        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));
        }
コード例 #6
0
 /// <summary>
 /// Returns a dot product of dense vector 'a' starting from offset 'aOffset' and sparse vector 'b'
 /// with first 'count' valid elements and their corresponding 'indices'.
 /// </summary>
 private static Float DotProduct(Float[] a, int aOffset, Float[] b, int[] indices, int count)
 {
     Contracts.Assert(count <= indices.Length);
     return(SseUtils.DotProductSparse(a, aOffset, b, indices, count));
 }
コード例 #7
0
 public static float DotProductSparse(float[] a, int offset, float[] b, int[] indices, int count) => SseUtils.DotProductSparse(a, offset, b, indices, count);