Esempio n. 1
0
        public virtual void TestNewEmptyClone(ConcatVectorTest.DenseTestVector d1)
        {
            ConcatVector empty      = new ConcatVector(d1.vector.GetNumberOfComponents());
            ConcatVector emptyClone = d1.vector.NewEmptyClone();

            NUnit.Framework.Assert.IsTrue(empty.ValueEquals(emptyClone, 1.0e-5));
        }
Esempio n. 2
0
        public virtual void TestElementwiseProduct(ConcatVectorTest.DenseTestVector d1, ConcatVectorTest.DenseTestVector d2)
        {
            for (int i = 0; i < d1.values.Length; i++)
            {
                for (int j = 0; j < d1.values[i].Length; j++)
                {
                    Assume.AssumeTrue(d1.values[i][j] == d1.vector.GetValueAt(i, j));
                }
            }
            for (int i_1 = 0; i_1 < d2.values.Length; i_1++)
            {
                for (int j = 0; j < d2.values[i_1].Length; j++)
                {
                    Assume.AssumeTrue(d2.values[i_1][j] == d2.vector.GetValueAt(i_1, j));
                }
            }
            ConcatVector clone = d1.vector.DeepClone();

            clone.ElementwiseProductInPlace(d2.vector);
            for (int i_2 = 0; i_2 < d1.values.Length; i_2++)
            {
                for (int j = 0; j < d1.values[i_2].Length; j++)
                {
                    double val = 0.0f;
                    if (i_2 < d2.values.Length)
                    {
                        if (j < d2.values[i_2].Length)
                        {
                            val = d1.values[i_2][j] * d2.values[i_2][j];
                        }
                    }
                    NUnit.Framework.Assert.AreEqual(clone.GetValueAt(i_2, j), 5.0e-4, val);
                }
            }
        }
Esempio n. 3
0
        public virtual void TestAddVector(ConcatVectorTest.DenseTestVector d1, ConcatVectorTest.DenseTestVector d2, ConcatVectorTest.DenseTestVector d3)
        {
            // Test the invariant x^Tz + 0.7*y^Tz == (x+0.7*y)^Tz
            double       expected = d1.vector.DotProduct(d3.vector) + (0.7f * d2.vector.DotProduct(d3.vector));
            ConcatVector clone    = d1.vector.DeepClone();

            clone.AddVectorInPlace(d2.vector, 0.7f);
            NUnit.Framework.Assert.AreEqual(clone.DotProduct(d3.vector), 5.0e-4, expected);
        }
Esempio n. 4
0
 public virtual void TestGetValueAt(ConcatVectorTest.DenseTestVector d1)
 {
     for (int i = 0; i < d1.values.Length; i++)
     {
         for (int j = 0; j < d1.values[i].Length; j++)
         {
             NUnit.Framework.Assert.AreEqual(d1.vector.GetValueAt(i, j), 5.0e-4, d1.values[i][j]);
         }
     }
 }
Esempio n. 5
0
            public virtual double TrueInnerProduct(ConcatVectorTest.DenseTestVector testVector)
            {
                double sum = 0.0f;

                for (int i = 0; i < Math.Min(values.Length, testVector.values.Length); i++)
                {
                    for (int j = 0; j < Math.Min(values[i].Length, testVector.values[i].Length); j++)
                    {
                        sum += values[i][j] * testVector.values[i][j];
                    }
                }
                return(sum);
            }
Esempio n. 6
0
        public virtual void TestRetrieveDense(ConcatVectorTest.DenseTestVector d1)
        {
            int size = d1.vector.GetNumberOfComponents();

            NUnit.Framework.Assert.AreEqual(d1.values.Length, size);
            for (int i = 0; i < d1.values.Length; i++)
            {
                if (!d1.vector.IsComponentSparse(i))
                {
                    Assert.AssertArrayEquals(d1.vector.GetDenseComponent(i), 1.0e-9, d1.values[i]);
                }
            }
        }
Esempio n. 7
0
        public virtual void TestInnerProduct2(ConcatVectorTest.DenseTestVector d1, ConcatVectorTest.DenseTestVector d2, ConcatVectorTest.DenseTestVector d3)
        {
            // Test the invariant x^Tz + 0.7*y^Tz == (x+0.7*y)^Tz
            double d1d3 = d1.vector.DotProduct(d3.vector);

            NUnit.Framework.Assert.AreEqual(d1d3, 5.0e-4, d1.TrueInnerProduct((ConcatVectorTest.DenseTestVector)d3));
            double d2d3 = d2.vector.DotProduct(d3.vector);

            NUnit.Framework.Assert.AreEqual(d2d3, 5.0e-4, d2.TrueInnerProduct((ConcatVectorTest.DenseTestVector)d3));
            double expected = d1d3 + (0.7f * d2d3);

            NUnit.Framework.Assert.AreEqual(expected, 5.0e-4, d1.TrueInnerProduct((ConcatVectorTest.DenseTestVector)d3) + (0.7 * d2.TrueInnerProduct((ConcatVectorTest.DenseTestVector)d3)));
        }
Esempio n. 8
0
        public virtual void TestDeepCloneGetValueAt(ConcatVectorTest.DenseTestVector d1)
        {
            ConcatVector mv    = d1.vector;
            ConcatVector clone = d1.vector.DeepClone();

            for (int i = 0; i < d1.values.Length; i++)
            {
                for (int j = 0; j < d1.values[i].Length; j++)
                {
                    NUnit.Framework.Assert.AreEqual(clone.GetValueAt(i, j), 1.0e-10, mv.GetValueAt(i, j));
                }
            }
        }
