C#——Dictionary 计算向量的余弦值

说明:三角函数的余弦值Cos我想,每个学计算机的理工人都知道,但是真的明白它的用途,我也是刚明白。每个人在初中或者高中的时候,都有这么个疑惑,学三角函数干什么用的?很直白的答案就是考试用的。而且当时的老师只管教,只管怎么解题,至于将学习的知识运用到生活中,没有这门课堂。最终的结果却是,我们只知道学,不知道用。说来也惭愧啊,我也是看了吴军博士的《数学之美》,才领悟到的。这本书真的给我很多的启发。

Cos的用途:

  1. 考试用。
  2. 通过计算2个向量,可以知道他们的相似度。余弦值越小,则2个向量越垂直,余弦值越接近1,则这个向量就越平行(相似)。这样,我们就可以将向量抽象为事物的特征集合了。计算向量的余弦值,就可以判断事物的相似度。至于详细的运用领域,还是读读这本书吧。

代码中的Cosine.cs是我很早从网上搜到的,地址也忘了。

计算代码如下:

Cosine.cs类

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace ConsoleApplication1

{

    class Cosine

    {

        /// <summary>

        /// 计算向量余弦值

        /// </summary>

        /// <param name="vector1"></param>

        /// <param name="vector2"></param>

        public static double Calculate(Dictionary<int, double> vector1, Dictionary<int, double> vector2)

        {

            double dotProduct = CalcDotProduct(vector1, vector2);

            double length1 = CalcLength(vector1);

            double length2 = CalcLength(vector2);

            double cosine = dotProduct / (length1 * length2);

            return cosine;

        }

        /// <summary>

        /// 计算向量长度(vector length)

        /// </summary>

        /// <param name="vector"></param>

        /// <returns></returns>

        private static double CalcLength(Dictionary<int, double> vector)

        {

            double length = 0;

            foreach (KeyValuePair<int, double> kvp in vector)

            {

                length += Math.Pow(kvp.Value, 2);

            }

            return Math.Sqrt(length);

        }



        /// <summary>

        /// 计算向量点积(dot product)/内积(inner product)

        /// </summary>

        /// <param name="vector1"></param>

        /// <param name="vector2"></param>

        /// <returns></returns>

        private static double CalcDotProduct(Dictionary<int, double> vector1, Dictionary<int, double> vector2)

        {

            double dotProduct = 0;

            foreach (KeyValuePair<int, double> kvp in vector1)

            {

                if (vector2.ContainsKey(kvp.Key))

                {

                    dotProduct += kvp.Value * vector2[kvp.Key];

                }

            }

            return dotProduct;

        }

    }

}

 

Program类,是我的,嘿嘿。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace ConsoleApplication1

{

    class Program

    {

        //自定义输入函数

        static void FunInput(Dictionary<int, double> Vector, int num)

        {

            for (int i = 0; i < num; i++)

            {

                Vector.Add(i, double.Parse(Console.ReadLine()));

            }

        }



        //自定义输出函数

        static void FunOutput(Dictionary<int, double> Vector)

        {

            //使用KeyValuePair输出

            foreach (KeyValuePair<int, double> number in Vector)

            {

                Console.WriteLine("{0}---{1}", number.Key, number.Value);

            }

        }



        static void Main(string[] args)

        {

            Dictionary<int, double> Vector1 = new Dictionary<int,double>();

            Dictionary<int, double> Vector2 = new Dictionary<int,double>();



            Console.WriteLine("这2个向量的维度数必须相同,请输入维度数值:");

            int num = int.Parse(Console.ReadLine());//字符串转化为整形



            Console.WriteLine("请输入Vector1向量的{0}个数值(每行一个):",num);

            FunInput(Vector1, num);

            //FunOutput(Vector1);



            Console.WriteLine("请输入Vector2向量的{0}个数值(每行一个):", num);

            FunInput(Vector2, num);

            //FunOutput(Vector2);

            Console.WriteLine("Vector1和Vector2的余弦值是:{0}",Cosine.Calculate(Vector1,Vector2));            

        }

    }

}

 

你可能感兴趣的:(value)