Norm.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. namespace VisualMath.Accord.Math
  2. {
  3. using VisualMath.Accord.Math.Decompositions;
  4. /// <summary>
  5. /// Static class Norm. Defines a set of extension methods defining norms measures.
  6. /// </summary>
  7. ///
  8. public static class Norm
  9. {
  10. /// <summary>
  11. /// Returns the maximum column sum of the given matrix.
  12. /// </summary>
  13. public static double Norm1(this double[,] a)
  14. {
  15. double[] columnSums = Matrix.Sum(a, 1);
  16. return Matrix.Max(columnSums);
  17. }
  18. /// <summary>
  19. /// Returns the maximum singular value of the given matrix.
  20. /// </summary>
  21. public static double Norm2(this double[,] a)
  22. {
  23. return new SingularValueDecomposition(a, false, false).TwoNorm;
  24. }
  25. /// <summary>
  26. /// Gets the square root of the sum of squares for all elements in a matrix.
  27. /// </summary>
  28. public static double Frobenius(this double[,] a)
  29. {
  30. int rows = a.GetLength(0);
  31. int cols = a.GetLength(1);
  32. double norm = 0.0;
  33. for (int j = 0; j < cols; j++)
  34. {
  35. for (int i = 0; i < rows; i++)
  36. {
  37. double v = a[i, j];
  38. norm += v * v;
  39. }
  40. }
  41. return System.Math.Sqrt(norm);
  42. }
  43. /// <summary>
  44. /// Gets the Squared Euclidean norm for a vector.
  45. /// </summary>
  46. public static double SquareEuclidean(this double[] a)
  47. {
  48. double sum = 0.0;
  49. for (int i = 0; i < a.Length; i++)
  50. sum += a[i] * a[i];
  51. return sum;
  52. }
  53. /// <summary>
  54. /// Gets the Euclidean norm for a vector.
  55. /// </summary>
  56. public static double Euclidean(this double[] a)
  57. {
  58. return System.Math.Sqrt(SquareEuclidean(a));
  59. }
  60. /// <summary>
  61. /// Gets the Squared Euclidean norm vector for a matrix.
  62. /// </summary>
  63. public static double[] SquareEuclidean(this double[,] a)
  64. {
  65. return SquareEuclidean(a, 0);
  66. }
  67. /// <summary>
  68. /// Gets the Squared Euclidean norm vector for a matrix.
  69. /// </summary>
  70. public static double[] SquareEuclidean(this double[,] a, int dimension)
  71. {
  72. int rows = a.GetLength(0);
  73. int cols = a.GetLength(1);
  74. double[] norm;
  75. if (dimension == 0)
  76. {
  77. norm = new double[cols];
  78. for (int j = 0; j < norm.Length; j++)
  79. {
  80. double sum = 0.0;
  81. for (int i = 0; i < rows; i++)
  82. {
  83. double v = a[i, j];
  84. sum += v * v;
  85. }
  86. norm[j] = sum;
  87. }
  88. }
  89. else
  90. {
  91. norm = new double[rows];
  92. for (int i = 0; i < norm.Length; i++)
  93. {
  94. double sum = 0.0;
  95. for (int j = 0; j < cols; j++)
  96. {
  97. double v = a[i, j];
  98. sum += v * v;
  99. }
  100. norm[i] = sum;
  101. }
  102. }
  103. return norm;
  104. }
  105. /// <summary>
  106. /// Gets the Euclidean norm for a matrix.
  107. /// </summary>
  108. public static double[] Euclidean(this double[,] a)
  109. {
  110. return Euclidean(a, 0);
  111. }
  112. /// <summary>
  113. /// Gets the Euclidean norm for a matrix.
  114. /// </summary>
  115. public static double[] Euclidean(this double[,] a, int dimension)
  116. {
  117. double[] norm = Norm.SquareEuclidean(a, dimension);
  118. for (int i = 0; i < norm.Length; i++)
  119. norm[i] = System.Math.Sqrt(norm[i]);
  120. return norm;
  121. }
  122. }
  123. }