public bool AddBatch <K>(double scalar, IReadOnlyOrderedMap <K, double> batch, out IReadOnlyOrderedMap <K, double> value)
        {
            var sm = batch as SortedMap <K, double>;

            if (!ReferenceEquals(sm, null))
            {
                double[] newValues = OptimizationSettings.ArrayPool.Take <double>(sm.size);
                double[] buffer    = new double[Vector <double> .Count];
                for (int c = 0; c < Vector <double> .Count; c++)
                {
                    buffer[c] = scalar;
                }
                var tempVector = new System.Numerics.Vector <double>(buffer);
                int i;
                for (i = 0; i < newValues.Length; i = i + Vector <double> .Count)
                {
                    var vec = new Vector <double>(sm.values, i);
                    vec = Vector.Add(vec, tempVector);
                    vec.CopyTo(newValues, i);
                }
                for (; i < newValues.Length; i++)
                {
                    newValues[i] = sm.values[i] + scalar;
                }

                var newKeys = sm.keys.ToArray();
                var newSm   = SortedMap <K, double> .OfSortedKeysAndValues(newKeys, newValues, sm.size, sm.Comparer, false, sm.IsRegular);

                value = newSm;
                return(true);
            }
            throw new NotImplementedException();
        }
Esempio n. 2
0
        public bool AddBatch <K>(double scalar, IReadOnlyOrderedMap <K, double> batch, out IReadOnlyOrderedMap <K, double> value)
        {
            var sm = batch as SortedMap <K, double>;

            if (!ReferenceEquals(sm, null))
            {
                double[] newValues = new double[sm.size];
                Yeppp.Core.Add_V64fS64f_V64f(sm.values, 0, scalar, newValues, 0, sm.size);
                //Yeppp.Math.Log_V64f_V64f(sm.values, 0, newValues, 0, sm.size);
                K[] newKeys;
                if (!sm.IsReadOnly)
                {
                    newKeys = sm.keys.ToArray();
                }
                else
                {
                    OptimizationSettings.ArrayPool.Borrow(sm.keys);
                    newKeys = sm.keys;
                }
                var newSm = SortedMap <K, double> .OfSortedKeysAndValues(newKeys, newValues, sm.size, sm.Comparer, false, sm.IsRegular);

                value = newSm;
                return(true);
            }
            throw new NotImplementedException();
        }
Esempio n. 3
0
        public void IncompleteMovingMedianIsCorrect()
        {
            var rng   = new System.Random();
            var count = 100000;
            var m     = rng.Next(10, 199);

            Console.WriteLine($"Window size: {m}");
            var keys = new int[count];
            var data = new double[count];

            //var sm = new SortedMap<int, double>(count);
            for (int i = 0; i < count; i++)
            {
                keys[i] = i;
                data[i] = rng.NextDouble();
                //sm.Add(i, data[i]);
            }
            var sm = SortedMap <int, double> .OfSortedKeysAndValues(keys, data, count);

            sm.Complete();
            var medianSeries  = sm.MovingMedian(m, true);
            var mm            = new MovingMedian(m);
            var medians       = new double[count];
            var naiveMedians  = new double[count];
            var cursorMedians = new double[count];

            var sw = new Stopwatch();

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                medians[i] = mm.Update(data[i]);
            }
            //var result = mm.Rngmed(data, medians);
            sw.Stop();
            Console.WriteLine($"Elapsed LIGO {sw.ElapsedMilliseconds} msec, {sw.MOPS(count):f2} Mops");

            sw.Restart();

            for (int i = 0; i < count; i++)
            {
                var arraySegment = new ArraySegment <double>(data, Math.Max(i - m + 1, 0), Math.Min(m, i + 1));
                //if (i + 1 >= m && arraySegment.Count != m) {
                //    Assert.Fail("Wrong array segment");
                //} else {
                //    Console.WriteLine($"{i} - {arraySegment.Offset} - {arraySegment.Count}");
                //}
                naiveMedians[i] = MovingMedian.NaiveMedian(arraySegment);
            }
            sw.Stop();
            Console.WriteLine($"Elapsed NAIVE {sw.ElapsedMilliseconds} msec, {sw.MOPS(count):f2} Mops");

            sw.Restart();
            var cursor = medianSeries.GetCursor();

            while (cursor.MoveNext())
            {
                cursorMedians[cursor.CurrentKey] = cursor.CurrentValue;
            }
            sw.Stop();
            Console.WriteLine($"Elapsed CURSOR {sw.ElapsedMilliseconds} msec, {sw.MOPS(count):f2} Mops");

            for (int i = 0; i < medians.Length; i++)
            {
                if (medians[i] != naiveMedians[i])
                {
                    Console.WriteLine($"{i} - LIGO {medians[i]} - Naive {naiveMedians[i]}");
                }
                Assert.AreEqual(medians[i], naiveMedians[i]);

                if (naiveMedians[i] != cursorMedians[i])
                {
                    Console.WriteLine($"{i} - CURSOR {cursorMedians[i]} - Naive {naiveMedians[i]}");
                }
                Assert.AreEqual(cursorMedians[i], naiveMedians[i]);
            }
        }
