public void HeapSortWithRandomIntegerArray()
 {
     var sortedIndices = new[] {5, 2, 8, 6, 0, 4, 1, 7, 3, 9};
     ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
     for (var i = 0; i < sortedIndices.Length; i++)
     {
         Assert.AreEqual(sortedIndices.Length - 1 - i, sortedIndices[i], "#01-" + i);
     }
 }
 public void CanConvertArrayToDenseVector()
 {
     var array = new[] {new Complex(1, 1), new Complex(2, 1), new Complex(3, 1), new Complex(4, 1)};
     var vector = (DenseVector) array;
     Assert.IsInstanceOf(typeof (DenseVector), vector);
     CollectionAssert.AreEqual(array, array);
 }
 public void CanConvertArrayToDenseVector()
 {
     var array = new[] { 0.0, 1.0, 2.0, 3.0, 4.0 };
     var vector = (DenseVector)array;
     Assert.IsInstanceOf(typeof(DenseVector), vector);
     CollectionAssert.AreEqual(array, array);
 }
        public void CanPointwiseMultiplySparseVector()
        {
            var zeroArray = new[] {Complex.Zero, new Complex(1.0, 1), Complex.Zero, new Complex(1.0, 1), Complex.Zero};
            var vector1 = SparseVector.OfEnumerable(Data);
            var vector2 = SparseVector.OfEnumerable(zeroArray);
            var result = new SparseVector(vector1.Count);

            vector1.PointwiseMultiply(vector2, result);

            for (var i = 0; i < vector1.Count; i++)
            {
                Assert.AreEqual(Data[i]*zeroArray[i], result[i]);
            }

            var resultStorage = (SparseVectorStorage<Complex>) result.Storage;
            Assert.AreEqual(2, resultStorage.ValueCount);
        }
 public void CanConvertArrayToSparseVector()
 {
     var array = new[] {new Complex(1, 1), new Complex(2, 1), new Complex(3, 1), new Complex(4, 1)};
     var vector = SparseVector.OfEnumerable(array);
     Assert.IsInstanceOf(typeof (SparseVector), vector);
     CollectionAssert.AreEqual(array, array);
 }
 public void HeapSortWithDuplicateEntries()
 {
     var sortedIndices = new[] {1, 1, 1, 1, 2, 2, 2, 2, 3, 4};
     ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
     for (var i = 0; i < sortedIndices.Length; i++)
     {
         if (i == 0)
         {
             Assert.AreEqual(4, sortedIndices[i], "#01-" + i);
         }
         else
         {
             if (i == 1)
             {
                 Assert.AreEqual(3, sortedIndices[i], "#01-" + i);
             }
             else
             {
                 if (i < 6)
                 {
                     if (sortedIndices[i] != 2)
                     {
                         Assert.Fail("#01-" + i);
                     }
                 }
                 else
                 {
                     if (sortedIndices[i] != 1)
                     {
                         Assert.Fail("#01-" + i);
                     }
                 }
             }
         }
     }
 }
        public void HeapSortWithSpecialConstructedIntegerArray()
        {
            var sortedIndices = new[] {0, 0, 0, 0, 0, 1, 0, 0, 0, 0};
            ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                if (i == 0)
                {
                    Assert.AreEqual(1, sortedIndices[i], "#01-" + i);
                    break;
                }
            }

            sortedIndices = new[] {1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                if (i == 0)
                {
                    Assert.AreEqual(1, sortedIndices[i], "#02-" + i);
                    break;
                }
            }

            sortedIndices = new[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
            ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                if (i == 0)
                {
                    Assert.AreEqual(1, sortedIndices[i], "#03-" + i);
                    break;
                }
            }

            sortedIndices = new[] {1, 1, 1, 0, 1, 1, 1, 1, 1, 1};
            ILUTPElementSorter.SortIntegersDecreasing(sortedIndices);
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                if (i == 9)
                {
                    Assert.AreEqual(0, sortedIndices[i], "#04-" + i);
                    break;
                }
            }
        }
        public void CanPointwiseDivideToSelfSparseVector()
        {
            var zeroArray = new[] { 0.0, 2.0, 0.0, 2.0, 0.0 };
            var vector = Vector<double>.Build.SparseOfEnumerable(zeroArray);
            var result = Vector<double>.Build.Sparse(vector.Count);

            vector.PointwiseDivide(vector, result);

            for (var i = 0; i < vector.Count; i++)
            {
                Assert.AreEqual(zeroArray[i] / zeroArray[i], result[i]);
            }

            var resultStorage = (SparseVectorStorage<double>)result.Storage;
            Assert.AreEqual(5, resultStorage.ValueCount);
        }
        public void CanPointwiseMultiplySparseVector()
        {
            var zeroArray = new[] {0.0, 1.0, 0.0, 1.0, 0.0};
            var vector1 = Vector<double>.Build.SparseOfEnumerable(Data);
            var vector2 = Vector<double>.Build.SparseOfEnumerable(zeroArray);
            var result = Vector<double>.Build.Sparse(vector1.Count);

            vector1.PointwiseMultiply(vector2, result);

            for (var i = 0; i < vector1.Count; i++)
            {
                Assert.AreEqual(Data[i]*zeroArray[i], result[i]);
            }

            var resultStorage = (SparseVectorStorage<double>) result.Storage;
            Assert.AreEqual(2, resultStorage.ValueCount);
        }
 public void CanConvertArrayToSparseVector()
 {
     var array = new[] {0.0, 1.0, 2.0, 3.0, 4.0};
     var vector = Vector<double>.Build.SparseOfEnumerable(array);
     Assert.IsInstanceOf(typeof (SparseVector), vector);
     CollectionAssert.AreEqual(array, array);
 }
        /// <summary>
        /// Test the WeightedConditioner class which is responsible for "conditioning"
        /// the A and b matrices in the equation Ax=b when solving for x.  b contains
        /// transition intensity values to be deconvolved, A encodes the precursor isolation
        /// window scheme used.  The conditioner weights the entries in the center rows of each
        /// matrix higher than those on the edges.
        /// </summary>
        private static void TestWeightedMatrixPrepare()
        {
            // If the matrices have less than 5 rows, the conditioner should have
            // no effect.
            DeconvBlock dbSmall = new DeconvBlock(5,5,4);
            double[] mask = {1.0,1.0,1.0,1.0, 0.0};
            double[] data = {2.0,2.0,2.0,2.0};
            for (int i = 0; i<4; ++i)
            {
                dbSmall.Add(mask, data);
            }
            var conditioner = new WeightedConditioner();
            conditioner.Condition(dbSmall);
            var conditionedMasks = dbSmall.Masks;
            var conditionedData = dbSmall.BinnedData;
            for (int i = 0; i<4; ++i)
            {
                var conditionedMask = conditionedMasks.Matrix.Row(i, 0, 5).ToArray();
                var condData = conditionedData.Matrix.Row(i, 0, 4).ToArray();
                AssertEx.AreEqualDeep(mask, conditionedMask);
                AssertEx.AreEqualDeep(data, condData);
            }

            // If the matrices have more than 5 rows, check that the weighting
            // is applied correctly.
            DeconvBlock dbLarge = new DeconvBlock(5, 11, 4);
            for (int i = 0; i<11; ++i)
            {
                dbLarge.Add(mask, data);
            }
            conditioner.Condition(dbLarge);
            conditionedMasks = dbLarge.Masks;
            conditionedData = dbLarge.BinnedData;
            var expectedMask = new[] {-0.086, -0.086, -0.086, -0.086, 0.0};
            var expectedData = new[] {2*-0.086, 2*-0.086, 2*-0.086, 2*-0.086};
            for (int i = 0; i<2; ++i)
            {
                var conditionedMask = conditionedMasks.Matrix.Row(i, 0, 5).ToArray();
                var condData = conditionedData.Matrix.Row(i, 0, 4).ToArray();
                AssertEx.AreEqualDeep(expectedMask, conditionedMask);
                AssertEx.AreEqualDeep(expectedData, condData);
            }
            expectedMask = new[] {0.343, 0.343, 0.343, 0.343, 0.0};
            expectedData = new[] {2*0.343, 2*0.343, 2*0.343, 2*0.343};
            for (int i = 2; i<4; ++i)
            {
                var conditionedMask = conditionedMasks.Matrix.Row(i, 0, 5).ToArray();
                var condData = conditionedData.Matrix.Row(i, 0, 4).ToArray();
                AssertEx.AreEqualDeep(expectedMask, conditionedMask);
                AssertEx.AreEqualDeep(expectedData, condData);
            }

            expectedMask = new[] {0.486, 0.486, 0.486, 0.486, 0.0};
            expectedData = new[] {2*0.486, 2*0.486, 2*0.486, 2*0.486};
            for (int i = 4; i<6; ++i)
            {
                var conditionedMask = conditionedMasks.Matrix.Row(i, 0, 5).ToArray();
                var condData = conditionedData.Matrix.Row(i, 0, 4).ToArray();
                AssertEx.AreEqualDeep(expectedMask, conditionedMask);
                AssertEx.AreEqualDeep(expectedData, condData);
            }

            expectedMask = new[] {0.343, 0.343, 0.343, 0.343, 0.0};
            expectedData = new[] {2*0.343, 2*0.343, 2*0.343, 2*0.343};
            for (int i = 6; i<8; ++i)
            {
                var conditionedMask = conditionedMasks.Matrix.Row(i, 0, 5).ToArray();
                var condData = conditionedData.Matrix.Row(i, 0, 4).ToArray();
                AssertEx.AreEqualDeep(expectedMask, conditionedMask);
                AssertEx.AreEqualDeep(expectedData, condData);
            }

            expectedMask = new[] {-0.086, -0.086, -0.086, -0.086, 0.0};
            expectedData = new[] {2*-0.086, 2*-0.086, 2*-0.086, 2*-0.086};
            for (int i = 8; i<11; ++i)
            {
                var conditionedMask = conditionedMasks.Matrix.Row(i, 0, 5).ToArray();
                var condData = conditionedData.Matrix.Row(i, 0, 4).ToArray();
                AssertEx.AreEqualDeep(expectedMask, conditionedMask);
                AssertEx.AreEqualDeep(expectedData, condData);
            }
        }