Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25
26namespace HeuristicLab.Algorithms.DataAnalysis {
27  internal static class Util {
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;
34    }
35
36    public static double SqrDist(IEnumerable<double> x, IEnumerable<double> y) {
37      return x.Zip(y, (a, b) => (a - b) * (a - b)).Sum();
38    }
39
40    public static double SqrDist(double x, double y) {
41      double d = x - y;
42      return d * d;
43    }
44
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);
47    }
48
49    public static double SqrDist(double[,] x, int i, double[,] xt, int j, int[] columnIndices, double scale = 1.0) {
50      double ss = 0.0;
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];
54        double d = x[i, columnIndex] - xt[j, columnIndex];
55        ss += d * d;
56      }
57      return scale * scale * ss;
58    }
59
60    public static double SqrDist(double[,] x, int i, int j, double[] scale, int[] columnIndices) {
61      return SqrDist(x, i, x, j, scale, columnIndices);
62    }
63
64    public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale, int[] columnIndices) {
65      double ss = 0.0;
66      int scaleIndex = 0;
67      for (int c = 0; c < columnIndices.Length; c++) {
68        var columnIndex = columnIndices[c];
69        double d = x[i, columnIndex] - xt[j, columnIndex];
70        ss += d * d * scale[scaleIndex] * scale[scaleIndex];
71        scaleIndex++;
72      }
73      // must be at the end of scale after iterating over columnIndices
74      if (scaleIndex != scale.Length)
75        throw new ArgumentException("Lengths of scales and covariance functions does not match.");
76      return ss;
77    }
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);
80    }
81
82    public static double ScalarProd(double[,] x, int i, double[,] xt, int j, int[] columnIndices, double scale = 1.0) {
83      double sum = 0.0;
84      for (int c = 0; c < columnIndices.Length; c++) {
85        var columnIndex = columnIndices[c];
86        sum += x[i, columnIndex] * xt[j, columnIndex];
87      }
88      return scale * scale * sum;
89    }
90    public static double ScalarProd(double[,] x, int i, int j, double[] scale, int[] columnIndices) {
91      return ScalarProd(x, i, x, j, scale, columnIndices);
92    }
93
94    public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, int[] columnIndices) {
95      double sum = 0.0;
96      int scaleIndex = 0;
97      for (int c = 0; c < columnIndices.Length; c++, scaleIndex++) {
98        var columnIndex = columnIndices[c];
99        sum += x[i, columnIndex] * scale[scaleIndex] * xt[j, columnIndex] * scale[scaleIndex];
100      }
101      // must be at the end of scale after iterating over columnIndices
102      if (scaleIndex != scale.Length)
103        throw new ArgumentException("Lengths of scales and covariance functions does not match.");
104
105      return sum;
106    }
107
108    public static IEnumerable<double> GetRow(double[,] x, int r) {
109      int cols = x.GetLength(1);
110      return GetRow(x, r, Enumerable.Range(0, cols));
111    }
112    public static IEnumerable<double> GetRow(double[,] x, int r, IEnumerable<int> columnIndices) {
113      return columnIndices.Select(c => x[r, c]);
114    }
115    public static IEnumerable<double> GetCol(double[,] x, int c) {
116      int rows = x.GetLength(0);
117      return Enumerable.Range(0, rows).Select(r => x[r, c]);
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.