コード例 #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(CpuMathUtils.DotProductDense(a, b.Values, b.Length));
     }
     return(CpuMathUtils.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(CpuMathUtils.DotProductDense(a.Values.AsSpan(offset), b.Values, b.Length));
                }
                return(CpuMathUtils.DotProductSparse(a.Values.AsSpan(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
        public void DotUTest(int test, float expected)
        {
            float[] src = (float[])testArrays[test].Clone();
            float[] dst = (float[])src.Clone();

            for (int i = 0; i < dst.Length; i++)
            {
                dst[i] += 1;
            }

            var actual = CpuMathUtils.DotProductDense(src, dst, dst.Length);

            Assert.Equal(expected, actual, 2);
        }
コード例 #4
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(CpuMathUtils.DotProductDense(a.AsSpan(offset), b.Values, b.Length));
            }
            return(CpuMathUtils.DotProductSparse(a.AsSpan(offset), b.Values, b.Indices, b.Count));
        }
コード例 #5
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(CpuMathUtils.DotProductDense(a.Values, b.Values, a.Length));
                }
                return(CpuMathUtils.DotProductSparse(a.Values, b.Values, b.Indices, b.Count));
            }

            if (b.IsDense)
            {
                return(CpuMathUtils.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
        public void DotUTest(string mode, string test, Dictionary <string, string> environmentVariables)
        {
            RemoteExecutor.RemoteInvoke((arg0, arg1) =>
            {
                CheckProperFlag(arg0);
                float[] src = (float[])_testArrays[int.Parse(arg1)].Clone();
                float[] dst = (float[])src.Clone();

                for (int i = 0; i < dst.Length; i++)
                {
                    dst[i] += 1;
                }

                float expected = 0;
                for (int i = 0; i < dst.Length; i++)
                {
                    expected += src[i] * dst[i];
                }

                var actual = CpuMathUtils.DotProductDense(src, dst, dst.Length);
                Assert.Equal(expected, actual, 1);
                return(RemoteExecutor.SuccessExitCode);
            }, mode, test, new RemoteInvokeOptions(environmentVariables));
        }
コード例 #7
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(CpuMathUtils.DotProductDense(a, b, a.Length));
 }
コード例 #8
0
 public float DotU()
 => CpuMathUtils.DotProductDense(src, dst, _smallInputLength);
コード例 #9
0
 public float ManagedDotUPerf() => CpuMathUtils.DotProductDense(src, dst, LEN);