- Timestamp:
- 03/23/16 16:19:04 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs
r12012 r13721 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Core;26 using HeuristicLab.Data;27 25 28 26 namespace HeuristicLab.Algorithms.DataAnalysis { 29 27 internal static class Util { 30 public static double ScalarProd(IEnumerable<double> v, IEnumerable<double> u) { 31 return v.Zip(u, (vi, ui) => vi * ui).Sum(); 28 public static double ScalarProd(double[] v, double[] u) { 29 if (v.Length != u.Length) throw new InvalidOperationException(); 30 double prod = 0.0; 31 for (int i = 0; i < v.Length; i++) 32 prod += v[i] * u[i]; 33 return prod; 32 34 } 33 35 … … 41 43 } 42 44 43 public static double SqrDist(double[,] x, int i, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {44 return SqrDist(x, i, x, j, scale, columnIndices);45 public static double SqrDist(double[,] x, int i, int j, int[] columnIndices, double scale = 1.0) { 46 return SqrDist(x, i, x, j, columnIndices, scale); 45 47 } 46 48 47 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {49 public static double SqrDist(double[,] x, int i, double[,] xt, int j, int[] columnIndices, double scale = 1.0) { 48 50 double ss = 0.0; 49 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1)); 50 foreach (int columnIndex in columnIndices) { 51 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1)).ToArray(); 52 for (int c = 0; c < columnIndices.Length; c++) { 53 var columnIndex = columnIndices[c]; 51 54 double d = x[i, columnIndex] - xt[j, columnIndex]; 52 55 ss += d * d; … … 55 58 } 56 59 57 public static double SqrDist(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {60 public static double SqrDist(double[,] x, int i, int j, double[] scale, int[] columnIndices) { 58 61 return SqrDist(x, i, x, j, scale, columnIndices); 59 62 } 60 63 61 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {64 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale, int[] columnIndices) { 62 65 double ss = 0.0; 63 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));64 66 int scaleIndex = 0; 65 foreach (int columnIndex in columnIndices) { 67 for (int c = 0; c < columnIndices.Length; c++) { 68 var columnIndex = columnIndices[c]; 66 69 double d = x[i, columnIndex] - xt[j, columnIndex]; 67 70 ss += d * d * scale[scaleIndex] * scale[scaleIndex]; … … 73 76 return ss; 74 77 } 75 public static double ScalarProd(double[,] x, int i, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {76 return ScalarProd(x, i, x, j, scale, columnIndices);78 public static double ScalarProd(double[,] x, int i, int j, int[] columnIndices, double scale = 1.0) { 79 return ScalarProd(x, i, x, j, columnIndices, scale); 77 80 } 78 81 79 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {82 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, int[] columnIndices, double scale = 1.0) { 80 83 double sum = 0.0; 81 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));82 foreach (int columnIndex in columnIndices) {84 for (int c = 0; c < columnIndices.Length; c++) { 85 var columnIndex = columnIndices[c]; 83 86 sum += x[i, columnIndex] * xt[j, columnIndex]; 84 87 } 85 88 return scale * scale * sum; 86 89 } 87 public static double ScalarProd(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {90 public static double ScalarProd(double[,] x, int i, int j, double[] scale, int[] columnIndices) { 88 91 return ScalarProd(x, i, x, j, scale, columnIndices); 89 92 } 90 93 91 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {94 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, int[] columnIndices) { 92 95 double sum = 0.0; 93 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));94 96 int scaleIndex = 0; 95 foreach (int columnIndex in columnIndices) { 97 for (int c = 0; c < columnIndices.Length; c++, scaleIndex++) { 98 var columnIndex = columnIndices[c]; 96 99 sum += x[i, columnIndex] * scale[scaleIndex] * xt[j, columnIndex] * scale[scaleIndex]; 97 scaleIndex++;98 100 } 99 101 // must be at the end of scale after iterating over columnIndices
Note: See TracChangeset
for help on using the changeset viewer.