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

Cos的用途:

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

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

计算代码如下:

Cosine.cs类

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication1
  6. {
  7. class Cosine
  8. {
  9. /// <summary>
  10. /// 计算向量余弦值
  11. /// </summary>
  12. /// <param name="vector1"></param>
  13. /// <param name="vector2"></param>
  14. public static double Calculate(Dictionary<int, double> vector1, Dictionary<int, double> vector2)
  15. {
  16. double dotProduct = CalcDotProduct(vector1, vector2);
  17. double length1 = CalcLength(vector1);
  18. double length2 = CalcLength(vector2);
  19. double cosine = dotProduct / (length1 * length2);
  20. return cosine;
  21. }
  22. /// <summary>
  23. /// 计算向量长度(vector length)
  24. /// </summary>
  25. /// <param name="vector"></param>
  26. /// <returns></returns>
  27. private static double CalcLength(Dictionary<int, double> vector)
  28. {
  29. double length = 0;
  30. foreach (KeyValuePair<int, double> kvp in vector)
  31. {
  32. length += Math.Pow(kvp.Value, 2);
  33. }
  34. return Math.Sqrt(length);
  35. }
  36. /// <summary>
  37. /// 计算向量点积(dot product)/内积(inner product)
  38. /// </summary>
  39. /// <param name="vector1"></param>
  40. /// <param name="vector2"></param>
  41. /// <returns></returns>
  42. private static double CalcDotProduct(Dictionary<int, double> vector1, Dictionary<int, double> vector2)
  43. {
  44. double dotProduct = 0;
  45. foreach (KeyValuePair<int, double> kvp in vector1)
  46. {
  47. if (vector2.ContainsKey(kvp.Key))
  48. {
  49. dotProduct += kvp.Value * vector2[kvp.Key];
  50. }
  51. }
  52. return dotProduct;
  53. }
  54. }
  55. }

 

Program类,是我的,嘿嘿。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication1
  6. {
  7. class Program
  8. {
  9. //自定义输入函数
  10. static void FunInput(Dictionary<int, double> Vector, int num)
  11. {
  12. for (int i = 0; i < num; i++)
  13. {
  14. Vector.Add(i, double.Parse(Console.ReadLine()));
  15. }
  16. }
  17. //自定义输出函数
  18. static void FunOutput(Dictionary<int, double> Vector)
  19. {
  20. //使用KeyValuePair输出
  21. foreach (KeyValuePair<int, double> number in Vector)
  22. {
  23. Console.WriteLine("{0}---{1}", number.Key, number.Value);
  24. }
  25. }
  26. static void Main(string[] args)
  27. {
  28. Dictionary<int, double> Vector1 = new Dictionary<int,double>();
  29. Dictionary<int, double> Vector2 = new Dictionary<int,double>();
  30. Console.WriteLine("这2个向量的维度数必须相同,请输入维度数值:");
  31. int num = int.Parse(Console.ReadLine());//字符串转化为整形
  32. Console.WriteLine("请输入Vector1向量的{0}个数值(每行一个):",num);
  33. FunInput(Vector1, num);
  34. //FunOutput(Vector1);
  35. Console.WriteLine("请输入Vector2向量的{0}个数值(每行一个):", num);
  36. FunInput(Vector2, num);
  37. //FunOutput(Vector2);
  38. Console.WriteLine("Vector1和Vector2的余弦值是:{0}",Cosine.Calculate(Vector1,Vector2));
  39. }
  40. }
  41. }

 

版权声明:本文为zi-xing原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zi-xing/p/4199634.html