Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8933 was 8933, checked in by gkronber, 12 years ago

#1902 corrected handling of length-parameter arrays in ARD functions and prevented stacking of mask covariance functions to make sure that the length-parameter and the enumerable of selected column indexes are equally long.

File size: 5.8 KB
RevLine 
[8401]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
[8323]21
[8612]22using System;
[8323]23using System.Collections.Generic;
24using System.Linq;
[8612]25using HeuristicLab.Core;
26using HeuristicLab.Data;
[8323]27
[8371]28namespace HeuristicLab.Algorithms.DataAnalysis {
[8612]29  internal static class Util {
[8323]30    public static double ScalarProd(IEnumerable<double> v, IEnumerable<double> u) {
31      return v.Zip(u, (vi, ui) => vi * ui).Sum();
32    }
33
[8562]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
[8323]38    public static double SqrDist(double x, double y) {
39      double d = x - y;
[8463]40      return d * d;
[8323]41    }
42
[8678]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);
[8491]45    }
46
[8678]47    public static double SqrDist(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {
[8491]48      double ss = 0.0;
[8678]49      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
[8933]50      foreach (int columnIndex in columnIndices) {
51        double d = x[i, columnIndex] - xt[j, columnIndex];
[8491]52        ss += d * d;
53      }
54      return scale * scale * ss;
55    }
[8562]56
[8678]57    public static double SqrDist(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {
[8827]58      return SqrDist(x, i, x, j, scale, columnIndices);
[8491]59    }
60
[8678]61    public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {
[8491]62      double ss = 0.0;
[8678]63      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
[8827]64      int scaleIndex = 0;
[8933]65      foreach (int columnIndex in columnIndices) {
66        double d = x[i, columnIndex] - xt[j, columnIndex];
[8827]67        ss += d * d * scale[scaleIndex] * scale[scaleIndex];
68        scaleIndex++;
[8491]69      }
[8933]70      // must be at the end of scale after iterating over columnIndices
71      if (scaleIndex != scale.Length)
72        throw new ArgumentException("Lengths of scales and covariance functions does not match.");
[8491]73      return ss;
74    }
[8678]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);
[8562]77    }
[8491]78
[8678]79    public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {
[8562]80      double sum = 0.0;
[8678]81      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
[8933]82      foreach (int columnIndex in columnIndices) {
83        sum += x[i, columnIndex] * xt[j, columnIndex];
[8562]84      }
85      return scale * scale * sum;
86    }
[8678]87    public static double ScalarProd(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {
88      return ScalarProd(x, i, x, j, scale, columnIndices);
[8562]89    }
90
[8678]91    public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {
[8562]92      double sum = 0.0;
[8678]93      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
[8827]94      int scaleIndex = 0;
[8933]95      foreach (int columnIndex in columnIndices) {
96        sum += x[i, columnIndex] * scale[scaleIndex] * xt[j, columnIndex] * scale[scaleIndex];
[8827]97        scaleIndex++;
[8562]98      }
[8933]99      // must be at the end of scale after iterating over columnIndices
100      if (scaleIndex != scale.Length)
101        throw new ArgumentException("Lengths of scales and covariance functions does not match.");
102
[8562]103      return sum;
104    }
105
[8323]106    public static IEnumerable<double> GetRow(double[,] x, int r) {
107      int cols = x.GetLength(1);
108      return Enumerable.Range(0, cols).Select(c => x[r, c]);
109    }
[8366]110    public static IEnumerable<double> GetCol(double[,] x, int c) {
111      int rows = x.GetLength(0);
112      return Enumerable.Range(0, rows).Select(r => x[r, c]);
113    }
[8612]114
115
116    public static void AttachValueChangeHandler<T, U>(IValueParameter<T> parameter, Action action)
117      where T : ValueTypeValue<U>
118      where U : struct {
119      parameter.ValueChanged += (sender, args) => {
120        if (parameter.Value != null) {
121          parameter.Value.ValueChanged += (s, a) => action();
122          action();
123        }
124      };
125      if (parameter.Value != null) {
126        parameter.Value.ValueChanged += (s, a) => action();
127      }
128    }
129
130    public static void AttachArrayChangeHandler<T, U>(IValueParameter<T> parameter, Action action)
131      where T : ValueTypeArray<U>
132      where U : struct {
133      parameter.ValueChanged += (sender, args) => {
134        if (parameter.Value != null) {
135          parameter.Value.ItemChanged += (s, a) => action();
136          parameter.Value.Reset += (s, a) => action();
137          action();
138        }
139      };
140      if (parameter.Value != null) {
141        parameter.Value.ItemChanged += (s, a) => action();
142        parameter.Value.Reset += (s, a) => action();
143      }
144    }
[8323]145  }
146}
Note: See TracBrowser for help on using the repository browser.