Exemplo n.º 1
0
        public virtual void TestAddSparseToSparse(int sparseIndex1, double val1, int sparseIndex2, double val2)
        {
            ConcatVector v1 = new ConcatVector(1);

            v1.SetSparseComponent(0, (int)sparseIndex1, val1);
            ConcatVector v2 = new ConcatVector(1);

            v2.SetSparseComponent(0, (int)sparseIndex2, val2);
            double expected = v1.DotProduct(v2) + 0.7f * (v2.DotProduct(v2));

            v1.AddVectorInPlace(v2, 0.7f);
            NUnit.Framework.Assert.AreEqual(v1.DotProduct(v2), 5.0e-3, expected);
        }
Exemplo n.º 2
0
        public virtual void TestAddSparseToDense(double[] dense1, int sparseIndex, double v)
        {
            ConcatVector v1 = new ConcatVector(1);

            v1.SetDenseComponent(0, dense1);
            ConcatVector v2 = new ConcatVector(1);

            v2.SetSparseComponent(0, (int)sparseIndex, v);
            double expected = v1.DotProduct(v2) + 0.7f * (v2.DotProduct(v2));

            v1.AddVectorInPlace(v2, 0.7f);
            NUnit.Framework.Assert.AreEqual(v1.DotProduct(v2), 5.0e-4, expected);
        }
Exemplo n.º 3
0
        public virtual void TestAppendSparseComponent(int sparse1, double sparse1Val, int sparse2, double sparse2Val)
        {
            ConcatVector v1 = new ConcatVector(1);
            ConcatVector v2 = new ConcatVector(1);

            v1.SetSparseComponent(0, (int)sparse1, sparse1Val);
            v2.SetSparseComponent(0, (int)sparse2, sparse2Val);
            if (sparse1 == sparse2)
            {
                NUnit.Framework.Assert.AreEqual(v1.DotProduct(v2), 5.0e-4, sparse1Val * sparse2Val);
            }
            else
            {
                NUnit.Framework.Assert.AreEqual(v1.DotProduct(v2), 5.0e-4, 0.0);
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public virtual void TestResizeOnSetComponent(IDictionary <int, int> featureMap1, IDictionary <int, int> featureMap2)
        {
            ConcatVectorNamespace @namespace = new ConcatVectorNamespace();
            ConcatVector          namespace1 = ToNamespaceVector(@namespace, (IDictionary <int, int>)featureMap1);
            ConcatVector          namespace2 = ToNamespaceVector(@namespace, (IDictionary <int, int>)featureMap2);
            ConcatVector          regular1   = ToVector((IDictionary <int, int>)featureMap1);
            ConcatVector          regular2   = ToVector((IDictionary <int, int>)featureMap2);

            NUnit.Framework.Assert.AreEqual(namespace1.DotProduct(namespace2), 1.0e-5, regular1.DotProduct(regular2));
            ConcatVector namespaceSum = namespace1.DeepClone();

            namespaceSum.AddVectorInPlace(namespace2, 1.0);
            ConcatVector regularSum = regular1.DeepClone();

            regularSum.AddVectorInPlace(regular2, 1.0);
            NUnit.Framework.Assert.AreEqual(namespace1.DotProduct(namespaceSum), 1.0e-5, regular1.DotProduct(regularSum));
            NUnit.Framework.Assert.AreEqual(namespaceSum.DotProduct(namespace2), 1.0e-5, regularSum.DotProduct(regular2));
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        public virtual void TestAppendDenseComponent(double[] vector1, double[] vector2)
        {
            ConcatVector v1 = new ConcatVector(1);
            ConcatVector v2 = new ConcatVector(1);

            v1.SetDenseComponent(0, vector1);
            v2.SetDenseComponent(0, vector2);
            double sum = 0.0f;

            for (int i = 0; i < Math.Min(vector1.Length, vector2.Length); i++)
            {
                sum += vector1[i] * vector2[i];
            }
            NUnit.Framework.Assert.AreEqual(v1.DotProduct(v2), 5.0e-4, sum);
        }