Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/23/16 16:19:04 (8 years ago)
Author:
mkommend
Message:

#2591: Changed all GP covariance and mean functions to use int[] for column indices instead of IEnumerable<int>. Changed GP utils, GPModel and StudentTProcessModell as well to use fewer iterators and adapted unit tests to new interface.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs

    r12012 r13721  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using HeuristicLab.Core;
    26 using HeuristicLab.Data;
    2725
    2826namespace HeuristicLab.Algorithms.DataAnalysis {
    2927  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;
    3234    }
    3335
     
    4143    }
    4244
    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);
    4547    }
    4648
    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) {
    4850      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];
    5154        double d = x[i, columnIndex] - xt[j, columnIndex];
    5255        ss += d * d;
     
    5558    }
    5659
    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) {
    5861      return SqrDist(x, i, x, j, scale, columnIndices);
    5962    }
    6063
    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) {
    6265      double ss = 0.0;
    63       if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
    6466      int scaleIndex = 0;
    65       foreach (int columnIndex in columnIndices) {
     67      for (int c = 0; c < columnIndices.Length; c++) {
     68        var columnIndex = columnIndices[c];
    6669        double d = x[i, columnIndex] - xt[j, columnIndex];
    6770        ss += d * d * scale[scaleIndex] * scale[scaleIndex];
     
    7376      return ss;
    7477    }
    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);
    7780    }
    7881
    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) {
    8083      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];
    8386        sum += x[i, columnIndex] * xt[j, columnIndex];
    8487      }
    8588      return scale * scale * sum;
    8689    }
    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) {
    8891      return ScalarProd(x, i, x, j, scale, columnIndices);
    8992    }
    9093
    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) {
    9295      double sum = 0.0;
    93       if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
    9496      int scaleIndex = 0;
    95       foreach (int columnIndex in columnIndices) {
     97      for (int c = 0; c < columnIndices.Length; c++, scaleIndex++) {
     98        var columnIndex = columnIndices[c];
    9699        sum += x[i, columnIndex] * scale[scaleIndex] * xt[j, columnIndex] * scale[scaleIndex];
    97         scaleIndex++;
    98100      }
    99101      // must be at the end of scale after iterating over columnIndices
Note: See TracChangeset for help on using the changeset viewer.