Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/05/12 11:58:17 (12 years ago)
Author:
mkommend
Message:

#1081: Merged trunk changes and fixed compilation errors due to the merge.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs

    r8477 r8742  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     25using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2427
    2528namespace HeuristicLab.Algorithms.DataAnalysis {
    26   public static class Util {
     29  internal static class Util {
    2730    public static double ScalarProd(IEnumerable<double> v, IEnumerable<double> u) {
    2831      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();
    2936    }
    3037
     
    3441    }
    3542
    36     public static double SqrDist(IEnumerable<double> x, IEnumerable<double> y) {
    37       return x.Zip(y, (a, b) => (a - b) * (a - b)).Sum();
     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);
     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      foreach (int k in columnIndices) {
     65        double d = x[i, k] - xt[j, k];
     66        ss += d * d * scale[k] * scale[k];
     67      }
     68      return ss;
     69    }
     70    public static double ScalarProd(double[,] x, int i, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {
     71      return ScalarProd(x, i, x, j, scale, columnIndices);
     72    }
     73
     74    public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {
     75      double sum = 0.0;
     76      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
     77      foreach (int k in columnIndices) {
     78        sum += x[i, k] * xt[j, k];
     79      }
     80      return scale * scale * sum;
     81    }
     82    public static double ScalarProd(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {
     83      return ScalarProd(x, i, x, j, scale, columnIndices);
     84    }
     85
     86    public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {
     87      double sum = 0.0;
     88      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
     89      foreach (int k in columnIndices) {
     90        sum += x[i, k] * scale[k] * xt[j, k] * scale[k];
     91      }
     92      return sum;
    3893    }
    3994
     
    46101      return Enumerable.Range(0, rows).Select(r => x[r, c]);
    47102    }
     103
     104
     105    public static void AttachValueChangeHandler<T, U>(IValueParameter<T> parameter, Action action)
     106      where T : ValueTypeValue<U>
     107      where U : struct {
     108      parameter.ValueChanged += (sender, args) => {
     109        if (parameter.Value != null) {
     110          parameter.Value.ValueChanged += (s, a) => action();
     111          action();
     112        }
     113      };
     114      if (parameter.Value != null) {
     115        parameter.Value.ValueChanged += (s, a) => action();
     116      }
     117    }
     118
     119    public static void AttachArrayChangeHandler<T, U>(IValueParameter<T> parameter, Action action)
     120      where T : ValueTypeArray<U>
     121      where U : struct {
     122      parameter.ValueChanged += (sender, args) => {
     123        if (parameter.Value != null) {
     124          parameter.Value.ItemChanged += (s, a) => action();
     125          parameter.Value.Reset += (s, a) => action();
     126          action();
     127        }
     128      };
     129      if (parameter.Value != null) {
     130        parameter.Value.ItemChanged += (s, a) => action();
     131        parameter.Value.Reset += (s, a) => action();
     132      }
     133    }
    48134  }
    49135}
Note: See TracChangeset for help on using the changeset viewer.