Esempio n. 9
0
        public virtual void TestCopyOnWrite(ConcatVectorTest.DenseTestVector d1)
        {
            ConcatVector v2 = d1.vector.DeepClone();

            v2.AddVectorInPlace(v2, 1.0);
            for (int i = 0; i < d1.values.Length; i++)
            {
                for (int j = 0; j < d1.values[i].Length; j++)
                {
                    NUnit.Framework.Assert.AreEqual(d1.vector.GetValueAt(i, j), 5.0e-4, d1.values[i][j]);
                    NUnit.Framework.Assert.AreEqual(v2.GetValueAt(i, j), 5.0e-4, d1.values[i][j] * 2);
                }
            }
        }
Esempio n. 10
0
        public virtual void TestProtoVector(ConcatVectorTest.DenseTestVector d1, ConcatVectorTest.DenseTestVector d2)
        {
            double expected = d1.vector.DotProduct(d2.vector);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            System.Diagnostics.Debug.Assert((d1.vector.GetType() == typeof(ConcatVector)));
            d1.vector.WriteToStream(byteArrayOutputStream);
            byteArrayOutputStream.Close();
            byte[] bytes = byteArrayOutputStream.ToByteArray();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            ConcatVector         recovered            = ConcatVector.ReadFromStream(byteArrayInputStream);

            NUnit.Framework.Assert.AreEqual(recovered.DotProduct(d2.vector), 5.0e-4, expected);
        }
Esempio n. 11
0
        public virtual void TestValueEquals(ConcatVectorTest.DenseTestVector d1)
        {
            ConcatVector clone = d1.vector.DeepClone();

            NUnit.Framework.Assert.IsTrue(clone.ValueEquals(d1.vector, 1.0e-5));
            NUnit.Framework.Assert.IsTrue(d1.vector.ValueEquals(clone, 1.0e-5));
            NUnit.Framework.Assert.IsTrue(d1.vector.ValueEquals(d1.vector, 1.0e-5));
            NUnit.Framework.Assert.IsTrue(clone.ValueEquals(clone, 1.0e-5));
            Random r    = new Random();
            int    size = clone.GetNumberOfComponents();

            if (size > 0)
            {
                clone.AddVectorInPlace(d1.vector, 1.0);
                // If the clone is a 0 vector
                bool isZero = true;
                foreach (double[] arr in d1.values)
                {
                    foreach (double d in arr)
                    {
                        if (d != 0)
                        {
                            isZero = false;
                        }
                    }
                }
                if (isZero)
                {
                    NUnit.Framework.Assert.IsTrue(clone.ValueEquals(d1.vector, 1.0e-5));
                    NUnit.Framework.Assert.IsTrue(d1.vector.ValueEquals(clone, 1.0e-5));
                }
                else
                {
                    NUnit.Framework.Assert.IsFalse(clone.ValueEquals(d1.vector, 1.0e-5));
                    NUnit.Framework.Assert.IsFalse(d1.vector.ValueEquals(clone, 1.0e-5));
                }
                NUnit.Framework.Assert.IsTrue(d1.vector.ValueEquals(d1.vector, 1.0e-5));
                NUnit.Framework.Assert.IsTrue(clone.ValueEquals(clone, 1.0e-5));
                // refresh the clone
                clone = d1.vector.DeepClone();
                int tinker = r.NextInt(size);
                d1.vector.SetDenseComponent(tinker, new double[] { 0, 0, 1 });
                clone.SetSparseComponent(tinker, 2, 1);
                NUnit.Framework.Assert.IsTrue(d1.vector.ValueEquals(clone, 1.0e-5));
                NUnit.Framework.Assert.IsTrue(clone.ValueEquals(d1.vector, 1.0e-5));
            }
        }
Esempio n. 12
0
        public virtual void TestIsSparse(ConcatVectorTest.DenseTestVector d1)
        {
            int size = d1.vector.GetNumberOfComponents();

            NUnit.Framework.Assert.AreEqual(d1.values.Length, size);
            for (int i = 0; i < d1.values.Length; i++)
            {
                if (d1.vector.IsComponentSparse(i))
                {
                    for (int j = 0; j < d1.values[i].Length; j++)
                    {
                        if (d1.vector.GetSparseIndex(i) != j)
                        {
                            NUnit.Framework.Assert.AreEqual(d1.values[i][j], 1.0e-9, 0.0);
                        }
                    }
                }
            }
        }
Esempio n. 13
0
 public virtual void TestResizeOnSetComponent(ConcatVectorTest.DenseTestVector d1)
 {
     d1.vector.SetSparseComponent(d1.values.Length, 1, 0.0);
     d1.vector.SetDenseComponent(d1.values.Length + 1, new double[] { 0.0 });
 }
Esempio n. 14
0
        public virtual void TestSizes(ConcatVectorTest.DenseTestVector d1)
        {
            int size = d1.vector.GetNumberOfComponents();

            NUnit.Framework.Assert.AreEqual(d1.values.Length, size);
        }
Esempio n. 15
0
 public virtual void TestDeepClone(ConcatVectorTest.DenseTestVector d1, ConcatVectorTest.DenseTestVector d2)
 {
     NUnit.Framework.Assert.AreEqual(d1.vector.DeepClone().DotProduct(d2.vector), 5.0e-4, d1.vector.DotProduct(d2.vector));
 }
Esempio n. 16
0
 public virtual void TestInnerProduct(ConcatVectorTest.DenseTestVector d1, ConcatVectorTest.DenseTestVector d2)
 {
     NUnit.Framework.Assert.AreEqual(d1.vector.DotProduct(d2.vector) + d2.vector.DotProduct(d2.vector), 5.0e-4, d1.TrueInnerProduct((ConcatVectorTest.DenseTestVector)d2) + d2.TrueInnerProduct((ConcatVectorTest.DenseTestVector)d2));
 }