Exemple #1
0
        public static double GetInnerDot(DoubleVec vec1, DoubleVec vec2)
        {
            double innerDot = 0.0d;

            double[] vec1a = vec1.GetVec();
            double[] vec2a = vec2.GetVec();
            // proceed the calculation only if same size
            if (vec1.GetSize() == vec2.GetSize())
            {
                for (int i = 0; i < vec1.GetSize(); i++)
                {
                    innerDot += (vec1a[i] * vec2a[i]);
                }
            }
            return(innerDot);
        }
        // private methods
        private static void Test()
        {
            DoubleVec vec = new DoubleVec(5);

            Console.WriteLine("- size: " + vec.GetSize());
            Console.WriteLine("- vec: " + vec.ToString());
            double[]  a1 = new double[] { 1.0d, 2.0d, 3.0d };
            DoubleVec v1 = new DoubleVec(a1);

            Console.WriteLine("- size: " + v1.GetSize());
            Console.WriteLine("- v1: " + v1.ToString());
            a1[0] = 5.0d;
            Console.WriteLine("- v1 (check ref): " + v1.ToString());
            double[]  a2 = new double[] { 3.0d, 5.0d, 3.0d };
            DoubleVec v2 = new DoubleVec(a2);

            Console.WriteLine("- v2: " + v2.ToString() + ", size:" + v2.GetSize());
            v2.Add(v1);
            Console.WriteLine("- Add, v1:  " + v1.ToString());
            Console.WriteLine("- Add, v2: " + v2.ToString());
            v2.Minus(v1);
            Console.WriteLine("- Minus, v1:  " + v1.ToString());
            Console.WriteLine("- Minus, v2: " + v2.ToString());
            v2.Divide(4);
            Console.WriteLine("- Div, v1: " + v1.ToString());
            Console.WriteLine("- Div, v2: " + v2.ToString());
        }
 public virtual void Minus(DoubleVec vec)
 {
     if ((vec != null) && (GetSize() == vec.GetSize()))
     {
         for (int i = 0; i < GetSize(); i++)
         {
             vec_[i] = vec_[i] - vec.GetElement(i);
         }
     }
 }
Exemple #4
0
        // public methods
        public static DoubleVec Divide(DoubleVec vec, int count)
        {
            DoubleVec outVec = null;

            if ((count != 0) && (vec != null))
            {
                outVec = new DoubleVec(vec.GetSize());
                for (int i = 0; i < outVec.GetSize(); i++)
                {
                    outVec.SetElement(vec.GetElement(i) / count, i);
                }
            }
            return(outVec);
        }
Exemple #5
0
        public static DoubleVec Minus(DoubleVec vec1, DoubleVec vec2)
        {
            int       size1  = vec1.GetSize();
            int       size2  = vec2.GetSize();
            DoubleVec outVec = new DoubleVec(size1);

            if (size1 == size2)
            {
                for (int i = 0; i < size1; i++)
                {
                    double @out = vec1.GetElement(i) - vec2.GetElement(i);
                    outVec.SetElement(@out, i);
                }
            }
            return(outVec);
        }
Exemple #6
0
        //the output is between 0.0 ~ 1.0
        public static double GetCosineSimilarity(DoubleVec vec1, DoubleVec vec2)
        {
            double similarity = 0.0d;
            double length1    = GetLength(vec1);
            double length2    = GetLength(vec2);

            //must has same size
            if ((vec1 != null) && (vec2 != null) && (length1 > 0.0d) && (length2 > 0.0d) && (vec1.GetSize() == vec2.GetSize()))
            {
                similarity = GetInnerDot(vec1, vec2) / (length1 * length2);
            }
            return(similarity);
        }