public void SumSqUTest(int test, float expected)
        {
            float[] src    = (float[])_testArrays[test].Clone();
            var     actual = CpuMathUtils.SumSq(src, src.Length);

            Assert.Equal(expected, actual, 2);
        }
Exemple #2
0
        public void SumSqDiffUTest(int test, float expected)
        {
            float[] src    = (float[])testArrays[test].Clone();
            var     actual = CpuMathUtils.SumSq(DEFAULT_SCALE, src, 0, src.Length);

            Assert.Equal(expected, actual, 2);
        }
        public void SumSqDiffUTest(int test, float expected)
        {
            float[] src    = (float[])_testArrays[test].Clone();
            var     actual = CpuMathUtils.SumSq(DefaultScale, src);

            Assert.Equal(expected, actual, 2);
        }
Exemple #4
0
 /// <summary>
 /// Compute L2-norm. L2-norm computation doesn't subtract the mean from the source values.
 /// However, we substract the mean here in case subMean is true (if subMean is false, mean is zero).
 /// </summary>
 private static Float L2Norm(Float[] values, int count, Float mean = 0)
 {
     if (count == 0)
     {
         return(0);
     }
     return(MathUtils.Sqrt(CpuMathUtils.SumSq(mean, values, 0, count)));
 }
Exemple #5
0
        public void SumSqDiffUTest(int test, float defaultScale)
        {
            float[] src    = (float[])_testArrays[test].Clone();
            var     actual = CpuMathUtils.SumSq(defaultScale, src);

            float expected = 0;

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

            Assert.Equal(expected, actual, 2);
        }
Exemple #6
0
        public void SumSqUTest(string mode, string test, Dictionary <string, string> environmentVariables)
        {
            RemoteExecutor.RemoteInvoke((arg0, arg1) =>
            {
                CheckProperFlag(arg0);
                float[] src    = (float[])_testArrays[int.Parse(arg1)].Clone();
                float expected = 0;
                for (int i = 0; i < src.Length; i++)
                {
                    expected += src[i] * src[i];
                }

                var actual = CpuMathUtils.SumSq(src);
                Assert.Equal(expected, actual, 2);
                return(RemoteExecutor.SuccessExitCode);
            }, mode, test, new RemoteInvokeOptions(environmentVariables));
        }
Exemple #7
0
        /// <summary>
        /// Compute Standard Deviation.
        /// We have two overloads of StdDev instead of one with <see cref="Nullable{Float}"/> mean for perf reasons.
        /// </summary>
        private static Float StdDev(Float[] values, int count, int length, Float mean)
        {
            Contracts.Assert(0 <= count && count <= length);
            if (count == 0)
            {
                return(0);
            }
            Float sumSq = 0;

            if (count != length && mean != 0)
            {
                // Sparse representation.
                Float meanSq = mean * mean;
                sumSq = (length - count) * meanSq;
            }
            sumSq += CpuMathUtils.SumSq(mean, values, 0, count);
            return(MathUtils.Sqrt(sumSq / length));
        }
Exemple #8
0
        public void SumSqDiffUTest(string mode, string test, string scale, Dictionary <string, string> environmentVariables)
        {
            RemoteExecutor.RemoteInvoke((arg0, arg1, arg2) =>
            {
                CheckProperFlag(arg0);
                float defaultScale = float.Parse(arg2, CultureInfo.InvariantCulture);
                float[] src        = (float[])_testArrays[int.Parse(arg1)].Clone();
                var actual         = CpuMathUtils.SumSq(defaultScale, src);

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

                Assert.Equal(expected, actual, 2);
                return(RemoteExecutor.SuccessExitCode);
            }, mode, test, scale, new RemoteInvokeOptions(environmentVariables));
        }
Exemple #9
0
        /// <summary>
        /// Compute Standard Deviation. In case of both subMean and useStd are true, we technically need to compute variance
        /// based on centered values (i.e. after subtracting the mean). But since the centered
        /// values mean is approximately zero, we can use variance of non-centered values.
        /// </summary>
        private static Float StdDev(Float[] values, int count, int length)
        {
            Contracts.Assert(0 <= count && count <= length);
            if (count == 0)
            {
                return(0);
            }
            // We need a mean to compute variance.
            Float tmpMean = CpuMathUtils.Sum(values, 0, count) / length;
            Float sumSq   = 0;

            if (count != length && tmpMean != 0)
            {
                // Sparse representation.
                Float meanSq = tmpMean * tmpMean;
                sumSq = (length - count) * meanSq;
            }
            sumSq += CpuMathUtils.SumSq(tmpMean, values, 0, count);
            return(MathUtils.Sqrt(sumSq / length));
        }
        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 = CpuMathUtils.SumSq(valuesA.AsSpan(0, lengthA));

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

            return(res < 0 ? 0 : res);
        }
 /// <summary>
 /// Returns the L2 norm of the vector (sum of squares of the components).
 /// </summary>
 public static Float Norm(Float[] a)
 {
     return(MathUtils.Sqrt(CpuMathUtils.SumSq(a)));
 }
Exemple #12
0
 public float SumSqDiffU()
 => CpuMathUtils.SumSq(DefaultScale, src.AsSpan(0, _smallInputLength));
Exemple #13
0
 public float SumSqU()
 => CpuMathUtils.SumSq(new Span <float>(src, 0, _smallInputLength));
 public float ManagedSumSqUPerf() => CpuMathUtils.SumSq(src, LEN);
Exemple #15
0
 public float ManagedSumSqDiffUPerf() => CpuMathUtils.SumSq(DEFAULT_SCALE, src, 0, LEN);