Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs @ 8827

Last change on this file since 8827 was 8827, checked in by gkronber, 11 years ago

#1902: fixed bugs concerning masking covariance function

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.Algorithms.DataAnalysis {
29  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();
32    }
33
34    public static double SqrDist(IEnumerable<double> x, IEnumerable<double> y) {
35      return x.Zip(y, (a, b) => (a - b) * (a - b)).Sum();
36    }
37
38    public static double SqrDist(double x, double y) {
39      double d = x - y;
40      return d * d;
41    }
42
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    }
46
47    public static double SqrDist(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {
48      double ss = 0.0;
49      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
50      foreach (int k in columnIndices) {
51        double d = x[i, k] - xt[j, k];
52        ss += d * d;
53      }
54      return scale * scale * ss;
55    }
56
57    public static double SqrDist(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {
58      return SqrDist(x, i, x, j, scale, columnIndices);
59    }
60
61    public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {
62      double ss = 0.0;
63      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
64      int scaleIndex = 0;
65      foreach (int k in columnIndices) {
66        double d = x[i, k] - xt[j, k];
67        ss += d * d * scale[scaleIndex] * scale[scaleIndex];
68        scaleIndex++;
69      }
70      return ss;
71    }
72    public static double ScalarProd(double[,] x, int i, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {
73      return ScalarProd(x, i, x, j, scale, columnIndices);
74    }
75
76    public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {
77      double sum = 0.0;
78      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
79      foreach (int k in columnIndices) {
80        sum += x[i, k] * xt[j, k];
81      }
82      return scale * scale * sum;
83    }
84    public static double ScalarProd(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {
85      return ScalarProd(x, i, x, j, scale, columnIndices);
86    }
87
88    public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {
89      double sum = 0.0;
90      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
91      int scaleIndex = 0;
92      foreach (int k in columnIndices) {
93        sum += x[i, k] * scale[scaleIndex] * xt[j, k] * scale[scaleIndex];
94        scaleIndex++;
95      }
96      return sum;
97    }
98
99    public static IEnumerable<double> GetRow(double[,] x, int r) {
100      int cols = x.GetLength(1);
101      return Enumerable.Range(0, cols).Select(c => x[r, c]);
102    }
103    public static IEnumerable<double> GetCol(double[,] x, int c) {
104      int rows = x.GetLength(0);
105      return Enumerable.Range(0, rows).Select(r => x[r, c]);
106    }
107
108
109    public static void AttachValueChangeHandler<T, U>(IValueParameter<T> parameter, Action action)
110      where T : ValueTypeValue<U>
111      where U : struct {
112      parameter.ValueChanged += (sender, args) => {
113        if (parameter.Value != null) {
114          parameter.Value.ValueChanged += (s, a) => action();
115          action();
116        }
117      };
118      if (parameter.Value != null) {
119        parameter.Value.ValueChanged += (s, a) => action();
120      }
121    }
122
123    public static void AttachArrayChangeHandler<T, U>(IValueParameter<T> parameter, Action action)
124      where T : ValueTypeArray<U>
125      where U : struct {
126      parameter.ValueChanged += (sender, args) => {
127        if (parameter.Value != null) {
128          parameter.Value.ItemChanged += (s, a) => action();
129          parameter.Value.Reset += (s, a) => action();
130          action();
131        }
132      };
133      if (parameter.Value != null) {
134        parameter.Value.ItemChanged += (s, a) => action();
135        parameter.Value.Reset += (s, a) => action();
136      }
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.