Index: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRQiso.cs
===================================================================
--- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRQiso.cs	(revision 8473)
+++ trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRQiso.cs	(revision 8484)
@@ -21,4 +21,5 @@
 
 using System;
+using System.Collections.Generic;
 using System.Linq;
 using HeuristicLab.Common;
@@ -32,8 +33,4 @@
   public class CovarianceRQiso : Item, ICovarianceFunction {
     [Storable]
-    private double[,] x;
-    [Storable]
-    private double[,] xt;
-    [Storable]
     private double sf2;
     public double Scale { get { return sf2; } }
@@ -44,7 +41,4 @@
     private double alpha;
     public double Shape { get { return alpha; } }
-    [Storable]
-    private bool symmetric;
-    private double[,] d2;
 
     [StorableConstructor]
@@ -55,19 +49,7 @@
     protected CovarianceRQiso(CovarianceRQiso original, Cloner cloner)
       : base(original, cloner) {
-      if (original.x != null) {
-        this.x = new double[original.x.GetLength(0), original.x.GetLength(1)];
-        Array.Copy(original.x, this.x, x.Length);
-
-        this.xt = new double[original.xt.GetLength(0), original.xt.GetLength(1)];
-        Array.Copy(original.xt, this.xt, xt.Length);
-
-        this.d2 = new double[original.d2.GetLength(0), original.d2.GetLength(1)];
-        Array.Copy(original.d2, this.d2, d2.Length);
-        this.sf2 = original.sf2;
-      }
       this.sf2 = original.sf2;
       this.l = original.l;
       this.alpha = original.alpha;
-      this.symmetric = original.symmetric;
     }
 
@@ -85,60 +67,35 @@
 
     public void SetParameter(double[] hyp) {
+      if (hyp.Length != 3) throw new ArgumentException("CovarianceRQiso has three hyperparameters", "k");
       this.l = Math.Exp(hyp[0]);
       this.sf2 = Math.Exp(2 * hyp[1]);
       this.alpha = Math.Exp(hyp[2]);
-      d2 = null;
-    }
-    public void SetData(double[,] x) {
-      SetData(x, x);
-      this.symmetric = true;
     }
 
 
-    public void SetData(double[,] x, double[,] xt) {
-      this.symmetric = false;
-      this.x = x;
-      this.xt = xt;
-      d2 = null;
+    public double GetCovariance(double[,] x, int i, int j) {
+      double lInv = 1.0 / l;
+      double d = i == j
+                   ? 0.0
+                   : Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(x, j).Select(e => e * lInv));
+      return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
     }
 
-    public double GetCovariance(int i, int j) {
-      if (d2 == null) CalculateSquaredDistances();
-      return sf2 * Math.Pow(1 + 0.5 * d2[i, j] / alpha, -alpha);
+    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
+      double lInv = 1.0 / l;
+      double d = i == j
+                   ? 0.0
+                   : Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(x, j).Select(e => e * lInv));
+
+      double b = 1 + 0.5 * d / alpha;
+      yield return sf2 * Math.Pow(b, -alpha - 1) * d;
+      yield return 2 * sf2 * Math.Pow(b, -alpha);
+      yield return sf2 * Math.Pow(b, -alpha) * (0.5 * d / b - alpha * Math.Log(b));
     }
 
-    public double GetGradient(int i, int j, int k) {
-      switch (k) {
-        case 0: return sf2 * Math.Pow(1 + 0.5 * d2[i, j] / alpha, -alpha - 1) * d2[i, j];
-        case 1: return 2 * sf2 * Math.Pow((1 + 0.5 * d2[i, j] / alpha), (-alpha));
-        case 2: {
-            double g = (1 + 0.5 * d2[i, j] / alpha);
-            g = sf2 * Math.Pow(g, -alpha) * (0.5 * d2[i, j] / g - alpha * Math.Log(g));
-            return g;
-          }
-        default: throw new ArgumentException("CovarianceRQiso has three hyperparameters", "k");
-      }
-    }
-
-    private void CalculateSquaredDistances() {
-      if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();
-      int rows = x.GetLength(0);
-      int cols = xt.GetLength(0);
-      d2 = new double[rows, cols];
+    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
       double lInv = 1.0 / l;
-      if (symmetric) {
-        for (int i = 0; i < rows; i++) {
-          for (int j = i; j < rows; j++) {
-            d2[i, j] = Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(xt, j).Select(e => e * lInv));
-            d2[j, i] = d2[i, j];
-          }
-        }
-      } else {
-        for (int i = 0; i < rows; i++) {
-          for (int j = 0; j < cols; j++) {
-            d2[i, j] = Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(xt, j).Select(e => e * lInv));
-          }
-        }
-      }
+      double d = Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(xt, j).Select(e => e * lInv));
+      return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
     }
   }