Esempio n. 4
0
        public void ContinuousZipIsCorrectByRandomCheck(SortedMap <int, int> sm1, SortedMap <int, int> sm2, int seed)
        {
            var series = new ISeries <int, int>[] { sm1.Repeat(), sm2 };

            int[] expectedKeys;
            int[] expectedValues;
            int   size;
            SortedMap <int, int> expectedMap;

            using (Benchmark.Run("Manual join", sm1.Count + sm2.Count))
            {
                var allKeys = sm1.keys.Union(sm2.keys).OrderBy(x => x).ToArray();

                expectedKeys = new int[allKeys.Length];

                expectedValues = new int[allKeys.Length];
                size           = 0;
                for (int i = 0; i < allKeys.Length; i++)
                {
                    var val = 0;
                    KeyValuePair <int, int> temp;
                    var hasFirst = sm1.TryFindAt(allKeys[i], Lookup.LE, out temp);
                    if (hasFirst)
                    {
                        val += temp.Value;
                        var hasSecond = sm2.TryFindAt(allKeys[i], Lookup.EQ, out temp);
                        if (hasSecond)
                        {
                            val += temp.Value;
                            expectedKeys[size]   = allKeys[i];
                            expectedValues[size] = val;
                            size++;
                        }
                    }
                }
                expectedMap = SortedMap <int, int> .OfSortedKeysAndValues(expectedKeys, expectedValues, size);
            }

            SortedMap <int, int> sum;
            Series <int, int>    ser;

            using (Benchmark.Run("Zip join", sm1.Count + sm2.Count))
            {
                sum = sm1.Repeat().Zip(sm2, (v1, v2) => v1 + v2).ToSortedMap();
            }
            Assert.AreEqual(expectedMap.Count, sum.Count, "Expected size");
            foreach (var kvp in expectedMap)
            {
                Assert.AreEqual(kvp.Value, sum[kvp.Key]);
            }

            //using (Benchmark.Run("ZipN join", sm1.Count + sm2.Count))
            //{
            //    ser = series.ZipOld((k, varr) => varr.Sum());
            //    sum = ser.ToSortedMap();
            //}
            //Assert.AreEqual(expectedMap.Count, sum.Count, "Expected size");
            //foreach (var kvp in expectedMap)
            //{
            //    Assert.AreEqual(kvp.Value, sum[kvp.Key]);
            //}

            var sum1 = new SortedMap <int, int>();

            using (Benchmark.Run("Zip Async join", sm1.Count + sm2.Count))
            {
                var zip  = sm1.Repeat().Zip(sm2, (v1, v2) => v1 + v2);
                var cur  = zip.GetAsyncCursor();
                var last = zip.Last.Present.Key;
                Task.Run(async() =>
                {
                    var prev = default(int);
                    while (await cur.MoveNextAsync())
                    {
                        if (cur.CurrentKey == prev)
                        {
                            Console.WriteLine($"Break on equal keys condition, seed {seed}");
                        }
                        prev = cur.CurrentKey;
                        sum1.Add(cur.CurrentKey, cur.CurrentValue);
                        //if (prev == last)
                        //{
                        //    Console.WriteLine("Next should be false");
                        //}
                    }
                })
                .Wait();
            }
            Assert.AreEqual(expectedMap.Count, sum1.Count, "Results of sync and async moves must be equal");
            foreach (var kvp in expectedMap)
            {
                Assert.AreEqual(kvp.Value, sum1[kvp.Key]);
            }

            // TODO this uses Subscribe which is not implemented yet
            //var sum2 = new SortedMap<int, int>();
            //using (Benchmark.Run("ZipN Async join", sm1.Count + sm2.Count))
            //{
            //    var cur = ser.GetCursor();

            //    var cur2 = cur.Clone();

            //    Task.Run(async () =>
            //        {
            //            while (await cur2.MoveNextAsync(CancellationToken.None))
            //            {
            //                sum2.Add(cur2.CurrentKey, cur2.CurrentValue);
            //            }
            //        })
            //        .Wait();
            //}

            //Assert.AreEqual(sum.Count, sum2.Count, "Results of sync and async moves must be equal");
            //foreach (var kvp in expectedMap)
            //{
            //    Assert.AreEqual(kvp.Value, sum2[kvp.Key]);
            //}
        }